Files
aigc-home/e2e/hel729.cjs
T
2026-10-24 21:05:08 +08:00

592 lines
22 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// B1 integration: disposable FastAPI -> fake ComfyUI, real UI and HTTP only.
const { chromium } = require(
process.env.PLAYWRIGHT_MODULE_PATH || "playwright",
);
const assert = require("node:assert/strict"),
fs = require("node:fs"),
path = require("node:path");
const API = "http://127.0.0.1:8247",
WEB = "http://127.0.0.1:5173",
out = "e2e/shots/hel729";
fs.mkdirSync(out, { recursive: true });
const f = JSON.parse(fs.readFileSync("e2e/shots/reference/fixtures.json"));
const checks = [];
function ok(n, v) {
assert(v, n);
checks.push(n);
console.log("PASS", n);
}
async function api(p, b, method) {
const r = await fetch(API + p, {
method: method || (b ? "POST" : "GET"),
headers: { "Content-Type": "application/json" },
body: b ? JSON.stringify(b) : undefined,
});
if (!r.ok) throw Error(p + " " + (await r.text()));
return r.json();
}
async function wait(fn) {
for (let i = 0; i < 120; i++) {
const v = await fn();
if (v) return v;
await new Promise((r) => setTimeout(r, 150));
}
throw Error("condition timeout");
}
function fixtureDimensions(file) {
const b = fs.readFileSync(file);
return b.readUInt32BE(16) === 480 && b.readUInt32BE(20) === 720;
}
function mode(video = false, extra = {}) {
fs.writeFileSync(
"e2e/fake_mode.json",
JSON.stringify({
mode: "ok",
delay_sec: 0,
unique_filename: true,
videos: video,
image_file: path.resolve("calliope-web/static/refract/coast.jpg"),
...extra,
}),
);
}
async function submit(p, wf, video = false) {
mode(video);
const before = Math.max(
0,
...(await api("/api/playground/jobs")).map((j) => j.id),
);
await p.locator(".gen").click();
const j = await wait(async () =>
(await api("/api/playground/jobs")).find(
(j) => j.id > before && j.workflow_id === wf && j.status === "done",
),
);
await p
.locator(".result-title")
.filter({ hasText: "第 " + j.id + " 次生成" })
.waitFor();
return j;
}
async function shot(p, name) {
await p.screenshot({ path: out + "/" + name + ".png" });
}
(async () => {
const browser = await chromium.launch({
executablePath: process.env.CHROMIUM_EXECUTABLE_PATH,
args: ["--no-sandbox", "--disable-dev-shm-usage"],
});
const errors = [];
let p;
const saved = fs.existsSync("e2e/fake_mode.json")
? fs.readFileSync("e2e/fake_mode.json")
: null;
try {
const c = await browser.newContext({
viewport: { width: 1440, height: 900 },
});
p = await c.newPage();
p.setDefaultTimeout(15000);
p.on("pageerror", (e) => errors.push(e.message));
await p.goto(WEB + "/image?wf=" + f.dual + "#source");
await p.locator("#desk-negative").waitFor();
ok(
"旧生图入口进入统一创作台并保留工作流和片段",
new URL(p.url()).pathname === "/create" &&
new URL(p.url()).hash === "#source" &&
new URL(p.url()).searchParams.get("wf") === String(f.dual),
);
await p.locator("#desk-prompt").fill("一位女孩站在海岸,清晨柔光。");
await p.locator("#desk-negative").fill("模糊、畸形");
await p.locator(".rail-tools input[type=search]").fill("Qwen");
ok("检索工作流", (await p.locator(".workflow-pick").count()) === 1);
await p.locator(".row-actions button").first().click();
ok(
"置顶保留输入",
(await p.locator("#desk-prompt").inputValue()) ===
"一位女孩站在海岸,清晨柔光。",
);
await p
.getByRole("button", { name: "创作设置⌄", exact: true })
.first()
.click();
await p.locator(".modal-panel input[type=number]").first().fill("99");
await p.getByRole("button", { name: "取消", exact: true }).click();
await p
.getByRole("button", { name: "创作设置⌄", exact: true })
.first()
.click();
ok(
"参数取消不保存",
(await p
.locator(".modal-panel input[type=number]")
.first()
.inputValue()) === "42",
);
await p.locator(".modal-panel input[type=number]").first().fill("101");
await p.getByRole("button", { name: "保存设置", exact: true }).click();
const j = await submit(p, f.dual);
ok(
"真实图片生成写入正反向和参数",
JSON.stringify(j.payload).includes("101") &&
JSON.stringify(j.payload).includes("模糊、畸形"),
);
await p.locator("#desk-prompt").fill("这是当前输入,不随历史变化");
await p
.locator(".history-take")
.filter({ hasText: "#" + j.id })
.click();
ok(
"历史状态明确且不修改输入",
(await p.locator(".history-banner").isVisible()) &&
(await p.locator("#desk-prompt").inputValue()) ===
"这是当前输入,不随历史变化",
);
await shot(p, "1440-history");
await p.getByRole("button", { name: "返回当前创作", exact: true }).click();
await p.getByRole("button", { name: "展开提示词", exact: true }).click();
await p.locator("#desk-prompt").fill("长提示词描述。".repeat(600));
ok(
"长提示词可展开且生成键可见",
(await p.locator("#desk-prompt").evaluate((e) => e.clientHeight > 180)) &&
(await p.locator(".gen").isVisible()),
);
await shot(p, "1440-long-prompt");
await p.getByRole("button", { name: "收起提示词", exact: true }).click();
await p.locator("#desk-prompt").fill("这是当前输入,不随历史变化");
// Previously unseen workflow inherits semantic prompts, returning restores its own parameters.
await p.locator(".rail-tools input[type=search]").fill("文生图·回归");
await p
.locator(".workflow-pick")
.filter({ hasText: "文生图·回归" })
.click();
await p.waitForFunction(() => !document.querySelector("#desk-negative"));
ok(
"切换新工作流保留提示词",
(await p.locator("#desk-prompt").inputValue()) ===
"这是当前输入,不随历史变化",
);
await p.reload();
await p.locator("#desk-prompt").waitFor();
ok(
"刷新恢复切换后的工作流及草稿",
(await p
.locator(".model-selector")
.innerText()
.then((s) => s.includes("文生图"))) &&
(await p.locator("#desk-prompt").inputValue()) ===
"这是当前输入,不随历史变化",
);
await p.locator(".rail-tools input[type=search]").fill("Qwen");
await p.locator(".workflow-pick").first().click();
await p
.getByRole("button", { name: "创作设置⌄", exact: true })
.first()
.click();
ok(
"切回工作流恢复独立参数",
(await p
.locator(".modal-panel input[type=number]")
.first()
.inputValue()) === "101",
);
await p.keyboard.press("Escape");
await p.getByRole("tab", { name: "视频", exact: true }).click();
await p.locator("#desk-prompt").waitFor();
await p.locator(".rail-tools input[type=search]").fill("H3");
await p.locator(".workflow-pick").first().click();
await p.locator("#desk-prompt").fill("视频独立草稿");
await p.getByRole("button", { name: "管理素材", exact: true }).click();
const first = p
.locator(".media-editor .a-slot")
.filter({ has: p.getByText("首帧", { exact: true }) }),
last = p
.locator(".media-editor .a-slot")
.filter({ has: p.getByText("尾帧", { exact: true }) });
await first
.locator("input[type=file]")
.setInputFiles("calliope-web/static/refract/coast.jpg");
await first.locator("img").waitFor();
await last
.locator("input[type=file]")
.setInputFiles("calliope-web/static/refract/arch.jpg");
await last.locator("img").waitFor();
await p.getByRole("button", { name: "完成", exact: true }).click();
await p.getByRole("tab", { name: "图片", exact: true }).click();
await p.locator("#desk-negative").waitFor();
ok(
"图视频切换恢复图片草稿",
(await p.locator("#desk-prompt").inputValue()) ===
"这是当前输入,不随历史变化",
);
await p.getByRole("tab", { name: "视频", exact: true }).click();
await p.locator("#desk-prompt").waitFor();
ok(
"图视频切换恢复视频工作流和上传素材",
(await p.locator("#desk-prompt").inputValue()) === "视频独立草稿" &&
(await p.locator(".media-summary img").count()) === 2,
);
const v = await submit(p, f.h3, true);
const player = p.locator(".print video");
await player.waitFor();
await wait(() => player.evaluate((e) => e.readyState >= 1));
ok(
"视频结果默认暂停",
await player.evaluate((e) => e.paused && !e.autoplay),
);
await p.locator("#desk-prompt").focus();
await p.keyboard.press("Space");
ok("提示词中的空格不播放视频", await player.evaluate((e) => e.paused));
await p.getByRole("button", { name: "播放此视频", exact: true }).click();
await wait(() => player.evaluate((e) => !e.paused));
await player.focus();
await p.keyboard.press("Space");
await wait(() => player.evaluate((e) => e.paused));
ok("中央大播放键与聚焦空格可控", true);
for (const [width, height] of [
[1440, 900],
[1366, 768],
[1280, 720],
]) {
await p.setViewportSize({ width, height });
await p.waitForTimeout(150);
ok(
width + " 视频完整且主按钮首屏",
(await p.locator(".gen").evaluate((e) => {
const r = e.getBoundingClientRect();
return r.top >= 0 && r.bottom <= innerHeight && r.right <= innerWidth;
})) &&
(await player.evaluate(
(e) => getComputedStyle(e).objectFit === "contain",
)),
);
await shot(p, width + "-video");
}
// Cross-medium reuse from the complete history switches the editor kind explicitly.
await p.getByRole("button", { name: "全部记录", exact: true }).click();
await p
.locator(".history-full .a-seg")
.getByRole("button", { name: "全部", exact: true })
.click();
await p
.locator('.history-full [data-job="' + j.id + '"]')
.getByRole("button", { name: "复用", exact: true })
.click();
await p
.getByRole("tab", { name: "图片", exact: true, selected: true })
.waitFor();
await p.locator("#desk-negative").waitFor();
ok(
"跨媒介历史复用切到所属类型并回填",
(await p.locator("#desk-prompt").inputValue()) ===
"一位女孩站在海岸,清晨柔光。",
);
await p.getByRole("tab", { name: "视频", exact: true }).click();
await p.locator(".print video").waitFor();
// Theme switches preserve the same editor node and state.
await p.locator("#desk-prompt").evaluate((e) => (window.__editor = e));
for (const [id, name] of [
["night", "夜片场"],
["day", "日光棚"],
["grade", "调色间"],
["bluehour", "蓝调时刻"],
]) {
await p.getByRole("button", { name: "主题", exact: true }).click();
await p.locator(".choice").filter({ hasText: name }).click();
await p.keyboard.press("Escape");
ok(
id + " 切换保持编辑和素材",
(await p
.locator("#desk-prompt")
.evaluate((e) => e === window.__editor)) &&
(await p.locator(".media-summary img").count()) === 2,
);
await shot(p, "1280-theme-" + id);
}
// Import minimal API fixtures with the same parser roles used by user workflows.
const base = await api("/api/workflows/" + f.dual);
const maskBody = {
...base.workflow_json,
5: {
class_type: "LoadImage",
inputs: { image: "" },
_meta: { title: "(Input:image) 输入图" },
},
6: {
class_type: "LoadImageMask",
inputs: { image: "", channel: "red" },
_meta: { title: "(Input:mask) 遮罩" },
},
};
const maskWf = await api("/api/workflows", {
name: "遮罩图生图测试",
kind: "image",
workflow_json: maskBody,
});
await p.goto(WEB + "/create?wf=" + maskWf.id);
await p.locator("#desk-prompt").waitFor();
await p.getByRole("button", { name: "管理素材", exact: true }).click();
const imageSlots = p.locator(".media-editor .a-slot");
ok("图生图与遮罩独立槽位", (await imageSlots.count()) === 2);
for (let i = 0; i < 2; i++) {
await imageSlots
.nth(i)
.locator("input[type=file]")
.setInputFiles("calliope-web/static/refract/coast.jpg");
await imageSlots.nth(i).locator("img").waitFor();
}
await p.getByRole("button", { name: "完成", exact: true }).click();
const masked = await submit(p, maskWf.id);
ok(
"上传和遮罩经真实队列生成",
!!masked.payload.input_values["5"] && !!masked.payload.input_values["6"],
);
const refBody = {
1: {
class_type: "CLIPTextEncode",
inputs: { text: "多参考视频" },
_meta: { title: "提示词" },
},
5: { class_type: "LoadImage", inputs: { image: "" } },
7: {
class_type: "MiniMaxH3ReferenceToVideo",
inputs: { "ref_images.ref_image_0": ["5", 0] },
},
9: {
class_type: "VHS_VideoCombine",
inputs: { images: "x" },
_meta: { title: "(Output:video) 视频输出" },
},
};
const refWf = await api("/api/workflows", {
name: "H3多参考测试",
kind: "video",
workflow_json: refBody,
});
await p.goto(WEB + "/create?kind=video&wf=" + refWf.id);
await p.locator("#desk-prompt").waitFor();
await p.getByRole("button", { name: "管理素材", exact: true }).click();
await p
.locator(".media-editor input[type=file]")
.first()
.setInputFiles([
"calliope-web/static/refract/coast.jpg",
"calliope-web/static/refract/arch.jpg",
]);
await wait(
async () => (await p.locator(".media-editor img").count()) === 2,
);
await p.getByRole("button", { name: "完成", exact: true }).click();
const refs = await submit(p, refWf.id, true);
ok(
"多参考素材真实上传并生成",
Array.isArray(refs.payload.input_values["7@ref_images"]) &&
refs.payload.input_values["7@ref_images"].length === 2,
);
await shot(p, "1280-multi-reference");
// Preserve failure, retry and cancellation paths.
await p.goto(WEB + "/create?wf=" + f.dual);
await p.locator("#desk-prompt").waitFor();
mode(false, { mode: "fail" });
await p.locator(".gen").click();
const failed = await wait(async () =>
(await api("/api/playground/jobs")).find(
(j) => j.workflow_id === f.dual && j.status === "failed",
),
);
await p.getByRole("button", { name: "重试", exact: true }).waitFor();
mode();
await p.getByRole("button", { name: "重试", exact: true }).click();
await wait(async () =>
(await api("/api/playground/jobs")).some(
(j) => j.id === failed.id && j.status === "done",
),
);
ok("失败后重试取得真实结果", true);
mode(false, { delay_sec: 10 });
await p.locator(".gen").click();
await p.getByRole("button", { name: "取消生成", exact: true }).waitFor();
await p.getByRole("button", { name: "取消生成", exact: true }).click();
await wait(async () =>
(await api("/api/playground/jobs")).some(
(j) =>
j.workflow_id === f.dual &&
j.status === "failed" &&
j.error === "cancelled",
),
);
ok("进行中生成可以取消", true);
mode();
// Pending rows are hidden by default and point to the actual mapping/testing row.
const w = await api("/api/workflows/" + f.dual);
const pending = await api("/api/workflows", {
name: "待配置测试工具",
kind: "image",
workflow_json: w.workflow_json,
});
await api("/api/workflows/" + pending.id, { is_enabled: false }, "PATCH");
await p.goto(WEB + "/create");
await p.locator("#desk-prompt").waitFor();
await p.locator(".rail-tools input[type=search]").fill("待配置测试");
ok("默认仅显示可运行", (await p.locator(".workflow-pick").count()) === 0);
await p.getByRole("checkbox", { name: "仅可运行" }).uncheck();
await p
.locator(".workflow-pick")
.filter({ hasText: "待配置测试工具" })
.click();
await p.waitForURL("**/workflows?manage=1&wf=" + pending.id);
// B2 replaced the old wf-test-btn class with the same real test action.
// Wait for its data-driven detail panel; keep the original behavior assertion.
const workflowTest = p.getByRole("button", { name: "试运行", exact: true });
await workflowTest.waitFor();
ok("待配置导向真实管理和测试入口", await workflowTest.isVisible());
// Source project round-trip uses original API and explicit attachment.
await api(
"/api/workbench/projects/" + f.project + "/stages/assets",
{
content: {
assets: [
{
id: "b1-source",
kind: "character",
name: "海边女孩",
prompt: "源项目提示词",
description: "",
},
],
},
},
"PATCH",
);
const q = new URLSearchParams({
kind: "image",
wf: String(f.dual),
from: "workbench",
project: String(f.project),
stage: "assets",
source_kind: "asset",
source_id: "b1-source",
label: "海边女孩",
prompt: "源项目提示词",
});
await p.goto(WEB + "/create?" + q);
await p.locator("#desk-prompt").waitFor();
const flow = await submit(p, f.dual);
ok(
"来源随真实生成请求发送",
flow.payload?.workbench_context?.source_id === "b1-source" ||
JSON.stringify(flow.payload).includes("b1-source"),
);
await p.getByRole("button", { name: "存入项目", exact: true }).click();
await wait(async () =>
JSON.stringify(
await api("/api/workbench/projects/" + f.project),
).includes(flow.output_paths[0]),
);
ok("结果显式回流来源项目", true);
// Create a deterministic portrait fixture locally (no external image request).
const fixture = await c.newPage();
await fixture.setViewportSize({ width: 480, height: 720 });
await fixture.setContent(
'<body style="margin:0;background:linear-gradient(160deg,#213b42,#d9a86c);height:100vh;display:grid;place-items:center"><div style="width:220px;height:360px;background:#192a32;border-radius:50% 50% 10% 10%;box-shadow:0 0 80px #ecbd7b"></div></body>',
);
await fixture.screenshot({ path: out + "/portrait.png" });
await fixture.close();
ok("竖图测试素材为480×720", await fixtureDimensions(out + "/portrait.png"));
// Portrait & landscape outputs in the real application.
for (const file of ["coast.jpg", "portrait.png"]) {
const img = path.resolve(
file === "portrait.png"
? out + "/portrait.png"
: "calliope-web/static/refract/" + file,
);
mode(false, { image_file: img });
const before = Math.max(
...(await api("/api/playground/jobs")).map((j) => j.id),
);
await p.locator(".gen").click();
const image = await wait(async () =>
(await api("/api/playground/jobs")).find(
(j) => j.id > before && j.status === "done",
),
);
await p
.locator(".result-title")
.filter({ hasText: "第 " + image.id + " 次生成" })
.waitFor();
await p.locator(".print img").waitFor();
for (const [width, height] of [
[1440, 900],
[1366, 768],
[1280, 720],
]) {
await p.setViewportSize({ width, height });
await p.waitForTimeout(120);
ok(
width + " " + file + " 媒体比例正确",
await p.locator(".print img").evaluate((e) => {
const r = e.getBoundingClientRect();
return (
Math.abs(r.width / r.height - e.naturalWidth / e.naturalHeight) <
0.02 &&
r.bottom <= innerHeight &&
r.top >= 0
);
}),
);
await shot(p, width + "-" + file);
}
}
await p.goto(
WEB + "/playground?kind=video&wf=" + f.h3 + "&from=test#legacy",
);
await p.locator("#desk-prompt").waitFor();
ok(
"试验台兼容入口保留参数及片段",
new URL(p.url()).pathname === "/create" &&
new URL(p.url()).searchParams.get("from") === "test" &&
new URL(p.url()).hash === "#legacy",
);
const blocked = await browser.newContext({
viewport: { width: 1280, height: 720 },
});
await blocked.addInitScript(() => {
Storage.prototype.getItem = function () {
throw Error("denied");
};
Storage.prototype.setItem = function () {
throw Error("denied");
};
});
const bp = await blocked.newPage();
await bp.goto(WEB + "/create?wf=" + f.dual);
await bp.locator("#desk-prompt").fill("存储禁止也保留草稿");
await bp.getByRole("tab", { name: "视频", exact: true }).click();
await bp.locator("#desk-prompt").fill("视频草稿");
await bp.getByRole("tab", { name: "图片", exact: true }).click();
await bp.locator("#desk-negative").waitFor();
ok(
"浏览器存储被禁止时仍可切换并恢复草稿",
(await bp.locator("#desk-prompt").inputValue()) === "存储禁止也保留草稿",
);
await blocked.close();
ok("没有浏览器运行异常", errors.length === 0);
fs.writeFileSync(
out + "/results.json",
JSON.stringify({ checks, errors }, null, 2),
);
} catch (e) {
if (p) await shot(p, "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 browser.close();
}
})().catch((e) => {
console.error(e);
process.exitCode = 1;
});