import { test, expect, type Page, type Route } from "@playwright/test"; const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005"; const businesses = [ { key: "personal", name: "个人品牌", description: "适合独立创作者与个人工作室" }, { key: "doors", name: "门作品牌", description: "适合门作、木作与建筑空间业务" }, { key: "food", name: "餐饮品牌", description: "适合餐饮与本地生活业务" }, ]; const pageSchemes = [ { templatePackageVersionId: "atelier-v133", name: "独立创作者画册", description: "个人品牌页面方案", version: "1.3.3", supportedBusinessTypes: ["personal"], isLatestForPackage: true, }, { templatePackageVersionId: "linjing-v107", name: "林境建筑画册", description: "门作品牌页面方案", version: "1.0.7", supportedBusinessTypes: ["doors"], isLatestForPackage: false, }, { templatePackageVersionId: "linjing-v108", name: "林境建筑画册", description: "门作品牌页面方案", version: "1.0.8", supportedBusinessTypes: ["doors"], isLatestForPackage: true, }, { templatePackageVersionId: "food-v103", name: "阿成校园餐饮", description: "餐饮品牌页面方案", version: "1.0.3", supportedBusinessTypes: ["food"], isLatestForPackage: true, }, ]; const currentBrand = { id: "brand-current", name: "独立工坊", slug: "atelier-studio", templateKey: "personal", }; const memberships = [{ id: "membership-current", role: "owner", brand: currentBrand }]; interface ApiMockOptions { createStatus?: number; onCreate?: (body: Record) => void; onSelectVersion?: (body: Record) => void; onUpgrade?: (body: Record) => void; } async function mockAdminApi(page: Page, options: ApiMockOptions = {}) { let createStatus = options.createStatus ?? 200; let currentVersion = "1.0.7"; let currentPackageVersionId = "linjing-v107"; await page.route("**/api/**", async (route: Route) => { const request = route.request(); const url = new URL(request.url()); const path = url.pathname.replace(/^\/api/, ""); const method = request.method(); if (path === "/admin/session/current") { return route.fulfill({ json: { authenticated: true, displayName: "品牌负责人", role: "owner", mustChangePassword: false, platformTemplateRole: "none", }, }); } if (path === "/admin/config/brand") return route.fulfill({ json: currentBrand }); if (path === "/admin/brands" && method === "GET") return route.fulfill({ json: memberships }); if (path === "/admin/brands/creation-options") { return route.fulfill({ json: { businesses, pageSchemes } }); } if (path === "/admin/brands" && method === "POST") { const body = JSON.parse(request.postData() ?? "{}") as Record; options.onCreate?.(body); if (createStatus !== 200) { return route.fulfill({ status: createStatus, json: { error: { code: "BRAND_SLUG_TAKEN", message: "小程序地址标识已被使用" }, requestId: "browser-create-error", }, }); } return route.fulfill({ json: { role: "owner", brand: { id: "brand-created", name: body.name, slug: body.slug, templateKey: body.businessType, }, }, }); } if (path === "/admin/workspace/template/options") { return route.fulfill({ json: { business: businesses[1], pageSchemes: pageSchemes.filter((scheme) => scheme.supportedBusinessTypes.includes("doors"), ), }, }); } if (path === "/admin/workspace/template/preview") { return route.fulfill({ json: { templateKey: "doors", currentVersion: "1.0.7", latestVersion: "1.0.8", canUpgrade: true, canRollback: false, currentPackageVersionId: "linjing-v107", currentContentChecksum: "current-checksum", targetPackageVersionId: "linjing-v108", targetContentChecksum: "target-checksum", changes: { brandFields: { available: false, reason: "不适用" }, templateBindings: { added: [], changed: [], removed: [] }, pages: { added: [], changed: [], removed: [] }, modules: { available: true, source: "platform_visual_manifest", packageUpgradeChangesRenderer: false, pages: [], }, assets: { slots: { added: [], changed: [], removed: [] }, files: { available: false, reason: "不适用" }, }, capabilities: { added: [], removed: [] }, renderer: { available: true, source: "platform_visual_manifest", packageUpgradeChangesRenderer: false, pages: [], }, package: { currentContentChecksum: "current-checksum", targetContentChecksum: "target-checksum", runtimeContract: { current: null, target: null }, }, }, conflicts: [{ key: "hero.title", binding: "brand.name", reason: "自定义标题" }], }, }); } if (path === "/admin/workspace/template/select-version" && method === "POST") { options.onSelectVersion?.(JSON.parse(request.postData() ?? "{}") as Record); currentVersion = "1.0.8"; currentPackageVersionId = "linjing-v108"; return route.fulfill({ json: { selected: true } }); } if (path === "/admin/workspace/template/upgrade" && method === "POST") { options.onUpgrade?.(JSON.parse(request.postData() ?? "{}") as Record); currentVersion = "1.0.8"; currentPackageVersionId = "linjing-v108"; return route.fulfill({ json: { upgraded: true } }); } if (path === "/admin/workspace") { return route.fulfill({ json: { brand: currentBrand, installations: [ { templateVersion: currentVersion, templatePackageVersionId: currentPackageVersionId, templateKey: "doors", }, ], templateContent: { displayName: "林境建筑画册" }, offerings: [], forms: [], }, }); } if (path === "/admin/releases/readiness") { return route.fulfill({ json: { ready: true, checks: [] } }); } if ( path === "/admin/config/trust-content" || path === "/admin/workspace/offerings" || path === "/admin/workspace/submissions" ) { return route.fulfill({ json: [] }); } return route.fulfill({ json: {} }); }); return { failCreate: () => { createStatus = 400; }, }; } test("创建品牌成功后进入新品牌工作区", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); let createdBody: Record | undefined; await mockAdminApi(page, { onCreate: (body) => (createdBody = body) }); await page.goto(`${adminOrigin}/admin/brands/new`); await page.getByText("个人品牌", { exact: true }).click(); await page.getByText(/最新版 1\.3\.2/).click(); await page.getByLabel("品牌名称").fill("新的独立品牌"); await page.getByLabel("小程序地址标识").fill("new-atelier"); await page.getByRole("button", { name: "创建并进入品牌" }).click(); await expect .poll(() => createdBody) .toEqual({ name: "新的独立品牌", slug: "new-atelier", businessType: "personal", templatePackageVersionId: "atelier-v133", }); await page.waitForURL("**/admin/overview"); await expect(page.getByRole("heading", { name: "品牌概览" })).toBeVisible(); }); test("创建品牌按业务类型过滤页面方案,失败后保留输入", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); let createdBody: Record | undefined; const api = await mockAdminApi(page, { onCreate: (body) => (createdBody = body) }); await page.goto(`${adminOrigin}/admin/brands/new`); await expect(page.getByRole("heading", { name: "创建品牌" })).toBeVisible(); await expect(page.getByText("先选择业务类型")).toBeVisible(); await page.getByText("个人品牌", { exact: true }).click(); await expect(page.getByText("独立创作者画册")).toBeVisible(); await expect(page.getByText("林境建筑画册")).toBeHidden(); await page.getByText("门作品牌", { exact: true }).click(); await expect(page.getByText("林境建筑画册").first()).toBeVisible(); await expect(page.getByText("独立创作者画册")).toBeHidden(); await page.getByText(/最新版 1\.0\.8/).click(); await page.getByText("个人品牌", { exact: true }).click(); await expect(page.getByRole("button", { name: "创建并进入品牌" })).toBeDisabled(); await page.getByText(/最新版 1\.3\.2/).click(); await page.getByLabel("品牌名称").fill("测试独立品牌"); await page.getByLabel("小程序地址标识").fill("test-atelier"); api.failCreate(); await page.getByRole("button", { name: "创建并进入品牌" }).click(); await expect(page.locator(".admin-error-note")).toContainText("小程序地址标识已被使用"); await expect(page.getByLabel("品牌名称")).toHaveValue("测试独立品牌"); await expect(page.getByLabel("小程序地址标识")).toHaveValue("test-atelier"); expect(createdBody).toEqual({ name: "测试独立品牌", slug: "test-atelier", businessType: "personal", templatePackageVersionId: "atelier-v133", }); }); test("页面方案切换和升级提交正确契约,并刷新查询缓存", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); let selectBody: Record | undefined; let upgradeBody: Record | undefined; await mockAdminApi(page, { onSelectVersion: (body) => (selectBody = body), onUpgrade: (body) => (upgradeBody = body), }); await page.goto(`${adminOrigin}/admin/page-scheme`); await expect(page.getByRole("heading", { name: "页面方案" })).toBeVisible(); await expect(page.getByText("门作品牌", { exact: true })).toBeVisible(); await expect(page.getByText("正在使用版本 1.0.7")).toBeVisible(); await expect(page.getByText("独立创作者画册")).toBeHidden(); await expect(page.getByRole("button", { name: "恢复上一版配置" })).toBeDisabled(); await page.getByRole("button", { name: "切换到此版本" }).click(); await page.getByRole("button", { name: "确认切换" }).click(); await expect.poll(() => selectBody).toBeTruthy(); expect(selectBody).toEqual({ templatePackageVersionId: "linjing-v108", expectedCurrentPackageVersionId: "linjing-v107", confirm: true, }); await expect(page.getByText("页面方案版本已切换")).toBeVisible(); await expect(page.getByText("正在使用版本 1.0.8")).toBeVisible(); await page.getByRole("button", { name: "升级到 1.0.8" }).click(); await expect(page.getByText("将升级到版本 1.0.8,并全部保留 1 项现有内容。")).toBeVisible(); await page.getByRole("button", { name: "确认升级" }).click(); await expect.poll(() => upgradeBody).toBeTruthy(); expect(upgradeBody).toMatchObject({ templatePackageVersionId: "linjing-v108", expectedContentChecksum: "target-checksum", expectedCurrentVersion: "1.0.7", expectedCurrentPackageVersionId: "linjing-v107", confirm: true, preservedBindings: [{ key: "hero.title", binding: "brand.name", action: "preserve" }], }); await expect(page.getByText("页面方案已升级,现有内容已全部保留")).toBeVisible(); });