gen-tool-catalog.spec.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Guarantee tests for the tool-schema catalog generator (`scripts/gen-tool-catalog.ts`).
  3. */
  4. import { describe, expect, it } from 'vitest'
  5. import {
  6. assertManifestComplete,
  7. collectToolCatalog,
  8. render,
  9. type ToolCatalog,
  10. } from '../../../../scripts/gen-tool-catalog.ts'
  11. /** JSON Schema shape enough to reach the values AST extraction can't. */
  12. interface JsonSchema {
  13. type: string
  14. properties?: Record<string, JsonSchema>
  15. items?: JsonSchema
  16. enum?: string[]
  17. required?: string[]
  18. }
  19. describe('gen-tool-catalog collectToolCatalog', () => {
  20. it('boots every shipped tool package and harvests its model-facing schemas', async () => {
  21. const catalog = await collectToolCatalog()
  22. const names = catalog.flatMap(entry => entry.schemas.map(s => s.name)).sort()
  23. expect(names).toEqual(['ask_user_question', 'bash', 'cordis_inspect', 'cordis_mount', 'cordis_unmount', 'edit', 'glob', 'grep', 'read', 'run_code', 'skill', 'subagent', 'task_kill', 'task_list', 'task_output', 'todo_write', 'web_fetch', 'web_search', 'workflow', 'write'])
  24. // Every tool carries a JSON-Schema `parameters` object (what the model sees).
  25. for (const entry of catalog) {
  26. for (const schema of entry.schemas) {
  27. expect((schema.parameters as unknown as JsonSchema).type).toBe('object')
  28. }
  29. }
  30. })
  31. it('resolves a runtime-spread enum to its literal members (the payoff over AST)', async () => {
  32. const catalog = await collectToolCatalog()
  33. const todo = catalog
  34. .flatMap(entry => entry.schemas)
  35. .find(s => s.name === 'todo_write')
  36. // `todo-todo` writes `enum: [...STATUSES]` — a source AST would see the
  37. // spread, not the values. Booting yields the shipped enum literals.
  38. const status = (((todo?.parameters as unknown as JsonSchema).properties?.todos)?.items)?.properties?.status
  39. expect(status?.enum).toEqual(['pending', 'in_progress', 'completed'])
  40. })
  41. it('attributes each package with a source pointer that names its index', async () => {
  42. const catalog = await collectToolCatalog()
  43. const bash = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-bash')
  44. expect(bash?.source).toBe('packages/bash/tool-bash/src/index.ts')
  45. })
  46. it('records the shipped `subagent_fork` alias in a note (config-driven tool name)', async () => {
  47. // `tool-subagent`'s registered name is the load-time `toolName` config, so the shipped
  48. // agents surface this one package as both `subagent` and `subagent_fork`.
  49. const catalog = await collectToolCatalog()
  50. const subagent = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-subagent')
  51. expect(subagent?.schemas.map(s => s.name)).toEqual(['subagent'])
  52. expect(subagent?.note).toMatch(/subagent_fork/)
  53. })
  54. })
  55. describe('gen-tool-catalog assertManifestComplete', () => {
  56. it('passes when the manifest lists every on-disk tool package (the default)', () => {
  57. expect(() => { assertManifestComplete() }).not.toThrow()
  58. })
  59. it('throws, naming the omitted package, when a tool package is missing from the manifest', () => {
  60. // An empty manifest scanned against the real tree: every `tool-*` package
  61. // is unlisted, so the guard must fire and name them.
  62. expect(() => { assertManifestComplete([]) }).toThrow(/not in the boot manifest/)
  63. expect(() => { assertManifestComplete([]) }).toThrow(/tool-bash/)
  64. })
  65. })
  66. describe('gen-tool-catalog render', () => {
  67. it('emits a package heading, a tool heading, and a json schema fence', () => {
  68. const catalog: ToolCatalog = [
  69. {
  70. pkg: '@deepseek-ai/dsh-tool-demo',
  71. source: 'packages/demo/tool-demo/src/index.ts',
  72. requires: ['ctx.tools'],
  73. writes: ['tool/result'],
  74. schemas: [{ name: 'demo', description: 'A demo tool.', parameters: { type: 'object', properties: {} } }],
  75. },
  76. ]
  77. const md = render(catalog)
  78. expect(md).toContain('| `@deepseek-ai/dsh-tool-demo` | `demo` | `ctx.tools` | `tool/result` |')
  79. expect(md).toContain('## `@deepseek-ai/dsh-tool-demo`')
  80. expect(md).toContain('### `demo`')
  81. expect(md).toContain('A demo tool.')
  82. expect(md).toContain('```json')
  83. expect(md).toContain('Source: [`packages/demo/tool-demo/src/index.ts`]')
  84. })
  85. })