forked from longtao.wu/droboticflow
207 lines
9.7 KiB
JavaScript
207 lines
9.7 KiB
JavaScript
// Real browser + public API acceptance. Run only against an explicitly reserved
|
|
// editable fixture; writes are never enabled by default.
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import { chromium } from "playwright";
|
|
const base = process.env.ANNOTATION_BASE_URL || "http://127.0.0.1:5173";
|
|
const project = process.env.ANNOTATION_PROJECT_ID;
|
|
const task = process.env.ANNOTATION_TASK_ID;
|
|
const auth = process.env.ANNOTATION_STORAGE_STATE;
|
|
assert.ok(project && task && auth, "Provide fixture project/task/auth");
|
|
assert.match(project, /continuous-actions|acceptance/);
|
|
assert.equal(process.env.ANNOTATION_ALLOW_FIXTURE_WRITES, "true", "Explicit fixture-write opt in required");
|
|
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: auth, viewport: { width: 1413, height: 615 } });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(20000);
|
|
const errors = [];
|
|
page.on("pageerror", (error) => errors.push(error.message));
|
|
const workbench = page.getByTestId("capture-annotation-workbench");
|
|
const button = (name) => page.getByRole("button", { name, exact: true });
|
|
const options = () => page.getByRole("listbox", { name: "动作区间", exact: true }).getByRole("option");
|
|
const selected = () => page.getByRole("listbox", { name: "动作区间", exact: true }).locator('[aria-selected="true"]');
|
|
const read = async () => {
|
|
const r = await context.request.get(`${base}/api/plugins/annotation-qa/tasks/${task}`);
|
|
assert.ok(r.ok());
|
|
return r.json();
|
|
};
|
|
const savedActions = (detail) => detail.humanItem.subject.payload.annotationWorkspace.segments;
|
|
const enter = async (fromPool = false) => {
|
|
await page.goto(
|
|
`${base}/plugins/annotation-qa/workflows/all/${fromPool ? "annotate" : "work"}?projectId=${project}&taskId=${task}`,
|
|
);
|
|
if (fromPool) {
|
|
await workbench.waitFor();
|
|
assert.equal(
|
|
await workbench.getAttribute("data-camera-inspection"),
|
|
null,
|
|
"normal layout does not force an inspection view",
|
|
);
|
|
await button("开始工作").click();
|
|
await page.waitForURL(/\/work\?/);
|
|
} else {
|
|
await button("继续工作").waitFor();
|
|
const row = page.getByRole("button").filter({ hasText: task });
|
|
if (await row.count()) await row.first().click();
|
|
await button("继续工作").click();
|
|
}
|
|
await workbench.waitFor();
|
|
await page.waitForFunction(
|
|
() =>
|
|
[...document.querySelectorAll("video")].length === 2 &&
|
|
[...document.querySelectorAll("video")].every((video) => video.readyState >= 2),
|
|
null,
|
|
{ timeout: 90000 },
|
|
);
|
|
};
|
|
const reachable = async (locator) =>
|
|
assert.ok(
|
|
await locator.evaluate((element) => {
|
|
const r = element.getBoundingClientRect();
|
|
return (
|
|
r.width > 0 &&
|
|
r.height > 0 &&
|
|
r.left >= 0 &&
|
|
r.top >= 0 &&
|
|
r.right <= innerWidth + 1 &&
|
|
r.bottom <= innerHeight + 1
|
|
);
|
|
}),
|
|
"core control remains within viewport",
|
|
);
|
|
const save = async (name) => {
|
|
const response = page.waitForResponse(
|
|
(r) => r.url().endsWith(`/tasks/${task}/annotation-confirmations`) && r.request().method() === "POST",
|
|
);
|
|
await button(name).click();
|
|
assert.ok((await response).ok());
|
|
};
|
|
try {
|
|
const original = await read();
|
|
assert.equal(original.task.continuousActions, true, "trusted server scope enabled");
|
|
await enter(true);
|
|
assert.equal(await workbench.getAttribute("data-stage"), "annotate", "fixture must already be in annotation stage");
|
|
assert.equal(await workbench.getAttribute("data-camera-inspection"), "left");
|
|
for (const camera of ["恢复双画面", "放大右相机", "放大左相机"]) {
|
|
await button(camera).click();
|
|
await reachable(page.getByTestId("capture-slice-timeline"));
|
|
await reachable(page.getByTestId("slice-annotation-timeline"));
|
|
await reachable(button("保存进度"));
|
|
}
|
|
// Search has no selected option: Up enters at the final visible reason,
|
|
// while Down enters at the first. Merely navigating must not discard.
|
|
await workbench.focus();
|
|
await page.keyboard.press("g");
|
|
const discardSearch = page.getByRole("textbox", { name: "搜索舍弃原因", exact: true });
|
|
await discardSearch.waitFor();
|
|
const discardDialog = page.getByRole("dialog", { name: "舍弃片段", exact: true });
|
|
await reachable(discardDialog.getByRole("button", { name: "确认舍弃", exact: true }));
|
|
await reachable(discardDialog.getByRole("button", { name: "取消", exact: true }));
|
|
await reachable(discardDialog.getByRole("textbox", { name: "舍弃补充说明", exact: true }));
|
|
assert.ok(
|
|
await discardDialog.evaluate((element) => element.scrollHeight <= element.clientHeight + 1),
|
|
"discard confirmation requires no dialog scrolling",
|
|
);
|
|
const reasons = page.getByRole("listbox", { name: "舍弃原因", exact: true }).getByRole("option");
|
|
await reasons.nth(1).waitFor();
|
|
assert.ok((await reasons.count()) >= 2, "Fixture needs at least two enabled discard reasons");
|
|
await discardSearch.focus();
|
|
await page.keyboard.press("ArrowUp");
|
|
assert.ok(await reasons.last().evaluate((element) => document.activeElement === element));
|
|
await discardSearch.focus();
|
|
await page.keyboard.press("ArrowDown");
|
|
assert.ok(await reasons.first().evaluate((element) => document.activeElement === element));
|
|
await page.keyboard.press("Escape");
|
|
await page.locator('video[data-camera="left"]').click();
|
|
await page.waitForFunction(() => !document.querySelector('video[data-camera="left"]').paused);
|
|
await page.locator('video[data-camera="left"]').click();
|
|
await page.waitForFunction(() => document.querySelector('video[data-camera="left"]').paused);
|
|
const slider = page.getByRole("slider", { name: "当前切片播放位置", exact: true });
|
|
const box = await slider.boundingBox();
|
|
await page.mouse.click(box.x + box.width * 0.12, box.y + box.height / 2);
|
|
await page.keyboard.press("Space");
|
|
await page.waitForFunction(() => !document.querySelector('video[data-camera="left"]').paused);
|
|
await page.keyboard.press("Space");
|
|
await options().first().click();
|
|
await page.keyboard.press("Alt+ArrowUp");
|
|
const initialCount = await options().count();
|
|
assert.equal(initialCount, 1, "Use a fresh full pending fixture for dense cutting");
|
|
for (let cut = 0; cut < 50; cut++) {
|
|
for (let frame = 0; frame < 4; frame++) await page.keyboard.press("ArrowRight");
|
|
await page.keyboard.press("c");
|
|
}
|
|
assert.equal(await options().count(), 51);
|
|
await page.keyboard.press("n");
|
|
assert.equal(await options().count(), 51, "N is removed");
|
|
await page.keyboard.press("Shift+Delete");
|
|
assert.equal(await options().count(), 51, "standalone action delete removed");
|
|
await save("保存进度");
|
|
let detail = await read();
|
|
assert.equal(savedActions(detail).length, 51);
|
|
assert.ok(savedActions(detail).every((a) => a.status === "pending"));
|
|
await enter();
|
|
assert.equal(await options().count(), 51, "pending partition survives refresh");
|
|
await options().first().click();
|
|
await page.keyboard.press("Control+ArrowDown");
|
|
assert.match(await selected().innerText(), /^#7 /);
|
|
await page.keyboard.press("ArrowUp");
|
|
assert.match(await selected().innerText(), /^#6 /);
|
|
await options().first().click();
|
|
await page.keyboard.press("ArrowUp");
|
|
assert.match(await selected().innerText(), /^#51 /);
|
|
await page.keyboard.press("ArrowDown");
|
|
assert.match(await selected().innerText(), /^#1 /);
|
|
const beforeBoundary = savedActions(await read())[0].end;
|
|
await page.keyboard.press("Shift+ArrowRight");
|
|
await save("保存进度");
|
|
detail = await read();
|
|
assert.ok(savedActions(detail)[0].end > beforeBoundary);
|
|
const description = page.getByRole("textbox", { name: "动作描述", exact: true });
|
|
// Programmatic/text focus must cancel an accelerated boundary gesture too.
|
|
await workbench.focus();
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.down("ArrowRight");
|
|
await description.focus();
|
|
const endField = page.getByRole("spinbutton", { name: "动作结束时间(秒)", exact: true });
|
|
const stoppedAt = await endField.inputValue();
|
|
await page.waitForTimeout(1600);
|
|
assert.equal(await endField.inputValue(), stoppedAt, "text focus stops held boundary updates");
|
|
await page.keyboard.up("ArrowRight");
|
|
await page.keyboard.up("Shift");
|
|
await description.fill("连续动作验收:拿起物品");
|
|
const descriptionSave = page.waitForResponse(
|
|
(r) => r.url().endsWith(`/tasks/${task}/annotation-confirmations`) && r.request().method() === "POST",
|
|
);
|
|
await page.keyboard.press("Control+Enter");
|
|
assert.ok((await descriptionSave).ok());
|
|
await options().first().click();
|
|
const invalid = page.waitForResponse(
|
|
(r) => r.url().endsWith(`/tasks/${task}/annotation-confirmations`) && r.request().method() === "POST",
|
|
);
|
|
await workbench.focus();
|
|
await page.keyboard.press("Control+x");
|
|
assert.ok((await invalid).ok());
|
|
detail = await read();
|
|
assert.equal(savedActions(detail)[0].isInvalid, true);
|
|
const undoSave = page.waitForResponse(
|
|
(r) => r.url().endsWith(`/tasks/${task}/annotation-confirmations`) && r.request().method() === "POST",
|
|
);
|
|
await page.keyboard.press("Control+z");
|
|
assert.ok((await undoSave).ok());
|
|
detail = await read();
|
|
assert.equal(savedActions(detail)[0].description, "连续动作验收:拿起物品");
|
|
assert.deepEqual(errors, []);
|
|
if (process.env.ANNOTATION_EVIDENCE_DIR) {
|
|
fs.mkdirSync(process.env.ANNOTATION_EVIDENCE_DIR, { recursive: true });
|
|
await page.screenshot({ path: `${process.env.ANNOTATION_EVIDENCE_DIR}/continuous-editor.png` });
|
|
}
|
|
console.log(JSON.stringify({ passed: true, actions: 51, viewport: [1413, 615], task, errors }));
|
|
} finally {
|
|
await browser.close();
|
|
}
|