built-worker.e2e.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.cjs')
  11. const run = promisify(execFile)
  12. /**
  13. * Keyless built-artifact guard: plain Node loads `lib/index.js` and its sibling
  14. * `lib/worker.cjs` without tsx. Skips until the build produces both bundles.
  15. */
  16. describe.skipIf(!existsSync(builtIndex) || !existsSync(builtWorker))('built worker entry (lib/worker.cjs)', () => {
  17. it('the built engine spawns its built worker under plain node and completes a run', async () => {
  18. // Keep the driver in-package so bare imports resolve its node_modules.
  19. const driver = join(packageRoot, `.built-worker-driver-${process.pid}.mjs`)
  20. try {
  21. await writeFile(driver, `
  22. import { Context } from '@deepseek-ai/cordis'
  23. import SubagentRuntime from '@deepseek-ai/dsh-subagent'
  24. import WorkerThreadWorkflowEngine from '@deepseek-ai/dsh-workflow-worker-thread'
  25. const ctx = new Context()
  26. await ctx.plugin(SubagentRuntime)
  27. let selectedStarts = 0
  28. ctx.subagents.registerProvider({
  29. name: 'built-selected',
  30. capabilities: { agentOptions: true, outputSchema: true, depthLimit: false, toolFilter: false, persona: false },
  31. inheritsParentContext: false,
  32. async start() {
  33. selectedStarts += 1
  34. return {
  35. id: 'built-child',
  36. result: Promise.resolve({ output: [], structured: { answer: 42 }, stopReason: 'completed' }),
  37. dispose: () => Promise.resolve(),
  38. }
  39. },
  40. })
  41. await ctx.plugin(WorkerThreadWorkflowEngine, { provider: 'must-not-be-used' })
  42. const run = ctx.workflowEngine.start({
  43. script: "const value = await agent('answer', { schema: { type: 'object', properties: { answer: { type: 'number' } }, required: ['answer'] } }); return value.answer",
  44. meta: { name: 'built-smoke', description: 'built worker smoke' },
  45. subagentProvider: 'built-selected',
  46. parent: { id: 'built-smoke-parent', options: {} },
  47. })
  48. const result = await run.result
  49. await run.dispose()
  50. if (result.stopReason !== 'completed' || result.value !== 42 || selectedStarts !== 1) {
  51. console.error('unexpected result: ' + JSON.stringify(result))
  52. process.exit(1)
  53. }
  54. console.log('built-worker-smoke-ok')
  55. `, 'utf8')
  56. const { stdout } = await run(process.execPath, [driver], { cwd: packageRoot, timeout: 60_000 })
  57. expect(stdout).toContain('built-worker-smoke-ok')
  58. } finally {
  59. await rm(driver, { force: true })
  60. }
  61. }, 120_000)
  62. })