built-lib.e2e.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { spawn } from 'node:child_process'
  2. import { existsSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { describe, expect, it } from 'vitest'
  6. /**
  7. * Keyless built-artifact smoke: plain Node imports the package by name through its exports map,
  8. * then exercises type stripping, sibling `worker.cjs` loading, bindings, and logs. Unit tests use
  9. * `src/worker.ts`; this pins the downstream `lib/index.js` path. It skips when `lib/` is absent,
  10. * and CI runs it after the build.
  11. */
  12. const pkgDir = fileURLToPath(new URL('..', import.meta.url))
  13. const built = ['lib/index.js', 'lib/worker.cjs'].every(file => existsSync(join(pkgDir, file)))
  14. && existsSync(join(pkgDir, '../code-runtime/lib/index.js'))
  15. describe.skipIf(!built)('built lib real load path (plain node)', () => {
  16. it('runs a TypeScript program with a binding through lib/index.js and its lib/worker.cjs entry', async () => {
  17. const script = `
  18. const { Context } = await import('cordis')
  19. const { WorkerCodeRuntime } = await import('@deepseek-ai/dsh-code-runtime-worker')
  20. const ctx = new Context()
  21. await ctx.plugin(WorkerCodeRuntime, {})
  22. const result = await ctx.codeRuntime.run({
  23. program: 'const doubled: number = await tools.double({ n: 21 }); console.log("halfway", doubled); return doubled;',
  24. bindings: [{ global: 'tools', functions: { double: async args => args.n * 2 } }],
  25. })
  26. console.log(JSON.stringify(result))
  27. process.exit(0)
  28. `
  29. const child = spawn(process.execPath, ['--input-type=module', '-e', script], { cwd: pkgDir, stdio: ['ignore', 'pipe', 'pipe'] })
  30. let stdout = ''
  31. let stderr = ''
  32. child.stdout.on('data', (chunk: Buffer) => { stdout += chunk.toString('utf8') })
  33. child.stderr.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8') })
  34. const exitCode = await new Promise<number | null>(resolve => child.on('close', resolve))
  35. expect(exitCode, `stderr:\n${stderr}`).toBe(0)
  36. const lastLine = stdout.trim().split('\n').at(-1) ?? ''
  37. const result = JSON.parse(lastLine) as { value?: unknown; logs: string[]; error?: unknown }
  38. expect(result.error).toBeUndefined()
  39. expect(result.value).toBe(42)
  40. expect(result.logs).toContain('halfway 42')
  41. })
  42. })