built-worker.e2e.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { existsSync } from 'node:fs'
  2. import { rm, writeFile } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. import { execFile } from 'node:child_process'
  5. import { promisify } from 'node:util'
  6. import { fileURLToPath } from 'node:url'
  7. import { describe, expect, it } from 'vitest'
  8. const packageRoot = fileURLToPath(new URL('..', import.meta.url))
  9. const builtIndex = join(packageRoot, 'lib', 'index.js')
  10. const builtWorker = join(packageRoot, 'lib', 'worker.js')
  11. const run = promisify(execFile)
  12. /**
  13. * The BUILT-output guard for the worker entry: every other suite runs
  14. * unbuilt (src/ + tsx), so nothing else proves that `lib/index.js` resolves
  15. * its sibling `lib/worker.js` and that the bundle boots a worker under plain
  16. * node (no tsx loader). Keyless — a zero-agent script needs no provider —
  17. * and self-skips until `pnpm run build` has produced the bundles.
  18. */
  19. describe.skipIf(!existsSync(builtIndex) || !existsSync(builtWorker))('built worker entry (lib/worker.js)', () => {
  20. it('the built engine spawns its built worker under plain node and completes a run', async () => {
  21. // ESM resolves bare specifiers from the IMPORTING FILE's location, so the
  22. // driver must live inside the package for its node_modules to apply — a
  23. // temp-named file at the package root, removed on the way out.
  24. const driver = join(packageRoot, `.built-worker-driver-${process.pid}.mjs`)
  25. try {
  26. await writeFile(driver, `
  27. import { Context } from 'cordis'
  28. import SubagentService from '@deepseek-ai/dsh-subagent'
  29. import WorkerWorkflowEngine from '@deepseek-ai/dsh-workflow-workerthread'
  30. const ctx = new Context()
  31. await ctx.plugin(SubagentService)
  32. await ctx.plugin(WorkerWorkflowEngine, {})
  33. const run = ctx.workflows.start({
  34. script: 'return 6 * 7',
  35. meta: { name: 'built-smoke', description: 'built worker smoke' },
  36. // A zero-agent script never touches the provider, so a bare id suffices.
  37. parent: { id: 'built-smoke-parent', options: {} },
  38. })
  39. const result = await run.result
  40. await run.dispose()
  41. if (result.stopReason !== 'completed' || result.value !== 42) {
  42. console.error('unexpected result: ' + JSON.stringify(result))
  43. process.exit(1)
  44. }
  45. console.log('built-worker-smoke-ok')
  46. `, 'utf8')
  47. // Plain node — no tsx loader anywhere; the bundle must stand on its own.
  48. const { stdout } = await run(process.execPath, [driver], { cwd: packageRoot, timeout: 60_000 })
  49. expect(stdout).toContain('built-worker-smoke-ok')
  50. } finally {
  51. await rm(driver, { force: true })
  52. }
  53. }, 120_000)
  54. })