fix: 恢复工作流 JSON Schema 导出
在导出视图中明确 alarm 转换后的可空对象结构,保留业务解析行为和输出契约。 重新生成 Schema,补齐真实导出、可重现性与递归引用回归,更新说明和修复报告。 验证:930 项测试通过,类型与版本检查通过,导出脚本及新增测试单独类型检查通过。
This commit is contained in:
1 parent
c71b20c25b
commit
01fcebdb1c
5 files changed
+415
-210
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { WorkflowDetailSchema } from "../../src/domains/platform/types.js";
|
||||
|
||||
test("工作流 Schema 导出可重现,保留归一化输出与有效递归引用", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hose-workflow-schema-"));
|
||||
try {
|
||||
const script = resolve(import.meta.dir, "export-workflow-schema.ts");
|
||||
const generate = () => {
|
||||
const result = spawnSync(process.execPath, [script], { cwd: dir, encoding: "utf8" });
|
||||
expect(result.status, result.stderr).toBe(0);
|
||||
return readFileSync(join(dir, "specs/workflow-schema.json"), "utf8");
|
||||
};
|
||||
const first = generate();
|
||||
expect(generate()).toBe(first);
|
||||
expect(first).toBe(readFileSync(resolve(import.meta.dir, "../../specs/workflow-schema.json"), "utf8"));
|
||||
const document = JSON.parse(first);
|
||||
expect(Object.keys(document.schemas)).toEqual(["WorkflowDetail", "WorkflowTask", "Workflow"]);
|
||||
const detail = document.schemas.WorkflowDetail;
|
||||
expect(detail.properties.state.type).toBe("number");
|
||||
expect(detail.required).toContain("tasks");
|
||||
expect(detail.required).toContain("state");
|
||||
expect(detail.required).not.toContain("alarm");
|
||||
const alarm = detail.properties.alarm.anyOf;
|
||||
expect(alarm.map((branch: { type: string }) => branch.type)).toEqual(["object", "null"]);
|
||||
expect(alarm[0].properties.active.type).toBe("boolean");
|
||||
expect(alarm[0].required).toContain("alarmUser");
|
||||
for (const input of [{ active: false }, '{"active":false}']) {
|
||||
const normalized = WorkflowDetailSchema.parse({ id: "fixture", state: "1", alarm: input });
|
||||
expect(normalized.state).toBe(1);
|
||||
expect(normalized.alarm).toEqual({ active: false, alarmUser: [], alarmType: [], alarmScope: "" });
|
||||
}
|
||||
expect(WorkflowDetailSchema.parse({ id: "fixture", alarm: null }).alarm).toBeNull();
|
||||
expect(WorkflowDetailSchema.parse({ id: "fixture", alarm: "invalid JSON" }).alarm).toBeUndefined();
|
||||
for (const schema of Object.values(document.schemas)) {
|
||||
let refs = 0;
|
||||
const visit = (node: any): void => {
|
||||
if (!node || typeof node !== "object") return;
|
||||
if (typeof node.$ref === "string") {
|
||||
expect(node.$ref === "#" || node.$ref.startsWith("#/")).toBe(true);
|
||||
const target = node.$ref === "#" ? schema : node.$ref.slice(2).split("/").reduce((value: any, key: string) => value?.[key.replace(/~1/g, "/").replace(/~0/g, "~")], schema);
|
||||
expect(target).toBeDefined();
|
||||
refs++;
|
||||
}
|
||||
Object.values(node).forEach(visit);
|
||||
};
|
||||
visit(schema);
|
||||
expect(refs).toBeGreaterThan(0);
|
||||
}
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
import { writeFileSync, mkdirSync } from "fs";
|
||||
import { join, dirname } from "path";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
WorkflowAlarmSchema,
|
||||
WorkflowDetailSchema,
|
||||
WorkflowTaskSchema,
|
||||
WorkflowSchema,
|
||||
@@ -14,9 +16,14 @@ function writeJson(path: string, data: unknown): void {
|
||||
writeFileSync(path, JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
const workflowDetailJsonSchema = (WorkflowDetailSchema as any).toJSONSchema();
|
||||
const workflowTaskJsonSchema = (WorkflowTaskSchema as any).toJSONSchema();
|
||||
const workflowJsonSchema = (WorkflowSchema as any).toJSONSchema();
|
||||
// 导出运行时归一化后的结构:alarm 的 JSON 字符串已解析为对象或 undefined。
|
||||
// JSON Schema 无法推导 transform 函数,因而只在导出视图中声明其输出,保留域层解析逻辑。
|
||||
const workflowDetailOutputSchema = WorkflowDetailSchema.extend({
|
||||
alarm: WorkflowAlarmSchema.nullable().optional(),
|
||||
});
|
||||
const workflowDetailJsonSchema = z.toJSONSchema(workflowDetailOutputSchema);
|
||||
const workflowTaskJsonSchema = z.toJSONSchema(WorkflowTaskSchema);
|
||||
const workflowJsonSchema = z.toJSONSchema(WorkflowSchema);
|
||||
|
||||
writeJson(OUTPUT_FILE, {
|
||||
$schema: "https://json-schema.org/draft/2020-12/schema",
|
||||
|
||||
Reference in new issue
Block a user