|
1 | | -import { describe, it, expect, mock } from 'bun:test'; |
| 1 | +import { beforeEach, describe, expect, it, mock } from 'bun:test'; |
2 | 2 | import { default as setCommand } from '../../../src/commands/config/set'; |
3 | 3 |
|
| 4 | +let storedConfig: Record<string, unknown> = {}; |
| 5 | +let writtenConfig: Record<string, unknown> | undefined; |
| 6 | +const writeConfigFile = mock(async (data: Record<string, unknown>) => { |
| 7 | + writtenConfig = { ...data }; |
| 8 | +}); |
| 9 | + |
4 | 10 | // Mock file I/O |
5 | 11 | mock.module('../../../src/config/loader', () => ({ |
6 | | - readConfigFile: () => ({}), |
7 | | - writeConfigFile: mock(() => Promise.resolve()), |
| 12 | + readConfigFile: () => ({ ...storedConfig }), |
| 13 | + writeConfigFile, |
8 | 14 | })); |
9 | 15 |
|
10 | 16 | describe('config set command', () => { |
| 17 | + beforeEach(() => { |
| 18 | + storedConfig = {}; |
| 19 | + writtenConfig = undefined; |
| 20 | + writeConfigFile.mockClear(); |
| 21 | + }); |
| 22 | + |
11 | 23 | it('has correct name', () => { |
12 | 24 | expect(setCommand.name).toBe('config set'); |
13 | 25 | }); |
@@ -135,4 +147,92 @@ describe('config set command', () => { |
135 | 147 | }), |
136 | 148 | ).resolves.toBeUndefined(); |
137 | 149 | }); |
| 150 | + |
| 151 | + it('masks api_key in dry-run output', async () => { |
| 152 | + const apiKey = 'sk-test-secret-123456'; |
| 153 | + let output = ''; |
| 154 | + const originalLog = console.log; |
| 155 | + console.log = (message: string) => { output += message; }; |
| 156 | + |
| 157 | + try { |
| 158 | + await setCommand.execute({ |
| 159 | + region: 'global', |
| 160 | + baseUrl: 'https://api.mmx.io', |
| 161 | + output: 'json', |
| 162 | + timeout: 10, |
| 163 | + verbose: false, |
| 164 | + quiet: false, |
| 165 | + noColor: true, |
| 166 | + yes: false, |
| 167 | + dryRun: true, |
| 168 | + nonInteractive: true, |
| 169 | + async: false, |
| 170 | + }, { |
| 171 | + key: 'api_key', |
| 172 | + value: apiKey, |
| 173 | + quiet: false, |
| 174 | + verbose: false, |
| 175 | + noColor: true, |
| 176 | + yes: false, |
| 177 | + dryRun: true, |
| 178 | + help: false, |
| 179 | + nonInteractive: true, |
| 180 | + async: false, |
| 181 | + }); |
| 182 | + } finally { |
| 183 | + console.log = originalLog; |
| 184 | + } |
| 185 | + |
| 186 | + expect(output).not.toContain(apiKey); |
| 187 | + expect(JSON.parse(output).would_set.api_key).toBe('sk-t...3456'); |
| 188 | + expect(writeConfigFile).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | + |
| 191 | + it('removes OAuth and masks output when setting api_key', async () => { |
| 192 | + const apiKey = 'sk-test-secret-123456'; |
| 193 | + storedConfig = { |
| 194 | + region: 'cn', |
| 195 | + oauth: { |
| 196 | + access_token: 'old-access-token', |
| 197 | + refresh_token: 'old-refresh-token', |
| 198 | + expires_at: '2099-01-01T00:00:00.000Z', |
| 199 | + }, |
| 200 | + }; |
| 201 | + let output = ''; |
| 202 | + const originalLog = console.log; |
| 203 | + console.log = (message: string) => { output += message; }; |
| 204 | + |
| 205 | + try { |
| 206 | + await setCommand.execute({ |
| 207 | + region: 'cn', |
| 208 | + baseUrl: 'https://api.mmx.io', |
| 209 | + output: 'json', |
| 210 | + timeout: 10, |
| 211 | + verbose: false, |
| 212 | + quiet: false, |
| 213 | + noColor: true, |
| 214 | + yes: false, |
| 215 | + dryRun: false, |
| 216 | + nonInteractive: true, |
| 217 | + async: false, |
| 218 | + }, { |
| 219 | + key: 'api_key', |
| 220 | + value: apiKey, |
| 221 | + quiet: false, |
| 222 | + verbose: false, |
| 223 | + noColor: true, |
| 224 | + yes: false, |
| 225 | + dryRun: false, |
| 226 | + help: false, |
| 227 | + nonInteractive: true, |
| 228 | + async: false, |
| 229 | + }); |
| 230 | + } finally { |
| 231 | + console.log = originalLog; |
| 232 | + } |
| 233 | + |
| 234 | + expect(writtenConfig).toEqual({ api_key: apiKey }); |
| 235 | + expect(output).not.toContain(apiKey); |
| 236 | + expect(JSON.parse(output).api_key).toBe('sk-t...3456'); |
| 237 | + }); |
138 | 238 | }); |
0 commit comments