|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; |
| 2 | +import { discoverExternalCredential } from '../../src/auth/discover'; |
| 3 | +import { mkdirSync, writeFileSync, rmSync, existsSync } from 'fs'; |
| 4 | +import { join } from 'path'; |
| 5 | +import { tmpdir } from 'os'; |
| 6 | + |
| 7 | +describe('discoverExternalCredential', () => { |
| 8 | + const testDir = join(tmpdir(), `mmx-discover-test-${Date.now()}`); |
| 9 | + const originalHome = process.env.HOME; |
| 10 | + |
| 11 | + const authProfilesDir = join(testDir, '.openclaw', 'agents', 'main', 'agent'); |
| 12 | + const authProfilesPath = join(authProfilesDir, 'auth-profiles.json'); |
| 13 | + const openclawConfigPath = join(testDir, '.openclaw', 'openclaw.json'); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + mkdirSync(authProfilesDir, { recursive: true }); |
| 17 | + process.env.HOME = testDir; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + process.env.HOME = originalHome; |
| 22 | + if (existsSync(testDir)) { |
| 23 | + rmSync(testDir, { recursive: true, force: true }); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns null when no OpenClaw directory exists', () => { |
| 28 | + rmSync(join(testDir, '.openclaw'), { recursive: true, force: true }); |
| 29 | + const result = discoverExternalCredential(testDir); |
| 30 | + expect(result).toBeNull(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('discovers API key from minimax:global profile', () => { |
| 34 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 35 | + version: 1, |
| 36 | + profiles: { |
| 37 | + 'minimax:global': { |
| 38 | + type: 'api_key', |
| 39 | + provider: 'minimax', |
| 40 | + key: 'sk-test-global-key', |
| 41 | + }, |
| 42 | + }, |
| 43 | + })); |
| 44 | + |
| 45 | + const result = discoverExternalCredential(testDir); |
| 46 | + expect(result).not.toBeNull(); |
| 47 | + expect(result!.key).toBe('sk-test-global-key'); |
| 48 | + expect(result!.region).toBe('global'); |
| 49 | + expect(result!.source).toBe('OpenClaw auth-profiles.json'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('discovers API key from minimax:cn profile', () => { |
| 53 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 54 | + version: 1, |
| 55 | + profiles: { |
| 56 | + 'minimax:cn': { |
| 57 | + type: 'api_key', |
| 58 | + provider: 'minimax', |
| 59 | + key: 'sk-test-cn-key', |
| 60 | + }, |
| 61 | + }, |
| 62 | + })); |
| 63 | + |
| 64 | + const result = discoverExternalCredential(testDir); |
| 65 | + expect(result).not.toBeNull(); |
| 66 | + expect(result!.key).toBe('sk-test-cn-key'); |
| 67 | + expect(result!.region).toBe('cn'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('extracts access field from OAuth profile as API key', () => { |
| 71 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 72 | + version: 1, |
| 73 | + profiles: { |
| 74 | + 'minimax-portal:default': { |
| 75 | + type: 'oauth', |
| 76 | + provider: 'minimax-portal', |
| 77 | + access: 'sk-oauth-access-key', |
| 78 | + refresh: 'refresh-token', |
| 79 | + expires: Date.now() + 3600000, |
| 80 | + }, |
| 81 | + }, |
| 82 | + })); |
| 83 | + |
| 84 | + const result = discoverExternalCredential(testDir); |
| 85 | + expect(result).not.toBeNull(); |
| 86 | + expect(result!.key).toBe('sk-oauth-access-key'); |
| 87 | + expect(result!.source).toBe('OpenClaw auth-profiles.json'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('prefers API key profile over OAuth profile', () => { |
| 91 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 92 | + version: 1, |
| 93 | + profiles: { |
| 94 | + 'minimax:global': { |
| 95 | + type: 'api_key', |
| 96 | + provider: 'minimax', |
| 97 | + key: 'sk-api-key', |
| 98 | + }, |
| 99 | + 'minimax-portal:default': { |
| 100 | + type: 'oauth', |
| 101 | + provider: 'minimax-portal', |
| 102 | + access: 'sk-oauth-key', |
| 103 | + refresh: 'refresh', |
| 104 | + expires: Date.now() + 3600000, |
| 105 | + }, |
| 106 | + }, |
| 107 | + })); |
| 108 | + |
| 109 | + const result = discoverExternalCredential(testDir); |
| 110 | + expect(result).not.toBeNull(); |
| 111 | + expect(result!.key).toBe('sk-api-key'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('prefers global over cn when both present', () => { |
| 115 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 116 | + version: 1, |
| 117 | + profiles: { |
| 118 | + 'minimax:global': { |
| 119 | + type: 'api_key', |
| 120 | + provider: 'minimax', |
| 121 | + key: 'sk-global', |
| 122 | + }, |
| 123 | + 'minimax:cn': { |
| 124 | + type: 'api_key', |
| 125 | + provider: 'minimax', |
| 126 | + key: 'sk-cn', |
| 127 | + }, |
| 128 | + }, |
| 129 | + })); |
| 130 | + |
| 131 | + const result = discoverExternalCredential(testDir); |
| 132 | + expect(result).not.toBeNull(); |
| 133 | + expect(result!.key).toBe('sk-global'); |
| 134 | + expect(result!.region).toBe('global'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('discovers API key from openclaw.json config', () => { |
| 138 | + writeFileSync(openclawConfigPath, JSON.stringify({ |
| 139 | + models: { |
| 140 | + providers: { |
| 141 | + minimax: { |
| 142 | + apiKey: 'sk-config-key', |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + })); |
| 147 | + |
| 148 | + const result = discoverExternalCredential(testDir); |
| 149 | + expect(result).not.toBeNull(); |
| 150 | + expect(result!.key).toBe('sk-config-key'); |
| 151 | + expect(result!.source).toBe('OpenClaw config'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('prefers auth-profiles.json over openclaw.json', () => { |
| 155 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 156 | + version: 1, |
| 157 | + profiles: { |
| 158 | + 'minimax:global': { |
| 159 | + type: 'api_key', |
| 160 | + provider: 'minimax', |
| 161 | + key: 'sk-from-profiles', |
| 162 | + }, |
| 163 | + }, |
| 164 | + })); |
| 165 | + writeFileSync(openclawConfigPath, JSON.stringify({ |
| 166 | + models: { |
| 167 | + providers: { |
| 168 | + minimax: { |
| 169 | + apiKey: 'sk-from-config', |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + })); |
| 174 | + |
| 175 | + const result = discoverExternalCredential(testDir); |
| 176 | + expect(result).not.toBeNull(); |
| 177 | + expect(result!.key).toBe('sk-from-profiles'); |
| 178 | + expect(result!.source).toBe('OpenClaw auth-profiles.json'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('returns null for corrupted JSON', () => { |
| 182 | + writeFileSync(authProfilesPath, '{not valid json!!!'); |
| 183 | + const result = discoverExternalCredential(testDir); |
| 184 | + expect(result).toBeNull(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('returns null for wrong schema (missing profiles key)', () => { |
| 188 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 189 | + version: 1, |
| 190 | + something_else: {}, |
| 191 | + })); |
| 192 | + |
| 193 | + const result = discoverExternalCredential(testDir); |
| 194 | + expect(result).toBeNull(); |
| 195 | + }); |
| 196 | + |
| 197 | + it('skips profiles with unknown type', () => { |
| 198 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 199 | + version: 1, |
| 200 | + profiles: { |
| 201 | + 'minimax:global': { |
| 202 | + type: 'saml', |
| 203 | + provider: 'minimax', |
| 204 | + }, |
| 205 | + }, |
| 206 | + })); |
| 207 | + |
| 208 | + const result = discoverExternalCredential(testDir); |
| 209 | + expect(result).toBeNull(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('skips API key profile with empty key', () => { |
| 213 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 214 | + version: 1, |
| 215 | + profiles: { |
| 216 | + 'minimax:global': { |
| 217 | + type: 'api_key', |
| 218 | + provider: 'minimax', |
| 219 | + key: '', |
| 220 | + }, |
| 221 | + }, |
| 222 | + })); |
| 223 | + |
| 224 | + const result = discoverExternalCredential(testDir); |
| 225 | + expect(result).toBeNull(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('skips OAuth profile with empty access', () => { |
| 229 | + writeFileSync(authProfilesPath, JSON.stringify({ |
| 230 | + version: 1, |
| 231 | + profiles: { |
| 232 | + 'minimax-portal:default': { |
| 233 | + type: 'oauth', |
| 234 | + provider: 'minimax-portal', |
| 235 | + access: '', |
| 236 | + refresh: 'refresh', |
| 237 | + expires: Date.now(), |
| 238 | + }, |
| 239 | + }, |
| 240 | + })); |
| 241 | + |
| 242 | + const result = discoverExternalCredential(testDir); |
| 243 | + expect(result).toBeNull(); |
| 244 | + }); |
| 245 | +}); |
0 commit comments