forked from longtao.wu/droboticflow
155 lines
7.1 KiB
JavaScript
155 lines
7.1 KiB
JavaScript
import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { chromium } from "playwright";
|
|
import { createServer } from "vite";
|
|
const api = process.env.ADMIN_POOL_API;
|
|
assert.match(api, /^http:\/\/127\.0\.0\.1:\d+$/);
|
|
const out = path.resolve("local-data/test-results/admin-annotation-pool");
|
|
fs.mkdirSync(out, { recursive: true });
|
|
const report = { pageErrors: [], requests: [], writes: [], cases: [] };
|
|
const server = await createServer({
|
|
cacheDir: "local-data/test-results/vite-admin-annotation-pool-replica",
|
|
server: { host: "127.0.0.1", port: 5205, strictPort: true, proxy: { "/api": { target: api, changeOrigin: true } } },
|
|
});
|
|
let browser, page;
|
|
let releaseSlow;
|
|
let delaySearch = "";
|
|
let pageFault = "";
|
|
try {
|
|
await server.listen();
|
|
browser = await chromium.launch({
|
|
...browserExecutableOptions(),
|
|
|
|
headless: true,
|
|
args: ["--no-proxy-server"],
|
|
});
|
|
page = await browser.newPage({ viewport: { width: 1600, height: 1100 } });
|
|
page.setDefaultTimeout(15000);
|
|
page.on("pageerror", (e) => report.pageErrors.push(e.message));
|
|
await page.route("**/*", async (route) => {
|
|
const r = route.request(),
|
|
u = new URL(r.url());
|
|
if (u.hostname !== "127.0.0.1") return route.abort();
|
|
if (u.pathname.includes("/worker-api/")) return route.fulfill({ json: { status: "ok" } });
|
|
if (!["GET", "HEAD"].includes(r.method())) report.writes.push(u.pathname);
|
|
if (u.pathname.includes("/admin-pool")) {
|
|
report.requests.push(Object.fromEntries(u.searchParams));
|
|
if (u.searchParams.has("cursor") && pageFault) {
|
|
const fault = pageFault;
|
|
pageFault = "";
|
|
if (fault === "reject") return route.fulfill({ status: 500, json: { error: "合成分页失败,请重试" } });
|
|
const response = await route.fetch();
|
|
await new Promise((resolve) => (releaseSlow = resolve));
|
|
return route.fulfill({ response });
|
|
}
|
|
if (delaySearch && u.searchParams.get("search") === delaySearch) {
|
|
delaySearch = "";
|
|
const response = await route.fetch();
|
|
await new Promise((r) => (releaseSlow = r));
|
|
return route.fulfill({ response });
|
|
}
|
|
}
|
|
return route.continue();
|
|
});
|
|
const base = "http://127.0.0.1:5205/plugins/annotation-qa/workflows/all/annotate?projectId=draft-project";
|
|
await page.goto(base);
|
|
const available = page.getByTestId("human-task-pool-available"),
|
|
all = page.getByTestId("human-task-pool-all"),
|
|
cards = page.getByTestId("annotation-task-card");
|
|
await available.getByText("可领取 606", { exact: true }).waitFor();
|
|
await all.getByText("全部任务 611", { exact: true }).waitFor();
|
|
assert.equal(await cards.count(), 200);
|
|
await page.getByTestId("task-pool-show-more").click();
|
|
await page.waitForTimeout(500);
|
|
assert.equal(await cards.count(), 400);
|
|
assert.match(await available.innerText(), /606/);
|
|
report.cases.push("total independent of human cursor pages");
|
|
const more = page.getByTestId("task-pool-show-more");
|
|
pageFault = "reject";
|
|
await more.click();
|
|
await page.getByText("合成分页失败。", { exact: true }).waitFor({ state: "attached" });
|
|
assert.equal(await cards.count(), 400);
|
|
await more.click();
|
|
await page.waitForFunction(() => document.querySelectorAll('[data-testid="annotation-task-card"]').length === 600);
|
|
report.cases.push("failed continuation retains rows and cursor for retry");
|
|
pageFault = "delay";
|
|
const previousRequests = report.requests.filter((r) => r.cursor).length;
|
|
await more.evaluate((button) => {
|
|
button.click();
|
|
button.click();
|
|
});
|
|
for (let n = 0; n < 80 && !releaseSlow; n++) await page.waitForTimeout(50);
|
|
assert.ok(releaseSlow, "held continuation response");
|
|
assert.equal(report.requests.filter((r) => r.cursor).length, previousRequests + 1);
|
|
await page.getByRole("button", { name: "刷新", exact: true }).click();
|
|
await page.waitForFunction(() => document.querySelectorAll('[data-testid="annotation-task-card"]').length === 200);
|
|
releaseSlow();
|
|
releaseSlow = null;
|
|
await page.waitForTimeout(300);
|
|
assert.equal(await cards.count(), 200);
|
|
await more.click();
|
|
await page.waitForFunction(() => document.querySelectorAll('[data-testid="annotation-task-card"]').length === 400);
|
|
report.cases.push("same-scope refresh rejects old continuation and restarts its cursor");
|
|
report.cases.push("rapid double activation sends one continuation");
|
|
|
|
const search = page.getByRole("textbox", { name: "搜索标注任务", exact: true });
|
|
await search.fill("CAPTURE_0001");
|
|
await cards.filter({ hasText: "CAPTURE_0001" }).waitFor();
|
|
assert.equal(await cards.count(), 1);
|
|
assert.match(await available.innerText(), /606/);
|
|
report.cases.push("old item search beyond first500");
|
|
await page.getByRole("combobox", { name: "按任务状态筛选", exact: true }).selectOption("pending_acceptance");
|
|
await page.waitForTimeout(700);
|
|
assert.equal(await cards.count(), 0);
|
|
assert.match(await available.innerText(), /606/);
|
|
report.cases.push("category does not change total");
|
|
await page.getByRole("combobox", { name: "按任务状态筛选", exact: true }).selectOption("all");
|
|
await search.fill("");
|
|
await available.getByText("可领取 606", { exact: true }).waitFor();
|
|
await page.waitForTimeout(500);
|
|
delaySearch = "CAPTURE_0002";
|
|
await search.fill(delaySearch);
|
|
for (let n = 0; n < 40 && !releaseSlow; n++) await page.waitForTimeout(50);
|
|
assert.ok(releaseSlow, "held older search response");
|
|
await search.fill("CAPTURE_0003");
|
|
await cards.filter({ hasText: "CAPTURE_0003" }).waitFor();
|
|
releaseSlow();
|
|
releaseSlow = null;
|
|
await page.waitForTimeout(300);
|
|
assert.equal(await cards.count(), 1);
|
|
assert.match(await cards.first().innerText(), /CAPTURE_0003/);
|
|
report.cases.push("stale search response ignored");
|
|
await search.fill("");
|
|
await all.click();
|
|
await page.waitForTimeout(700);
|
|
await page.getByRole("combobox", { name: "按任务状态筛选", exact: true }).selectOption("pending_quality");
|
|
for (let n = 0; n < 80 && (await cards.count()) !== 4; n++) await page.waitForTimeout(50);
|
|
assert.equal(await cards.count(), 4);
|
|
assert.match(await all.innerText(), /611/);
|
|
assert.match(await available.innerText(), /606/);
|
|
report.cases.push("all pool and server status filter include older review-stage rows");
|
|
const claimed = await fetch(api + "/__fixture/claim", { method: "POST" });
|
|
assert.equal(claimed.status, 200);
|
|
await page.reload();
|
|
await available.getByText("可领取 605", { exact: true }).waitFor();
|
|
assert.match(await all.innerText(), /611/);
|
|
report.cases.push("real claim invalidates cached total");
|
|
assert.deepEqual(report.pageErrors, []);
|
|
assert.deepEqual(report.writes, []);
|
|
console.log(JSON.stringify(report));
|
|
} catch (e) {
|
|
report.failure = e.message;
|
|
if (page) {
|
|
fs.writeFileSync(path.join(out, "failure.txt"), await page.locator("body").innerText());
|
|
await page.screenshot({ path: path.join(out, "failure.png") });
|
|
}
|
|
throw e;
|
|
} finally {
|
|
releaseSlow?.();
|
|
fs.writeFileSync(path.join(out, "report.json"), JSON.stringify(report, null, 2));
|
|
await browser?.close();
|
|
await server.close();
|
|
}
|