droboticflow/tests/browser/annotation-workspace-shell.mjs

303 lines
16 KiB
JavaScript

// Local integration acceptance. Uses an authenticated local browser session and
// existing isolated keyboard fixtures; never point this at a deployed service.
// Removing chrome isolation, remounting the editor, or dropping account preference
// must fail observable UI assertions here, independently of component internals.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { chromium } from "playwright";
const baseURL = "http://127.0.0.1:5173";
assert.ok(process.env.ANNOTATION_STORAGE_STATE, "Set ANNOTATION_STORAGE_STATE to a local authenticated state file");
const browser = await chromium.launch({
executablePath:
process.env.PLAYWRIGHT_EXECUTABLE_PATH || "/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,
viewport: { width: 1440, height: 900 },
});
const page = await context.newPage();
page.setDefaultTimeout(15000);
const project = process.env.ANNOTATION_PROJECT_ID || "project-workspace-layout-acceptance";
const task = process.env.ANNOTATION_TASK_ID || "annotation-task-workspace-layout-acceptance-annotate-primary";
const pool = `/plugins/annotation-qa/workflows/all/annotate?projectId=${project}&taskId=${task}`;
const work = `/plugins/annotation-qa/workflows/all/work?projectId=${project}&taskId=${task}`;
const chrome = page.locator('header[aria-label="平台状态与主题"]');
try {
// The ordinary task entry remains the established workbench and platform shell.
await page.goto(baseURL + pool, { waitUntil: "domcontentloaded" });
await page.getByTestId("capture-annotation-workbench").waitFor();
assert.ok(await chrome.isVisible(), "original task entry has platform navigation");
assert.equal(await page.getByRole("button", { name: "切回原布局", exact: true }).count(), 0);
console.log("PASS original task entry baseline");
const writes = [];
page.on("request", (request) => {
if (request.method() !== "GET" && /random-claims|\/tasks\/|\/human-items\//.test(request.url()))
writes.push(request.url());
});
await page.getByRole("button", { name: "开始工作", exact: true }).click();
await page.waitForURL((url) => url.pathname.endsWith("/work"), { waitUntil: "domcontentloaded" });
await page.getByTestId("capture-annotation-workbench").waitFor();
assert.ok(new URL(page.url()).pathname.endsWith("/work"), "existing start-work entry opens focused work in this tab");
assert.equal(context.pages().length, 1, "work remains in the same tab");
await page.getByRole("button", { name: "完成当前任务后停止", exact: true }).click();
assert.equal(writes.length, 0, "start work resumes the owned task without a new claim");
console.log("PASS existing start-work entry, owned-first continuation and stop control");
await page.goto(baseURL + work, { waitUntil: "domcontentloaded" });
await page.getByRole("region", { name: "恢复工作会话" }).waitFor();
assert.equal(await chrome.isVisible(), false, "default focused layout hides platform chrome");
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
assert.ok(await chrome.isVisible(), "original layout restores real platform chrome");
await page.reload({ waitUntil: "domcontentloaded" });
await page.getByRole("region", { name: "恢复工作会话" }).waitFor();
assert.ok(await chrome.isVisible(), "same-account original preference survives reload");
assert.equal(
await page.getByTestId("capture-annotation-workbench").count(),
0,
"refresh still requires explicit recovery",
);
assert.equal(writes.length, 0, "preference and refresh never acquire or write tasks");
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
assert.equal(await chrome.isVisible(), false);
console.log("PASS focused default, original fallback, preference and explicit refresh recovery");
await page.getByRole("button", { name: "继续工作", exact: true }).click();
const workbench = page.getByTestId("capture-annotation-workbench");
await workbench.waitFor();
const continuousWork = page.getByRole("region", { name: "连续工作", exact: true });
const activeTaskURL = page.url();
await continuousWork.getByRole("button", { name: "开始连续工作", exact: true }).click();
for (const switchLabel of ["切回原布局", "进入专注视图"]) {
await page.getByRole("button", { name: switchLabel, exact: true }).click();
assert.match(await continuousWork.innerText(), /连续工作中:真实送检成功后继续下一条/);
assert.ok(
await continuousWork.getByRole("button", { name: "完成当前任务后停止", exact: true }).isVisible(),
"continuous intent survives each layout switch in the recovered session",
);
assert.equal(page.url(), activeTaskURL, "continuous-mode toggle keeps the same task");
assert.equal(writes.length, 0, "continuous-mode layout switch never claims or writes");
}
await continuousWork.getByRole("button", { name: "完成当前任务后停止", exact: true }).click();
for (const switchLabel of ["切回原布局", "进入专注视图"]) {
await page.getByRole("button", { name: switchLabel, exact: true }).click();
assert.match(await continuousWork.innerText(), /已设置完成当前任务后停止;不会再领取下一条/);
assert.ok(
await continuousWork.getByRole("button", { name: "恢复连续工作", exact: true }).isVisible(),
"stop-after-current stays selected after each layout switch",
);
assert.equal(
await continuousWork.getByRole("button", { name: "完成当前任务后停止", exact: true }).count(),
0,
"layout switching does not silently re-enable continuous work",
);
assert.equal(page.url(), activeTaskURL, "stop-after-current keeps the same task");
assert.equal(writes.length, 0, "stopped layout switch never claims or writes");
}
console.log(
"PASS recovered-session continuous intent and stop-after-current survive both layout directions without claims",
);
await page.waitForFunction(
() =>
[...document.querySelectorAll("video")].length === 2 &&
[...document.querySelectorAll("video")].every((video) => video.readyState >= 2),
{ timeout: 60000 },
);
const videos = await page.locator("video").elementHandles();
const editorNode = await workbench.elementHandle();
await workbench.focus();
await page.keyboard.press("ArrowRight");
await page.waitForFunction(() => document.querySelector("video").currentTime > 0);
const frameBefore = await page
.locator("video")
.first()
.evaluate((video) => video.currentTime);
await page.getByTestId("annotation-description-block").first().click();
const description = page.getByRole("textbox", { name: "动作描述", exact: true });
const originalText = await description.inputValue();
const savedText = `Layout acceptance ${Date.now()}`;
await description.fill(savedText);
await description.scrollIntoViewIfNeeded();
const switchBounds = await page.getByRole("button", { name: "切回原布局", exact: true }).boundingBox();
assert.ok(
switchBounds && switchBounds.y >= 0 && switchBounds.y + switchBounds.height <= 900,
"layout fallback stays visible while editing lower content",
);
for (let index = 0; index < 3; index++) {
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
}
assert.equal(await description.inputValue(), savedText);
assert.ok(await editorNode.evaluate((node) => node.isConnected), "same editor DOM remains mounted");
for (const video of videos)
assert.ok(await video.evaluate((node) => node.isConnected), "same media nodes remain mounted");
assert.equal(
await page
.locator("video")
.first()
.evaluate((video) => video.currentTime),
frameBefore,
);
assert.equal(writes.length, 0, "toggles never save or claim");
if (process.env.ANNOTATION_EVIDENCE_DIR)
await page.screenshot({ path: `${process.env.ANNOTATION_EVIDENCE_DIR}/task01-focused.png` });
// Park another slice's unfinished description and verify both buffers survive.
await workbench.focus();
await page.keyboard.press("Control+ArrowRight");
await page.getByRole("button", { name: "新增动作描述", exact: true }).click();
await workbench.focus();
await page.keyboard.press("ArrowRight");
await page.getByRole("button", { name: "确认结束点", exact: true }).click();
await description.fill("Second slice parked draft");
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
await workbench.focus();
await page.keyboard.press("Control+ArrowLeft");
assert.equal(await description.inputValue(), savedText);
await workbench.focus();
await page.keyboard.press("Control+ArrowRight");
assert.equal(await description.inputValue(), "Second slice parked draft");
await workbench.focus();
await page.keyboard.press("Control+ArrowLeft");
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
assert.equal(await description.inputValue(), savedText);
await page.getByRole("link", { name: "返回任务池", exact: true }).click();
await page.getByRole("dialog", { name: "离开未保存修改确认" }).waitFor();
await page.getByRole("button", { name: "继续编辑", exact: true }).click();
assert.equal(await description.inputValue(), savedText);
console.log("PASS media/frame identity, repeated toggle, per-slice drafts, shortcuts, dirty leave guard");
const itemURL = `${baseURL}/api/human-items/${task.replace("annotation-task-", "human-item-")}`;
const readItem = async () => {
const response = await context.request.get(itemURL);
assert.ok(response.ok(), "authoritative API readback succeeds");
return (await response.json()).item;
};
const beforeSave = await readItem();
let releaseResponse;
const responseHeld = new Promise((resolve) => {
releaseResponse = resolve;
});
let serverSaved;
const serverDone = new Promise((resolve) => {
serverSaved = resolve;
});
const confirmationURL = `**/tasks/${task}/annotation-confirmations`;
await page.route(confirmationURL, async (route) => {
const response = await route.fetch();
assert.ok(response.ok(), "real save reaches server successfully");
serverSaved();
await responseHeld;
await route.fulfill({ response });
});
await description.press("Control+Enter");
await serverDone;
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
assert.ok(
await page.getByRole("button", { name: "确认保存描述", exact: true }).isDisabled(),
"pending save remains locked after layout switch",
);
assert.equal(await description.inputValue(), savedText);
assert.equal(writes.length, 1, "exactly one save while response held");
releaseResponse();
await page.waitForFunction(() => !document.querySelector('[aria-label="动作描述"]'));
await page.unroute(confirmationURL);
const afterSave = await readItem();
assert.equal(afterSave.version, beforeSave.version + 1);
assert.ok(
afterSave.subject.payload.annotationWorkspace.segments.some((segment) => segment.description === savedText),
);
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
await workbench.focus();
const undone = page.waitForResponse(
(response) => response.url().endsWith("annotation-confirmations") && response.request().method() === "POST",
);
await page.keyboard.press("Control+z");
assert.ok((await undone).ok());
assert.ok(
(await readItem()).subject.payload.annotationWorkspace.segments.some(
(segment) => segment.description === originalText,
),
"undo restores original persisted description after toggling",
);
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
await page.waitForTimeout(1100); // Existing history contract throttles undo/redo for one second.
await workbench.focus();
const redone = page.waitForResponse(
(response) => response.url().endsWith("annotation-confirmations") && response.request().method() === "POST",
);
await page.keyboard.press("Control+Shift+z");
assert.ok((await redone).ok());
assert.ok(
(await readItem()).subject.payload.annotationWorkspace.segments.some(
(segment) => segment.description === savedText,
),
"redo restores the saved edit in original layout",
);
assert.equal(writes.length, 3, "save, undo and redo are the only writes; no claim or release");
if (process.env.ANNOTATION_EVIDENCE_DIR)
await page.screenshot({ path: `${process.env.ANNOTATION_EVIDENCE_DIR}/task01-original.png` });
console.log(
"PASS delayed real save across toggle, pending restrictions, public API readback and persisted undo/redo",
);
// A fresh route entry preserves the original slice editor and its history.
page.on("dialog", (dialog) => dialog.accept());
const sliceTask = process.env.ANNOTATION_SLICE_TASK_ID || "annotation-task-workspace-layout-acceptance-pool-01";
assert.match(sliceTask, /^annotation-task-workspace-layout-acceptance-pool-0[1-4]$/);
await page.goto(baseURL + work.replace(task, sliceTask), { waitUntil: "domcontentloaded" });
await page.getByRole("button", { name: "继续工作", exact: true }).click();
await workbench.waitFor();
await page.waitForFunction(() => [...document.querySelectorAll("video")].every((video) => video.readyState >= 2));
const boundaries = page.getByRole("button", { name: "删除切片", exact: true });
const countBefore = await boundaries.count();
await workbench.focus();
for (let frame = 0; frame < 6; frame++) await page.keyboard.press("ArrowRight");
await page.keyboard.press("c");
assert.equal(await boundaries.count(), countBefore + 1, "C splits in the original editor");
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
assert.equal(await boundaries.count(), countBefore + 1);
await workbench.focus();
await page.keyboard.press("Control+z");
assert.equal(await boundaries.count(), countBefore);
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
await page.waitForTimeout(1100);
await workbench.focus();
await page.keyboard.press("Control+Shift+z");
assert.equal(await boundaries.count(), countBefore + 1);
assert.equal(writes.length, 3, "slice edits and toggles do not submit, release or claim");
console.log("PASS original slicing shortcut and in-memory undo/redo across layouts");
if (process.env.ANNOTATION_SECOND_STORAGE_STATE) {
// Replace only authenticated cookies, retaining this exact origin's preference
// storage. A new empty browser context would not prove account isolation.
await context.addCookies(JSON.parse(readFileSync(process.env.ANNOTATION_SECOND_STORAGE_STATE, "utf8")).cookies);
await page.goto(baseURL + work, { waitUntil: "domcontentloaded" });
await page.getByRole("button", { name: "切回原布局", exact: true }).waitFor();
assert.equal(
await chrome.isVisible(),
false,
"another actual account defaults focused despite first account's original preference",
);
await page.getByRole("button", { name: "切回原布局", exact: true }).click();
await context.addCookies(JSON.parse(readFileSync(process.env.ANNOTATION_STORAGE_STATE, "utf8")).cookies);
await page.reload({ waitUntil: "domcontentloaded" });
await page.getByRole("button", { name: "进入专注视图", exact: true }).waitFor();
await page.getByRole("button", { name: "进入专注视图", exact: true }).click();
await context.addCookies(JSON.parse(readFileSync(process.env.ANNOTATION_SECOND_STORAGE_STATE, "utf8")).cookies);
await page.reload({ waitUntil: "domcontentloaded" });
await page.getByRole("button", { name: "进入专注视图", exact: true }).waitFor();
assert.ok(
await chrome.isVisible(),
"second account original preference is independent of first account focused choice",
);
assert.equal(writes.length, 3, "account preference checks perform no task writes");
console.log("PASS actual account isolation in the same browser origin storage");
}
} finally {
await context.close();
await browser.close();
}