droboticflow/tests/browser/acceptance-responsiveness.mjs

284 lines
13 KiB
JavaScript

// Diagnostic fixture only: actual production components, synthetic media/API.
import { chromium } from "playwright";
import fs from "node:fs/promises";
import { execFileSync } from "node:child_process";
import assert from "node:assert/strict";
const out =
process.env.ACCEPTANCE_TEST_OUTPUT || new URL("../../local-data/verification/acceptance/", import.meta.url).pathname;
await fs.mkdir(out, { recursive: true });
execFileSync("ffmpeg", [
"-nostdin",
"-hide_banner",
"-loglevel",
"error",
"-y",
"-f",
"lavfi",
"-i",
"testsrc2=s=320x240:r=30:d=12",
"-c:v",
"libx264",
"-g",
"30",
"-pix_fmt",
"yuv420p",
out + "fixture.mp4",
]);
const media = await fs.readFile(out + "fixture.mp4");
const browser = await chromium.launch({
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
const delay = Number(process.env.DIAG_METADATA_DELAY_MS || "300");
assert(
["127.0.0.1", "localhost"].includes(new URL(process.env.ACCEPTANCE_TEST_URL || "http://127.0.0.1:5199").hostname),
);
const report = { baseline: process.env.TEST_REVISION || "candidate", metadataDelayMs: delay, runs: [], errors: [] };
try {
for (const mode of ["quality", "acceptance-physical", "acceptance-source"]) {
const page = await browser.newPage({ viewport: { width: 1500, height: 1000 } });
page.on("pageerror", (e) => report.errors.push(e.message));
const requests = [];
const source = mode === "acceptance-source";
const names = [
"left.mp4",
"right.mp4",
"annotation.json",
"synchronized-slice.json",
...(source ? ["source-index.json"] : ["left_pts.csv", "right_pts.csv", "imu.csv"]),
];
await page.route("**/api/**", async (route) => {
const p = decodeURIComponent(new URL(route.request().url()).pathname);
if (!p.startsWith("/api/")) return route.continue();
requests.push(p);
if (p.endsWith("/members"))
return route.fulfill({
json: {
items: [1, 2, 3].flatMap((i) =>
names.map((name) => ({
id: `${i}-${name}`,
name,
group: `slices/slice-${String(i).padStart(6, "0")}`,
sizeBytes: 100,
contentAvailable: true,
})),
),
total: names.length * 3,
offset: 0,
limit: 200,
recognized: true,
truncated: false,
},
});
if (p.endsWith("/content")) {
if (p.includes(".mp4")) {
const range = route
.request()
.headers()
["range"]?.match(/bytes=(\d+)-(\d*)/);
if (!range)
return route.fulfill({ contentType: "video/mp4", body: media, headers: { "Accept-Ranges": "bytes" } });
const start = Number(range[1]),
end = range[2] ? Math.min(Number(range[2]), media.length - 1) : media.length - 1;
return route.fulfill({
status: 206,
contentType: "video/mp4",
body: media.subarray(start, end + 1),
headers: { "Accept-Ranges": "bytes", "Content-Range": `bytes ${start}-${end}/${media.length}` },
});
}
await new Promise((r) => setTimeout(r, delay));
if (p.includes("annotation.json"))
return route.fulfill({
json: { actions: [{ id: "a", clipStartMs: 0, clipEndMs: 4000, description: "合成动作" }] },
});
if (p.includes("synchronized-slice.json")) {
const i = Number(p.match(/\/(\d)-synchronized/)?.[1] || 1);
return route.fulfill({
json: {
window: { startTimestampUs: (i - 1) * 4000000, endTimestampUs: i * 4000000 },
boundaryContract: "hardware-frame-v1",
boundarySeconds: Array.from({ length: 121 }, (_, j) => j / 30),
left: { pts: { rowCount: 120 } },
...(source
? {
sourceIndexSchema: "droboticflow.source-slice-index/v1",
sourceMediaOffsets: { left: (i - 1) * 4, right: (i - 1) * 4 },
rightBoundarySeconds: Array.from({ length: 121 }, (_, j) => j / 30),
}
: {}),
},
});
}
return route.fulfill({
contentType: "text/csv",
body: "clip_time_ms\n" + Array.from({ length: 120 }, (_, i) => (i * 1000) / 30).join("\n"),
});
}
return route.fulfill({ json: { items: [] } });
});
await page.route("**/__acceptance_diagnosis", (route) =>
route.fulfill({
contentType: "text/html; charset=utf-8",
body: `<!doctype html><html><body><main class="annotation-operations-page"><div id="root"></div></main><script type="module">
import F from '/@react-refresh';F.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>x=>x;window.__vite_plugin_react_preamble_installed__=true;
await import('/src/index.css');await import('/src/styles/capture-workspace.css');await import('/src/styles/review-workspace.css');
const R=await import('/node_modules/.vite/deps/react.js');const React=R.default||R;const D=await import('/node_modules/.vite/deps/react-dom_client.js');
const {FinalAcceptanceWorkspace}=await import('/src/components/FinalAcceptanceWorkspace.tsx');
const {CaptureAnnotationWorkbench}=await import('/src/components/CaptureAnnotationWorkbench.tsx');
const slices=[{id:'s1',name:'商品盘点',start:0,end:4},{id:'s2',name:'',start:4,end:8,deliveryDisposition:'discard',discardReason:'玩手机'},{id:'s3',name:'门店巡视',start:8,end:12}];
const item={id:'accept',version:1,status:'claimed',assignedUserId:'u',processingProfile:'raw_human',subject:{payload:{items:[{sourceHumanItemId:'source',materialization:{artifactId:'mat',revision:1,manifestSha256:'one',sliceCount:3,coverage:{contiguous:true,gapMicroseconds:0,overlapMicroseconds:0}},finalSlices:slices}]}}};
const acceptProps={item,mine:true,saving:false,focusedLayout:true,draft:{headpose:'include',handpose:'include',exclusionReason:''},comment:'切片问题:请修改',onDraftChange:()=>{},onCommentChange:()=>{},onApprove:()=>{},onReject:()=>{},onReviewPositionChange:p=>window.lastReviewPosition=p};
const qcProps={taskId:'quality',focusedLayout:true,isQualityReview:true,captionsViewable:true,videoSource:'/api/mock/left.mp4/content',rightVideoSource:'/api/mock/right.mp4/content',browserReadyMedia:true,clipStart:0,clipEnd:12,sourceIsFullVideo:true,slices,segments:[],labels:[],statuses:{},phase:'annotate',frameTimelineStatus:'ready',frameBoundaries:Array.from({length:361},(_,j)=>j/30),unifiedBoundaries:true,onChange:()=>{}};
const root=(D.createRoot||D.default.createRoot)(document.getElementById('root'));
window.setProblem=(comment)=>root.render(React.createElement(${JSON.stringify(mode)}==='quality'?CaptureAnnotationWorkbench:FinalAcceptanceWorkspace,${JSON.stringify(mode)}==='quality'?qcProps:{...acceptProps,comment}));
window.setProblem(acceptProps.comment);
</script></body></html>`,
}),
);
await page.goto((process.env.ACCEPTANCE_TEST_URL || "http://127.0.0.1:5199") + "/__acceptance_diagnosis");
const settle = () =>
page.waitForFunction(
() => {
const v = document.querySelector('[data-camera="left"]');
return (
v && v.readyState >= 2 && !v.seeking && !document.querySelector('[data-testid="frame-timeline-status"]')
);
},
{},
{ timeout: 30000 },
);
try {
await settle();
} catch (e) {
console.log(mode, await page.locator("body").innerText(), report.errors);
throw e;
}
const row = { mode, initialRequests: requests.length, switches: [] };
if (mode !== "quality") {
row.discardTitle = await page.getByLabel("验收切片选择", { exact: true }).getByRole("button").nth(1).innerText();
row.approveEnabledWithProblem = await page.getByRole("button", { name: "验收通过", exact: true }).isEnabled();
}
if (mode === "acceptance-source") {
const root = page.getByTestId("final-acceptance-workspace");
await root.focus();
await page.keyboard.press("Control+ArrowRight");
await page.keyboard.press("Control+ArrowRight");
await page.keyboard.press("Control+ArrowLeft");
await page.waitForFunction(
() =>
document.querySelectorAll('[aria-label="验收切片选择"] button')[1]?.getAttribute("aria-pressed") === "true" &&
Math.abs(document.querySelector('[data-camera="left"]').currentTime - 4) < 0.04,
);
await settle();
row.latestNavigationWins = true;
await page.keyboard.press("Control+ArrowLeft");
await page.waitForFunction(() => Math.abs(document.querySelector('[data-camera="left"]').currentTime) < 0.04);
await settle();
}
for (const key of ["Control+ArrowRight", "Control+ArrowLeft", "Control+ArrowRight"]) {
const root = page.getByTestId(mode === "quality" ? "capture-annotation-workbench" : "final-acceptance-workspace");
await root.focus();
await page.waitForTimeout(delay + 150);
await page.evaluate(() => (window.previousVideo = document.querySelector('[data-camera="left"]')));
const before = requests.length,
start = Date.now();
await page.keyboard.press(key);
if (mode !== "quality")
await page.waitForFunction(
(index) => {
const buttons = [...document.querySelectorAll('[aria-label="验收切片选择"] button')];
const v = document.querySelector('[data-camera="left"]');
return buttons[index]?.getAttribute("aria-pressed") === "true" && v && v.readyState >= 2 && !v.seeking;
},
key === "Control+ArrowRight" ? 1 : 0,
);
await settle();
if (mode === "acceptance-source")
try {
await page.waitForFunction(
(expected) => Math.abs(document.querySelector('[data-camera="left"]').currentTime - expected) < 0.04,
key === "Control+ArrowRight" ? 4 : 0,
{ timeout: 5000 },
);
} catch (e) {
console.log(
"POSITION",
key,
await page.evaluate(() => {
const v = document.querySelector('[data-camera="left"]');
return {
time: v.currentTime,
src: v.getAttribute("src"),
tag: v.tagName,
ready: v.readyState,
seeking: v.seeking,
text: document.body.innerText.slice(-2000),
};
}),
);
throw e;
}
const expectedIndex = key === "Control+ArrowRight" ? 1 : 0;
const selectedIndex =
mode === "quality"
? Number(
(await page.locator('[data-slice-id][data-selected="true"]').first().getAttribute("data-slice-id")).slice(
1,
),
) - 1
: await page
.getByLabel("验收切片选择", { exact: true })
.getByRole("button")
.evaluateAll((bs) => bs.findIndex((b) => b.getAttribute("aria-pressed") === "true"));
assert.equal(selectedIndex, expectedIndex, "shortcut must really change selected slice");
row.switches.push({
key,
elapsedMs: Date.now() - start,
newRequests: requests.slice(before),
videoRecreated: await page.evaluate(
() => document.querySelector('[data-camera="left"]') !== window.previousVideo,
),
});
}
if (mode !== "quality") {
await page.evaluate(() => window.setProblem(""));
await page.getByRole("button", { name: "验收通过", exact: true }).waitFor();
await page.waitForFunction(() =>
[...document.querySelectorAll("button")].some((b) => b.textContent.trim() === "验收通过" && !b.disabled),
);
row.approveRestoredAfterClear = true;
assert.ok(
requests.every((p) => !p.includes("right.mp4")),
"disabled camera must not load media",
);
}
await page.screenshot({ path: out + mode + ".png" });
report.runs.push(row);
await page.close();
}
assert.deepEqual(report.errors, []);
for (const row of report.runs.filter((r) => r.mode !== "quality")) {
assert.equal(row.approveEnabledWithProblem, false, "current problems must disable approval");
assert.ok(row.discardTitle.includes("已舍弃:玩手机"));
}
const sourceRow = report.runs.find((r) => r.mode === "acceptance-source");
assert.ok(
sourceRow.switches.every((s) => !s.videoRecreated),
"same frozen source must reuse its media node",
);
assert.ok(
sourceRow.switches.every((s) => s.newRequests.every((p) => p.includes("/3-") && !p.includes(".mp4"))),
"prepared slices may only prefetch the next neighbor",
);
assert.ok(
sourceRow.switches.every((s) => s.elapsedMs <= 200),
"prepared same-source navigation budget",
);
console.log(JSON.stringify(report, null, 2));
} finally {
await fs.writeFile(out + "report-" + delay + ".json", JSON.stringify(report, null, 2));
await browser.close();
}