built-lib.e2e.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { spawn } from 'node:child_process'
  2. import { existsSync } from 'node:fs'
  3. import { mkdtemp, mkdir, rm, writeFile, realpath } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { fileURLToPath, pathToFileURL } from 'node:url'
  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 built = existsSync(join(pkgDir, 'lib/index.js')) && existsSync(seamLib)
  18. const fixtureServer = fileURLToPath(new URL('./fixture-server.ts', import.meta.url))
  19. let root: string
  20. let ws: string
  21. beforeAll(async () => {
  22. root = await realpath(await mkdtemp(join(tmpdir(), 'lsp-built-')))
  23. ws = join(root, 'ws')
  24. await mkdir(ws)
  25. await writeFile(join(ws, 'a.ts'), 'const x = 1\n')
  26. })
  27. afterAll(async () => {
  28. if (root) await rm(root, { recursive: true, force: true })
  29. })
  30. describe.skipIf(!built)('built lib real load path (plain node)', () => {
  31. it('runs a query through lib/index.js and disposes cleanly, framing over the base protocol', async () => {
  32. const location = JSON.stringify({ uri: pathToFileURL(join(ws, 'a.ts')).href, range: { start: { line: 0, character: 0 }, end: { line: 0, character: 3 } } })
  33. const script = `
  34. const { Context } = await import('cordis')
  35. const { default: Lsp } = await import('@deepseek-ai/dsh-lsp')
  36. const LspLocal = await import('@deepseek-ai/dsh-lsp-local')
  37. const ctx = new Context()
  38. await ctx.plugin(Lsp)
  39. await ctx.plugin(LspLocal, {
  40. servers: {
  41. fake: {
  42. command: ${JSON.stringify(process.execPath)},
  43. args: [${JSON.stringify(fixtureServer)}],
  44. env: { LSP_FAKE_DEF: ${JSON.stringify(location)} },
  45. extensionToLanguage: { '.ts': 'typescript' },
  46. },
  47. },
  48. })
  49. const result = await ctx.lsp.query({ operation: 'goToDefinition', filePath: 'a.ts', position: { line: 0, character: 6 }, workspaceRoot: ${JSON.stringify(ws)} })
  50. console.log(JSON.stringify(result))
  51. await ctx.fiber.dispose()
  52. `
  53. const child = spawn(process.execPath, ['--input-type=module', '-e', script], { cwd: pkgDir, stdio: ['ignore', 'pipe', 'pipe'] })
  54. let stdout = ''
  55. let stderr = ''
  56. child.stdout.on('data', (chunk: Buffer) => { stdout += chunk.toString('utf8') })
  57. child.stderr.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8') })
  58. const exitCode = await new Promise<number | null>(resolve => child.on('close', resolve))
  59. expect(exitCode, `stderr:\n${stderr}`).toBe(0)
  60. const lastLine = stdout.trim().split('\n').at(-1) ?? ''
  61. const result = JSON.parse(lastLine) as { kind: string; locations: unknown[] }
  62. expect(result.kind).toBe('locations')
  63. expect(result.locations).toHaveLength(1)
  64. }, 60_000)
  65. })