Files
persionalBrand/apps/taro/test/browser/admin-editor.spec.ts
T

635 lines
25 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 adminOffering = {
id: "menu-fixture",
kind: "service",
name: "家庭聚餐定制",
summary: "为同学聚餐、家庭聚会提供常见的私房菜定制。",
description: "餐品详情",
priceText: "88.88",
coverUrl: brand.avatarUrl,
isEnabled: true,
sortOrder: 0,
config: { categoryLabel: "业务提供项" },
};
const adminTrust = {
id: "schedule-fixture",
type: "schedule",
title: "出摊安排",
summary: "工作日安排",
content: "周末安排",
sortOrder: 0,
isPublished: true,
config: {
weekdayLabel: "周一至周五",
weekdayDetail: "东门附近",
weekendLabel: "周六至周日",
weekendDetail: "周末晚间出摊",
},
};
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: adminOffering.id,
name: adminOffering.name,
summary: adminOffering.summary,
priceText: adminOffering.priceText,
coverUrl: adminOffering.coverUrl,
presentation: {
categoryLabel: "业务提供项",
keywords: [],
details: { background: "餐品详情", designConcept: "", crafts: [], materialsList: [] },
},
},
],
inquiryForms: [
{
key: "brand-inquiry",
version: "1",
purpose: "inquiry",
target: "brand",
submissionAction: "create-inquiry",
fields: [
{ key: "name", label: "称呼", type: "text", required: true },
{ key: "message", label: "需求说明", type: "textarea", required: true },
],
},
],
trustItems: [{ ...adminTrust }],
trustPresentation: {},
capabilities: [],
theme: { colors: {} },
};
const templateContent = {
displayName: "餐饮模板",
navigation: [
{ key: "home", page: "home", label: "首页" },
{ key: "offerings", page: "offerings", label: "套餐餐品" },
{ key: "service", page: "service", label: "出摊服务" },
{ key: "detail", page: "detail", label: "详情页" },
{ key: "inquiry", page: "inquiry", label: "定制/咨询" },
{ key: "success", page: "success", label: "提交成功页" },
{ key: "records", page: "records", label: "记录页" },
],
assetSlots: [],
trustContentTypes: [
{
type: "schedule",
label: "每周出摊安排",
adminFields: { title: "安排标题", summary: "安排摘要", content: "安排正文" },
hideStandardFields: ["summary", "content"],
pages: ["service"],
configFields: [
{
key: "weekdayLabel",
binding: "trust.config.weekdayLabel",
label: "工作日标题",
input: "text",
},
{
key: "weekdayDetail",
binding: "trust.config.weekdayDetail",
label: "工作日地点",
input: "text",
},
{
key: "weekendLabel",
binding: "trust.config.weekendLabel",
label: "周末标题",
input: "text",
},
{
key: "weekendDetail",
binding: "trust.config.weekendDetail",
label: "周末地点",
input: "text",
},
],
},
],
admin: {
sections: [
{
key: "detailCopy",
title: "餐品说明",
description: "编辑详情页说明",
page: "detail",
target: "brand",
previewTargets: [{ key: "acheng-campus-food.detail.detailCopy", label: "餐品说明" }],
fields: [
{
key: "priceLabel",
binding: "brand.config.visualPresentation.detail.priceLabel",
label: "价格标题",
input: "text",
previewTarget: "acheng-campus-food.detail.detailCopy",
},
],
},
{
key: "offering-card",
title: "餐品卡片",
description: "编辑单条餐品",
page: "offerings",
target: "offering",
previewTargets: [{ key: "acheng-campus-food.offerings.item", label: "餐品卡片" }],
fields: [
{
key: "name",
binding: "offering.name",
label: "餐品名称",
input: "text",
previewTarget: "acheng-campus-food.offerings.item",
},
{
key: "summary",
binding: "offering.summary",
label: "餐品简介",
input: "textarea",
previewTarget: "acheng-campus-food.offerings.item",
},
],
},
{
key: "home",
title: "首页内容",
description: "首页可编辑项",
page: "home",
target: "brand",
previewTargets: [
{
key: "food.home.brand-card",
label: "品牌信息区",
composite: true,
children: ["food.home.identity", "food.home.location", "food.home.actions"],
},
{ key: "food.home.identity", label: "主理人身份" },
{ key: "food.home.location", label: "服务地点" },
{ key: "food.home.offerings-heading", label: "餐品区域标题" },
{ key: "food.home.actions", label: "首页操作按钮" },
],
fields: [
{
key: "identity",
binding: "brand.config.founder",
previewTarget: "food.home.identity",
label: "主理人姓名",
input: "text",
},
{
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",
},
{
key: "primaryAction",
binding: "brand.config.visualPresentation.home.primaryAction",
previewTarget: "food.home.actions",
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/offerings")
return route.fulfill({ json: [adminOffering] });
if (url.pathname === "/admin/workspace/forms")
return route.fulfill({
json: [
{
id: "brand-inquiry",
name: "品牌咨询",
version: 1,
isEnabled: true,
purpose: "inquiry",
targetType: "brand",
submissionAction: "create-inquiry",
fieldsSnapshot: runtimeWorkspace.inquiryForms[0].fields,
},
],
});
if (
url.pathname === `/admin/workspace/offerings/${adminOffering.id}` &&
request.method() === "PATCH"
) {
Object.assign(adminOffering, request.postDataJSON());
Object.assign(runtimeWorkspace.offerings[0], {
name: adminOffering.name,
summary: adminOffering.summary,
});
return route.fulfill({ json: adminOffering });
}
if (url.pathname === "/admin/config/trust-content")
return route.fulfill({ json: [adminTrust] });
if (
url.pathname === `/admin/config/trust-content/${adminTrust.id}` &&
request.method() === "PATCH"
) {
Object.assign(adminTrust, request.postDataJSON());
Object.assign(runtimeWorkspace.trustItems[0], {
title: adminTrust.title,
config: adminTrust.config,
});
return route.fulfill({ json: adminTrust });
}
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("测试主理人");
await page.getByRole("button", { name: "选择品牌信息区" }).click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-target",
"food.home.brand-card",
);
await expect(page.getByRole("button", { name: "编辑主理人身份" })).toBeVisible();
await page.getByRole("button", { name: "编辑服务地点" }).click();
await expect(page.getByLabel("出摊信息标题")).toBeVisible();
await page.getByRole("button", { name: "选择品牌信息区" }).click();
await page.getByRole("button", { name: "编辑主理人身份" }).click();
await expect(page.getByLabel("主理人姓名")).toBeVisible();
await page.keyboard.press("Escape");
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 page.getByRole("button", { name: "选择品牌信息区" }).click();
await page.getByRole("button", { name: "编辑首页操作按钮" }).click();
await expect(page.getByLabel("咨询按钮")).toBeVisible();
await page.keyboard.press("Escape");
await page.getByRole("button", { name: "选择品牌信息区" }).click();
await page.getByRole("button", { name: "编辑服务地点" }).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(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-target",
"food.home.location",
);
await page.getByRole("button", { name: "浏览页面" }).click();
await preview.locator(".food-home .actions button").nth(1).click();
await expect(page.getByText("招牌餐品标题")).toHaveCount(0);
await page.getByRole("button", { name: "点选编辑" }).click();
await page.getByRole("button", { name: "选择品牌信息区" }).click();
await page.getByRole("button", { name: "编辑服务地点" }).click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-target",
"food.home.location",
);
await page.keyboard.press("Escape");
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveCount(0);
await page.getByText("套餐餐品", { exact: true }).click();
await expect(preview.locator(".offering-card")).toHaveCount(1);
await preview.locator('[data-editor-target="acheng-campus-food.offerings.item"]').click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-record-id",
adminOffering.id,
);
await page.getByLabel("餐品名称").fill("编辑后的家庭餐");
await expect(preview.locator(".offering-card")).toContainText("编辑后的家庭餐");
await page.getByRole("button", { name: "保存此项内容" }).click();
await expect.poll(() => adminOffering.name).toBe("编辑后的家庭餐");
await page.reload();
await page.getByText("套餐餐品", { exact: true }).click();
await expect(preview.locator(".offering-card")).toContainText("编辑后的家庭餐");
await page.getByText("出摊服务", { exact: true }).click();
await preview.locator('[data-editor-target="trust:schedule"]').click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute(
"data-selected-record-id",
adminTrust.id,
);
await page.getByLabel("工作日标题").fill("周一到周五营业");
await expect(preview.locator(".schedule-card")).toContainText("周一到周五营业");
await page.getByRole("button", { name: "保存此项内容" }).click();
await expect.poll(() => adminTrust.config.weekdayLabel).toBe("周一到周五营业");
await page.reload();
await page.getByText("出摊服务", { exact: true }).click();
await expect(preview.locator(".schedule-card")).toContainText("周一到周五营业");
await page.getByText("详情页", { exact: true }).click();
await expect(page.locator(".component-template-preview")).toHaveCount(1);
await expect(preview.locator(".component-template-preview__canvas")).toHaveCount(1);
await expect(preview.locator(".food-detail")).toHaveCount(1);
const detailBounds = await preview.evaluate((element) => {
const canvas = element.querySelector(".component-template-preview__canvas")!;
const bar = element.querySelector(".food-detail .bar")!;
const canvasBox = canvas.getBoundingClientRect();
const barBox = bar.getBoundingClientRect();
return {
position: getComputedStyle(bar).position,
left: barBox.left - canvasBox.left,
right: canvasBox.right - barBox.right,
bottom: canvasBox.bottom - barBox.bottom,
};
});
expect(detailBounds.position).toBe("absolute");
expect(detailBounds.left).toBeGreaterThanOrEqual(-1);
expect(detailBounds.right).toBeGreaterThanOrEqual(-1);
expect(detailBounds.bottom).toBeGreaterThanOrEqual(-1);
await preview.locator(".food-detail .bar").scrollIntoViewIfNeeded();
await expect(preview.locator(".food-detail .bar button")).toBeVisible();
await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/detail.png" });
await expect(page.getByText("详情页由所选业务内容动态生成")).toBeVisible();
await expect(page.getByLabel("价格标题")).toHaveCount(0);
await expect(page.getByLabel("餐品介绍")).toHaveCount(0);
await page.getByText("定制/咨询", { exact: true }).click();
await expect(preview.locator(".inquiry-form .form-field")).toHaveCount(2);
await expect(preview.locator(".inquiry-form")).toContainText("称呼");
await expect(preview.locator(".inquiry-form")).toContainText("需求说明");
await preview.locator('[data-editor-target="inquiry-field:name"]').click();
await expect(page.getByLabel("字段标题")).toHaveValue("称呼");
await preview.locator(".inquiry-form").scrollIntoViewIfNeeded();
await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/inquiry.png" });
const formOverflow = await preview
.locator(".inquiry-form")
.evaluate((form) => form.scrollWidth - form.clientWidth);
expect(formOverflow).toBeLessThanOrEqual(1);
await page.getByRole("button", { name: "浏览页面" }).click();
await preview.locator(".inquiry-form input").first().fill("预览访客");
await expect(preview.locator(".inquiry-form input").first()).toHaveValue("预览访客");
await page.getByText("提交成功页", { exact: true }).click();
await expect(preview.locator(".success-page")).toBeVisible();
await expect(preview.locator(".success-page")).toContainText("已收到你的需求");
await page.getByText("记录页", { exact: true }).click();
await expect(preview.locator(".records-page")).toBeVisible();
await expect(preview.locator(".records-page")).toContainText("请在微信小程序内登录查看");
await page.getByRole("button", { name: "点选编辑" }).click();
await page.getByText("套餐餐品", { exact: true }).click();
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",
});
}
});
test("林境模板支持首页和主理人区域点选", async ({ page }) => {
await page.setViewportSize({ width: 1600, height: 900 });
const editorialBrand = {
...structuredClone(brand),
templateKey: "personal",
config: {
visualTemplateKey: "linjing-editorial",
founder: "林境主理人",
title: "建筑设计师",
heroImage: "/assets/tab/home.png",
homeHero: { title: "林境作品" },
},
};
const editorialWorkspace = {
...structuredClone(runtimeWorkspace),
brand: {
...structuredClone(runtimeWorkspace.brand),
...editorialBrand,
founder: "林境主理人",
title: "建筑设计师",
heroImage: "/assets/tab/home.png",
homeHero: { title: "林境作品" },
},
offerings: [],
trustItems: [],
};
const editorialContent = {
displayName: "林境模板",
navigation: [
{ key: "home", page: "home", label: "首页" },
{ key: "profile", page: "profile", label: "主理人与我" },
],
assetSlots: [],
trustContentTypes: [],
admin: {
sections: [
{
key: "home",
title: "首页主视觉",
page: "home",
target: "brand",
previewTargets: [{ key: "editorial.home.hero", label: "首页主视觉" }],
fields: [
{
key: "heroTitle",
binding: "brand.config.homeHero.title",
label: "首页标题",
input: "text",
previewTarget: "editorial.home.hero",
},
],
},
{
key: "identity",
title: "主理人与联系方式",
page: "profile",
target: "brand",
previewTargets: [
{ key: "linjing-editorial.profile.identity", label: "主理人与联系方式" },
],
fields: [
{
key: "founder",
binding: "brand.config.founder",
label: "主理人姓名",
input: "text",
previewTarget: "linjing-editorial.profile.identity",
},
],
},
],
},
};
await page.route("**/api/**", (route) => {
const url = new URL(route.request().url());
const path = url.pathname.replace(/^\/api/, "");
if (path === "/admin/session/login")
return route.fulfill({
json: { token: "fixture-token", displayName: "测试管理员", role: "owner" },
});
if (path === "/admin/brands")
return route.fulfill({
json: [{ id: editorialBrand.id, role: "owner", brand: editorialBrand }],
});
if (path === "/admin/config/brand") return route.fulfill({ json: editorialBrand });
if (path === "/public/templates/personal")
return route.fulfill({
json: { name: "个人品牌", description: "林境模板", serviceCategories: [] },
});
if (path === "/admin/workspace")
return route.fulfill({
json: {
brand: editorialBrand,
installations: [],
fields: [],
offerings: [],
forms: [],
templateContent: editorialContent,
},
});
if (path === "/admin/workspace/preview" || path === "/public/workspace")
return route.fulfill({ json: editorialWorkspace });
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();
const preview = page.locator(".component-template-preview");
await expect(preview.locator(".editorial-home")).toBeVisible();
await expect(preview.locator(".hero-title")).toContainText("林境作品");
await expect
.poll(() =>
preview.locator(".hero-image").evaluate((image: HTMLImageElement) => image.naturalWidth),
)
.toBeGreaterThan(0);
await preview.locator('[data-editor-target="editorial.home.hero"]').click();
await expect(page.getByLabel("首页标题")).toBeVisible();
await page.getByText("主理人与我", { exact: true }).click();
await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveCount(0);
await preview.locator('[data-editor-target="linjing-editorial.profile.identity"]').click();
await expect(page.getByLabel("主理人姓名")).toBeVisible();
await page.getByLabel("主理人姓名").fill("修改后的主理人");
await expect(preview.locator(".editorial-profile .founder")).toContainText("修改后的主理人");
});