built-lib.e2e.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { existsSync } from 'node:fs'
  2. import { join } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { execa } from 'execa'
  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('@deepseek-ai/cordis')
  19. const { WorkerThreadCodeRuntime } = await import('@deepseek-ai/dsh-code-runtime-worker-thread')
  20. const ctx = new Context()
  21. await ctx.plugin(WorkerThreadCodeRuntime, {})
  22. const result = await ctx.codeRuntime.run({
  23. program: 'const doubled: number = await tools.double({ n: 21 }); console.log("halfway", doubled); let failure; try { await tools.fail({}) } catch (error) { failure = { typed: error instanceof ToolCallError, name: error.name, toolName: error.toolName, message: error.message } } return { doubled, failure };',
  24. bindings: [{
  25. global: 'tools',
  26. functions: {
  27. double: async args => args.n * 2,
  28. fail: async () => { throw new Error('denied') },
  29. },
  30. errorClass: { name: 'ToolCallError', memberNameProperty: 'toolName' },
  31. }],
  32. })
  33. console.log(JSON.stringify(result))
  34. process.exit(0)
  35. `
  36. const { exitCode, stdout, stderr } = await execa(process.execPath, ['--input-type=module', '-e', script], {
  37. cwd: pkgDir,
  38. stdin: 'ignore',
  39. timeout: 55_000,
  40. killSignal: 'SIGKILL',
  41. reject: false,
  42. })
  43. expect(exitCode, `stderr:\n${stderr}`).toBe(0)
  44. const lastLine = stdout.trim().split('\n').at(-1) ?? ''
  45. const result = JSON.parse(lastLine) as { value?: unknown; logs: string[]; error?: unknown }
  46. expect(result.error).toBeUndefined()
  47. expect(result.value).toEqual({
  48. doubled: 42,
  49. failure: { typed: true, name: 'ToolCallError', toolName: 'fail', message: 'denied' },
  50. })
  51. expect(result.logs).toContain('halfway 42')
  52. })
  53. })