built-lib.e2e.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { existsSync } from 'node:fs'
  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 { fileURLToPath, pathToFileURL } from 'node:url'
  6. import { execa } from 'execa'
  7. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  8. /**
  9. * Keyless built-artifact smoke: plain Node imports `@deepseek-ai/dsh-lsp` and
  10. * `@deepseek-ai/dsh-lsp-local` by name through their exports maps, spawns the fixture server, runs
  11. * one query (exercising real `Content-Length` framing over `lib/index.js`), and disposes (exercising
  12. * subprocess cleanup). Unit tests use `src/`; this pins the downstream `lib/` path. Skips when `lib/`
  13. * is absent; CI runs it after the build.
  14. */
  15. const pkgDir = fileURLToPath(new URL('..', import.meta.url))
  16. const seamLib = join(pkgDir, '../lsp/lib/index.js')
  17. const subprocessLib = join(pkgDir, '../../subprocess/subprocess-local/lib/index.js')
  18. const built = existsSync(join(pkgDir, 'lib/index.js')) && existsSync(seamLib) && existsSync(subprocessLib)
  19. const fixtureServer = fileURLToPath(new URL('./fixture-server.ts', import.meta.url))
  20. let root: string
  21. let ws: string
  22. beforeAll(async () => {
  23. root = await realpath(await mkdtemp(join(tmpdir(), 'lsp-built-')))
  24. ws = join(root, 'ws')
  25. await mkdir(ws)
  26. await writeFile(join(ws, 'a.ts'), 'const x = 1\n')
  27. })
  28. afterAll(async () => {
  29. if (root) await rm(root, { recursive: true, force: true })
  30. })
  31. describe.skipIf(!built)('built lib real load path (plain node)', () => {
  32. it('runs a query through lib/index.js and disposes cleanly, framing over the base protocol', async () => {
  33. const location = JSON.stringify({ uri: pathToFileURL(join(ws, 'a.ts')).href, range: { start: { line: 0, character: 0 }, end: { line: 0, character: 3 } } })
  34. const script = `
  35. const { Context } = await import('cordis')
  36. const { default: Lsp } = await import('@deepseek-ai/dsh-lsp')
  37. const LspLocal = await import('@deepseek-ai/dsh-lsp-local')
  38. const { default: LocalSubprocessService } = await import('@deepseek-ai/dsh-subprocess-local')
  39. const ctx = new Context()
  40. await ctx.plugin(Lsp)
  41. await ctx.plugin(LocalSubprocessService)
  42. await ctx.plugin(LspLocal, {
  43. servers: {
  44. fake: {
  45. command: ${JSON.stringify(process.execPath)},
  46. args: [${JSON.stringify(fixtureServer)}],
  47. env: { LSP_FAKE_DEF: ${JSON.stringify(location)} },
  48. extensionToLanguage: { '.ts': 'typescript' },
  49. },
  50. },
  51. })
  52. const result = await ctx.lsp.query({ operation: 'goToDefinition', filePath: 'a.ts', position: { line: 0, character: 6 }, workspaceRoot: ${JSON.stringify(ws)} })
  53. console.log(JSON.stringify(result))
  54. await ctx.fiber.dispose()
  55. `
  56. const { exitCode, stdout, stderr } = await execa(process.execPath, ['--input-type=module', '-e', script], {
  57. cwd: pkgDir,
  58. stdin: 'ignore',
  59. timeout: 55_000,
  60. killSignal: 'SIGKILL',
  61. reject: false,
  62. })
  63. expect(exitCode, `stderr:\n${stderr}`).toBe(0)
  64. const lastLine = stdout.trim().split('\n').at(-1) ?? ''
  65. const result = JSON.parse(lastLine) as { kind: string; locations: unknown[] }
  66. expect(result.kind).toBe('locations')
  67. expect(result.locations).toHaveLength(1)
  68. }, 60_000)
  69. })