444 lines
15 KiB
JavaScript
444 lines
15 KiB
JavaScript
// HEL-740: same-viewport sample comparisons plus anchored-dialog/state evidence.
|
|
const { chromium } = require(
|
|
process.env.PLAYWRIGHT_MODULE_PATH || "playwright",
|
|
);
|
|
const fs = require("node:fs"),
|
|
path = require("node:path"),
|
|
assert = require("node:assert/strict");
|
|
const out = "e2e/shots/hel740";
|
|
fs.mkdirSync(out, { recursive: true });
|
|
let currentPage;
|
|
const checks = [],
|
|
measurements = [],
|
|
errors = [];
|
|
const ok = (name, value) => {
|
|
assert(value, name);
|
|
checks.push(name);
|
|
console.log("PASS", name);
|
|
};
|
|
const api = async (p) => (await fetch("http://127.0.0.1:8247" + p)).json();
|
|
const pause = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
async function wait(fn) {
|
|
for (let i = 0; i < 160; i++) {
|
|
if (await fn()) return;
|
|
await pause(100);
|
|
}
|
|
throw Error("timeout");
|
|
}
|
|
async function metrics(page, sample = false) {
|
|
return page.evaluate((sample) => {
|
|
const selectors = sample
|
|
? {
|
|
button: "#genBtn",
|
|
dock: "#dock",
|
|
positive: "#posTa",
|
|
negative: "#negTa",
|
|
segment: "#kindSeg",
|
|
name: "#hdName",
|
|
history: ".hist",
|
|
actions: "#actbar",
|
|
pop: "#paramPop",
|
|
}
|
|
: {
|
|
button: ".dock .gen",
|
|
dock: ".dock .composer",
|
|
positive: "#desk-prompt",
|
|
negative: "#desk-negative",
|
|
segment: ".kind-tabs",
|
|
name: ".model-selector",
|
|
history: ".history-rail",
|
|
actions: ".artboard-footer",
|
|
pop: ".anchored .modal-panel",
|
|
};
|
|
return Object.fromEntries(
|
|
Object.entries(selectors).map(([key, sel]) => {
|
|
let e = document.querySelector(sel);
|
|
if (!e) return [key, null];
|
|
let r = e.getBoundingClientRect(),
|
|
s = getComputedStyle(e);
|
|
return [
|
|
key,
|
|
{
|
|
x: r.x,
|
|
y: r.y,
|
|
width: r.width,
|
|
height: r.height,
|
|
bottom: r.bottom,
|
|
radius: s.borderRadius,
|
|
font: s.fontSize,
|
|
weight: s.fontWeight,
|
|
background: s.backgroundImage,
|
|
border: s.borderTopWidth,
|
|
shadow: s.boxShadow,
|
|
},
|
|
];
|
|
}),
|
|
);
|
|
}, sample);
|
|
}
|
|
async function shot(page, name) {
|
|
await page.screenshot({ path: out + "/" + name + ".png" });
|
|
}
|
|
(async () => {
|
|
const b = await chromium.launch({
|
|
executablePath: process.env.CHROMIUM_EXECUTABLE_PATH,
|
|
args: ["--no-sandbox", "--disable-dev-shm-usage"],
|
|
});
|
|
const saved = fs.existsSync("e2e/fake_mode.json")
|
|
? fs.readFileSync("e2e/fake_mode.json")
|
|
: null;
|
|
try {
|
|
const c = await b.newContext({
|
|
viewport: { width: 1440, height: 900 },
|
|
recordVideo: {
|
|
dir: out + "/video",
|
|
size: { width: 1440, height: 900 },
|
|
},
|
|
}),
|
|
p = await c.newPage(),
|
|
s = await c.newPage();
|
|
currentPage = p;
|
|
p.on("pageerror", (e) => errors.push(e.message));
|
|
p.setDefaultTimeout(12000);
|
|
await s.goto(
|
|
"file://" +
|
|
path.resolve(
|
|
process.env.HEL740_SAMPLE ||
|
|
"../references/夜片场-多主题交互样本.html",
|
|
) +
|
|
"?still=1&fill=1#create/image",
|
|
);
|
|
await s.locator("#posTa").waitFor();
|
|
const data = await s.evaluate(async () => {
|
|
const img = new Image();
|
|
img.src = st.hist.image[0].img;
|
|
await img.decode();
|
|
const cv = document.createElement("canvas");
|
|
cv.width = img.naturalWidth;
|
|
cv.height = img.naturalHeight;
|
|
cv.getContext("2d").drawImage(img, 0, 0);
|
|
return {
|
|
img: cv.toDataURL("image/png"),
|
|
pos: $("#posTa").value,
|
|
neg: $("#negTa").value,
|
|
};
|
|
});
|
|
fs.writeFileSync(
|
|
out + "/sample-media.png",
|
|
Buffer.from(data.img.split(",")[1], "base64"),
|
|
);
|
|
const workflows = await api("/api/workflows"),
|
|
mask = workflows.find((w) => w.name === "遮罩图生图测试");
|
|
assert(mask);
|
|
await p.goto("http://127.0.0.1:5173/create?wf=" + mask.id);
|
|
await p.locator("#desk-negative").waitFor();
|
|
const jobs = await api("/api/playground/jobs"),
|
|
job = jobs.find((j) => j.workflow_id === mask.id && j.status === "done");
|
|
assert(job);
|
|
await p
|
|
.locator(".history-take")
|
|
.filter({ hasText: "#" + job.id })
|
|
.click();
|
|
await p.getByRole("button", { name: "复用参数", exact: true }).click();
|
|
await p.locator("#desk-prompt").fill(data.pos);
|
|
await p.locator("#desk-negative").fill(data.neg);
|
|
fs.writeFileSync(
|
|
"e2e/fake_mode.json",
|
|
JSON.stringify({
|
|
mode: "ok",
|
|
unique_filename: true,
|
|
image_file: path.resolve(out + "/sample-media.png"),
|
|
}),
|
|
);
|
|
const before = Math.max(...jobs.map((j) => j.id));
|
|
await p.locator(".gen").click();
|
|
await wait(async () =>
|
|
(await api("/api/playground/jobs")).some(
|
|
(j) => j.id > before && j.status === "done",
|
|
),
|
|
);
|
|
await wait(() =>
|
|
p.locator(".print img").evaluate((e) => e.complete && e.naturalWidth > 0),
|
|
);
|
|
await pause(1000);
|
|
for (const [width, height] of [
|
|
[1440, 900],
|
|
[1366, 768],
|
|
[1280, 720],
|
|
]) {
|
|
await p.setViewportSize({ width, height });
|
|
await s.setViewportSize({ width, height });
|
|
for (const theme of ["night", "day", "grade", "bluehour"]) {
|
|
await p.getByRole("button", { name: "主题", exact: true }).click();
|
|
await p
|
|
.locator(".choice")
|
|
.filter({
|
|
hasText: {
|
|
night: "夜片场",
|
|
day: "日光棚",
|
|
grade: "调色间",
|
|
bluehour: "蓝调时刻",
|
|
}[theme],
|
|
})
|
|
.click();
|
|
await p.keyboard.press("Escape");
|
|
await s.evaluate((t) => {
|
|
document.documentElement.dataset.theme = t;
|
|
renderCreate();
|
|
}, theme);
|
|
await pause(650);
|
|
const actual = await metrics(p),
|
|
sample = await metrics(s, true);
|
|
measurements.push({ width, height, theme, actual, sample });
|
|
ok(
|
|
`${width}/${theme} 正负提示词宽度与样本误差不超过1px`,
|
|
Math.abs(actual.positive.width - sample.positive.width) <= 1 &&
|
|
Math.abs(actual.negative.width - sample.negative.width) <= 1,
|
|
);
|
|
ok(
|
|
`${width}/${theme} 按钮38高、提示词60高13px、操作条40高、分段36高`,
|
|
actual.button.height === 38 &&
|
|
actual.positive.height === 60 &&
|
|
actual.positive.font === "13px" &&
|
|
actual.actions.height === 40 &&
|
|
actual.segment.height === 36,
|
|
);
|
|
ok(
|
|
`${width}/${theme} 首屏按钮与作品完整`,
|
|
actual.button.bottom <= height &&
|
|
(await p.locator(".print img").evaluate((e) => {
|
|
let r = e.getBoundingClientRect();
|
|
return (
|
|
r.top >= 48 &&
|
|
r.bottom < innerHeight &&
|
|
getComputedStyle(e).objectFit === "contain"
|
|
);
|
|
})),
|
|
);
|
|
await shot(s, `${width}-${theme}-sample`);
|
|
await shot(p, `${width}-${theme}-actual`);
|
|
const actions = p.locator(
|
|
".artboard-footer > button,.artboard-footer > a",
|
|
);
|
|
ok(`${width}/${theme} 八入口显式存在`, (await actions.count()) === 8);
|
|
}
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.click();
|
|
await pause(250);
|
|
const popup = await metrics(p);
|
|
ok(
|
|
`${width} 参数锚定底座上方且不遮主按钮`,
|
|
popup.pop.bottom <= popup.dock.y - 7 &&
|
|
popup.pop.y >= 48 &&
|
|
popup.pop.width === 620,
|
|
);
|
|
await s.evaluate(() => openPop());
|
|
await pause(250);
|
|
measurements.push({
|
|
width,
|
|
height,
|
|
state: "params",
|
|
actual: await metrics(p),
|
|
sample: await metrics(s, true),
|
|
});
|
|
await shot(p, `${width}-params-actual`);
|
|
await shot(s, `${width}-params-sample`);
|
|
// Focus cycle and draft-only reset.
|
|
await p.locator(".anchored input[type=number]").first().fill("333");
|
|
await p
|
|
.getByRole("button", { name: "恢复工作流默认", exact: true })
|
|
.click();
|
|
ok(
|
|
`${width} 恢复默认只改弹层草稿`,
|
|
(await p
|
|
.locator(".anchored input[type=number]")
|
|
.first()
|
|
.inputValue()) === "42",
|
|
);
|
|
await p.keyboard.press("Escape");
|
|
ok(
|
|
`${width} Escape归还焦点`,
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.evaluate((e) => e === document.activeElement),
|
|
);
|
|
await s.evaluate(() => $("#paramPop").classList.remove("on"));
|
|
}
|
|
await p.setViewportSize({ width: 1440, height: 900 });
|
|
await p.getByRole("button", { name: "收起历史栏", exact: true }).click();
|
|
ok(
|
|
"历史收起列宽归零",
|
|
(await p
|
|
.locator(".history-rail")
|
|
.evaluate((e) => e.getBoundingClientRect().width)) === 0,
|
|
);
|
|
await shot(p, "history-collapsed");
|
|
await p.getByRole("button", { name: "展开历史栏", exact: true }).click();
|
|
await p
|
|
.locator(".history-take")
|
|
.filter({ hasText: "#" + job.id })
|
|
.click();
|
|
await shot(p, "history-banner");
|
|
ok(
|
|
"历史胶囊999圆角",
|
|
(await p
|
|
.locator(".history-banner")
|
|
.evaluate((e) => getComputedStyle(e).borderRadius)) === "999px",
|
|
);
|
|
await p.getByRole("button", { name: "返回当前创作", exact: true }).click();
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.click();
|
|
const dialog = p.locator(".anchored .modal-panel");
|
|
await dialog.locator("button").last().focus();
|
|
await p.keyboard.press("Tab");
|
|
ok(
|
|
"参数Tab焦点循环",
|
|
await dialog.evaluate((e) => e.contains(document.activeElement)),
|
|
);
|
|
await p.locator(".anchored input[type=number]").first().fill("234");
|
|
await p.getByRole("button", { name: "取消", exact: true }).click();
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.click();
|
|
ok(
|
|
"参数取消丢弃修改",
|
|
(await p.locator(".anchored input[type=number]").first().inputValue()) !==
|
|
"234",
|
|
);
|
|
await p.locator(".anchored input[type=number]").first().fill("235");
|
|
await p.getByRole("button", { name: "保存设置", exact: true }).click();
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.click();
|
|
ok(
|
|
"参数保存草稿持久化",
|
|
(await p.locator(".anchored input[type=number]").first().inputValue()) ===
|
|
"235",
|
|
);
|
|
await p
|
|
.getByRole("button", { name: "恢复工作流默认", exact: true })
|
|
.click();
|
|
await p.getByRole("button", { name: "保存设置", exact: true }).click();
|
|
await p
|
|
.getByRole("button", { name: "创作设置", exact: true })
|
|
.first()
|
|
.click();
|
|
ok(
|
|
"恢复默认可保存",
|
|
(await p.locator(".anchored input[type=number]").first().inputValue()) ===
|
|
"42",
|
|
);
|
|
await p.keyboard.press("Escape");
|
|
// Actual existing disabled abilities have explanatory titles; no fake success.
|
|
ok(
|
|
"未具备送视频能力如实禁用",
|
|
await p
|
|
.getByRole("button", { name: "送去生视频", exact: true })
|
|
.isDisabled(),
|
|
);
|
|
ok(
|
|
"无来源时通用存入项目禁用",
|
|
await p
|
|
.getByRole("button", { name: "存入项目", exact: true })
|
|
.isDisabled(),
|
|
);
|
|
await p.getByRole("button", { name: "放大查看", exact: true }).click();
|
|
await shot(p, "zoom");
|
|
await p.keyboard.press("Escape");
|
|
await p.getByRole("button", { name: "更多", exact: true }).click();
|
|
await shot(p, "more-actions");
|
|
await p.keyboard.press("Escape");
|
|
// Video state evidence across the same three viewports.
|
|
await p.getByRole("tab", { name: "视频", exact: true }).click();
|
|
await p.locator("#desk-prompt").waitFor();
|
|
const videos = (await api("/api/playground/jobs")).filter(
|
|
(j) => j.kind === "video" && j.status === "done",
|
|
);
|
|
const vj = videos.find((j) => j.workflow_id === 4);
|
|
assert(vj);
|
|
await p
|
|
.locator(".history-take")
|
|
.filter({ hasText: "#" + vj.id })
|
|
.click();
|
|
await p.getByRole("button", { name: "复用参数", exact: true }).click();
|
|
await p.locator(".print video").waitFor();
|
|
await s.evaluate(() => go("create", { kind: "video" }));
|
|
for (const [width, height] of [
|
|
[1440, 900],
|
|
[1366, 768],
|
|
[1280, 720],
|
|
]) {
|
|
await p.setViewportSize({ width, height });
|
|
await s.setViewportSize({ width, height });
|
|
await pause(350);
|
|
await shot(p, `${width}-video-actual`);
|
|
await shot(s, `${width}-video-sample`);
|
|
ok(
|
|
`${width} 视频默认暂停且播放键64`,
|
|
(await p.locator(".print video").evaluate((e) => e.paused)) &&
|
|
(await p
|
|
.locator(".big-play")
|
|
.evaluate((e) => e.getBoundingClientRect().width === 64)),
|
|
);
|
|
}
|
|
await p.getByRole("button", { name: "播放此视频", exact: true }).click();
|
|
await pause(500);
|
|
await shot(p, "video-playing");
|
|
await p.locator(".print video").focus();
|
|
await p.keyboard.press("Space");
|
|
await shot(p, "video-paused");
|
|
await p.getByRole("tab", { name: "图片", exact: true }).click();
|
|
await p.locator(".workflow-pick").first().hover();
|
|
await shot(p, "workflow-hover");
|
|
await p.locator(".gen").hover();
|
|
await shot(p, "generate-hover");
|
|
fs.writeFileSync(
|
|
"e2e/fake_mode.json",
|
|
JSON.stringify({
|
|
mode: "ok",
|
|
delay_sec: 10,
|
|
unique_filename: true,
|
|
image_file: path.resolve(out + "/sample-media.png"),
|
|
}),
|
|
);
|
|
await p.locator(".gen").click();
|
|
await p.getByRole("button", { name: "取消生成", exact: true }).waitFor();
|
|
await shot(p, "generating");
|
|
await p.getByRole("button", { name: "取消生成", exact: true }).click();
|
|
ok("无浏览器异常", errors.length === 0);
|
|
fs.writeFileSync(
|
|
out + "/measurements.json",
|
|
JSON.stringify(measurements, null, 2),
|
|
);
|
|
fs.writeFileSync(
|
|
out + "/results.json",
|
|
JSON.stringify({ checks, errors }, null, 2),
|
|
);
|
|
await c.close();
|
|
fs.copyFileSync(await p.video().path(), out + "/actual-interactions.webm");
|
|
fs.copyFileSync(await s.video().path(), out + "/sample-interactions.webm");
|
|
} catch (e) {
|
|
fs.writeFileSync(
|
|
out + "/measurements.json",
|
|
JSON.stringify(measurements, null, 2),
|
|
);
|
|
if (currentPage) await shot(currentPage, "failure").catch(() => {});
|
|
throw e;
|
|
} finally {
|
|
if (saved) fs.writeFileSync("e2e/fake_mode.json", saved);
|
|
else if (fs.existsSync("e2e/fake_mode.json"))
|
|
fs.unlinkSync("e2e/fake_mode.json");
|
|
await b.close();
|
|
}
|
|
})().catch((e) => {
|
|
console.error(e);
|
|
process.exitCode = 1;
|
|
});
|