helpers.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * 集成测试共享装配:脚本文本 → 真实 schema 合成 + 真实 validate + 真实 lowering → 沙箱子进程
  3. * → 引擎核心 → AutoDriver。整条 phase-1 管线(短一个真模型)都在这里跑通。
  4. */
  5. import { mkdtempSync } from "node:fs";
  6. import { tmpdir } from "node:os";
  7. import { join } from "node:path";
  8. import {
  9. InMemoryJournalStore,
  10. buildAskSpecs,
  11. collectSites,
  12. createWorkflowProgram,
  13. synthesizeAskSchemas,
  14. validate,
  15. type AskSpec,
  16. type Caps,
  17. type JsonSchema,
  18. type RunSettlement,
  19. type ValidateFn,
  20. } from "@zcode/dynamic-workflow";
  21. import { runWorkflowScript } from "../src/index.js";
  22. import { AutoDriver, type AutoDriverConfig } from "./auto-driver.js";
  23. /** 适配真实校验器到引擎的 ValidateFn 契约(schema: JsonSchema → unknown 的形参放宽)。 */
  24. const validateFn: ValidateFn = (schema, value) => validate(schema as JsonSchema, value);
  25. /**
  26. * 由脚本文本合成 ask 规格:走包里的规范构造器 {@link buildAskSpecs}(按站点表遍历,
  27. * untyped 站点显式记 `{typed:false}`——引擎把站点缺席当接线错误硬失败)。
  28. *
  29. * 同时演示「编译一次」:一个 ts.Program 同时喂站点表与 schema 合成。
  30. */
  31. export function askSpecsFor(scriptText: string): Map<string, AskSpec> {
  32. const workflow = createWorkflowProgram(scriptText);
  33. const table = collectSites(workflow);
  34. const { schemas } = synthesizeAskSchemas(workflow, table);
  35. return buildAskSpecs(table, schemas);
  36. }
  37. interface RunScriptResult {
  38. settlement: RunSettlement;
  39. journal: InMemoryJournalStore;
  40. driver: AutoDriver;
  41. }
  42. interface RunScriptOptions extends AutoDriverConfig {
  43. caps?: Caps;
  44. timeoutMs?: number;
  45. journal?: InMemoryJournalStore;
  46. runId?: string;
  47. /** 外部取消信号(resume 测试用它把首趟打断在执行中,制造 running 记录)。 */
  48. signal?: AbortSignal;
  49. }
  50. const DEFAULT_CAPS: Caps = { maxConcurrency: 16 };
  51. /**
  52. * 每个测试文件一份独立的 run cwd。harness 把入口文件写到 `<cwd>/.zcode/workflow-runs/<runId>.mjs`
  53. * 且**保留**;测试几乎都用 runId "run",若共用 process.cwd(),并行的测试文件会互相覆写同一个
  54. * 入口文件(一个子进程跑成另一个文件的脚本),也会把存档留在包目录里。vitest 按文件隔离模块,
  55. * 这个常量因此是每文件一份;文件内的用例串行,同名覆写无害。
  56. */
  57. export const TEST_CWD: string = mkdtempSync(join(tmpdir(), "dwf-test-cwd-"));
  58. /** 用真实管线跑一份脚本;AutoDriver 按 opts 里的 asks/worldReads 自动扮演模型。 */
  59. export async function runScript(scriptText: string, opts: RunScriptOptions = {}): Promise<RunScriptResult> {
  60. const journal = opts.journal ?? new InMemoryJournalStore();
  61. const driver = new AutoDriver(journal, {
  62. asks: opts.asks,
  63. worldReads: opts.worldReads,
  64. artifacts: opts.artifacts,
  65. onStartAsk: opts.onStartAsk,
  66. });
  67. const settlement = await runWorkflowScript({
  68. scriptText,
  69. cwd: TEST_CWD,
  70. runId: opts.runId ?? "run",
  71. caps: opts.caps ?? DEFAULT_CAPS,
  72. askSpecs: askSpecsFor(scriptText),
  73. validate: validateFn,
  74. makeDriver: (sink) => {
  75. driver.attach(sink);
  76. return driver;
  77. },
  78. timeoutMs: opts.timeoutMs ?? 20000,
  79. ...(opts.signal === undefined ? {} : { signal: opts.signal }),
  80. });
  81. return { settlement, journal, driver };
  82. }