|
| 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 { hasAuthProfileStoreSourceForProvider } from "./source-check.js"; |
| 6 | + |
| 7 | +describe("hasAuthProfileStoreSourceForProvider", () => { |
| 8 | + afterEach(() => { |
| 9 | + vi.unstubAllEnvs(); |
| 10 | + }); |
| 11 | + |
| 12 | + async function withAgentStore(profiles: Record<string, unknown>) { |
| 13 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-source-")); |
| 14 | + const stateDir = path.join(root, "state"); |
| 15 | + const agentDir = path.join(root, "agent"); |
| 16 | + await fs.mkdir(agentDir, { recursive: true }); |
| 17 | + await fs.mkdir(stateDir, { recursive: true }); |
| 18 | + vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); |
| 19 | + await fs.writeFile( |
| 20 | + path.join(agentDir, "auth-profiles.json"), |
| 21 | + JSON.stringify({ version: 1, profiles }), |
| 22 | + ); |
| 23 | + return { agentDir }; |
| 24 | + } |
| 25 | + |
| 26 | + async function withLegacyAuthStore(profiles: Record<string, unknown>) { |
| 27 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-auth-source-")); |
| 28 | + const stateDir = path.join(root, "state"); |
| 29 | + const agentDir = path.join(root, "agent"); |
| 30 | + await fs.mkdir(agentDir, { recursive: true }); |
| 31 | + await fs.mkdir(stateDir, { recursive: true }); |
| 32 | + vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); |
| 33 | + await fs.writeFile(path.join(agentDir, "auth.json"), JSON.stringify(profiles)); |
| 34 | + return { agentDir }; |
| 35 | + } |
| 36 | + |
| 37 | + it("counts provider-specific usable credentials", async () => { |
| 38 | + const { agentDir } = await withAgentStore({ |
| 39 | + "openai:default": { type: "api_key", provider: "openai", key: "sk-test" }, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it("counts legacy auth stores with alias fields and fallback providers", async () => { |
| 46 | + const { agentDir } = await withLegacyAuthStore({ |
| 47 | + openai: { mode: "apiKey", apiKey: "sk-test" }, |
| 48 | + }); |
| 49 | + |
| 50 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("ignores malformed provider fields instead of throwing", async () => { |
| 54 | + const { agentDir } = await withAgentStore({ |
| 55 | + "openai:default": { type: "api_key", key: "sk-test" }, |
| 56 | + "openai:other": { type: "api_key", provider: 123, key: "sk-test" }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("does not count profile ids that are bound to a different credential provider", async () => { |
| 63 | + const { agentDir } = await withAgentStore({ |
| 64 | + "openai:default": { type: "api_key", provider: "anthropic", key: "sk-test" }, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it("honors configured profile order constraints", async () => { |
| 71 | + const { agentDir } = await withAgentStore({ |
| 72 | + "openai:default": { type: "api_key", provider: "openai", key: "sk-test" }, |
| 73 | + "openai:expired": { |
| 74 | + type: "token", |
| 75 | + provider: "openai", |
| 76 | + token: "expired-token", |
| 77 | + expires: Date.now() - 1000, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect( |
| 82 | + hasAuthProfileStoreSourceForProvider("openai", agentDir, { |
| 83 | + profileIds: ["openai:expired"], |
| 84 | + }), |
| 85 | + ).toBe(false); |
| 86 | + expect( |
| 87 | + hasAuthProfileStoreSourceForProvider("openai", agentDir, { |
| 88 | + profileIds: ["openai:default"], |
| 89 | + }), |
| 90 | + ).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it("treats explicit empty profile order as no usable profile", async () => { |
| 94 | + const { agentDir } = await withAgentStore({ |
| 95 | + "openai:default": { type: "api_key", provider: "openai", key: "sk-test" }, |
| 96 | + }); |
| 97 | + |
| 98 | + expect( |
| 99 | + hasAuthProfileStoreSourceForProvider("openai", agentDir, { |
| 100 | + profileIds: [], |
| 101 | + }), |
| 102 | + ).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it("does not count empty provider profiles as credential evidence", async () => { |
| 106 | + const { agentDir } = await withAgentStore({ |
| 107 | + "openai:default": { type: "api_key", provider: "openai" }, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it("does not count expired token profiles as credential evidence", async () => { |
| 114 | + const { agentDir } = await withAgentStore({ |
| 115 | + "openai:token": { |
| 116 | + type: "token", |
| 117 | + provider: "openai", |
| 118 | + token: "expired-token", |
| 119 | + expires: Date.now() - 1000, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + expect(hasAuthProfileStoreSourceForProvider("openai", agentDir)).toBe(false); |
| 124 | + }); |
| 125 | +}); |
0 commit comments