liveBigmodelProfileFixture.test.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { platform, tmpdir, userInfo } from "node:os";
  3. import { join } from "node:path";
  4. import { expect, it, vi } from "vitest";
  5. import { getDataBaseDir, setDataBaseDir } from "@zcode/services/node";
  6. import { createLegacyCredentialCipher } from "@zcode/shared/credential-security";
  7. const fixture = vi.hoisted(() => ({ homeDir: "", credentialsFile: "" }));
  8. vi.mock("#e2e/helpers/desktop-app", () => ({ getE2EAppDataPaths: () => fixture }));
  9. import { importLiveBigModelCredentials } from "#e2e/helpers/live-bigmodel-profile";
  10. it("imports OAuth without reading an unrelated cached Team key, and re-encrypts for the target profile", async () => {
  11. const root = await mkdtemp(join(tmpdir(), "live-profile-fixture-"));
  12. const sourceHome = join(root, "source");
  13. fixture.homeDir = join(root, "target");
  14. fixture.credentialsFile = join(fixture.homeDir, ".zcode/v2/credentials.json");
  15. const sourceDirectory = join(sourceHome, ".zcode/v2");
  16. const previousHome = process.env.R3_LIVE_SOURCE_HOME;
  17. const previousSecret = process.env.ZCODE_CREDENTIAL_SECRET;
  18. const previousDataBaseDir = getDataBaseDir();
  19. process.env.R3_LIVE_SOURCE_HOME = sourceHome;
  20. process.env.ZCODE_CREDENTIAL_SECRET = "live-profile-target-test-secret";
  21. const sourceCipher = createLegacyCredentialCipher({
  22. ZCODE_CREDENTIAL_SECRET: `zcode-credential-fallback:${platform()}:${sourceHome}:${userInfo().username}`,
  23. });
  24. const credentials = {
  25. "oauth:bigmodel:access_token": "test-access",
  26. "oauth:bigmodel:refresh_token": "test-refresh",
  27. "oauth:bigmodel:user_info": JSON.stringify({ id: "fixture-user" }),
  28. "oauth:active_provider": "bigmodel",
  29. zcodejwttoken: "test-jwt",
  30. };
  31. const staleKey = "account-provider:coding-plan:account:bigmodel-team-coding-plan:fixture:api-key";
  32. try {
  33. await mkdir(sourceDirectory, { recursive: true });
  34. await writeFile(
  35. join(sourceDirectory, "credentials.json"),
  36. JSON.stringify({
  37. ...Object.fromEntries(
  38. Object.entries(credentials).map(([key, value]) => [key, sourceCipher.encrypt(value)]),
  39. ),
  40. [staleKey]: "enc:v2:unrelated-old-cache",
  41. }),
  42. );
  43. await writeFile(
  44. join(sourceDirectory, "setting.json"),
  45. JSON.stringify({
  46. providerFamilyConnectionSelections: {
  47. bigmodel: {
  48. kind: "team-coding-plan",
  49. productId: "test-product",
  50. organizationId: "test-org",
  51. projectId: "test-project",
  52. },
  53. },
  54. }),
  55. );
  56. const result = await importLiveBigModelCredentials("E2E_LIVE_PROFILE");
  57. expect(result.imported.sort()).toEqual(Object.keys(credentials).sort());
  58. expect(result.refreshTokenPresent).toBe(true);
  59. const written = JSON.parse(await readFile(fixture.credentialsFile, "utf8"));
  60. expect(written).not.toHaveProperty(staleKey);
  61. const targetCipher = createLegacyCredentialCipher();
  62. for (const [key, value] of Object.entries(credentials)) {
  63. expect(targetCipher.decrypt(written[key])).toBe(value);
  64. }
  65. } finally {
  66. setDataBaseDir(previousDataBaseDir);
  67. if (previousHome === undefined) delete process.env.R3_LIVE_SOURCE_HOME;
  68. else process.env.R3_LIVE_SOURCE_HOME = previousHome;
  69. if (previousSecret === undefined) delete process.env.ZCODE_CREDENTIAL_SECRET;
  70. else process.env.ZCODE_CREDENTIAL_SECRET = previousSecret;
  71. await rm(root, { recursive: true, force: true });
  72. }
  73. });