From ab17f80607158f0a3260b8c5cb2b9682eaf2c132 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 21 Oct 2026 10:22:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=93=81=E7=89=8C?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=B8=8E=E9=A1=B5=E9=9D=A2=E6=96=B9=E6=A1=88?= =?UTF-8?q?=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/brands/CreateBrandPage.tsx | 6 +- .../features/page-scheme/PageSchemePage.tsx | 1 + .../brand-creation-and-page-scheme.spec.ts | 315 ++++++++++++++++++ 3 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 apps/taro/test/browser/brand-creation-and-page-scheme.spec.ts diff --git a/apps/admin/src/features/brands/CreateBrandPage.tsx b/apps/admin/src/features/brands/CreateBrandPage.tsx index 8ab6c88..8efc5c2 100644 --- a/apps/admin/src/features/brands/CreateBrandPage.tsx +++ b/apps/admin/src/features/brands/CreateBrandPage.tsx @@ -28,6 +28,7 @@ export function CreateBrandPage() { const navigate = useNavigate(); const [form] = Form.useForm(); const businessType = Form.useWatch("businessType", form); + const selectedSchemeId = Form.useWatch("templatePackageVersionId", form); const options = useQuery({ queryKey: ["admin-brand-creation-options"], queryFn: () => client.getAdminBrandCreationOptions(), @@ -190,7 +191,10 @@ export function CreateBrandPage() { htmlType="submit" size="large" loading={create.isPending} - disabled={!businessType || schemes.length === 0} + disabled={ + !businessType || + !schemes.some((scheme) => scheme.templatePackageVersionId === selectedSchemeId) + } > 创建并进入品牌 diff --git a/apps/admin/src/features/page-scheme/PageSchemePage.tsx b/apps/admin/src/features/page-scheme/PageSchemePage.tsx index 151091f..689cc83 100644 --- a/apps/admin/src/features/page-scheme/PageSchemePage.tsx +++ b/apps/admin/src/features/page-scheme/PageSchemePage.tsx @@ -43,6 +43,7 @@ export function PageSchemePage() { const refresh = () => { for (const queryKey of [ "page-scheme-workspace", + "admin-page-scheme-options", "page-scheme-upgrade-preview", "admin-workspace", "admin-preview-workspace", diff --git a/apps/taro/test/browser/brand-creation-and-page-scheme.spec.ts b/apps/taro/test/browser/brand-creation-and-page-scheme.spec.ts new file mode 100644 index 0000000..f8ccd5d --- /dev/null +++ b/apps/taro/test/browser/brand-creation-and-page-scheme.spec.ts @@ -0,0 +1,315 @@ +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-v132", + name: "独立创作者画册", + description: "个人品牌页面方案", + version: "1.3.2", + 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-v132", + }); + 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-v132", + }); +}); + +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(); +});