// 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", search: "#wfSearch", searchBox: "#wfSearch", filter: "#taskChips .chip", meta: ".wfb-meta", head: ".cc > .ph", file: "#hdFile", action: "#actbar > button", separator: "#actbar .sep", remove: "#actbar [data-act=del]", icon: "#genBtn svg", } : { 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", search: ".rail-search input", searchBox: ".rail-search", filter: ".filters button", meta: ".list-meta", head: ".studio-heading", file: ".workflow-file", action: ".artboard-footer > button", separator: ".action-separator", remove: ".delete-output", icon: ".dock .gen svg", }; return Object.fromEntries( Object.entries(selectors).map(([key, sel]) => { let e = document.querySelector(sel); if (!e) return [key, null]; if (sample && key === "searchBox") e = e.parentElement; 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" ); })), ); ok( `${width}/${theme} R1 搜索与筛选chips几何`, actual.searchBox.height === sample.searchBox.height && actual.searchBox.radius === sample.searchBox.radius && actual.search.font === sample.search.font && actual.filter.height === sample.filter.height && actual.filter.font === sample.filter.font && actual.filter.radius === sample.filter.radius, ); ok( `${width}/${theme} R2 头行与裸文本文件名`, actual.head.height === sample.head.height && Math.abs(actual.name.height - sample.name.height) < 0.1 && actual.name.font === sample.name.font && actual.name.weight === sample.name.weight && actual.file.font === sample.file.font && (await p.locator(".studio-heading button").count()) === 0, ); ok( `${width}/${theme} R3 删除钮与分隔线`, actual.action.radius === sample.action.radius && actual.remove.width === 26 && actual.remove.height === 26 && actual.separator.width === 1 && (await p.locator(".action-separator").count()) === 2, ); ok( `${width}/${theme} R4 14px图标及主题按钮宽度`, actual.icon.width === 14 && Math.abs(actual.button.width - sample.button.width) <= 0.5, ); ok( `${width}/${theme} meta真实未知且无横向溢出`, (await p .locator(".list-meta") .innerText() .then((t) => t.includes("仓库文件数未知"))) && (await p.evaluate( () => document.documentElement.scrollWidth <= innerWidth, )), ); 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"); // HEL-741: real filtering, retained entries and deletion of isolated data. const search = p.getByRole("searchbox", { name: "搜索工作流" }); await search.fill("遮罩图生图测试"); ok( "搜索更新真实筛选数", await p .locator(".list-meta") .innerText() .then((t) => t.startsWith("1 个")), ); const usageGroup = p.getByRole("group", { name: "筛选工作流用途" }); await usageGroup .getByRole("button", { name: "文生图", exact: true }) .click(); ok( "用途chips与搜索组合筛选", (await p.locator(".workflow-pick").count()) === 0, ); await usageGroup.getByRole("button", { name: "全部", exact: true }).click(); if ( (await p .locator(".row-actions button") .last() .getAttribute("aria-pressed")) !== "true" ) await p.locator(".row-actions button").last().click(); await usageGroup .getByRole("button", { name: "☆ 收藏", exact: true }) .click(); ok( "收藏chip保留真实收藏筛选", (await p.locator(".workflow-pick").count()) === 1, ); await shot(p, "filters-favorite"); await usageGroup .getByRole("button", { name: "☆ 收藏", exact: true }) .click(); await search.fill(""); await p.locator("[data-workflow-details]").click(); await p.getByRole("dialog").waitFor(); await shot(p, "workflow-details"); await p.keyboard.press("Escape"); ok( "说明入口关闭归还焦点", await p .locator("[data-workflow-details]") .evaluate((e) => e === document.activeElement), ); await p.getByRole("button", { name: "创作设置⌄", exact: true }).click(); await p.getByRole("button", { name: "专注创作 ⤢", exact: true }).click(); await p.keyboard.press("Escape"); ok("参数层保留专注功能", !(await p.locator(".workflow-rail").isVisible())); await shot(p, "focus-mode"); await p.getByRole("button", { name: "创作设置⌄", exact: true }).click(); await p.getByRole("button", { name: "退出专注 ⤢", exact: true }).click(); await p.keyboard.press("Escape"); const deleteTarget = (await api("/api/playground/jobs")).find( (j) => j.workflow_id === mask.id && j.status === "done", ); assert(deleteTarget); await p.locator(".delete-output").click(); ok( "删除图标先确认且不发DELETE", (await api("/api/playground/jobs")).some((j) => j.id === deleteTarget.id), ); await shot(p, "delete-confirm"); await p.getByRole("button", { name: "取消", exact: true }).click(); ok( "删除取消保留记录并归还焦点", (await api("/api/playground/jobs")).some( (j) => j.id === deleteTarget.id, ) && (await p .locator(".delete-output") .evaluate((e) => e === document.activeElement)), ); await p.locator(".delete-output").click(); await p.getByRole("button", { name: "确认移除", exact: true }).click(); await p.getByRole("button", { name: "撤销", exact: true }).click(); ok( "确认后沿用撤销窗口", (await api("/api/playground/jobs")).some((j) => j.id === deleteTarget.id), ); await shot(p, "delete-undone"); await p.locator(".delete-output").click(); await p.getByRole("button", { name: "确认移除", exact: true }).click(); await wait( async () => !(await api("/api/playground/jobs")).some( (j) => j.id === deleteTarget.id, ), ); ok( "隔离数据确认后真实删除", !(await api("/api/playground/jobs")).some( (j) => j.id === deleteTarget.id, ), ); // 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; });