|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + clearRuntimeAuthProfileStoreSnapshots, |
| 4 | + getRuntimeAuthProfileStoreSnapshot, |
| 5 | + replaceRuntimeAuthProfileStoreSnapshots, |
| 6 | + setRuntimeAuthProfileStoreSnapshot, |
| 7 | +} from "./runtime-snapshots.js"; |
| 8 | +import type { AuthProfileStore } from "./types.js"; |
| 9 | + |
| 10 | +function createStore(access: string): AuthProfileStore { |
| 11 | + return { |
| 12 | + version: 1, |
| 13 | + profiles: { |
| 14 | + "openai-codex:default": { |
| 15 | + type: "oauth", |
| 16 | + provider: "openai-codex", |
| 17 | + access, |
| 18 | + refresh: `refresh-${access}`, |
| 19 | + expires: Date.now() + 60_000, |
| 20 | + accountId: "acct-1", |
| 21 | + }, |
| 22 | + }, |
| 23 | + order: { |
| 24 | + "openai-codex": ["openai-codex:default"], |
| 25 | + }, |
| 26 | + usageStats: { |
| 27 | + "openai-codex:default": { |
| 28 | + lastUsed: 1, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe("runtime auth profile snapshots", () => { |
| 35 | + it("isolates set/get/replace snapshot mutations without structuredClone", () => { |
| 36 | + const structuredCloneSpy = vi.spyOn(globalThis, "structuredClone"); |
| 37 | + const agentDir = "/tmp/openclaw-auth-runtime-snapshot-agent"; |
| 38 | + try { |
| 39 | + const stored = createStore("access-1"); |
| 40 | + setRuntimeAuthProfileStoreSnapshot(stored, agentDir); |
| 41 | + stored.profiles["openai-codex:default"].provider = "mutated"; |
| 42 | + stored.order!["openai-codex"].push("mutated"); |
| 43 | + |
| 44 | + const first = getRuntimeAuthProfileStoreSnapshot(agentDir); |
| 45 | + expect(first?.profiles["openai-codex:default"]).toMatchObject({ |
| 46 | + provider: "openai-codex", |
| 47 | + access: "access-1", |
| 48 | + }); |
| 49 | + expect(first?.order?.["openai-codex"]).toEqual(["openai-codex:default"]); |
| 50 | + |
| 51 | + first!.profiles["openai-codex:default"].provider = "mutated-again"; |
| 52 | + first!.usageStats!["openai-codex:default"].lastUsed = 99; |
| 53 | + |
| 54 | + const second = getRuntimeAuthProfileStoreSnapshot(agentDir); |
| 55 | + expect(second?.profiles["openai-codex:default"]).toMatchObject({ |
| 56 | + provider: "openai-codex", |
| 57 | + access: "access-1", |
| 58 | + }); |
| 59 | + expect(second?.usageStats?.["openai-codex:default"]?.lastUsed).toBe(1); |
| 60 | + |
| 61 | + const replacement = createStore("access-2"); |
| 62 | + replaceRuntimeAuthProfileStoreSnapshots([{ agentDir, store: replacement }]); |
| 63 | + const replacementCredential = replacement.profiles["openai-codex:default"]; |
| 64 | + expect(replacementCredential?.type).toBe("oauth"); |
| 65 | + if (replacementCredential?.type === "oauth") { |
| 66 | + replacementCredential.access = "mutated-replacement"; |
| 67 | + } |
| 68 | + |
| 69 | + const replaced = getRuntimeAuthProfileStoreSnapshot(agentDir); |
| 70 | + expect(replaced?.profiles["openai-codex:default"]).toMatchObject({ |
| 71 | + access: "access-2", |
| 72 | + refresh: "refresh-access-2", |
| 73 | + }); |
| 74 | + expect(structuredCloneSpy).not.toHaveBeenCalled(); |
| 75 | + } finally { |
| 76 | + structuredCloneSpy.mockRestore(); |
| 77 | + clearRuntimeAuthProfileStoreSnapshots(); |
| 78 | + } |
| 79 | + }); |
| 80 | +}); |
0 commit comments