droboticflow/tests/browser/camera-defaults.mjs

127 lines
5.4 KiB
JavaScript

import { chromium } from "playwright";
import assert from "node:assert/strict";
import fs from "node:fs";
const origin = process.env.CAPTION_PREVIEW_ORIGIN || "http://127.0.0.1:5192";
assert(["localhost", "127.0.0.1"].includes(new URL(origin).hostname));
const video = fs.readFileSync("local-data/verification/caption-overlay/preview.mp4");
const rows = [];
const browser = await chromium.launch({
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
try {
for (const mode of ["annotate", "quality", "acceptance"])
for (const preference of ["both", "right"]) {
const ctx = await browser.newContext({ viewport: { width: 1500, height: 1000 }, serviceWorkers: "block" });
await ctx.addInitScript((value) => {
localStorage.setItem("annotation-camera-default:camera-fixture", value);
localStorage.setItem("review-camera-default:camera-fixture", value);
}, preference);
const page = await ctx.newPage();
page.setDefaultTimeout(12000);
let rightLoads = 0;
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", async (route) => {
const req = route.request(),
u = new URL(req.url()),
p = decodeURIComponent(u.pathname);
if (u.origin !== origin || req.method() !== "GET") return route.abort();
if (p.endsWith(".mp4") || /\.mp4\/content$/.test(p)) {
if (u.searchParams.get("camera") === "right" || p.includes("right.mp4")) rightLoads++;
const match = /bytes=(\d+)-(\d*)/.exec(req.headers().range || ""),
start = match ? Number(match[1]) : 0,
end = match?.[2] ? Math.min(Number(match[2]), video.length - 1) : video.length - 1;
return route.fulfill({
status: match ? 206 : 200,
contentType: "video/mp4",
headers: {
"Accept-Ranges": "bytes",
...(match ? { "Content-Range": `bytes ${start}-${end}/${video.length}` } : {}),
},
body: video.subarray(start, end + 1),
});
}
if (p.startsWith("/api/")) {
if (p.endsWith("/members"))
return route.fulfill({
json: {
items: [
"left.mp4",
"right.mp4",
"left_pts.csv",
"right_pts.csv",
"imu.csv",
"annotation.json",
"synchronized-slice.json",
].map((name) => ({
id: name,
name,
group: "slices/slice-000001",
sizeBytes: 100,
contentAvailable: true,
})),
total: 7,
offset: 0,
limit: 200,
truncated: false,
recognized: true,
},
});
if (p.includes("annotation.json"))
return route.fulfill({
json: { descriptions: [{ id: "a", clipStartMs: 0, clipEndMs: 12000, description: "合成动作" }] },
});
if (p.includes("synchronized-slice.json"))
return route.fulfill({
json: {
window: { startTimestampUs: 0, endTimestampUs: 12000000 },
boundaryContract: "hardware-frame-v1",
boundarySeconds: Array.from({ length: 121 }, (_, i) => i / 10),
left: { pts: { rowCount: 120 } },
},
});
if (p.endsWith(".csv/content"))
return route.fulfill({
contentType: "text/csv",
body: "clip_time_ms\n" + Array.from({ length: 120 }, (_, i) => i * 100).join("\n"),
});
return route.fulfill({ json: { items: [] } });
}
return route.continue();
});
await page.goto(origin + "/tests/browser/fixtures/camera-defaults.html?mode=" + mode);
const right = page.getByRole("checkbox", { name: "右相机", exact: true });
await right.waitFor();
await page.waitForFunction(() => document.querySelector('video[data-camera="left"]')?.readyState >= 2);
assert.equal(await right.isChecked(), false, mode + " ignores focused preference in ordinary layout");
const rightVideo = page.locator('video[data-camera="right"]');
assert.equal(await rightVideo.getAttribute("src"), null);
assert.equal(await rightVideo.getAttribute("preload"), "none");
assert.equal(rightLoads, 0, "unchecked right must have zero media requests");
await right.check();
await page.waitForFunction(() => document.querySelector('video[data-camera="right"]')?.readyState >= 2);
assert(rightLoads > 0, "checking right must load its stream");
await right.uncheck();
assert.equal(await rightVideo.getAttribute("src"), null);
assert.deepEqual(errors, []);
rows.push({
mode,
savedFocusedPreference: preference,
defaultLeftOnly: true,
noInitialRightRequests: true,
loadOnCheck: true,
unloadOnUncheck: true,
});
await ctx.close();
}
fs.writeFileSync(
"local-data/verification/caption-overlay/camera-defaults-report.json",
JSON.stringify({ passed: true, rows }, null, 2),
);
console.log(JSON.stringify({ passed: true, rows }));
} finally {
await browser.close();
}