274 lines
13 KiB
JavaScript
274 lines
13 KiB
JavaScript
// Real local browser/API acceptance for the focused slicing stage.
|
||
import assert from "node:assert/strict";
|
||
import { chromium } from "playwright";
|
||
const browser = await chromium.launch({
|
||
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
||
headless: true,
|
||
args: ["--no-proxy-server"],
|
||
});
|
||
const context = await browser.newContext({
|
||
storageState: process.env.ANNOTATION_STORAGE_STATE || "local-data/verification/annotation-workspace-layout/auth.json",
|
||
viewport: { width: 1280, height: 640 },
|
||
});
|
||
const page = await context.newPage();
|
||
page.setDefaultTimeout(15000);
|
||
page.on("dialog", (dialog) => dialog.accept());
|
||
const task = process.env.ANNOTATION_TASK_ID || "annotation-task-workspace-layout-acceptance-slice-primary";
|
||
assert.match(task, /^annotation-task-workspace-layout-acceptance-(slice-primary|pool-0[1-4])$/);
|
||
try {
|
||
await page.goto(
|
||
`http://127.0.0.1:5173/plugins/annotation-qa/workflows/all/work?projectId=project-workspace-layout-acceptance&taskId=${task}`,
|
||
);
|
||
await page.getByRole("button", { name: "继续工作", exact: true }).click();
|
||
if (await page.getByRole("button", { name: "进入专注视图", exact: true }).count())
|
||
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
|
||
const workbench = page.getByTestId("capture-annotation-workbench");
|
||
await workbench.waitFor();
|
||
await page.waitForFunction(
|
||
() =>
|
||
document.querySelectorAll("video").length === 2 &&
|
||
[...document.querySelectorAll("video")].every((v) => v.readyState >= 2),
|
||
null,
|
||
{ timeout: 60000 },
|
||
);
|
||
const readItem = async () => {
|
||
const response = await context.request.get(
|
||
`http://127.0.0.1:5173/api/human-items/${task.replace("annotation-task-", "human-item-")}`,
|
||
);
|
||
assert.ok(response.ok(), "authoritative local readback succeeds");
|
||
return (await response.json()).item;
|
||
};
|
||
const assertGenerated = (item, sliceCount) => {
|
||
const { sliceWorkflow, annotationWorkspace } = item.subject.payload;
|
||
assert.equal(sliceWorkflow.status, "completed");
|
||
assert.equal(sliceWorkflow.phase, "annotate");
|
||
assert.equal(sliceWorkflow.materialization.status, "completed");
|
||
assert.equal(sliceWorkflow.materialization.coverage.contiguous, true);
|
||
assert.equal(sliceWorkflow.materialization.coverage.gapMicroseconds, 0);
|
||
assert.equal(sliceWorkflow.materialization.coverage.overlapMicroseconds, 0);
|
||
assert.equal(annotationWorkspace.sliceSegments.length, sliceCount);
|
||
assert.ok(annotationWorkspace.sliceSegments.every((s) => s.name === "布局验收切片 001"));
|
||
console.log(
|
||
"PASS generated public API readback",
|
||
JSON.stringify({
|
||
version: item.version,
|
||
status: sliceWorkflow.status,
|
||
phase: sliceWorkflow.phase,
|
||
slices: sliceCount,
|
||
coverage: sliceWorkflow.materialization.coverage.durationSeconds,
|
||
}),
|
||
);
|
||
};
|
||
if (process.env.ANNOTATION_VERIFY_GENERATED === "1") {
|
||
await page.getByRole("button", { name: "新增动作描述", exact: true }).waitFor();
|
||
assertGenerated(await readItem(), 4);
|
||
} else {
|
||
const geometry = async () =>
|
||
page.evaluate(() => ({
|
||
videos: [...document.querySelectorAll("video")].map((v) => {
|
||
const r = v.getBoundingClientRect();
|
||
const scale = Math.min(r.width / v.videoWidth, r.height / v.videoHeight);
|
||
return {
|
||
width: v.videoWidth * scale,
|
||
height: v.videoHeight * scale,
|
||
x: r.x,
|
||
y: r.y,
|
||
right: r.right,
|
||
bottom: r.bottom,
|
||
fit: getComputedStyle(v).objectFit,
|
||
};
|
||
}),
|
||
scrollers: [
|
||
document.documentElement,
|
||
...document.querySelectorAll("main, main > div,.annotation-operations-page,.capture-workbench-body"),
|
||
]
|
||
.filter((e) => e.scrollHeight > e.clientHeight + 1 && getComputedStyle(e).overflowY !== "visible")
|
||
.map((e) => ({ tag: e.tagName, classes: e.className, height: e.clientHeight, scroll: e.scrollHeight })),
|
||
}));
|
||
let previous = 0;
|
||
for (const [width, height] of [
|
||
[1280, 640],
|
||
[1366, 680],
|
||
[1440, 800],
|
||
[1920, 980],
|
||
[2560, 1320],
|
||
]) {
|
||
await page.setViewportSize({ width, height });
|
||
const g = await geometry();
|
||
console.log(JSON.stringify({ viewport: [width, height], ...g }));
|
||
for (const v of g.videos) {
|
||
assert.ok(v.width >= 400 && v.height >= 325, "each effective image meets 400×325 floor");
|
||
assert.equal(v.fit, "contain");
|
||
assert.ok(v.x >= 0 && v.y >= 0 && v.right <= width && v.bottom <= height);
|
||
}
|
||
assert.ok(g.videos[0].width >= previous, "video grows with available space");
|
||
previous = g.videos[0].width;
|
||
assert.deepEqual(g.scrollers, [], "focused stage requires no document/container scrolling");
|
||
for (const name of [
|
||
"播放",
|
||
"上一帧",
|
||
"下一帧",
|
||
"复查本片",
|
||
"返回工作点",
|
||
"在播放头分割切片",
|
||
"从切片字典选择",
|
||
"确认全部切片",
|
||
"撤销",
|
||
"重做",
|
||
"切回原布局",
|
||
"快捷键帮助",
|
||
"开始连续工作",
|
||
]) {
|
||
const r = await page.getByRole("button", { name, exact: true }).boundingBox();
|
||
assert.ok(
|
||
r && r.y >= 0 && r.y + r.height <= height && r.x + r.width <= width,
|
||
`${name} visible at ${width}×${height}`,
|
||
);
|
||
assert.ok(
|
||
await page.getByRole("button", { name, exact: true }).evaluate((b) => {
|
||
const r = b.getBoundingClientRect();
|
||
return b.contains(document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2));
|
||
}),
|
||
`${name} remains hit-test reachable`,
|
||
);
|
||
}
|
||
}
|
||
console.log("PASS five-size effective dual-picture geometry and visible operations");
|
||
|
||
await page.setViewportSize({ width: 1280, height: 640 });
|
||
const videos = await page.locator("video").elementHandles();
|
||
const beforeFrame = await page
|
||
.locator("video")
|
||
.first()
|
||
.evaluate((v) => v.currentTime);
|
||
const boundaries = page.getByRole("button", { name: "删除切片", exact: true });
|
||
const count = await boundaries.count();
|
||
await workbench.focus();
|
||
await page.keyboard.press("c");
|
||
assert.ok(await workbench.getByRole("alert").isVisible(), "invalid boundary exposes its actual edit error");
|
||
assert.deepEqual((await geometry()).scrollers, [], "edit errors cannot displace the fixed controls");
|
||
for (let i = 0; i < 6; i++) await page.keyboard.press("ArrowRight");
|
||
await page.waitForFunction((t) => document.querySelector("video").currentTime > t, beforeFrame);
|
||
const frame = await page
|
||
.locator("video")
|
||
.first()
|
||
.evaluate((v) => v.currentTime);
|
||
await page.keyboard.press("c");
|
||
assert.equal(await boundaries.count(), count + 1, "C retains a new cut");
|
||
await page.keyboard.press("Space");
|
||
await page.waitForFunction(() => !document.querySelector("video").paused);
|
||
await page.keyboard.press("Space");
|
||
await page.waitForFunction(() => document.querySelector("video").paused);
|
||
await page.keyboard.press("b");
|
||
await page.waitForFunction(() => !document.querySelector("video").paused);
|
||
await page.keyboard.press("v");
|
||
await page.waitForFunction(() => document.querySelector("video").paused);
|
||
const workTime = await page
|
||
.locator("video")
|
||
.first()
|
||
.evaluate((v) => v.currentTime);
|
||
for (const name of ["切回原布局", "进入专注视图"]) {
|
||
await page.getByRole("button", { name, exact: true }).click();
|
||
assert.equal(await boundaries.count(), count + 1);
|
||
for (const v of videos) assert.ok(await v.evaluate((n) => n.isConnected), "same media DOM across layouts");
|
||
assert.ok(
|
||
Math.abs(
|
||
(await page
|
||
.locator("video")
|
||
.first()
|
||
.evaluate((v) => v.currentTime)) - workTime,
|
||
) < 0.05,
|
||
);
|
||
}
|
||
await page.getByRole("button", { name: "撤销", exact: true }).click();
|
||
assert.equal(await boundaries.count(), count, "visible undo restores the cut");
|
||
await page.waitForTimeout(1100);
|
||
await page.getByRole("button", { name: "重做", exact: true }).click();
|
||
assert.equal(await boundaries.count(), count + 1, "visible redo restores the cut");
|
||
await workbench.focus();
|
||
await page.keyboard.press("Control+ArrowLeft");
|
||
const sliceCount = await boundaries.count();
|
||
await page.getByRole("button", { name: "从切片字典选择", exact: true }).click();
|
||
await page.getByRole("textbox", { name: "搜索切片字典", exact: true }).press("Escape");
|
||
assert.ok(await workbench.evaluate((e) => e === document.activeElement), "one Escape restores workbench focus");
|
||
|
||
for (let i = 0; i < sliceCount; i++) {
|
||
await page.getByRole("button", { name: "从切片字典选择", exact: true }).click();
|
||
const dictionary = page.getByRole("dialog", { name: "选择切片名称", exact: true });
|
||
await dictionary.getByRole("textbox", { name: "搜索切片字典", exact: true }).fill("布局验收切片 001");
|
||
await dictionary.getByRole("button", { name: "布局验收切片 001", exact: true }).click();
|
||
assert.equal(await page.locator("#selected-slice-name-input").inputValue(), "布局验收切片 001");
|
||
if (i < sliceCount - 1) await page.getByRole("button", { name: "下一片", exact: true }).click();
|
||
}
|
||
const times = await page.locator("video").evaluateAll((vs) => vs.map((v) => v.currentTime));
|
||
assert.ok(Math.abs(times[0] - times[1]) < 0.05, "dual video stays synchronized after frame/navigation operations");
|
||
assert.equal(await page.getByText("任务已归您处理;切片名称从项目字典选择。", { exact: false }).count(), 0);
|
||
assert.deepEqual((await geometry()).scrollers, [], "compact workspace stays within the viewport");
|
||
if (process.env.ANNOTATION_EVIDENCE_DIR)
|
||
await page.screenshot({ path: `${process.env.ANNOTATION_EVIDENCE_DIR}/task02-focused.png` });
|
||
console.log(
|
||
"PASS C/Space/frame/B/V, synchronized media, original toggle, undo/redo, dictionary names, bounded explanations",
|
||
);
|
||
// Generation is opt-in because it authoritatively advances the isolated fixture.
|
||
if (process.env.ANNOTATION_GENERATE === "1") {
|
||
const before = await readItem();
|
||
let submitted = 0;
|
||
page.on("request", (r) => {
|
||
if (r.method() === "POST" && r.url().includes("/slice-confirmations")) submitted++;
|
||
});
|
||
await page.getByRole("button", { name: "确认全部切片", exact: true }).click();
|
||
const confirmation = page.getByRole("dialog", { name: "确认生成切片", exact: true });
|
||
await confirmation.waitFor();
|
||
assert.equal(submitted, 0, "first step only opens review");
|
||
assert.ok(await confirmation.getByRole("button", { name: "确认生成", exact: true }).isVisible());
|
||
assert.ok(
|
||
await confirmation.getByRole("button", { name: "确认生成", exact: true }).evaluate((b) => {
|
||
const r = b.getBoundingClientRect();
|
||
return (
|
||
r.y >= 0 &&
|
||
r.bottom <= innerHeight &&
|
||
b.contains(document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2))
|
||
);
|
||
}),
|
||
"confirmation action is inside the minimum viewport and hit-test reachable",
|
||
);
|
||
const responsePromise = page.waitForResponse(
|
||
(r) => r.url().includes("/slice-confirmations") && r.request().method() === "POST",
|
||
);
|
||
await confirmation.getByRole("button", { name: "确认生成", exact: true }).click();
|
||
const response = await responsePromise;
|
||
assert.ok(response.ok());
|
||
const result = await response.json();
|
||
console.log("Generation response", JSON.stringify({ status: result.sliceWorkflow.status }));
|
||
await page.getByTestId("slice-materialization-progress").waitFor();
|
||
assert.equal(
|
||
await page.getByRole("button", { name: "新增动作描述", exact: true }).count(),
|
||
0,
|
||
"processing is not completed",
|
||
);
|
||
assert.equal(submitted, 1, "one generation request");
|
||
assert.deepEqual((await geometry()).scrollers, [], "processing status never introduces page scrolling");
|
||
await page.getByRole("button", { name: "新增动作描述", exact: true }).waitFor({ timeout: 180000 });
|
||
const after = await readItem();
|
||
console.log(
|
||
"API generation readback",
|
||
JSON.stringify({
|
||
versionBefore: before.version,
|
||
versionAfter: after.version,
|
||
phase: after.subject.payload.sliceWorkflow?.phase,
|
||
slices: after.subject.payload.annotationWorkspace?.sliceSegments?.length,
|
||
}),
|
||
);
|
||
assert.ok(after.version > before.version, "authoritative version advanced");
|
||
assertGenerated(after, sliceCount);
|
||
console.log("PASS two-step real asynchronous generation and authoritative API readback");
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error(error);
|
||
throw error;
|
||
} finally {
|
||
await context.close();
|
||
await browser.close();
|
||
}
|