63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import {
|
|
executePlans,
|
|
mergeMeasurements,
|
|
type BackfillPlan,
|
|
} from "../scripts/backfill-flatness-suite-measurements";
|
|
|
|
function plan(id: number): BackfillPlan {
|
|
return {
|
|
row: { id, taskId: 24, measurements: [], resultData: { kind: "plugin", marker: "kept" } } as BackfillPlan["row"],
|
|
resultJsonPath: `/tmp/${id}/result.json`,
|
|
originalResultJson: { status: "completed", extra: { module: "contour_batch" } },
|
|
measurements: [{
|
|
name: "任务11 · 车钩面板的平整度",
|
|
value: null,
|
|
unit: "mm",
|
|
toleranceLabel: "0.5mm",
|
|
status: "fail",
|
|
passLabel: "成功",
|
|
warningLabel: "部分成功",
|
|
failLabel: "失败",
|
|
}],
|
|
};
|
|
}
|
|
|
|
describe("flatness-suite 历史回填", () => {
|
|
it("dry-run 不调用持久化", async () => {
|
|
const persist = vi.fn(async () => true);
|
|
const summary = await executePlans([plan(234)], false, persist);
|
|
expect(persist).not.toHaveBeenCalled();
|
|
expect(summary).toEqual({ planned: 1, applied: 0, skipped: 1, failed: 0 });
|
|
});
|
|
|
|
it("apply 由持久化层守卫实现幂等", async () => {
|
|
const applied = new Set<number>();
|
|
const persist = vi.fn(async (item: BackfillPlan) => {
|
|
if (applied.has(item.row.id)) return false;
|
|
applied.add(item.row.id);
|
|
return true;
|
|
});
|
|
expect((await executePlans([plan(234)], true, persist)).applied).toBe(1);
|
|
const second = await executePlans([plan(234)], true, persist);
|
|
expect(second.applied).toBe(0);
|
|
expect(second.skipped).toBe(1);
|
|
});
|
|
|
|
it("异常记录不阻断其他记录", async () => {
|
|
const persist = vi.fn(async (item: BackfillPlan) => {
|
|
if (item.row.id === 1) throw new Error("bad excel");
|
|
return true;
|
|
});
|
|
const summary = await executePlans([plan(1), plan(2)], true, persist);
|
|
expect(summary).toEqual({ planned: 2, applied: 1, skipped: 0, failed: 1 });
|
|
});
|
|
|
|
it("扩展字段完整进入 resultData 且保留已有字段", () => {
|
|
const item = plan(234);
|
|
const merged = mergeMeasurements(item.row.resultData, item.measurements);
|
|
expect(merged.marker).toBe("kept");
|
|
expect(merged.measurements).toEqual(item.measurements);
|
|
});
|
|
});
|