import { expect, test } from "@playwright/test"; function mockWecomConfigPayload() { return { enabled: true, entryEnabled: true, oauthEnabled: true, corpId: "wx-test-corp", agentId: "1000001", loginUrl: "/api/auth/wecom/login?returnTo=%2Fmobile", pcWebLoginEnabled: true, pcLoginUrl: "/api/auth/wecom/login?returnTo=%2Fportal&entrySource=pc_qr", bindEnabled: true, defaultReturnTo: "/mobile", status: "ready", statusReasons: [], }; } function envelope(data: unknown) { return { requestId: "mock-request", data, }; } async function mockAuthBase(page: import("@playwright/test").Page) { await page.route("**/api/auth/capabilities", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(envelope({ emailDelivery: { ready: true, statusReasons: [] }, emailLoginCode: { enabled: true }, passwordReset: { enabled: true }, })), }); }); await page.route("**/api/auth/sso/providers", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(envelope([])), }); }); } async function mockWecomConfig(page: import("@playwright/test").Page) { await page.route("**/api/auth/wecom/config", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(envelope(mockWecomConfigPayload())), }); }); } test("E2E 企微入口:普通浏览器显示托管扫码入口并保留安全回跳", async ({ page }) => { await mockAuthBase(page); await mockWecomConfig(page); let loginEntryUrl = ""; await page.route("**/api/auth/wecom/login?**", async (route) => { loginEntryUrl = route.request().url(); await route.fulfill({ status: 200, contentType: "text/plain", body: "mock pc wecom login entry" }); }); await page.goto("/login?redirect=%2Freimbursements"); await expect(page.getByTestId("login-password-primary-panel")).toBeVisible(); await expect(page.getByTestId("wecom-entry-pc")).toHaveText("企业微信扫码登录"); await page.getByTestId("wecom-entry-pc").click(); await expect.poll(() => loginEntryUrl).toContain("entrySource=pc_qr"); expect(new URL(loginEntryUrl).searchParams.get("returnTo")).toBe("/reimbursements"); expect(new URL(loginEntryUrl).searchParams.get("deviceFingerprint")).toBeTruthy(); await expect(page.getByTestId("wecom-entry-browser-notice")).toHaveCount(0); }); test("E2E 企微入口:企业微信场景优先显示企微 CTA,并展示映射缺失提示", async ({ page }) => { await page.addInitScript(() => { Object.defineProperty(window.navigator, "userAgent", { configurable: true, value: "Mozilla/5.0 wxwork/4.1.20 MicroMessenger/7.0.1", }); }); await mockAuthBase(page); await mockWecomConfig(page); await page.route("**/api/auth/wecom/login?**", async (route) => { await route.fulfill({ status: 200, contentType: "text/plain", body: "mock wecom login entry", }); }); await page.goto("/login?redirect=%2Fportal&wecom=account_mapping_required"); await expect(page.getByTestId("login-wecom-primary-panel")).toBeVisible(); await expect(page.getByTestId("wecom-entry-primary")).toBeVisible(); await expect(page.getByTestId("wecom-entry-pc")).toHaveCount(0); await expect(page.getByTestId("login-wecom-notice")).toContainText("企业微信账号映射尚未完成"); await expect(page.getByTestId("login-wecom-notice")).not.toContainText("账号密码登录"); await expect(page.getByTestId("login-password-secondary-panel")).toBeVisible(); });