gen-tool-catalog.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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', 'bash', 'cordis_inspect', 'cordis_mount', 'cordis_unmount', 'create_goal', 'edit', 'exit_plan_mode', 'get_goal', 'glob', 'grep', 'list_agents', 'lsp', 'pwsh', 'ralph', 'read', 'report', 'run_code', 'send_message', 'session_event_read', 'session_event_search', 'session_event_trace', 'session_search', 'session_trace', 'skill', 'str_replace_editor', 'subagent', 'task_kill', 'task_list', 'task_output', 'terminal_close', 'terminal_list', 'terminal_open', 'terminal_read', 'terminal_send', 'terminal_signal', 'todo_write', 'update_goal', '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 harvested tool with its registering plugin source', async () => {
  42. const catalog = await collectToolCatalog()
  43. const bash = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-bash')
  44. expect(bash?.sources.bash).toBe('packages/bash/tool-bash/src/index.ts')
  45. const control = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-subagent-control')
  46. expect(control?.sources).toEqual({
  47. list_agents: 'packages/subagent/tool-subagent-control/src/list-agents.ts',
  48. send_message: 'packages/subagent/tool-subagent-control/src/index.ts',
  49. })
  50. })
  51. it('harvests search tools without depending on the generator process PATH', async () => {
  52. const oldPath = process.env.PATH
  53. try {
  54. process.env.PATH = ''
  55. const catalog = await collectToolCatalog()
  56. const search = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-fs-search')
  57. expect(search?.schemas.map(s => s.name).sort()).toEqual(['glob', 'grep'])
  58. } finally {
  59. if (oldPath === undefined) delete process.env.PATH
  60. else process.env.PATH = oldPath
  61. }
  62. })
  63. it('records the shipped `subagent_fork` alias in a note (config-driven tool name)', async () => {
  64. // `tool-subagent`'s registered name is the load-time `toolName` config, so the shipped
  65. // agents surface this one package as both `subagent` and `subagent_fork`.
  66. const catalog = await collectToolCatalog()
  67. const subagent = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-subagent')
  68. expect(subagent?.schemas.map(s => s.name)).toEqual(['subagent'])
  69. expect(subagent?.note).toMatch(/subagent_fork/)
  70. })
  71. })
  72. describe('gen-tool-catalog assertManifestComplete', () => {
  73. it('passes when the manifest lists every on-disk tool package (the default)', () => {
  74. expect(() => { assertManifestComplete() }).not.toThrow()
  75. })
  76. it('throws, naming the omitted package, when a tool package is missing from the manifest', () => {
  77. // An empty manifest scanned against the real tree: every `tool-*` package
  78. // is unlisted, so the guard must fire and name them.
  79. expect(() => { assertManifestComplete([]) }).toThrow(/not in the boot manifest/)
  80. expect(() => { assertManifestComplete([]) }).toThrow(/tool-bash/)
  81. })
  82. })
  83. describe('gen-tool-catalog render', () => {
  84. it('emits a package heading, a tool heading, and a json schema fence', () => {
  85. const catalog: ToolCatalog = [
  86. {
  87. pkg: '@deepseek-ai/dsh-tool-demo',
  88. sources: { demo: 'packages/demo/tool-demo/src/index.ts' },
  89. requires: ['ctx.tools'],
  90. writes: ['tool/result'],
  91. schemas: [{ name: 'demo', description: 'A demo tool.', parameters: { type: 'object', properties: {} } }],
  92. },
  93. ]
  94. const md = render(catalog)
  95. expect(md).toContain('| `@deepseek-ai/dsh-tool-demo` | `demo` | `ctx.tools` | `tool/result` |')
  96. expect(md).toContain('## `@deepseek-ai/dsh-tool-demo`')
  97. expect(md).toContain('### `demo`')
  98. expect(md).toContain('A demo tool.')
  99. expect(md).toContain('```json')
  100. expect(md).toContain('Source: [`packages/demo/tool-demo/src/index.ts`]')
  101. })
  102. })