219 lines
8.5 KiB
TypeScript
219 lines
8.5 KiB
TypeScript
import { test, expect } from "@playwright/test";
|
|
|
|
const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005";
|
|
const brand = {
|
|
id: "brand-fixture",
|
|
slug: "preview-fixture",
|
|
name: "预览品牌",
|
|
templateKey: "food",
|
|
tagline: "校园家常餐",
|
|
description: "认真做好每一餐",
|
|
avatarUrl: "/assets/owner/portrait.jpg",
|
|
coverUrl: "",
|
|
hours: "每日 17:00",
|
|
address: "东门附近",
|
|
deliveryNote: "提前预约",
|
|
contactPhone: "13800138000",
|
|
contactWechat: "food-example",
|
|
config: {
|
|
visualTemplateKey: "acheng-campus-food",
|
|
founder: "测试主理人",
|
|
title: "私房菜主理人",
|
|
heroImage: "",
|
|
visualPresentation: { home: { stallTitle: "原服务地点", worksTitle: "原餐品标题" } },
|
|
},
|
|
};
|
|
const runtimeWorkspace = {
|
|
schemaVersion: "1.0.0",
|
|
publication: { status: "published", version: 1 },
|
|
brand: {
|
|
...brand,
|
|
founder: "测试主理人",
|
|
title: "私房菜主理人",
|
|
visualPresentation: brand.config.visualPresentation,
|
|
visualTemplateKey: "acheng-campus-food",
|
|
},
|
|
templateData: {},
|
|
navigation: { tabs: [] },
|
|
offerings: [
|
|
{
|
|
id: "menu-fixture",
|
|
name: "家庭聚餐定制",
|
|
summary: "为同学聚餐、家庭聚会提供常见的私房菜定制。",
|
|
priceText: "88.88",
|
|
coverUrl: brand.avatarUrl,
|
|
presentation: { categoryLabel: "业务提供项", keywords: [], details: [] },
|
|
},
|
|
],
|
|
inquiryForms: [],
|
|
trustItems: [],
|
|
trustPresentation: {},
|
|
capabilities: [],
|
|
theme: { colors: {} },
|
|
};
|
|
const templateContent = {
|
|
displayName: "餐饮模板",
|
|
navigation: [
|
|
{ key: "home", page: "home", label: "首页" },
|
|
{ key: "offerings", page: "offerings", label: "套餐餐品" },
|
|
],
|
|
assetSlots: [],
|
|
trustContentTypes: [],
|
|
admin: {
|
|
sections: [
|
|
{
|
|
key: "home",
|
|
title: "首页内容",
|
|
description: "首页可编辑项",
|
|
page: "home",
|
|
target: "brand",
|
|
previewTargets: [
|
|
{ key: "food.home.location", label: "服务地点" },
|
|
{ key: "food.home.offerings-heading", label: "餐品区域标题" },
|
|
],
|
|
fields: [
|
|
{
|
|
key: "stallTitle",
|
|
binding: "brand.config.visualPresentation.home.stallTitle",
|
|
previewTarget: "food.home.location",
|
|
label: "出摊信息标题",
|
|
input: "text",
|
|
},
|
|
{
|
|
key: "stallTime",
|
|
binding: "brand.config.visualPresentation.home.stallTime",
|
|
previewTarget: "food.home.location",
|
|
label: "营业时间说明",
|
|
input: "text",
|
|
},
|
|
{
|
|
key: "worksTitle",
|
|
binding: "brand.config.visualPresentation.home.worksTitle",
|
|
previewTarget: "food.home.offerings-heading",
|
|
label: "招牌餐品标题",
|
|
input: "text",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ page }) => {
|
|
await page.setViewportSize({ width: 1920, height: 1000 });
|
|
await page.route("http://localhost:3001/public/workspace**", (route) =>
|
|
route.fulfill({ json: runtimeWorkspace }),
|
|
);
|
|
await page.route("**/api/**", async (route) => {
|
|
const request = route.request();
|
|
const url = new URL(request.url());
|
|
url.pathname = url.pathname.replace(/^\/api/, "");
|
|
if (url.pathname === "/admin/session/login")
|
|
return route.fulfill({
|
|
json: {
|
|
token: "fixture-token",
|
|
displayName: "测试管理员",
|
|
role: "owner",
|
|
mustChangePassword: false,
|
|
},
|
|
});
|
|
if (url.pathname === "/admin/brands")
|
|
return route.fulfill({ json: [{ id: brand.id, role: "owner", brand }] });
|
|
if (url.pathname === "/admin/config/brand" && request.method() === "PATCH") {
|
|
Object.assign(brand, request.postDataJSON());
|
|
Object.assign(runtimeWorkspace.brand, brand);
|
|
return route.fulfill({ json: brand });
|
|
}
|
|
if (url.pathname === "/admin/config/brand") return route.fulfill({ json: brand });
|
|
if (url.pathname === "/admin/workspace")
|
|
return route.fulfill({
|
|
json: { brand, installations: [], fields: [], offerings: [], forms: [], templateContent },
|
|
});
|
|
if (url.pathname === "/admin/workspace/preview")
|
|
return route.fulfill({ json: runtimeWorkspace });
|
|
if (url.pathname === "/public/templates/food")
|
|
return route.fulfill({
|
|
json: { name: "餐饮模板", description: "fixture", serviceCategories: [] },
|
|
});
|
|
if (url.pathname === "/public/workspace") return route.fulfill({ json: runtimeWorkspace });
|
|
return route.fulfill({ json: {} });
|
|
});
|
|
|
|
await page.goto(`${adminOrigin}/admin/brand`);
|
|
await page.getByLabel("账号").fill("admin");
|
|
await page.getByLabel("密码").fill("fixture-password");
|
|
await page.locator('button[type="submit"]').click();
|
|
await expect(page.getByText("小程序页面配置")).toBeVisible();
|
|
const preview = page.locator(".component-template-preview");
|
|
await expect(preview.locator(".food-home .name")).toContainText("测试主理人");
|
|
const dimensions = await preview.evaluate((element) => {
|
|
const canvas = element.querySelector(".component-template-preview__canvas")!;
|
|
const name = element.querySelector(".name")!;
|
|
const avatar = element.querySelector(".identity img")!;
|
|
return {
|
|
width: canvas.getBoundingClientRect().width,
|
|
font: parseFloat(getComputedStyle(name).fontSize),
|
|
avatar: avatar.getBoundingClientRect().width,
|
|
transform: getComputedStyle(canvas.firstElementChild!).transform,
|
|
};
|
|
});
|
|
expect(dimensions.font / dimensions.width).toBeCloseTo(32 / 750, 3);
|
|
expect(dimensions.avatar / dimensions.width).toBeCloseTo(128 / 750, 3);
|
|
expect(dimensions.transform).toBe("none");
|
|
await expect
|
|
.poll(() =>
|
|
preview.locator(".identity img").evaluate((image: HTMLImageElement) => image.naturalWidth),
|
|
)
|
|
.toBeGreaterThan(0);
|
|
await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/home.png" });
|
|
const actionButtons = preview.locator(".food-home .actions button");
|
|
await expect(actionButtons).toHaveCount(2);
|
|
const previewBox = await preview.locator(".component-template-preview__canvas").boundingBox();
|
|
const firstActionBox = await actionButtons.nth(0).boundingBox();
|
|
const secondActionBox = await actionButtons.nth(1).boundingBox();
|
|
expect(previewBox && firstActionBox && secondActionBox).toBeTruthy();
|
|
expect(firstActionBox!.x).toBeGreaterThanOrEqual(previewBox!.x);
|
|
expect(secondActionBox!.x + secondActionBox!.width).toBeLessThanOrEqual(
|
|
previewBox!.x + previewBox!.width,
|
|
);
|
|
await expect(actionButtons.nth(0)).toContainText("咨询 / 预订");
|
|
await expect(actionButtons.nth(1)).toContainText("查看餐品");
|
|
await preview.locator('[data-editor-target="food.home.location"]').click();
|
|
await expect(page.getByText("出摊信息标题")).toBeVisible();
|
|
await expect(page.getByText("招牌餐品标题")).toHaveCount(0);
|
|
await page.getByLabel("出摊信息标题").fill("后台草稿地点");
|
|
await expect(preview.locator(".notice-title")).toContainText("后台草稿地点");
|
|
await page.getByRole("button", { name: "保存页面内容" }).click();
|
|
await expect(page.getByText("页面内容已保存为草稿,发布后小程序才会更新")).toBeVisible();
|
|
await expect(preview.locator('[data-editor-target="food.home.location"]')).toHaveClass(
|
|
/editor-target-selected/,
|
|
);
|
|
await page.getByText("套餐餐品", { exact: true }).click();
|
|
await expect(preview.locator(".offering-card")).toHaveCount(1);
|
|
await expect
|
|
.poll(() =>
|
|
preview.locator(".dish-media img").evaluate((image: HTMLImageElement) => image.naturalWidth),
|
|
)
|
|
.toBeGreaterThan(0);
|
|
for (const width of [390, 320]) {
|
|
await preview.locator(".component-template-preview__canvas").evaluate((element, size) => {
|
|
(element as HTMLElement).style.width = size + "px";
|
|
}, width);
|
|
const layout = await preview.evaluate((element) => {
|
|
const canvas = element.querySelector(".component-template-preview__canvas")!;
|
|
const card = element.querySelector(".offering-card")!;
|
|
const media = element.querySelector(".dish-media")!;
|
|
return {
|
|
canvas: canvas.getBoundingClientRect().width,
|
|
overflow: card.scrollWidth - card.clientWidth,
|
|
media: media.getBoundingClientRect().width,
|
|
};
|
|
});
|
|
expect(layout.overflow).toBeLessThanOrEqual(1);
|
|
expect(layout.media / layout.canvas).toBeCloseTo(224 / 750, 3);
|
|
await preview.screenshot({
|
|
path: "../../.codex-tasks/preview-proportion/menu-" + width + ".png",
|
|
});
|
|
}
|
|
});
|