droboticflow/tests/frontend/annotation-source-validation-ui.test.mjs

110 lines
4.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from "node:test";
import assert from "node:assert/strict";
import { annotationTimelineFailure } from "../../src/lib/annotation-source-validation.js";
import { reviewSeekHarness } from "./review-seek-harness.mjs";
function text(node) {
if (node == null || typeof node === "boolean") return "";
if (Array.isArray(node)) return node.map(text).join("");
return typeof node === "object" ? text(node.props?.children) : String(node);
}
test("terminal source failure renders the exact safe cause and recovery instead of loading", () => {
let reads = 0;
const f = reviewSeekHarness({
frameTimelineStatus: "error",
frameTimelineFailure: {
sourceValidation: true,
status: "failed",
message: "IMU 第 418451 行类型无效:gyr",
recovery: "数据提供方修复源数据后,由管理员重新校验。",
retryAllowed: false,
},
onRetryFrameTimeline: () => reads++,
});
const initial = structuredClone(f.props.slices);
try {
const notice = f.nodes().find((node) => node.props["data-testid"] === "frame-timeline-status");
assert.equal(notice.props.role, "alert");
assert.match(text(notice), /418451.*gyr/);
assert.match(text(notice), /数据提供方.*管理员重新校验/);
assert.doesNotMatch(text(notice), /正在加载/);
assert.equal(f.nodes().filter((node) => node.type === "button" && text(node) === "重新校验").length, 0);
const refresh = f.nodes().find((node) => node.type === "button" && text(node) === "刷新状态");
refresh.props.onClick();
assert.equal(reads, 1);
assert.deepEqual(f.props.slices, initial);
} finally {
f.destroy();
}
});
test("source retry requires the server permission and disappears after recovery", () => {
let retries = 0;
const f = reviewSeekHarness({
frameTimelineStatus: "error",
frameTimelineFailure: {
sourceValidation: true,
status: "failed",
message: "源数据校验失败",
recovery: "数据提供方修复源数据后,由管理员重新校验。",
retryAllowed: true,
},
onRetrySourceValidation: () => retries++,
});
try {
const retry = f.nodes().find((node) => node.type === "button" && text(node) === "重新校验");
assert.ok(retry);
retry.props.onClick();
assert.equal(retries, 1);
f.props.frameTimelineStatus = "ready";
f.flush();
assert.equal(
f.nodes().some((node) => node.props["data-testid"] === "frame-timeline-status"),
false,
);
} finally {
f.destroy();
}
});
test("source status mapping preserves actionable facts and limits retry to server permission", () => {
const error = {
code: "source_validation_failed",
details: { sourceValidation: { status: "failed", message: "IMU 第 418451 行类型无效:gyr", retryAllowed: false } },
};
assert.match(annotationTimelineFailure(error).message, /418451.*gyr/);
assert.equal(annotationTimelineFailure(error).retryAllowed, false);
error.details.sourceValidation.retryAllowed = true;
assert.equal(annotationTimelineFailure(error).retryAllowed, true);
error.code = "source_validation_pending";
error.details.sourceValidation.status = "pending";
error.details.sourceValidation.message = "源数据校验中";
assert.equal(annotationTimelineFailure(error).retryAllowed, false);
assert.equal(annotationTimelineFailure(error).recovery, "");
assert.equal(annotationTimelineFailure({ code: "other", details: error.details }).sourceValidation, false);
assert.equal(annotationTimelineFailure(new Error("Failed to fetch")).retryAllowed, false);
});
test("pending validation is a refreshable state, not an endless loading spinner or a failure", () => {
const f = reviewSeekHarness({
frameTimelineStatus: "error",
frameTimelineFailure: {
sourceValidation: true,
status: "pending",
message: "源数据校验中",
recovery: "",
retryAllowed: false,
},
onRetryFrameTimeline: () => {},
});
try {
const notice = f.nodes().find((node) => node.props["data-testid"] === "frame-timeline-status");
assert.equal(notice.props.role, "status");
assert.match(text(notice), /源数据校验中.*刷新状态/);
assert.doesNotMatch(text(notice), /加载失败|正在加载/);
} finally {
f.destroy();
}
});