import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared"; import { TID_LOGIN_TRIGGER } from "@zcode/shared/test-ids"; import { clearAppData, DEFAULT_WORKSPACE, readModelProvider, seedCredentials, seedSettings, waitForWorkspaceApp, } from "#e2e/helpers/desktop-app"; import { openBigModelCodingPlanPersonalUpgradePanel, openZaiCodingPlanPersonalUpgradePanel, } from "#e2e/helpers/coding-plan-upgrade"; import { readCodingPlanWebviewLocalStorage, readCodingPlanUpgradeMockRequests, triggerCodingPlanWebviewPurchaseComplete, waitForCodingPlanUpgradeMockRequest, waitForCodingPlanWebviewClosed, waitForCodingPlanWebviewDisplayed, } from "#e2e/helpers/coding-plan-upgrade-webview"; describe("BigModel Coding Plan 官网 webview 支付完成 E2E", () => { afterEach(async () => { await browser.electron.restoreAllMocks(); await clearAppData(); }); it("CPUW-01: webview 通知 App 关闭升级弹窗并刷新当前 provider", async function () { this.timeout(120000); const setInventoryMode = async (mode: "hold" | "fail" | "ready") => { const base = process.env.ZCODE_CODING_PLAN_UPGRADE_MOCK_BASE_URL ?? process.env.BIGMODEL_TEST_API_BASE_URL; if (!base) throw new Error("Missing mock URL"); const response = await fetch(`${base}/__e2e/coding-plan/inventory-mode`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ mode }), }); expect(response.ok).toBe(true); }; await setInventoryMode("hold"); await browser.refresh(); const telemetryFetch = await browser.electron.mock("net", "fetch"); await telemetryFetch.mockResolvedValue({ ok: true, status: 204 }); await openBigModelCodingPlanPersonalUpgradePanel(async () => { const entry = () => $('[data-testid="sidebar-coding-plan-upgrade-button"]'); await browser.waitUntil(async () => (await entry().getAttribute("data-disabled")) !== null, { timeout: 10000, }); // 测试窗口沿用当前语言;英文专用断言会把正确的中文 loading/error 误判为业务失败。 expect(await entry().getText()).toMatch(/Loading plans|正在查询套餐/); expect(await $('[data-testid="coding-plan-embedded-webview"]').isExisting()).toBe(false); await setInventoryMode("fail"); await browser.waitUntil( async () => { if (!(await entry().isExisting())) await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).click(); return /Retry|重试/.test(await entry().getText()); }, { timeout: 15000, }, ); await telemetryFetch.update(); expect( telemetryFetch.mock.calls.filter(([, init]) => String((init as { body?: string })?.body).includes("coding_plan_upgrade_ck"), ), ).toHaveLength(0); await setInventoryMode("ready"); await entry().click(); expect(await $('[data-testid="coding-plan-embedded-webview"]').isExisting()).toBe(false); await browser.waitUntil( async () => { if (!(await entry().isExisting())) await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).click(); const label = await entry().getText(); return !/Retry|重试|Loading plans|正在查询套餐/.test(label); }, { timeout: 15000 }, ); await telemetryFetch.update(); expect( telemetryFetch.mock.calls.filter(([, init]) => String((init as { body?: string })?.body).includes("coding_plan_upgrade_ck"), ), ).toHaveLength(0); }); await waitForCodingPlanWebviewDisplayed(); const readEntryEvents = async () => { await telemetryFetch.update(); return telemetryFetch.mock.calls.flatMap(([url, init]) => { if (!String(url).includes("/event/report")) return []; const body = (init as { body?: string } | undefined)?.body; if (!body) return []; const event = JSON.parse(body) as { element_name: string; event_extra_detail: Record; }; return event.element_name === "coding_plan_upgrade_ck" ? [event] : []; }); }; await browser.waitUntil(async () => (await readEntryEvents()).length === 1, { timeout: 10000, timeoutMsg: "购买入口没有通过 App net.fetch 上报一次", }); const [entry] = await readEntryEvents(); if (!entry) throw new Error("App 入口事件不存在"); expect(entry.event_extra_detail.purchase_funnel_id).toBeTruthy(); expect(entry.event_extra_detail.entry_plan_list).toBe( "coding_plan__personal_pro,start_plan__zcode_v3_start_plan", ); await browser.waitUntil( async () => { const storage = await readCodingPlanWebviewLocalStorage([ "zcode:coding-plan:report-context", ]); const context = JSON.parse(storage["zcode:coding-plan:report-context"] || "{}"); return ( context.purchase_funnel_id === entry.event_extra_detail.purchase_funnel_id && context.purchase_entry_reporter === "app" && (context.entry_plan_list ?? "") === entry.event_extra_detail.entry_plan_list ); }, { timeout: 10000, timeoutMsg: "App 上报字段与 WebView 注入上下文不一致" }, ); await browser.electron.execute((electron) => { const guest = electron.webContents .getAllWebContents() .find( (contents) => contents.getType() === "webview" && contents.getURL().includes("coding-plan"), ); if (!guest) throw new Error("购买 WebView 不存在"); return new Promise((resolve) => { guest.once("did-finish-load", () => resolve()); guest.reload(); }); }); await browser.waitUntil( async () => { const storage = await readCodingPlanWebviewLocalStorage([ "zcode:coding-plan:report-context", ]); const context = JSON.parse(storage["zcode:coding-plan:report-context"] || "{}"); return ( context.purchase_entry_reporter === "app" && context.purchase_funnel_id === entry.event_extra_detail.purchase_funnel_id ); }, { timeout: 10000 }, ); expect(await readEntryEvents()).toHaveLength(1); // 账号已迁到 Key ID 换 PAT;Copy Secret 已删除,有效 PAT 在购买刷新时允许复用。 await waitForCodingPlanUpgradeMockRequest( (request) => request.method === "POST" && request.path === "/api/biz/v1/organization/org-e2e/projects/proj-e2e/api_keys/bigmodel-personal-api-key/access_tokens" && request.headers.authorization === "Bearer e2e-bigmodel-oauth-token", "账号初始化没有签发 BigModel PAT", ); await completePurchaseAndWaitForRefresh("bigmodel"); expect( (await readCodingPlanUpgradeMockRequests()).requests.some((request) => request.path.includes("/copy/"), ), ).toBe(false); // PAT 由账号 owner 管理,不落盘到 Personal Provider Config。 expect( (await readModelProvider(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan))?.apiKey ?? "", ).toBe(""); }); it("CPUW-03: Z.ai webview 通知 App 关闭升级弹窗并刷新当前 provider 与 Team Plan products", async function () { this.timeout(120000); await prepareZaiCodingPlanUpgradeState(); await openZaiCodingPlanPersonalUpgradePanel(); await waitForCodingPlanWebviewDisplayed(); await waitForZaiWebviewCredentialsInjected(); await completePurchaseAndWaitForRefresh("zai"); // PAT 由账号 owner 管理,不落盘到 Personal Provider Config。 expect( (await readModelProvider(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan))?.apiKey ?? "", ).toBe(""); }); }); async function completePurchaseAndWaitForRefresh(provider: "bigmodel" | "zai") { // 旧断言会命中启动请求;冻结边界后只读购买完成后的新增请求。 const before = (await readCodingPlanUpgradeMockRequests()).requests; // 订阅接口使用已签发的 PAT,企业目录仍使用 OAuth,不能把两种凭据混作同一个断言。 const subscriptionAuthorization = before.findLast( (request) => request.path === "/api/biz/subscription/list", )?.headers.authorization; expect(Boolean(subscriptionAuthorization)).toBe(true); await triggerCodingPlanWebviewPurchaseComplete(provider); await waitForCodingPlanWebviewClosed(); const expectedToken = provider === "bigmodel" ? "e2e-bigmodel-oauth-token" : "zai-webview-oauth-token"; await browser.waitUntil( async () => { const requests = (await readCodingPlanUpgradeMockRequests()).requests.slice(before.length); return ["/api/biz/subscription/list", "/api/biz/subscription/enterprise/v2/pricing"].every( (path) => requests.some( (request) => request.path === path && request.headers.authorization === (path.endsWith("/subscription/list") ? subscriptionAuthorization : expectedToken), ), ); }, { timeout: 30000, timeoutMsg: `${provider} 购买完成后未按当前账号刷新订阅与企业套餐` }, ); } async function prepareZaiCodingPlanUpgradeState() { await clearAppData(); await seedCredentials("zai-webview-oauth-token"); await seedSettings({ providerFamilyConnectionSelections: { zai: { kind: "individual-coding-plan" }, bigmodel: { kind: "individual-coding-plan" }, }, providerFamilyDomain: "zai", providerFamilyDomainMigrated: true, lastWorkspaceSession: [ { kind: "local", workspacePath: DEFAULT_WORKSPACE, workspacePurpose: "project", }, ], lastActiveTabIndex: 0, }); await browser.reloadSession(); await waitForWorkspaceApp(DEFAULT_WORKSPACE, 60000); } async function waitForZaiWebviewCredentialsInjected() { let latestStorage: Record = {}; await browser.waitUntil( async () => { latestStorage = await readCodingPlanWebviewLocalStorage([ "oauth:zai:access_token", "zcodejwttoken", "oauth:bigmodel:access_token", ]); return ( latestStorage["oauth:zai:access_token"] === "zai-webview-oauth-token" && latestStorage.zcodejwttoken === "zai-webview-oauth-token" && latestStorage["oauth:bigmodel:access_token"] === null ); }, { timeout: 30000, timeoutMsg: `Z.ai credentials 没有注入到 Coding Plan webview localStorage: ${JSON.stringify(latestStorage)}`, }, ); }