oauth-attribution.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import assert from "node:assert/strict";
  2. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. const home = await mkdtemp(join(tmpdir(), "oauth-attribution-recovery-"));
  6. process.env.ZCODE_DATA_BASE_DIR = home;
  7. const { createCredentialService, createOAuthService } = await import("@zcode/services/node");
  8. const file = join(home, ".zcode/v2/credentials.json");
  9. await mkdir(join(home, ".zcode/v2"), { recursive: true });
  10. const credential = createCredentialService({
  11. cipherProvider: { encrypt: (value) => value, decrypt: (value) => value },
  12. });
  13. const oauth = createOAuthService(credential, {
  14. apiClient: {
  15. request: async () => {
  16. throw new Error("Attribution fixture must not request a network token");
  17. },
  18. },
  19. captureCredentialGuard: () => credential.captureRecoveryGuard(),
  20. });
  21. const results = [];
  22. try {
  23. for (const provider of ["zai", "bigmodel"] as const) {
  24. await writeFile(file, "{}");
  25. const flow = await oauth.startOAuth(provider);
  26. await writeFile(file, '{"unfinished":');
  27. const inspection = await credential.inspectStore();
  28. assert.equal(inspection.status, "corrupt");
  29. if (inspection.status !== "corrupt") throw new Error("Expected synthetic corrupt store");
  30. assert.equal((await credential.recoverStore(inspection.fingerprint)).status, "recovered");
  31. const empty = await readFile(file, "utf8");
  32. const generation = await readFile(`${file}.recovery-generation`, "utf8");
  33. const callback = (state: string) =>
  34. `zcode://oauth/callback?state=${state}&utm_source=synthetic-${provider}`;
  35. await assert.rejects(oauth.handleCallback(callback(flow.state)), {
  36. code: "ZCODE_CREDENTIAL_STORE_CHANGED",
  37. });
  38. assert.equal(await readFile(file, "utf8"), empty);
  39. assert.equal(await readFile(`${file}.recovery-generation`, "utf8"), generation);
  40. assert.equal(await credential.load("oauth:login_attribution"), null);
  41. const fresh = await oauth.startOAuth(provider);
  42. assert.equal((await oauth.handleCallback(callback(fresh.state)))?.kind, "attribution");
  43. assert.deepEqual(JSON.parse((await credential.load("oauth:login_attribution"))!), {
  44. utm_source: `synthetic-${provider}`,
  45. });
  46. await assert.rejects(oauth.handleCallback(callback(fresh.state)), /归因回调已处理/);
  47. await oauth.cancelPending(provider);
  48. results.push({
  49. provider,
  50. staleAttributionRejected: true,
  51. emptyStorePreserved: true,
  52. freshAttributionPreserved: true,
  53. duplicateRejected: true,
  54. });
  55. }
  56. const result = { results, realUserCredentials: false };
  57. if (process.argv[2]) await writeFile(process.argv[2], JSON.stringify(result, null, 2));
  58. console.log(JSON.stringify(result));
  59. } finally {
  60. await oauth.cancelPending();
  61. await rm(home, { recursive: true, force: true });
  62. }