droboticflow/tests/browser/review-focus-layout.mjs

286 lines
13 KiB
JavaScript

// Actual application and authenticated local/cluster API; fixtures are isolated.
import { chromium } from "playwright";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
const dir = process.env.REVIEW_FIXTURES || "local-data/verification/review-focused-20260922";
const base = process.env.REVIEW_TEST_URL || "http://review-session.test:5178";
const hostname = new URL(base).hostname;
const fixtures = JSON.parse(await fs.readFile(`${dir}/fixture-index.json`, "utf8"));
const browser = await chromium.launch({
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: hostname.endsWith(".test") ? [`--host-resolver-rules=MAP ${hostname} 127.0.0.1`, "--no-proxy-server"] : [],
});
try {
const report = [];
for (const mode of ["quality", "acceptance"]) {
const auth = JSON.parse(
await fs.readFile(`${dir}/fixture-${mode === "quality" ? "qc" : "acceptance"}-auth.json`, "utf8"),
);
auth.cookies = auth.cookies.map((c) => ({ ...c, domain: hostname }));
const context = await browser.newContext({ storageState: auth, viewport: { width: 1413, height: 615 } });
const page = await context.newPage();
page.setDefaultTimeout(20000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const fixture = fixtures[mode][0];
console.log("CHECK", mode);
await page.goto(
`${base}/plugins/annotation-qa/workflows/all/${mode}?projectId=${fixtures.projectId}${fixture.taskId ? `&taskId=${fixture.taskId}` : ""}`,
);
await page.getByRole("button", { name: /^我的任务/ }).click();
if (mode === "acceptance")
await page
.getByTestId("acceptance-task-card")
.first()
.getByRole("button", { name: /^预览 / })
.click();
await page.getByRole("button", { name: "进入专注审查", exact: true }).click();
await page.getByRole("button", { name: "返回任务池", exact: true }).first().waitFor();
assert.equal(new URL(page.url()).searchParams.get("reviewFocus"), "1");
await page.waitForFunction(() => [...document.querySelectorAll("video")].some((v) => v.readyState >= 2));
await page.getByRole("textbox", { name: "审查问题说明", exact: true }).waitFor();
for (const [camera, label] of [
["left", "放大左相机"],
["right", "放大右相机"],
["both", "恢复双画面"],
]) {
await page.getByRole("button", { name: label, exact: true }).click();
await page.waitForFunction(() =>
[...document.querySelectorAll("video")]
.filter((v) => v.getBoundingClientRect().width > 0)
.every((v) => v.readyState >= 2),
);
await page.waitForFunction(
(count) => document.querySelectorAll(".review-picture-caption-text").length === count,
camera === "both" ? 2 : 1,
);
const result = await page.evaluate(
({ mode, camera }) => {
const selectors =
mode === "quality"
? [
".capture-video-surface",
".capture-media-controls",
'[data-testid="capture-slice-timeline"]',
'[data-testid="slice-annotation-timeline"]',
".capture-workbench > footer",
".review-problem-panel textarea",
".review-problem-panel [data-review-save]",
]
: [
".capture-video-surface",
".capture-media-controls",
'[aria-label="验收切片选择"]',
'[data-testid="acceptance-action-timeline"]',
'[data-testid="final-acceptance-workspace"] > footer',
".review-problem-panel textarea",
".review-problem-panel [data-review-save]",
];
const actualSave = [...document.querySelectorAll(".review-problem-panel button")].find(
(button) => button.textContent.trim() === "保存批注",
);
actualSave?.setAttribute("data-review-save", "");
const regions = selectors.map((selector) => {
const el = document.querySelector(selector),
r = el?.getBoundingClientRect();
return { selector, top: r?.top, bottom: r?.bottom, width: r?.width, height: r?.height };
});
const panel = document.querySelector(".review-problem-panel");
const saveButton = [...(panel?.querySelectorAll("button") || [])].find(
(button) => button.textContent.trim() === "保存批注",
);
saveButton?.setAttribute("data-review-save", "");
const controls = [panel?.querySelector("textarea"), saveButton].map((element) => {
if (!element) return { visible: false };
const r = element.getBoundingClientRect();
let top = r.top,
bottom = r.bottom,
left = r.left,
right = r.right;
for (let parent = element.parentElement; parent; parent = parent.parentElement) {
const style = getComputedStyle(parent),
bounds = parent.getBoundingClientRect();
if (/auto|hidden|scroll|clip/.test(style.overflowY)) {
top = Math.max(top, bounds.top);
bottom = Math.min(bottom, bounds.bottom);
}
if (/auto|hidden|scroll|clip/.test(style.overflowX)) {
left = Math.max(left, bounds.left);
right = Math.min(right, bounds.right);
}
}
return {
label: element.getAttribute("aria-label") || element.textContent.trim(),
visible: top <= r.top + 1 && bottom >= r.bottom - 1 && left <= r.left + 1 && right >= r.right - 1,
top: r.top,
bottom: r.bottom,
height: r.height,
};
});
const captions = [...document.querySelectorAll(".review-picture-caption-text")].map((el) => {
const r = el.getBoundingClientRect(),
picture = el.parentElement.getBoundingClientRect();
return {
camera: el.parentElement.dataset.cameraCaption,
fontSize: parseFloat(getComputedStyle(el).fontSize),
top: r.top,
bottom: r.bottom,
height: r.height,
contained:
r.top >= picture.top - 1 &&
r.bottom <= picture.bottom + 1 &&
r.left >= picture.left - 1 &&
r.right <= picture.right + 1,
};
});
const videos = [...document.querySelectorAll("video")]
.map((v) => {
const r = v.getBoundingClientRect();
return { camera: v.dataset.camera, width: r.width, height: r.height, fit: getComputedStyle(v).objectFit };
})
.filter((v) => v.width > 0 && v.height > 0);
return {
mode,
camera,
regions,
videos,
controls,
captions,
documentHeight: document.documentElement.scrollHeight,
};
},
{ mode, camera },
);
await page.screenshot({ path: `${dir}/focus-${mode}-${camera}-minimum.png`, fullPage: true });
assert.ok(result.documentHeight <= 615 + 2, "no page scrolling");
for (const r of result.regions)
assert.ok(
r.top >= 0 &&
r.bottom <= 616 &&
r.width > 0 &&
r.height > 0 &&
(r.selector !== ".capture-video-surface" || r.height >= 120),
`${mode} ${camera}: ${JSON.stringify(r)}`,
);
assert.equal(result.videos.length, camera === "both" ? 2 : 1);
for (const control of result.controls)
assert.ok(control.visible, `core draft control clipped: ${JSON.stringify(control)}`);
assert.equal(result.captions.length, camera === "both" ? 2 : 1);
for (const caption of result.captions)
assert.ok(
caption.contained && caption.fontSize >= 18 && caption.height > 0 && caption.bottom <= 616,
`caption clipped: ${JSON.stringify(caption)}`,
);
for (const v of result.videos) {
assert.equal(v.fit, "contain");
assert.ok(v.height >= 120, "video needs usable height");
}
report.push(result);
}
if (process.env.REVIEW_TEST_VIEWING === "true") {
const bench = page.getByTestId("capture-annotation-workbench").first();
const range = bench.getByRole("slider", { name: "Capture 播放位置", exact: true });
const actionBlocks =
mode === "quality"
? bench.getByTestId("annotation-description-block")
: page.getByTestId("acceptance-action-timeline").getByRole("button", { name: /^动作 / });
assert.ok((await actionBlocks.count()) >= 3, "fixture needs at least three actions");
await actionBlocks.nth(1).click();
const secondStart = Number(await range.inputValue());
await page.keyboard.press("ArrowDown");
await page.waitForFunction(
(start) => Number(document.querySelector('[aria-label="Capture 播放位置"]').value) > start,
secondStart,
);
await page.keyboard.press("ArrowUp");
await page.waitForFunction(
(start) => Math.abs(Number(document.querySelector('[aria-label="Capture 播放位置"]').value) - start) < 0.000001,
secondStart,
);
const rangeBox = await range.boundingBox();
await range.click({ position: { x: rangeBox.width / 10, y: rangeBox.height / 2 } });
await page
.waitForFunction(() =>
[...document.querySelectorAll("video")]
.filter((v) => v.getBoundingClientRect().width > 0)
.every((v) => !v.seeking),
)
.catch(async (error) => {
console.log(
"SEEK_DIAGNOSTIC",
mode,
await page.evaluate(() => ({
range: [...document.querySelectorAll("input[type=range]")].map((r) => ({
label: r.ariaLabel,
value: r.value,
min: r.min,
max: r.max,
})),
videos: [...document.querySelectorAll("video")].map((v) => ({
time: v.currentTime,
duration: v.duration,
seeking: v.seeking,
ready: v.readyState,
error: v.error?.message,
seekable: [...Array(v.seekable.length)].map((_, i) => [v.seekable.start(i), v.seekable.end(i)]),
})),
})),
);
throw error;
});
const dragged = Number(await range.inputValue());
await page.keyboard.press("ArrowRight");
await page.waitForFunction(
(start) => Number(document.querySelector('[aria-label="Capture 播放位置"]').value) > start,
dragged,
);
for (const key of ["c", "g", "Control+x"]) await page.keyboard.press(key);
assert.ok((await actionBlocks.count()) >= 3, "review keys do not remove actions");
await page.keyboard.press("?");
const help = page.getByRole("dialog", { name: "审查快捷键帮助", exact: true });
await help.waitFor();
assert.ok((await help.innerText()).includes("起始帧"));
await page.keyboard.press("Escape");
await help.waitFor({ state: "hidden" });
await page.keyboard.press("Control+ArrowRight");
await page.waitForFunction(() =>
document.querySelector('[aria-label="审查导航位置"]')?.textContent.includes("切片 2/"),
);
console.log(
"NEXT_SLICE",
mode,
await page.locator('[aria-label="审查导航位置"]').allTextContents(),
await page.evaluate(() => ({
tag: document.activeElement?.tagName,
testid: document.activeElement?.getAttribute("data-testid"),
})),
);
await page.keyboard.press("Control+ArrowLeft");
await page.waitForFunction(() =>
document.querySelector('[aria-label="审查导航位置"]')?.textContent.includes("切片 1/"),
);
report.push({
mode,
focusedViewingShortcuts: true,
actionStartNavigation: true,
mouseToKeyboard: true,
originalEditingKeysDisabled: true,
});
}
assert.deepEqual(errors, []);
await page.setViewportSize({ width: 1280, height: 600 });
await page.getByRole("dialog", { name: "工作区已暂停", exact: true }).waitFor();
await page.setViewportSize({ width: 1413, height: 615 });
await page.getByRole("button", { name: "继续当前工作", exact: true }).click();
await page.getByRole("button", { name: "返回任务池", exact: true }).first().click();
assert.equal(new URL(page.url()).searchParams.get("reviewFocus"), null);
await page.getByRole("button", { name: /^我的任务/ }).waitFor();
await context.close();
}
await fs.writeFile(`${dir}/review-focus-layout.json`, JSON.stringify(report, null, 2));
console.log("PASS review focus: both roles, three cameras, minimum viewport, pause/resume and return");
} finally {
await browser.close();
}