droboticflow/tests/browser/discarded-slice-editing.mjs

156 lines
6.8 KiB
JavaScript

// Isolated company/local fixture only. Does not generate media or submit to QC.
import assert from "node:assert/strict";
import { chromium } from "playwright";
const {
ANNOTATION_BASE_URL: base,
ANNOTATION_PROJECT_ID: project,
ANNOTATION_TASK_ID: task,
ANNOTATION_STORAGE_STATE: auth,
ANNOTATION_VERIFY_PERSISTENCE: persistence,
} = process.env;
assert.ok(base && project && task && auth);
assert.match(project, /continuous-actions|acceptance/);
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"],
});
try {
const context = await browser.newContext({ storageState: auth, viewport: { width: 1440, height: 900 } });
context.setDefaultTimeout(30000);
const page = await context.newPage();
const errors = [];
page.on("pageerror", (error) => errors.push(error.message));
const read = async () => {
const r = await context.request.get(`${base}/api/plugins/annotation-qa/tasks/${task}`);
assert.equal(r.status(), 200);
return (await r.json()).humanItem.subject.payload.annotationWorkspace.sliceSegments;
};
const before = await read();
assert.ok(
before.length >= 2 && before.every((s) => s.deliveryDisposition !== "discard"),
"use a kept partition fixture",
);
const first = before[0],
last = before.at(-1);
await page.goto(`${base}/plugins/annotation-qa/workflows/all/annotate?projectId=${project}&taskId=${task}`, {
waitUntil: "domcontentloaded",
});
const bench = page.getByTestId("capture-annotation-workbench");
const blocks = bench.getByTestId("capture-slice-timeline").locator("[data-slice-id]");
const slider = bench.locator('input[type="range"]').first();
const ready = async () => {
await bench.waitFor();
await page.getByTestId("frame-timeline-status").waitFor({ state: "hidden", timeout: 180000 });
await page.waitForFunction(() =>
document.querySelector('[data-testid="capture-annotation-workbench"]')?.contains(document.activeElement),
);
};
const count = async (n) =>
page.waitForFunction(
(n) => document.querySelectorAll('[data-testid="capture-slice-timeline"] [data-slice-id]').length === n,
n,
);
const confirm = async (dialog, name) => {
const response = page.waitForResponse(
(r) => r.url().endsWith("/slice-dispositions") && r.request().method() === "POST",
{ timeout: 180000 },
);
await dialog.getByRole("button", { name, exact: true }).click();
const r = await response;
assert.equal(r.status(), 200, await r.text());
await dialog.waitFor({ state: "hidden" });
await page.waitForFunction(() =>
document.querySelector('[data-testid="capture-annotation-workbench"]')?.contains(document.activeElement),
);
};
const undo = async () => {
await page.waitForTimeout(1100);
await page.keyboard.press("Control+z");
};
await ready();
if (process.env.ANNOTATION_LAYOUT === "focused") {
await page.getByRole("button", { name: "开始工作", exact: true }).click();
await page.waitForURL("**/work?**", { timeout: 180000, waitUntil: "domcontentloaded" });
await ready();
}
await page.keyboard.press("g");
const discard = page.getByRole("dialog", { name: "舍弃片段", exact: true });
await discard.waitFor();
await discard.locator("[data-dictionary-option]").first().click();
await confirm(discard, "确认舍弃");
const reason = (await read())[0].discardReason;
assert.equal(await bench.evaluate((root) => root.contains(document.activeElement)), true);
// The selected slice remains discarded while the playhead moves into a kept one.
await slider.fill(((last.start + last.end) / 2).toFixed(6));
await page.keyboard.press("c");
await count(before.length + 1);
await undo();
await count(before.length);
// A discarded slice is still an editable part of the partition.
await slider.fill(((first.start + first.end) / 2).toFixed(6));
await page.keyboard.press("c");
await count(before.length + 1);
await page.keyboard.press("g");
await discard.waitFor();
await page.keyboard.press("Escape");
await discard.waitFor({ state: "hidden" });
await page.waitForFunction(() =>
document.querySelector('[data-testid="capture-annotation-workbench"]')?.contains(document.activeElement),
);
const at = Number(await slider.inputValue());
await page.keyboard.press("Shift+ArrowRight");
const moved = Number(await slider.inputValue());
assert.ok(moved > at && moved - at < 0.1, "discard boundary must move by one frame");
await undo();
// Mouse boundary handles are enabled for discarded intervals too.
assert.equal(await bench.getByRole("button", { name: "拖动切片边界", exact: true }).first().isEnabled(), true);
if (persistence === "true") {
await page.keyboard.press("g");
await discard.waitFor();
await confirm(discard, "确认舍弃");
const split = await read();
assert.equal(split.length, before.length + 1);
assert.ok(split.slice(0, 2).every((s) => s.deliveryDisposition === "discard" && s.discardReason === reason));
// Wait for the persisted undo before issuing the next (local) history step.
const undone = page.waitForResponse(
(r) => r.url().endsWith("/slice-dispositions") && r.request().method() === "POST",
{ timeout: 180000 },
);
await undo();
assert.equal((await undone).status(), 200);
await undo();
await count(before.length);
await page.keyboard.press("g");
await discard.waitFor();
await confirm(discard, "确认舍弃");
assert.equal((await read()).length, before.length);
await page.reload({ waitUntil: "domcontentloaded" });
if (process.env.ANNOTATION_LAYOUT === "focused") {
const resume = page.getByRole("button", { name: "继续工作", exact: true });
await resume.waitFor({ timeout: 180000 });
await resume.click();
await resume.waitFor({ state: "hidden", timeout: 180000 });
}
await ready();
await count(before.length);
assert.equal((await read())[0].discardReason, reason);
console.log("PASS split discard persists, inherits reason, undo repartition persists, refresh retains result");
} else {
await undo();
await count(before.length);
}
await bench.getByRole("button", { name: "恢复为保留", exact: true }).click();
const restore = page.getByRole("dialog", { name: "恢复片段", exact: true });
await restore.waitFor();
await confirm(restore, "确认恢复");
const restored = await read();
assert.equal(restored.length, before.length);
assert.ok(restored.every((s) => s.deliveryDisposition !== "discard"));
assert.deepEqual(errors, []);
console.log("PASS discard → cut later kept slice → cut discarded slice → G → boundary nudge → undo → restore");
} finally {
await browser.close();
}