schemas.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 { serializeSchema, synthesizeWorkflowSchemas } from "../src/index.js";
  6. // Fixture-driven schema-synthesis snapshots: every tests/schemas/*.ts file must
  7. // typecheck + synthesize clean, and each typed ask site's JSON Schema is snapshotted
  8. // under tests/schemas/expected/<base>.<siteId>.json (siteId `ask#1` -> `ask-1` on disk,
  9. // to stay cross-platform). Run `pnpm test -- -u` to (re)generate, then review by hand.
  10. const schemasDir = join(dirname(fileURLToPath(import.meta.url)), "schemas");
  11. describe("schema synthesis fixtures", () => {
  12. const fixtures = readdirSync(schemasDir)
  13. .filter((name) => name.endsWith(".ts"))
  14. .sort();
  15. it("finds fixtures", () => {
  16. expect(fixtures.length).toBeGreaterThan(0);
  17. });
  18. for (const fixture of fixtures) {
  19. it(fixture, async () => {
  20. const source = readFileSync(join(schemasDir, fixture), "utf8");
  21. const result = synthesizeWorkflowSchemas(source);
  22. expect(result.diagnostics).toEqual([]);
  23. const ids = Object.keys(result.schemas).sort(bySiteOrdinal);
  24. expect(ids.length).toBeGreaterThan(0);
  25. const base = fixture.replace(/\.ts$/, "");
  26. for (const id of ids) {
  27. const fileId = id.replace("#", "-");
  28. await expect(serializeSchema(result.schemas[id]!)).toMatchFileSnapshot(
  29. join(schemasDir, "expected", `${base}.${fileId}.json`),
  30. );
  31. }
  32. });
  33. }
  34. });
  35. /** `ask#2` < `ask#10`:按尾部序号数值排序,snapshot 顺序稳定。 */
  36. function bySiteOrdinal(a: string, b: string): number {
  37. const ordinal = (id: string): number => Number(id.slice(id.indexOf("#") + 1));
  38. return ordinal(a) - ordinal(b);
  39. }