|
1 | | -import fs from "node:fs/promises"; |
2 | | -import os from "node:os"; |
3 | | -import path from "node:path"; |
4 | | -import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | | -import { writePersistedAuthProfileStoreRaw } from "../agents/auth-profiles/sqlite.js"; |
6 | | -import { resolveApiKeyForProvider } from "../agents/model-auth.js"; |
7 | | -import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js"; |
8 | | -import { |
9 | | - activateSecretsRuntimeSnapshot, |
10 | | - clearSecretsRuntimeSnapshot, |
11 | | - prepareSecretsRuntimeSnapshot, |
12 | | -} from "./runtime.js"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +let resolveApiKeyForProvider: typeof import("../agents/model-auth.js").resolveApiKeyForProvider; |
| 4 | +let closeOpenClawAgentDatabasesForTest: typeof import("../state/openclaw-agent-db.js").closeOpenClawAgentDatabasesForTest; |
| 5 | +let withOpenClawTestState: typeof import("../test-utils/openclaw-test-state.js").withOpenClawTestState; |
| 6 | +let activateSecretsRuntimeSnapshot: typeof import("./runtime.js").activateSecretsRuntimeSnapshot; |
| 7 | +let clearSecretsRuntimeSnapshot: typeof import("./runtime.js").clearSecretsRuntimeSnapshot; |
| 8 | +let prepareSecretsRuntimeSnapshot: typeof import("./runtime.js").prepareSecretsRuntimeSnapshot; |
13 | 9 |
|
14 | 10 | describe("auth profile migration isolation", () => { |
15 | | - const roots: string[] = []; |
| 11 | + beforeEach(async () => { |
| 12 | + // This shard is non-isolated, so load the singleton-backed runtime and auth helpers |
| 13 | + // from one fresh graph after neighboring tests reset the module cache. |
| 14 | + vi.resetModules(); |
| 15 | + ({ |
| 16 | + activateSecretsRuntimeSnapshot, |
| 17 | + clearSecretsRuntimeSnapshot, |
| 18 | + prepareSecretsRuntimeSnapshot, |
| 19 | + } = await import("./runtime.js")); |
| 20 | + ({ resolveApiKeyForProvider } = await import("../agents/model-auth.js")); |
| 21 | + ({ closeOpenClawAgentDatabasesForTest } = await import("../state/openclaw-agent-db.js")); |
| 22 | + ({ withOpenClawTestState } = await import("../test-utils/openclaw-test-state.js")); |
| 23 | + clearSecretsRuntimeSnapshot(); |
| 24 | + closeOpenClawAgentDatabasesForTest(); |
| 25 | + vi.unstubAllEnvs(); |
| 26 | + }); |
16 | 27 |
|
17 | | - afterEach(async () => { |
| 28 | + afterEach(() => { |
18 | 29 | clearSecretsRuntimeSnapshot(); |
19 | 30 | closeOpenClawAgentDatabasesForTest(); |
20 | 31 | vi.unstubAllEnvs(); |
21 | | - for (const root of roots.splice(0)) { |
22 | | - await fs.rm(root, { recursive: true, force: true }); |
23 | | - } |
24 | 32 | }); |
25 | 33 |
|
26 | 34 | it("isolates one legacy owner and blocks env fallback without affecting a healthy agent", async () => { |
27 | | - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-migration-isolation-")); |
28 | | - roots.push(root); |
29 | | - const legacyAgentDir = path.join(root, "agents", "legacy", "agent"); |
30 | | - const healthyAgentDir = path.join(root, "agents", "healthy", "agent"); |
31 | | - await fs.mkdir(legacyAgentDir, { recursive: true }); |
32 | | - await fs.mkdir(healthyAgentDir, { recursive: true }); |
33 | | - await fs.writeFile( |
34 | | - path.join(legacyAgentDir, "auth.json"), |
35 | | - `${JSON.stringify({ openai: { type: "api_key", key: "fake-legacy-key" } })}\n`, |
36 | | - ); |
37 | | - writePersistedAuthProfileStoreRaw( |
| 35 | + await withOpenClawTestState( |
38 | 36 | { |
39 | | - version: 1, |
40 | | - profiles: { |
41 | | - "openai:default": { |
42 | | - type: "api_key", |
43 | | - provider: "openai", |
44 | | - key: "fake-healthy-key", |
45 | | - }, |
| 37 | + layout: "state-only", |
| 38 | + label: "auth-migration-isolation", |
| 39 | + env: { |
| 40 | + OPENAI_API_KEY: "fake-env-fallback-key", |
46 | 41 | }, |
47 | 42 | }, |
48 | | - healthyAgentDir, |
49 | | - ); |
50 | | - vi.stubEnv("OPENAI_API_KEY", "fake-env-fallback-key"); |
| 43 | + async (state) => { |
| 44 | + const legacyAgentDir = state.agentDir("legacy"); |
| 45 | + const healthyAgentDir = state.agentDir("healthy"); |
| 46 | + await state.writeText( |
| 47 | + "agents/legacy/agent/auth.json", |
| 48 | + `${JSON.stringify({ openai: { type: "api_key", key: "fake-legacy-key" } })}\n`, |
| 49 | + ); |
| 50 | + await state.writeAuthProfiles( |
| 51 | + { |
| 52 | + version: 1, |
| 53 | + profiles: { |
| 54 | + "openai:default": { |
| 55 | + type: "api_key", |
| 56 | + provider: "openai", |
| 57 | + key: "fake-healthy-key", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + "healthy", |
| 62 | + ); |
51 | 63 |
|
52 | | - const snapshot = await prepareSecretsRuntimeSnapshot({ |
53 | | - config: {}, |
54 | | - agentDirs: [legacyAgentDir, healthyAgentDir], |
55 | | - includeConfigRefs: false, |
56 | | - allowUnavailableSecretOwners: true, |
57 | | - loadablePluginOrigins: new Map(), |
58 | | - }); |
59 | | - expect(snapshot.degradedOwners).toEqual([ |
60 | | - expect.objectContaining({ |
61 | | - ownerKind: "route", |
62 | | - reason: "auth profile migration required", |
63 | | - paths: ["auth-profile-legacy:legacy-auth"], |
64 | | - }), |
65 | | - ]); |
66 | | - activateSecretsRuntimeSnapshot(snapshot); |
| 64 | + const snapshot = await prepareSecretsRuntimeSnapshot({ |
| 65 | + config: {}, |
| 66 | + env: state.env, |
| 67 | + agentDirs: [legacyAgentDir, healthyAgentDir], |
| 68 | + includeConfigRefs: false, |
| 69 | + allowUnavailableSecretOwners: true, |
| 70 | + loadablePluginOrigins: new Map(), |
| 71 | + }); |
| 72 | + expect(snapshot.degradedOwners).toEqual([ |
| 73 | + expect.objectContaining({ |
| 74 | + ownerKind: "route", |
| 75 | + reason: "auth profile migration required", |
| 76 | + paths: ["auth-profile-legacy:legacy-auth"], |
| 77 | + }), |
| 78 | + ]); |
| 79 | + activateSecretsRuntimeSnapshot(snapshot); |
67 | 80 |
|
68 | | - await expect( |
69 | | - resolveApiKeyForProvider({ provider: "openai", agentDir: legacyAgentDir }), |
70 | | - ).rejects.toMatchObject({ |
71 | | - code: "AUTH_PROFILE_MIGRATION_REQUIRED", |
72 | | - action: "openclaw doctor --fix", |
73 | | - sourceKinds: ["legacy-auth"], |
74 | | - }); |
75 | | - await expect( |
76 | | - resolveApiKeyForProvider({ |
77 | | - provider: "openai", |
78 | | - agentDir: healthyAgentDir, |
79 | | - profileId: "openai:default", |
80 | | - lockedProfile: true, |
81 | | - store: snapshot.authStores.find((entry) => entry.agentDir === healthyAgentDir)?.store, |
82 | | - }), |
83 | | - ).resolves.toMatchObject({ apiKey: "fake-healthy-key" }); |
| 81 | + await expect( |
| 82 | + resolveApiKeyForProvider({ provider: "openai", agentDir: legacyAgentDir }), |
| 83 | + ).rejects.toMatchObject({ |
| 84 | + code: "AUTH_PROFILE_MIGRATION_REQUIRED", |
| 85 | + action: "openclaw doctor --fix", |
| 86 | + sourceKinds: ["legacy-auth"], |
| 87 | + }); |
| 88 | + await expect( |
| 89 | + resolveApiKeyForProvider({ |
| 90 | + provider: "openai", |
| 91 | + agentDir: healthyAgentDir, |
| 92 | + profileId: "openai:default", |
| 93 | + lockedProfile: true, |
| 94 | + store: snapshot.authStores.find((entry) => entry.agentDir === healthyAgentDir)?.store, |
| 95 | + }), |
| 96 | + ).resolves.toMatchObject({ apiKey: "fake-healthy-key" }); |
| 97 | + }, |
| 98 | + ); |
84 | 99 | }); |
85 | 100 | }); |
0 commit comments