import path from 'node:path' import { camelCase } from '@internals/utils' import { ast, type InputMeta } from '@kubb/ast' import { describe, expect, it, test } from 'vitest' import { createResolver } from './createResolver.ts' import { Resolver, toFilePath } from './resolver.ts' import type { ResolvePathOptions } from './types.ts' import type { Config } from '@kubb/core' type TestResolver = Resolver & { greet(name: string): string farewell(name: string): string } type TestPluginFactory = { name: 'test' options: {} resolvedOptions: {} resolver: TestResolver } const baseResolver = new Resolver({ pluginName: 'test' }) const context = { root: '/root', output: { path: 'types' as const, mode: 'directory' as const }, group: undefined, } describe('createResolver', () => { it('a plugin overrides the top-level name; default.name keeps the built-in casing', () => { const resolver = createResolver({ pluginName: 'test', name(name) { return name.toUpperCase() }, greet(name) { return this.name(name) }, farewell(name) { return `bye ${this.name(name)}` }, }) expect(resolver.name('hello')).toBe('HELLO') expect(resolver.greet('world')).toBe('WORLD') expect(resolver.default.name('list pets')).toBe('listPets') }) it('a namespace method reaches the resolver root through `this`', () => { type SchemaResolver = Resolver & { schema: { name(name: string): string; typeName(name: string): string } } type SchemaFactory = { name: 'test'; options: {}; resolvedOptions: {}; resolver: SchemaResolver } const resolver = createResolver({ pluginName: 'test', schema: { name(name) { return `${this.name(name)}Schema` }, typeName(name) { return `${this.name(name)}SchemaType` }, }, }) expect(resolver.schema.name('list pets')).toBe('listPetsSchema') expect(resolver.schema.typeName('list pets')).toBe('listPetsSchemaType') }) it('returns the base name from file.baseName, which reaches sibling helpers through `this`', () => { const resolver = createResolver({ pluginName: 'test', name(name) { return name.toUpperCase() }, file: { baseName({ name, extname }) { return `${this.name(name)}.schema${extname}` }, }, greet: (name: string) => name, farewell: (name: string) => name, }) expect(resolver.file({ name: 'pet', extname: '.ts', ...context }).baseName).toBe('PET.schema.ts') }) it('Resolver.merge accepts file and imports patches without a cast', () => { const base = createResolver({ pluginName: 'test', greet: (name: string) => name, farewell: (name: string) => name, }) const merged = Resolver.merge(base, { file: { baseName({ name, extname }) { return `${name}.mock${extname}` }, }, imports: () => [], }) const node = ast.factory.createSchema({ type: 'object', properties: [ ast.factory.createProperty({ name: 'pet', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Pet', name: 'Pet' }) }), ], }) expect(merged.file({ name: 'pet', extname: '.ts', ...context }).baseName).toBe('pet.mock.ts') expect(base.imports({ node, ...context })).toMatchObject([{ name: ['pet'], path: '/root/types/pet.ts' }]) expect(merged.imports({ node, ...context })).toStrictEqual([]) }) it('a file.path owns the whole path, resolved against root and bypassing output.path', () => { const resolver = createResolver({ pluginName: 'test', file: { path({ baseName }) { return `mocks/${baseName}` }, }, greet: (name: string) => name, farewell: (name: string) => name, }) const file = resolver.file({ name: 'pet', extname: '.ts', ...context }) expect(file.path).toBe('/root/mocks/pet.ts') expect(file.baseName).toBe('pet.ts') }) it('file.path receives the base name from file.baseName', () => { const resolver = createResolver({ pluginName: 'test', file: { baseName({ name, extname }) { return `${name}.gen${extname}` }, path({ baseName }) { return `custom/${baseName}` }, }, greet: (name: string) => name, farewell: (name: string) => name, }) const file = resolver.file({ name: 'pet', extname: '.ts', ...context }) expect(file.path).toBe('/root/custom/pet.gen.ts') expect(file.baseName).toBe('pet.gen.ts') }) it('a file.path that escapes the project root throws', () => { const resolver = createResolver({ pluginName: 'test', file: { path() { return '../outside/pet.ts' }, }, greet: (name: string) => name, farewell: (name: string) => name, }) expect(() => resolver.file({ name: 'pet', extname: '.ts', ...context })).toThrow('outside the output directory') }) it('Resolver.merge() rebuilds helpers on a new instance', () => { type SchemaResolver = Resolver & { schema: { label(name: string): string } } type SchemaFactory = { name: 'test'; options: {}; resolvedOptions: {}; resolver: SchemaResolver } const base = createResolver({ pluginName: 'test', schema: { label(name) { return `base:${this.name(name)}` }, }, }) const merged = Resolver.merge(base, { name(name) { return name.toUpperCase() }, }) expect(merged).not.toBe(base) expect(merged).toBeInstanceOf(Resolver) expect(merged.name('hello')).toBe('HELLO') expect(merged.schema.label('pets')).toBe('base:PETS') }) it('Resolver.merge() merges a namespace per method across several overrides', () => { type MutationResolver = Resolver & { mutation: { name(node: { operationId: string }): string keyName(node: { operationId: string }): string typeName(node: { operationId: string }): string } } type MutationFactory = { name: 'test'; options: {}; resolvedOptions: {}; resolver: MutationResolver } const base = createResolver({ pluginName: 'test', mutation: { name(node) { return this.name(node.operationId) }, keyName(node) { return `${this.name(node.operationId)}MutationKey` }, typeName(node) { return this.name(node.operationId) }, }, }) const merged = Resolver.merge( base, { mutation: { name(node) { return `use_${this.name(node.operationId)}` }, }, }, { mutation: { typeName(node) { return `${this.name(node.operationId)}Type` }, }, }, ) expect(merged.mutation.name({ operationId: 'update pet' })).toBe('use_updatePet') expect(merged.mutation.keyName({ operationId: 'update pet' })).toBe('updatePetMutationKey') expect(merged.mutation.typeName({ operationId: 'update pet' })).toBe('updatePetType') }) it('Resolver.merge() keeps a file override from a resolver in another @kubb/core copy, anywhere in the fold', () => { type TestFactory = { name: 'test'; options: {}; resolvedOptions: {}; resolver: Resolver } const base = createResolver({ pluginName: 'test' }) // A resolver loaded from a second @kubb/core copy (a CommonJS config next to the ESM CLI) is not // `instanceof Resolver`, but shares the brand, so its `file` override must still be honored. const foreign = { [Symbol.for('@kubb/core/resolver/options')]: { pluginName: 'test', file: { baseName: ({ name, extname }: { name: string; extname: string }) => `${name}Faker${extname}` }, }, } as unknown as Resolver const merged = Resolver.merge(base, foreign, { name(name) { return name.toUpperCase() }, }) expect(merged.name('pet')).toBe('PET') expect(merged.file({ name: 'pet', extname: '.ts', ...context }).baseName).toBe('petFaker.ts') }) }) describe('default.path', () => { it.each([ ['the output directory in directory mode', { output: { path: 'types', mode: 'directory' } }, '/root/types/petTypes.ts'], ['the output file as-is in file mode', { output: { path: 'types.ts', mode: 'file' } }, '/root/types.ts'], ['a camelCased tag directory for a tag group', { tag: 'pet store', group: { type: 'tag' } }, '/root/types/petStore/petTypes.ts'], [ 'the custom group.name for a tag group', { tag: 'pets', group: { type: 'tag', name: ({ group }) => `custom_${group}` } }, '/root/types/custom_pets/petTypes.ts', ], [ 'the custom group.name for a path group', { path: '/pets/list', group: { type: 'path', name: ({ group }) => `${camelCase(group)}Controller` } }, '/root/types/petsListController/petTypes.ts', ], ['the output directory for a path group with only a tag, such as an SDK class', { tag: 'pet', group: { type: 'path' } }, '/root/types/petTypes.ts'], ] satisfies Array<[string, Partial, string]>)('returns %s', (_name, params, expected) => { expect( baseResolver.default.path({ baseName: 'petTypes.ts', root: '/root', output: { path: 'types', mode: 'directory' }, group: undefined, ...params }), ).toBe(expected) }) it('nests a tag group in the folders of its parent tags and stops at a parent loop', () => { const resolver = createResolver({ pluginName: 'test' }) resolver.setTags([ { name: 'house cats', parent: 'cats' }, { name: 'cats', parent: 'animals' }, { name: 'a', parent: 'b' }, { name: 'b', parent: 'a' }, ]) const options = { baseName: 'petTypes.ts', root: '/root', output: { path: 'types', mode: 'directory' }, group: { type: 'tag' } } as const expect(resolver.default.path({ ...options, tag: 'house cats' })).toBe('/root/types/animals/cats/houseCats/petTypes.ts') expect(resolver.default.path({ ...options, tag: 'a' })).toBe('/root/types/b/a/petTypes.ts') }) it('names the group folder the way default.path does', () => { const resolver = createResolver({ pluginName: 'test' }) resolver.setTags([{ name: 'house cats', parent: 'cats' }]) expect(resolver.groupDir({ type: 'tag' }, 'pet store')).toBe('petStore') expect(resolver.groupDir({ type: 'tag' }, 'house cats')).toBe(path.join('cats', 'houseCats')) expect(resolver.groupDir({ type: 'path' }, '/pet-store/{id}')).toBe('petStore') expect(resolver.groupDir({ type: 'tag', name: ({ group }) => `${group}Api` }, 'pets')).toBe('petsApi') }) it('sanitizes traversal segments in default path-based grouping', () => { const result = baseResolver.default.path({ baseName: 'petTypes.ts', path: '../../etc/passwd', root: '/root', output: { path: 'types', mode: 'directory' }, group: { type: 'path' }, }) // Traversal components (..) are stripped; first valid segment ('etc') is used as the directory expect(result).toBe('/root/types/etc/petTypes.ts') // Verify traversal did not escape the output directory expect(result).toContain('/root/types/') expect(result).not.toContain('..') }) it.each([ [ 'a custom group.name returns a path outside the output directory', { baseName: 'petTypes.ts', path: '/pets', group: { type: 'path', name: () => '../../secrets' } }, ], ['baseName contains a traversal sequence', { baseName: '../../etc/passwd' }], ] satisfies Array<[string, Pick]>)('throws when %s', (_name, params) => { expect(() => baseResolver.default.path({ root: '/root', output: { path: 'types', mode: 'directory' }, group: undefined, ...params })).toThrow( 'outside the output directory', ) }) }) describe('default.file', () => { const resolver = createResolver({ pluginName: 'test', greet: () => '', farewell: () => '', }) it('returns an empty file at the camelCased name under the output directory', () => { const file = resolver.default.file({ name: 'list pets', extname: '.ts', ...context }) expect(file).toMatchObject({ baseName: 'listPets.ts', path: '/root/types/listPets.ts', sources: [], imports: [], exports: [] }) }) }) describe('resolver.imports', () => { const refNode = ast.factory.createSchema({ type: 'object', properties: [ ast.factory.createProperty({ name: 'pet', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Pet', name: 'Pet' }) }), ast.factory.createProperty({ name: 'order', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Order', name: 'Order' }) }), ], }) it('builds one import per ref with the resolver name and file path', () => { const imports = baseResolver.imports({ node: refNode, ...context }) expect(imports).toMatchObject([ { kind: 'Import', name: ['pet'], path: '/root/types/pet.ts' }, { kind: 'Import', name: ['order'], path: '/root/types/order.ts' }, ]) }) it('resolves a collision-renamed ref through targetName', () => { const node = ast.factory.createSchema({ type: 'object', properties: [ ast.factory.createProperty({ name: 'order', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Order', name: 'Order', targetName: 'OrderSchema' }), }), ], }) const imports = baseResolver.imports({ node, ...context }) expect(imports).toMatchObject([{ kind: 'Import', name: ['orderSchema'], path: '/root/types/orderSchema.ts' }]) }) it('a per-call name override wins over the resolver name', () => { const imports = baseResolver.imports({ node: refNode, ...context, name: (schemaName) => `${schemaName}Type`, }) expect(imports.map((imp) => imp.name)).toStrictEqual([['PetType'], ['OrderType']]) }) it('emits one import per unique target when a schema is referenced repeatedly', () => { const petRef = () => ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Pet', name: 'Pet' }) const node = ast.factory.createSchema({ type: 'object', properties: [ast.factory.createProperty({ name: 'first', schema: petRef() }), ast.factory.createProperty({ name: 'second', schema: petRef() })], }) expect(baseResolver.imports({ node, ...context })).toMatchObject([{ name: ['pet'], path: '/root/types/pet.ts' }]) }) test.each([ ['ungrouped output', new Resolver({ pluginName: 'test' }), undefined], ['a tag group', new Resolver({ pluginName: 'test' }), { type: 'tag' } as const], ['a path group', new Resolver({ pluginName: 'test' }), { type: 'path' } as const], ['a file.baseName override', new Resolver({ pluginName: 'test', file: { baseName: ({ name, extname }) => `${name}.mock${extname}` } }), undefined], ])('repeated calls return the paths resolver.file gives for %s', (_label, resolver, group) => { const output = { path: 'types', mode: 'directory' } as const const expected = ['Pet', 'Order'].map((name) => resolver.file({ name, extname: '.ts', root: '/root', output, group }).path) const paths = () => resolver.imports({ node: refNode, root: '/root', output, group }).map((entry) => entry.path) expect(paths()).toStrictEqual(expected) expect(paths()).toStrictEqual(expected) }) it('keeps cached paths apart per root and extname for one output', () => { const resolver = new Resolver({ pluginName: 'test' }) const output = { path: 'types', mode: 'directory' } as const const paths = (root: string, extname?: '.ts' | '.js') => resolver.imports({ node: refNode, root, output, extname }).map((entry) => entry.path) expect(paths('/root')).toStrictEqual(['/root/types/pet.ts', '/root/types/order.ts']) expect(paths('/other')).toStrictEqual(['/other/types/pet.ts', '/other/types/order.ts']) expect(paths('/root', '.js')).toStrictEqual(['/root/types/pet.js', '/root/types/order.js']) }) }) describe('shared', () => { const shared = { path: '/common', schemas: ['Name'] } const sharedResolver = new Resolver({ pluginName: 'test', shared }) it('keeps a shared schema of a plugin with a custom file.path in its own output', () => { const resolver = createResolver({ pluginName: 'test', file: { path: () => 'mocks/name.ts' }, greet: () => '', farewell: () => '' }) const withShared = Resolver.merge(new Resolver({ pluginName: 'test', shared }), resolver) expect(withShared.file({ name: 'Name', extname: '.ts', ...context }).path).toBe('/root/mocks/name.ts') }) it('keeps an operation file in its own output when its name matches a shared schema', () => { expect(sharedResolver.file({ name: 'Name', extname: '.ts', tag: 'pets', ...context }).path).toBe('/root/types/name.ts') }) it('keeps a shared schema of a file-mode plugin, and every import of it, in its own file', () => { const fileContext = { ...context, output: { path: 'types.ts', mode: 'file' as const } } const node = ast.factory.createSchema({ type: 'object', properties: [ ast.factory.createProperty({ name: 'name', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Name', name: 'Name' }) }), ], }) expect(sharedResolver.file({ name: 'Name', extname: '.ts', ...fileContext }).path).toBe('/root/types.ts') expect(sharedResolver.imports({ node, ...fileContext }).map((imp) => imp.path)).toStrictEqual(['/root/types.ts']) }) it('imports a shared schema from the shared path', () => { const node = ast.factory.createSchema({ type: 'object', properties: [ ast.factory.createProperty({ name: 'name', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Name', name: 'Name' }) }), ast.factory.createProperty({ name: 'pet', schema: ast.factory.createSchema({ type: 'ref', ref: '#/components/schemas/Pet', name: 'Pet' }) }), ], }) expect(sharedResolver.imports({ node, ...context }).map((imp) => imp.path)).toStrictEqual(['/common/types/name.ts', '/root/types/pet.ts']) }) }) const mockConfig = { input: 'petStore.yaml', output: { path: 'src/generated', defaultBanner: true }, } as unknown as Config describe('default.banner', () => { it('returns default banner when no output.banner is configured', () => { const result = baseResolver.default.banner(undefined, { config: mockConfig }) expect(result).toContain('Generated by Kubb') expect(result).toContain('petStore.yaml') }) it('returns simple banner when defaultBanner is "simple"', () => { const config = { ...mockConfig, output: { ...mockConfig.output, defaultBanner: 'simple' }, } as unknown as Config const result = baseResolver.default.banner(undefined, { config }) expect(result).toBe('/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/\n') }) it('returns null when defaultBanner is false and no user banner', () => { const config = { ...mockConfig, output: { ...mockConfig.output, defaultBanner: false }, } as unknown as Config const result = baseResolver.default.banner(undefined, { config }) expect(result).toBeNull() }) it('includes meta title and version (but not description) in the Kubb banner when meta is provided', () => { const meta: InputMeta = { title: 'Pet API', description: 'A very long description', version: '2.0.0', } const result = baseResolver.default.banner(meta, { config: mockConfig, }) expect(result).toContain('Pet API') expect(result).toContain('2.0.0') expect(result).not.toContain('A very long description') }) }) describe('default.footer', () => { it('returns null when no output.footer is configured', () => { const result = baseResolver.default.footer(undefined, { config: mockConfig }) expect(result).toBeNull() }) }) // banner and footer share one user-text path: a string wins verbatim, and a function receives the // spec meta plus the per-file context, with the per-file fields defaulting to false/empty. describe('default.banner / default.footer user text', () => { const meta: InputMeta = { title: 'Petstore', description: 'Test API', version: '1.0.0' } const describeFile = (m: { title?: string; isBarrel: boolean; isAggregation: boolean; filePath: string; baseName: string }) => `${m.title ?? ''}|${m.isBarrel}|${m.isAggregation}|${m.filePath}|${m.baseName}` it.each(['banner', 'footer'] as const)('returns the user string %s verbatim', (method) => { expect(baseResolver.default[method](undefined, { config: mockConfig, output: { [method]: '// custom' } })).toBe('// custom') }) it.each([ ['banner', meta, undefined, 'Petstore|false|false||'], [ 'banner', undefined, { path: 'src/gen/clients/stocks/stocks.ts', baseName: 'stocks.ts', isAggregation: true }, '|false|true|src/gen/clients/stocks/stocks.ts|stocks.ts', ], ['footer', undefined, { path: 'src/gen/index.ts', baseName: 'index.ts', isBarrel: true }, '|true|false|src/gen/index.ts|index.ts'], ] satisfies Array< ['banner' | 'footer', InputMeta | undefined, { path: string; baseName: string; isBarrel?: boolean; isAggregation?: boolean } | undefined, string] >)('calls the user %s function with meta %o and file %o', (method, inputMeta, file, expected) => { expect(baseResolver.default[method](inputMeta, { config: mockConfig, output: { [method]: describeFile }, file })).toBe(expected) }) }) describe('toFilePath', () => { test.each([ // version numbers (dot before a digit) stay in one segment ['get_enterprise_configurations_id_v2025.0', 'getEnterpriseConfigurationsIdV20250'], // dots before a letter split into nested path segments ['pet.petId', 'pet/petId'], // Security: leading dots must NOT produce a leading slash (path traversal guard) ['..Schema', 'schema'], ])('toFilePath(%s) -> %s (camelCase segments)', (input, expected) => { expect(toFilePath(input)).toBe(expected) }) }) describe('toFilePath caseLast', () => { it('cases only the file name with caseLast and keeps folders camelCase', () => { expect(toFilePath('pet store.pet id', (part) => part.toUpperCase())).toBe('petStore/PET ID') }) })