gen-tool-catalog.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. assertToolsHarvested,
  8. collectToolCatalog,
  9. render,
  10. type ToolCatalog,
  11. type ToolPackage,
  12. } from '../../../../scripts/gen-tool-catalog.ts'
  13. /** JSON Schema shape enough to reach the values AST extraction can't. */
  14. interface JsonSchema {
  15. type: string
  16. properties?: Record<string, JsonSchema>
  17. items?: JsonSchema
  18. enum?: string[]
  19. required?: string[]
  20. }
  21. describe('gen-tool-catalog collectToolCatalog', () => {
  22. it('boots every shipped tool package and harvests its model-facing schemas', async () => {
  23. const catalog = await collectToolCatalog()
  24. const names = catalog.flatMap(entry => entry.schemas.map(s => s.name)).sort()
  25. expect(names).toEqual([
  26. 'ask_user_question', 'bash', 'bash', 'cordis_define', 'cordis_inspect_list',
  27. 'cordis_inspect_query', 'cordis_inspect_self', 'cordis_run', 'cordis_stop',
  28. 'cordis_undefine', 'create_goal', 'edit', 'exit_plan_mode', 'followup_task', 'get_goal', 'glob', 'grep',
  29. 'interrupt_agent', 'interrupt_agent', 'job_kill', 'job_list', 'job_output',
  30. 'list_agents', 'list_agents', 'list_subagent_models', 'lsp', 'pwsh', 'pwsh', 'ralph',
  31. 'read', 'read_image', 'report', 'run_code', 'schedule_create', 'schedule_delete',
  32. 'schedule_list', 'send_message', 'send_message', 'session_event_read', 'session_event_search',
  33. 'session_event_trace', 'session_search', 'session_trace', 'skill', 'spawn_teammate',
  34. 'str_replace_editor', 'subagent', 'team_task_create',
  35. 'team_task_get', 'team_task_list', 'team_task_update', 'terminal_close', 'terminal_list',
  36. 'terminal_open', 'terminal_read', 'terminal_send', 'terminal_signal', 'todo_write',
  37. 'update_goal', 'wait_agent', 'web_fetch', 'web_search', 'workflow', 'write',
  38. ])
  39. // Every tool carries a JSON-Schema `parameters` object (what the model sees).
  40. for (const entry of catalog) {
  41. for (const schema of entry.schemas) {
  42. expect((schema.parameters as unknown as JsonSchema).type).toBe('object')
  43. }
  44. }
  45. })
  46. it('resolves a runtime-spread enum to its literal members (the payoff over AST)', async () => {
  47. const catalog = await collectToolCatalog()
  48. const todo = catalog
  49. .flatMap(entry => entry.schemas)
  50. .find(s => s.name === 'todo_write')
  51. // `todo-todo` writes `enum: [...STATUSES]` — a source AST would see the
  52. // spread, not the values. Booting yields the shipped enum literals.
  53. const status = (((todo?.parameters as unknown as JsonSchema).properties?.todos)?.items)?.properties?.status
  54. expect(status?.enum).toEqual(['pending', 'in_progress', 'completed'])
  55. })
  56. it('attributes each harvested tool with its registering plugin source', async () => {
  57. const catalog = await collectToolCatalog()
  58. const bash = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-bash')
  59. expect(bash?.sources.bash).toBe('packages/shell/tool-bash/src/index.ts')
  60. const control = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-subagent-control')
  61. expect(control?.sources).toEqual({
  62. interrupt_agent: 'packages/subagent/tool-subagent-control/src/index.ts',
  63. list_agents: 'packages/subagent/tool-subagent-control/src/list-agents.ts',
  64. send_message: 'packages/subagent/tool-subagent-control/src/index.ts',
  65. })
  66. })
  67. it('harvests search tools without depending on the generator process PATH', async () => {
  68. const oldPath = process.env.PATH
  69. try {
  70. process.env.PATH = ''
  71. const catalog = await collectToolCatalog()
  72. const search = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-fs-search')
  73. expect(search?.schemas.map(s => s.name).sort()).toEqual(['glob', 'grep'])
  74. } finally {
  75. if (oldPath === undefined) delete process.env.PATH
  76. else process.env.PATH = oldPath
  77. }
  78. })
  79. it('records the shipped `subagent_fork` alias in a note (config-driven tool name)', async () => {
  80. // `tool-subagent`'s registered name is the load-time `toolName` config, so the shipped
  81. // agents surface this one package as both `subagent` and `subagent_fork`.
  82. const catalog = await collectToolCatalog()
  83. const subagent = catalog.find(entry => entry.pkg === '@deepseek-ai/dsh-tool-subagent')
  84. expect(subagent?.schemas.map(s => s.name)).toEqual(['list_subagent_models', 'subagent'])
  85. expect(subagent?.note).toMatch(/subagent_fork/)
  86. })
  87. })
  88. describe('gen-tool-catalog assertManifestComplete', () => {
  89. it('passes when the manifest lists every on-disk tool package (the default)', () => {
  90. expect(() => { assertManifestComplete() }).not.toThrow()
  91. })
  92. it('throws, naming the omitted package, when a tool package is missing from the manifest', () => {
  93. // An empty manifest scanned against the real tree: every `tool-*` package
  94. // is unlisted, so the guard must fire and name them.
  95. expect(() => { assertManifestComplete([]) }).toThrow(/not in the boot manifest/)
  96. expect(() => { assertManifestComplete([]) }).toThrow(/tool-bash/)
  97. })
  98. })
  99. describe('gen-tool-catalog assertToolsHarvested', () => {
  100. const entry: ToolPackage = {
  101. pkg: '@deepseek-ai/dsh-tool-demo',
  102. dir: 'tool-demo',
  103. source: 'packages/demo/tool-demo/src/index.ts',
  104. requires: ['ctx.tools', 'ctx.somethingUnmounted'],
  105. writes: ['tool/result'],
  106. mount: () => Promise.resolve(),
  107. }
  108. it('accepts a boot that registered at least one tool', () => {
  109. expect(() => { assertToolsHarvested(entry, 1) }).not.toThrow()
  110. })
  111. it('throws, naming the package and its requirements, when a boot registers nothing', () => {
  112. // The failure this guards is silent by construction: the package is in the
  113. // manifest, its plugin merely stays PENDING on an unmounted service, and the
  114. // catalog would ship without its tools while every gate stays green.
  115. expect(() => { assertToolsHarvested(entry, 0) }).toThrow(/@deepseek-ai\/dsh-tool-demo booted without registering a single tool/)
  116. expect(() => { assertToolsHarvested(entry, 0) }).toThrow(/ctx.somethingUnmounted/)
  117. })
  118. })
  119. describe('gen-tool-catalog render', () => {
  120. it('emits a package heading, a tool heading, and a json schema fence', () => {
  121. const catalog: ToolCatalog = [
  122. {
  123. pkg: '@deepseek-ai/dsh-tool-demo',
  124. sources: { demo: 'packages/demo/tool-demo/src/index.ts' },
  125. requires: ['ctx.tools'],
  126. writes: ['tool/result'],
  127. schemas: [{ name: 'demo', description: 'A demo tool.', parameters: { type: 'object', properties: {} } }],
  128. },
  129. ]
  130. const md = render(catalog)
  131. expect(md).toContain('| `@deepseek-ai/dsh-tool-demo` | `demo` | `ctx.tools` | `tool/result` |')
  132. expect(md).toContain('## `@deepseek-ai/dsh-tool-demo`')
  133. expect(md).toContain('### `demo`')
  134. expect(md).toContain('A demo tool.')
  135. expect(md).toContain('```json')
  136. expect(md).toContain('Source: [`packages/demo/tool-demo/src/index.ts`]')
  137. })
  138. })