account-key-usage-scene.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  3. import {
  4. clearAppData,
  5. getE2EAppDataPaths,
  6. DEFAULT_WORKSPACE,
  7. readModelProvider,
  8. seedCredentials,
  9. seedSettings,
  10. waitForWorkspaceApp,
  11. } from "#e2e/helpers/desktop-app";
  12. import { skipOccupationOnboardingIfPresent } from "#e2e/helpers/occupation-onboarding";
  13. import {
  14. waitForCodingPlanUpgradeMockRequest,
  15. readCodingPlanUpgradeMockRequests,
  16. } from "#e2e/helpers/coding-plan-upgrade-webview";
  17. // 复用已有账号 mock 数据;没有目标名称 Key,由真实 Host resolver 发起创建。
  18. describe("PAT-KEY-E2E: 账号就绪时创建 Coding Plan Key", () => {
  19. afterEach(async () => {
  20. await browser.electron.restoreAllMocks();
  21. await clearAppData();
  22. });
  23. it("BigModel 创建 Key 显式声明套餐用途,凭据不写入个人配置", async function () {
  24. this.timeout(90000);
  25. await skipOccupationOnboardingIfPresent();
  26. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  27. await assertCreation("e2e-bigmodel-oauth-token");
  28. expect(
  29. (await readModelProvider(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan))?.apiKey ??
  30. "",
  31. ).toBe("");
  32. });
  33. it("Z.AI 旧 Key 损坏但 OAuth 有效,重启直接换 Token 不要求重登", async function () {
  34. this.timeout(90000);
  35. await seedCredentials("zai-webview-oauth-token");
  36. const { credentialsFile } = getE2EAppDataPaths();
  37. const credentials = JSON.parse(await readFile(credentialsFile, "utf8")) as Record<
  38. string,
  39. string
  40. >;
  41. const profile = JSON.parse(credentials["oauth:zai:user_info"]!) as { id: string };
  42. const legacyKey = `account-provider:coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan}:account:${encodeURIComponent(profile.id)}:api-key`;
  43. credentials[legacyKey] = "enc:v1:broken-legacy-key";
  44. await writeFile(credentialsFile, JSON.stringify(credentials), "utf8");
  45. await seedSettings({
  46. providerFamilyConnectionSelections: {
  47. zai: { kind: "individual-coding-plan" },
  48. bigmodel: { kind: "individual-coding-plan" },
  49. },
  50. providerFamilyDomain: "zai",
  51. providerFamilyDomainMigrated: true,
  52. lastWorkspaceSession: [
  53. {
  54. kind: "local",
  55. workspacePath: DEFAULT_WORKSPACE,
  56. workspacePurpose: "project",
  57. },
  58. ],
  59. lastActiveTabIndex: 0,
  60. });
  61. await browser.reloadSession();
  62. await skipOccupationOnboardingIfPresent();
  63. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  64. await assertCreation("Bearer zai-webview-oauth-token");
  65. const restored = JSON.parse(await readFile(credentialsFile, "utf8")) as Record<string, string>;
  66. expect(restored[legacyKey]).toBe("enc:v1:broken-legacy-key");
  67. expect(restored["oauth:zai:access_token"]).toBe(credentials["oauth:zai:access_token"]);
  68. expect(
  69. (await readModelProvider(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan))?.apiKey ?? "",
  70. ).toBe("");
  71. });
  72. });
  73. async function assertCreation(authorization: string) {
  74. await waitForCodingPlanUpgradeMockRequest(
  75. (request) =>
  76. request.method === "POST" &&
  77. request.path.endsWith("/organization/org-e2e/projects/proj-e2e/api_keys") &&
  78. request.headers.authorization === authorization &&
  79. request.keyCreation?.name === "zcode-api-key" &&
  80. request.keyCreation.usageScene === 1,
  81. "真实 Host 没有创建 usageScene=1 的套餐 Key",
  82. );
  83. await assertTokenIssuance(`Bearer ${authorization.replace(/^Bearer\s+/i, "")}`);
  84. }
  85. async function assertTokenIssuance(authorization: string) {
  86. await waitForCodingPlanUpgradeMockRequest(
  87. (request) =>
  88. request.method === "POST" &&
  89. request.path.endsWith("/access_tokens") &&
  90. request.headers.authorization === authorization,
  91. "Host 未使用登录 JWT 换取 Project Token",
  92. );
  93. const result = await readCodingPlanUpgradeMockRequests();
  94. expect(result.requests.some((request) => request.path.includes("/api_keys/copy/"))).toBe(false);
  95. }