schema-errors.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { readdirSync, readFileSync } from "node:fs";
  2. import { dirname, join } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { describe, expect, it } from "vitest";
  5. import { collectDiagnostics, createWorkflowProgram } from "../src/compiler/compile.js";
  6. import { collectSites } from "../src/analysis/sites.js";
  7. import { synthesizeAskSchemas } from "../src/index.js";
  8. import { diffAgainstMarkers, parseExpectedErrors } from "./helpers/markers.js";
  9. // Fixture-driven schema-REJECTION suite. A sibling of tests/workflows/, but driven by
  10. // schema synthesis rather than by compileWorkflowScript (which only typechecks and never
  11. // runs synthesis). Every tests/schema-errors/*.ts file must typecheck clean (so T
  12. // resolves) and its trailing `// error` markers state the schema-rejection diagnostics
  13. // expected at the ask site. Matching is strict and bidirectional (see helpers/markers.ts).
  14. const errorsDir = join(dirname(fileURLToPath(import.meta.url)), "schema-errors");
  15. describe("schema rejection fixtures", () => {
  16. const fixtures = readdirSync(errorsDir)
  17. .filter((name) => name.endsWith(".ts"))
  18. .sort();
  19. it("finds fixtures", () => {
  20. expect(fixtures.length).toBeGreaterThan(0);
  21. });
  22. for (const fixture of fixtures) {
  23. it(fixture, () => {
  24. const source = readFileSync(join(errorsDir, fixture), "utf8");
  25. const workflow = createWorkflowProgram(source);
  26. // 前置:脚本必须 typecheck 通过,否则是 fixture 自身的问题,而非 schema 拒绝。
  27. expect(collectDiagnostics(workflow.program)).toEqual([]);
  28. const { diagnostics } = synthesizeAskSchemas(workflow, collectSites(workflow));
  29. const expected = parseExpectedErrors(source);
  30. expect(diffAgainstMarkers(expected, diagnostics)).toEqual([]);
  31. });
  32. }
  33. });