integration.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  2. import { mkdtemp, mkdir, rm, writeFile, realpath } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { pathToFileURL } from 'node:url'
  6. import { Context } from 'cordis'
  7. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  8. import ToolRegistry from '@deepseek-ai/dsh-tools'
  9. import Lsp from '@deepseek-ai/dsh-lsp'
  10. import * as LspLocal from '@deepseek-ai/dsh-lsp-local'
  11. import * as TimeoutPolicy from '@deepseek-ai/dsh-timeout-policy'
  12. import * as ToolLsp from '@deepseek-ai/dsh-tool-lsp'
  13. /**
  14. * Focused in-process integration of the model-facing tool, seam, local provider, and timeout policy.
  15. * The `lsp-definition` ACP snapshot owns the shipped Loader/app entry path.
  16. */
  17. let root: string
  18. let ws: string
  19. beforeEach(async () => {
  20. root = await realpath(await mkdtemp(join(tmpdir(), 'lsp-tool-int-')))
  21. ws = join(root, 'ws')
  22. await mkdir(ws)
  23. await writeFile(join(ws, 'a.ts'), 'const x = 1\n')
  24. })
  25. afterEach(async () => {
  26. await rm(root, { recursive: true, force: true })
  27. })
  28. /** An inline stdio server that answers initialize + definition; `hang` makes textDocument/* stall. */
  29. function serverScript(hang: boolean): string {
  30. const definition = JSON.stringify({ uri: pathToFileURL(join(ws, 'a.ts')).href, range: { start: { line: 0, character: 0 }, end: { line: 0, character: 3 } } })
  31. return 'let b=Buffer.alloc(0);'
  32. + `const DEF=${definition};`
  33. + 'const fr=(o)=>{const x=Buffer.from(JSON.stringify({jsonrpc:"2.0",...o}));return Buffer.concat([Buffer.from(`Content-Length: ${x.length}\\r\\n\\r\\n`),x]);};'
  34. + 'process.stdin.on("data",c=>{b=Buffer.concat([b,c]);for(;;){const s=b.indexOf("\\r\\n\\r\\n");if(s<0)break;const len=Number(/(\\d+)/.exec(b.toString("ascii",0,s))[1]);if(b.length<s+4+len)break;const m=JSON.parse(b.toString("utf8",s+4,s+4+len));b=b.subarray(s+4+len);'
  35. + 'if(m.method==="initialize")process.stdout.write(fr({id:m.id,result:{capabilities:{positionEncoding:"utf-16",textDocumentSync:1,definitionProvider:true}}}));'
  36. + `else if(m.method==="textDocument/definition"){${hang ? '' : 'process.stdout.write(fr({id:m.id,result:DEF}));'}}`
  37. + 'else if(m.method==="shutdown")process.stdout.write(fr({id:m.id,result:null}));'
  38. + 'else if(m.method==="exit")process.exit(0);'
  39. + '}});'
  40. }
  41. async function mount(hang: boolean, timeoutMs?: number): Promise<Context> {
  42. const ctx = new Context()
  43. await ctx.plugin(SystemPrompt)
  44. await ctx.plugin(ToolRegistry)
  45. await ctx.plugin(Lsp)
  46. await ctx.plugin(LspLocal, {
  47. servers: {
  48. inline: {
  49. command: process.execPath,
  50. args: ['-e', serverScript(hang)],
  51. extensionToLanguage: { '.ts': 'typescript' },
  52. shutdownTimeoutMs: 200,
  53. killGraceMs: 200,
  54. },
  55. },
  56. })
  57. await ctx.plugin(TimeoutPolicy)
  58. await ctx.plugin(ToolLsp, timeoutMs !== undefined ? { timeoutMs } : {})
  59. return ctx
  60. }
  61. let seq = 0
  62. const testToolSignal = new AbortController().signal
  63. function call(ctx: Context, args: unknown) {
  64. return ctx.tools.execute({
  65. signal: testToolSignal,
  66. callId: `int-${++seq}` as never,
  67. name: 'lsp',
  68. arguments: args,
  69. agent: { session: { header: { cwd: ws } } } as never,
  70. })
  71. }
  72. describe('tool-lsp integration', () => {
  73. it('round-trips a definition query through the real provider and renders a location', async () => {
  74. const ctx = await mount(false)
  75. const result = await call(ctx, { operation: 'goToDefinition', file_path: 'a.ts', line: 1, character: 7 })
  76. expect(result.isError).toBe(false)
  77. expect(result.content[0]).toEqual({ type: 'text', text: 'a.ts:1:1' })
  78. await ctx.fiber.dispose()
  79. }, 30_000)
  80. it('enforces the TOOL_TIMEOUT budget when the server hangs', async () => {
  81. const ctx = await mount(true, 300)
  82. const result = await call(ctx, { operation: 'goToDefinition', file_path: 'a.ts', line: 1, character: 7 })
  83. expect(result.isError).toBe(true)
  84. expect(result.error?.info?.code).toBe('TOOL_TIMEOUT')
  85. await ctx.fiber.dispose()
  86. }, 30_000)
  87. })