import type { OperationNode, OperationsNode } from '@kubb/ast' import { describe, expectTypeOf, it } from 'vitest' import type { getResolver, provide } from './composables.ts' import type { from, RefName } from './from.ts' import type { Resolver } from './resolver/resolver.ts' import type { defineTemplate } from './defineTemplate.ts' import type { TemplateContext, PluginType, Template, TemplateNode } from './types.ts' type Options = { output?: string } type MyResolver = Resolver & { custom(): string } type MyPrinter = { print(): string } describe('PluginType', () => { it('defaults the resolver to kit Resolver', () => { type Plugin = PluginType<'plugin-a', Options> expectTypeOf().toEqualTypeOf() }) it('types ctx.resolver and ctx.printer from the 4th and 5th arguments', () => { type Plugin = PluginType<'plugin-a', Options, Options, MyResolver, MyPrinter> expectTypeOf['resolver']>().toEqualTypeOf() expectTypeOf['printer']>().toEqualTypeOf() }) }) type TypesResolver = Resolver & { response: { options(node: OperationNode): string; label(): string | null } tags(): Array lookup: Map } // Test-only names. They join the registries for the whole typecheck program. declare global { namespace Kubb { interface PluginRegistry { 'plugin-types-test': PluginType<'plugin-types-test', object, object, TypesResolver> } interface ProvideRegistry { 'types-test': TypesResolver } } } describe('from', () => { type FromTypes = ReturnType>['resolver'] it('returns a RefName for each string a resolver member returns', () => { expectTypeOf>().toEqualTypeOf() expectTypeOf>().toEqualTypeOf() }) it('keeps arrays, Maps and other results as they are', () => { expectTypeOf>().toEqualTypeOf>() expectTypeOf().toEqualTypeOf>() }) it('types a provided name by its provider and no source as a plain resolver', () => { expectTypeOf>['resolver']>().toEqualTypeOf() expectTypeOf>['resolver']['name']>().toEqualTypeOf<(name: string) => RefName>() }) it("types ctx.from() without a source as the current plugin's resolver", () => { const ctx = {} as TemplateContext expectTypeOf>['resolver']>().toEqualTypeOf() }) it('types getResolver by the plugin or provided name', () => { expectTypeOf>>().toEqualTypeOf() }) it('takes only a factory of the provided type in provide', () => { expectTypeOf>[1]>().toEqualTypeOf<() => TypesResolver>() }) }) describe('Template', () => { it('types the node render gets from kind', () => { expectTypeOf['render']>[0]>().toEqualTypeOf() expectTypeOf['render']>[0]>().toEqualTypeOf() }) it("keeps the kind, so a template can call another template's render", () => { const check = (define: typeof defineTemplate) => { const schema = define({ kind: 'Schema', render: () => null }) return define({ kind: 'Schema', render: (node, ctx) => schema.render(node, ctx) }) } expectTypeOf['kind']>().toEqualTypeOf<'Schema'>() }) it('rejects a render that reads a field its kind does not have', () => { const check = (define: typeof defineTemplate) => define({ kind: 'Operation', // @ts-expect-error an operation node has no `type` render: (node) => (node.type ? null : null), }) expectTypeOf(check).toBeFunction() }) })