|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | +import { |
| 3 | + chooseTools, |
| 4 | + DEFAULT_PRESET, |
| 5 | + getPreset, |
| 6 | + getMcpPrompt, |
| 7 | + getSystemPrompt, |
| 8 | + getSystemPromptForProvider, |
| 9 | + getToolCatalog, |
| 10 | + listPresets, |
| 11 | + listTools, |
| 12 | +} from '../tools.ts'; |
| 13 | +import { SuperDocCliError } from '../runtime/errors.js'; |
| 14 | + |
| 15 | +const PROVIDERS = ['openai', 'anthropic', 'vercel', 'generic'] as const; |
| 16 | + |
| 17 | +describe('preset registry', () => { |
| 18 | + test('DEFAULT_PRESET is "legacy"', () => { |
| 19 | + expect(DEFAULT_PRESET).toBe('legacy'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('listPresets() includes "legacy"', () => { |
| 23 | + const presets = listPresets(); |
| 24 | + expect(presets).toContain('legacy'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('getPreset() (no arg) returns the legacy preset', () => { |
| 28 | + const preset = getPreset(); |
| 29 | + expect(preset.id).toBe('legacy'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('getPreset("legacy") returns the legacy preset', () => { |
| 33 | + const preset = getPreset('legacy'); |
| 34 | + expect(preset.id).toBe('legacy'); |
| 35 | + expect(preset.description).toBeDefined(); |
| 36 | + expect(preset.supportsCacheControl).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + test('getPreset("nonexistent") throws PRESET_NOT_FOUND', () => { |
| 40 | + try { |
| 41 | + getPreset('nonexistent-preset'); |
| 42 | + throw new Error('Expected getPreset to throw.'); |
| 43 | + } catch (error) { |
| 44 | + expect(error).toBeInstanceOf(SuperDocCliError); |
| 45 | + const cliError = error as SuperDocCliError; |
| 46 | + expect(cliError.code).toBe('PRESET_NOT_FOUND'); |
| 47 | + expect(cliError.message).toContain('nonexistent-preset'); |
| 48 | + const details = cliError.details as { id: string; availablePresets: string[] }; |
| 49 | + expect(details.id).toBe('nonexistent-preset'); |
| 50 | + expect(details.availablePresets).toContain('legacy'); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + test('getPreset("") throws PRESET_NOT_FOUND (empty string is not the default)', () => { |
| 55 | + try { |
| 56 | + getPreset(''); |
| 57 | + throw new Error('Expected getPreset("") to throw.'); |
| 58 | + } catch (error) { |
| 59 | + expect(error).toBeInstanceOf(SuperDocCliError); |
| 60 | + expect((error as SuperDocCliError).code).toBe('PRESET_NOT_FOUND'); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + test('chooseTools({preset: ""}) throws PRESET_NOT_FOUND (cross-lang parity)', async () => { |
| 65 | + await expect(chooseTools({ provider: 'openai', preset: '' })).rejects.toMatchObject({ |
| 66 | + code: 'PRESET_NOT_FOUND', |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('public ToolCatalog type — structural access', () => { |
| 72 | + test('getToolCatalog().tools entries expose typed properties', async () => { |
| 73 | + const catalog = await getToolCatalog(); |
| 74 | + expect(catalog.tools.length).toBeGreaterThan(0); |
| 75 | + const first = catalog.tools[0]!; |
| 76 | + // These property accesses validate that ToolCatalog.tools is structurally |
| 77 | + // typed (ToolCatalogEntry[]) — not unknown[]. Compile failure here means |
| 78 | + // the public catalog row type regressed. |
| 79 | + expect(typeof first.toolName).toBe('string'); |
| 80 | + expect(typeof first.description).toBe('string'); |
| 81 | + expect(typeof first.mutates).toBe('boolean'); |
| 82 | + expect(Array.isArray(first.operations)).toBe(true); |
| 83 | + expect(typeof first.operations[0]?.operationId).toBe('string'); |
| 84 | + expect(typeof first.operations[0]?.intentAction).toBe('string'); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('chooseTools — default preset equivalence', () => { |
| 89 | + for (const provider of PROVIDERS) { |
| 90 | + test(`omitting preset equals preset: 'legacy' (${provider})`, async () => { |
| 91 | + const implicit = await chooseTools({ provider }); |
| 92 | + const explicit = await chooseTools({ provider, preset: 'legacy' }); |
| 93 | + // Tools content identical |
| 94 | + expect(implicit.tools).toEqual(explicit.tools); |
| 95 | + // Same tool count |
| 96 | + expect(implicit.meta.toolCount).toBe(explicit.meta.toolCount); |
| 97 | + // Same provider, same cache strategy |
| 98 | + expect(implicit.meta.provider).toBe(explicit.meta.provider); |
| 99 | + expect(implicit.meta.cacheStrategy).toBe(explicit.meta.cacheStrategy); |
| 100 | + // Both echo legacy as resolved preset |
| 101 | + expect(implicit.meta.preset).toBe('legacy'); |
| 102 | + expect(explicit.meta.preset).toBe('legacy'); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + test(`chooseTools(provider, preset: 'nonexistent') throws PRESET_NOT_FOUND`, async () => { |
| 107 | + await expect(chooseTools({ provider: 'openai', preset: 'nonexistent-preset' })).rejects.toMatchObject({ |
| 108 | + code: 'PRESET_NOT_FOUND', |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + test('meta.preset field is included', async () => { |
| 113 | + const { meta } = await chooseTools({ provider: 'openai' }); |
| 114 | + expect(meta.preset).toBe('legacy'); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('catalog + listings — default preset equivalence', () => { |
| 119 | + test(`getToolCatalog() equals getToolCatalog('legacy')`, async () => { |
| 120 | + const implicit = await getToolCatalog(); |
| 121 | + const explicit = await getToolCatalog('legacy'); |
| 122 | + expect(implicit).toEqual(explicit); |
| 123 | + }); |
| 124 | + |
| 125 | + for (const provider of PROVIDERS) { |
| 126 | + test(`listTools(${provider}) equals listTools(${provider}, 'legacy')`, async () => { |
| 127 | + const implicit = await listTools(provider); |
| 128 | + const explicit = await listTools(provider, 'legacy'); |
| 129 | + expect(implicit).toEqual(explicit); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + test(`getToolCatalog('nonexistent') throws PRESET_NOT_FOUND`, async () => { |
| 134 | + await expect(getToolCatalog('nonexistent-preset')).rejects.toMatchObject({ |
| 135 | + code: 'PRESET_NOT_FOUND', |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe('system prompts — default preset equivalence', () => { |
| 141 | + test(`getSystemPrompt() equals getSystemPrompt('legacy')`, async () => { |
| 142 | + const implicit = await getSystemPrompt(); |
| 143 | + const explicit = await getSystemPrompt('legacy'); |
| 144 | + expect(implicit).toBe(explicit); |
| 145 | + }); |
| 146 | + |
| 147 | + test(`getMcpPrompt() equals getMcpPrompt('legacy')`, async () => { |
| 148 | + const implicit = await getMcpPrompt(); |
| 149 | + const explicit = await getMcpPrompt('legacy'); |
| 150 | + expect(implicit).toBe(explicit); |
| 151 | + }); |
| 152 | + |
| 153 | + test(`getSystemPromptForProvider({provider}) equals preset: 'legacy'`, async () => { |
| 154 | + const implicit = await getSystemPromptForProvider({ provider: 'anthropic', cache: true }); |
| 155 | + const explicit = await getSystemPromptForProvider({ |
| 156 | + provider: 'anthropic', |
| 157 | + preset: 'legacy', |
| 158 | + cache: true, |
| 159 | + }); |
| 160 | + expect(implicit).toEqual(explicit); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +describe('legacy preset direct access', () => { |
| 165 | + test('getPreset("legacy").getCatalog() matches getToolCatalog()', async () => { |
| 166 | + const direct = await getPreset('legacy').getCatalog(); |
| 167 | + const viaTopLevel = await getToolCatalog(); |
| 168 | + expect(direct).toEqual(viaTopLevel); |
| 169 | + }); |
| 170 | + |
| 171 | + for (const provider of PROVIDERS) { |
| 172 | + test(`getPreset("legacy").getTools(${provider}) matches chooseTools({provider}).tools`, async () => { |
| 173 | + const direct = await getPreset('legacy').getTools(provider); |
| 174 | + const viaTopLevel = await chooseTools({ provider }); |
| 175 | + expect(direct.tools).toEqual(viaTopLevel.tools); |
| 176 | + expect(direct.cacheStrategy).toBe(viaTopLevel.meta.cacheStrategy); |
| 177 | + }); |
| 178 | + } |
| 179 | +}); |
0 commit comments