From 9389235a6c36ed94dc226a340caa0914b053be20 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 21 Oct 2026 10:47:30 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E8=A1=A5=E5=85=85=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=E6=B5=8F=E8=A7=88=E5=99=A8=E5=9B=9E=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/browser/publication-release.spec.ts | 310 ++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 apps/taro/test/browser/publication-release.spec.ts diff --git a/apps/taro/test/browser/publication-release.spec.ts b/apps/taro/test/browser/publication-release.spec.ts new file mode 100644 index 0000000..3d6a825 --- /dev/null +++ b/apps/taro/test/browser/publication-release.spec.ts @@ -0,0 +1,310 @@ +import { test, expect, type Page, type Route } from "@playwright/test"; + +const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005"; + +const pageErrors = new WeakMap(); + +test.beforeEach(({ page }) => { + const errors: string[] = []; + pageErrors.set(page, errors); + page.on("pageerror", (error) => errors.push(error.message)); +}); + +test.afterEach(({ page }) => { + expect(pageErrors.get(page) ?? []).toEqual([]); +}); + +interface ReleaseRow { + id: string; + version: number; + status: string; + note: string | null; + actorId: string; + createdAt: string; + revokedAt: string | null; +} + +interface MockOptions { + ready?: boolean; + onPublish?: (body: Record) => void; + onRollback?: (body: Record, version: number) => void; + onRevoke?: () => void; + publishStatus?: number; +} + +async function mockPublicationApi(page: Page, options: MockOptions = {}) { + const releases: ReleaseRow[] = [ + { + id: "release-2", + version: 2, + status: "published", + note: "第二版", + actorId: "actor-1", + createdAt: "2026-10-20T02:00:00.000Z", + revokedAt: null, + }, + { + id: "release-1", + version: 1, + status: "superseded", + note: null, + actorId: "actor-1", + createdAt: "2026-10-19T02:00:00.000Z", + revokedAt: null, + }, + ]; + + 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: { + id: "brand-current", + name: "独立工坊", + slug: "atelier-studio", + templateKey: "personal", + }, + }); + } + if (path === "/admin/brands" && method === "GET") { + return route.fulfill({ + json: [ + { + id: "membership-current", + role: "owner", + brand: { + id: "brand-current", + name: "独立工坊", + slug: "atelier-studio", + templateKey: "personal", + }, + }, + ], + }); + } + if (path === "/admin/workspace") { + return route.fulfill({ + json: { + brand: { id: "brand-current", name: "独立工坊" }, + installations: [], + templateContent: { displayName: "独立创作者画册" }, + offerings: [], + forms: [], + }, + }); + } + if (path === "/admin/releases/readiness") { + return route.fulfill({ + json: { + ready: options.ready ?? true, + checks: [ + { key: "brand", label: "品牌资料", passed: true }, + { + key: "offerings", + label: "业务提供项", + passed: options.ready ?? true, + help: "至少添加一个业务提供项", + }, + ], + }, + }); + } + if (path === "/admin/releases" && method === "GET") { + return route.fulfill({ json: releases }); + } + if (path === "/admin/releases/publish" && method === "POST") { + const body = JSON.parse(request.postData() ?? "{}") as Record; + options.onPublish?.(body); + if (options.publishStatus && options.publishStatus !== 200) { + return route.fulfill({ + status: options.publishStatus, + json: { + error: { code: "RELEASE_FAILED", message: "发布检查未通过" }, + requestId: "browser-release-error", + }, + }); + } + for (const item of releases) item.status = "superseded"; + const created: ReleaseRow = { + id: "release-3", + version: 3, + status: "published", + note: typeof body.note === "string" ? body.note : null, + actorId: "actor-1", + createdAt: "2026-10-21T02:00:00.000Z", + revokedAt: null, + }; + releases.unshift(created); + return route.fulfill({ json: created }); + } + if (path === "/admin/releases/revoke" && method === "POST") { + options.onRevoke?.(); + return route.fulfill({ json: { revoked: true } }); + } + const rollback = path.match(/^\/admin\/releases\/(\d+)\/rollback$/); + if (rollback && method === "POST") { + const body = JSON.parse(request.postData() ?? "{}") as Record; + options.onRollback?.(body, Number(rollback[1])); + for (const item of releases) item.status = "superseded"; + const created: ReleaseRow = { + id: "release-rollback", + version: 4, + status: "published", + note: null, + actorId: "actor-1", + createdAt: "2026-10-21T03:00:00.000Z", + revokedAt: null, + }; + releases.unshift(created); + return route.fulfill({ json: created }); + } + if (path === "/admin/miniapp-releases") return route.fulfill({ json: [] }); + if (path === "/admin/miniapp-releases/settings") { + return route.fulfill({ + json: { + appId: "wx0000000000000000", + apiBaseUrl: "https://wechat.app.que01.top/api", + hasStoredCredentials: false, + }, + }); + } + return route.fulfill({ json: {} }); + }); +} + +test("发布按钮按就绪状态启用,空备注发布不写入 note", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + const bodies: Record[] = []; + await mockPublicationApi(page, { onPublish: (body) => bodies.push(body) }); + + await page.goto(`${adminOrigin}/admin/publication`); + await expect(page.getByRole("heading", { name: "内容与发布" })).toBeVisible(); + await page.getByRole("button", { name: "发布当前草稿" }).click(); + await page.getByRole("button", { name: "确认发布" }).click(); + + await expect.poll(() => bodies).toEqual([{}]); + await expect(page.getByText("品牌已发布")).toBeVisible(); + await expect(page.getByRole("button", { name: "确认发布" })).toBeHidden(); + await expect(page.getByText("v3")).toBeVisible(); +}); + +test("发布备注会去除首尾空格并随请求提交", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + const bodies: Record[] = []; + await mockPublicationApi(page, { onPublish: (body) => bodies.push(body) }); + + await page.goto(`${adminOrigin}/admin/publication`); + await page.getByRole("button", { name: "发布当前草稿" }).click(); + await page.getByPlaceholder("发布备注(可选)").fill(" 国庆版本 "); + await page.getByRole("button", { name: "确认发布" }).click(); + + await expect.poll(() => bodies).toEqual([{ note: "国庆版本" }]); + await expect(page.getByText("国庆版本")).toBeVisible(); +}); + +test("发布失败时保留备注与确认弹窗", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + await mockPublicationApi(page, { publishStatus: 409 }); + + await page.goto(`${adminOrigin}/admin/publication`); + await page.getByRole("button", { name: "发布当前草稿" }).click(); + await page.getByPlaceholder("发布备注(可选)").fill("待重试的版本"); + await page.getByRole("button", { name: "确认发布" }).click(); + + await expect(page.getByText("发布检查未通过")).toBeVisible(); + await expect(page.getByRole("button", { name: "确认发布" })).toBeVisible(); + await expect(page.getByPlaceholder("发布备注(可选)")).toHaveValue("待重试的版本"); +}); + +test("未通过发布检查时禁用发布按钮", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + await mockPublicationApi(page, { ready: false }); + + await page.goto(`${adminOrigin}/admin/publication`); + await expect(page.getByRole("button", { name: "发布当前草稿" })).toBeDisabled(); + await expect(page.getByText("至少添加一个业务提供项")).toBeVisible(); +}); + +test("撤回当前版本并刷新发布记录", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + let revoked = 0; + await mockPublicationApi(page, { onRevoke: () => (revoked += 1) }); + + await page.goto(`${adminOrigin}/admin/publication`); + await page.getByRole("button", { name: "撤回当前版本" }).click(); + await page.getByRole("button", { name: "确认撤回" }).click(); + + await expect.poll(() => revoked).toBe(1); + await expect(page.getByText("品牌已撤回,小程序将显示未发布状态")).toBeVisible(); +}); + +test("恢复历史版本会提交版本号并刷新发布记录", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + const requests: Array<{ body: Record; version: number }> = []; + await mockPublicationApi(page, { + onRollback: (body, version) => requests.push({ body, version }), + }); + + await page.goto(`${adminOrigin}/admin/publication`); + await page.getByRole("button", { name: "恢复此版本" }).click(); + await page.getByRole("button", { name: "确 定" }).click(); + + await expect.poll(() => requests).toEqual([{ body: {}, version: 1 }]); + await expect(page.getByText("历史版本已重新发布")).toBeVisible(); +}); + +test("小程序配置页回填服务端设置并仅提交新私钥", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 1000 }); + const bodies: Record[] = []; + await mockPublicationApi(page, {}); + await page.route("**/api/admin/miniapp-releases/settings", async (route) => { + if (route.request().method() === "PATCH") { + const body = JSON.parse(route.request().postData() ?? "{}") as Record; + bodies.push(body); + return route.fulfill({ + json: { + appId: body.appId, + apiBaseUrl: body.apiBaseUrl, + hasStoredCredentials: true, + }, + }); + } + return route.fallback(); + }); + + await page.goto(`${adminOrigin}/admin/publication`); + await page.getByRole("tab", { name: "小程序配置" }).click(); + await expect(page.getByLabel("微信 AppID")).toHaveValue("wx0000000000000000"); + await expect(page.getByLabel("小程序请求地址")).toHaveValue("https://wechat.app.que01.top/api"); + + await page + .getByLabel("上传私钥") + .fill("-----BEGIN PRIVATE KEY-----\nKEY\n-----END PRIVATE KEY-----"); + await page.getByRole("button", { name: "保存上传配置" }).click(); + + await expect + .poll(() => bodies) + .toEqual([ + { + appId: "wx0000000000000000", + apiBaseUrl: "https://wechat.app.que01.top/api", + privateKey: "-----BEGIN PRIVATE KEY-----\nKEY\n-----END PRIVATE KEY-----", + }, + ]); + await expect(page.getByText("小程序上传配置已保存")).toBeVisible(); +});