|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; |
| 2 | +import { mkdirSync, rmSync } from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { tmpdir } from 'os'; |
| 5 | +import { loadConfig } from '../../src/config/loader'; |
| 6 | +import { CLIError } from '../../src/errors/base'; |
| 7 | +import type { GlobalFlags } from '../../src/types/flags'; |
| 8 | + |
| 9 | +const baseFlags: GlobalFlags = { |
| 10 | + quiet: false, |
| 11 | + verbose: false, |
| 12 | + noColor: true, |
| 13 | + yes: false, |
| 14 | + dryRun: false, |
| 15 | + help: false, |
| 16 | + nonInteractive: true, |
| 17 | + async: false, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('loadConfig', () => { |
| 21 | + const testDir = join(tmpdir(), `mmx-config-test-${Date.now()}`); |
| 22 | + const originalHome = process.env.HOME; |
| 23 | + const originalRegion = process.env.MINIMAX_REGION; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + mkdirSync(join(testDir, '.mmx'), { recursive: true }); |
| 27 | + process.env.HOME = testDir; |
| 28 | + delete process.env.MINIMAX_REGION; |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + process.env.HOME = originalHome; |
| 33 | + if (originalRegion === undefined) delete process.env.MINIMAX_REGION; |
| 34 | + else process.env.MINIMAX_REGION = originalRegion; |
| 35 | + rmSync(testDir, { recursive: true, force: true }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('rejects invalid --region values', () => { |
| 39 | + expect(() => loadConfig({ ...baseFlags, region: 'mars' })).toThrow(CLIError); |
| 40 | + expect(() => loadConfig({ ...baseFlags, region: 'mars' })).toThrow( |
| 41 | + 'Invalid region "mars". Valid values: global, cn', |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('rejects invalid MINIMAX_REGION values', () => { |
| 46 | + process.env.MINIMAX_REGION = 'moon'; |
| 47 | + |
| 48 | + expect(() => loadConfig(baseFlags)).toThrow( |
| 49 | + 'Invalid region "moon". Valid values: global, cn', |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('accepts valid explicit region values', () => { |
| 54 | + const config = loadConfig({ ...baseFlags, region: 'cn' }); |
| 55 | + |
| 56 | + expect(config.region).toBe('cn'); |
| 57 | + expect(config.baseUrl).toBe('https://api.minimaxi.com'); |
| 58 | + }); |
| 59 | +}); |
0 commit comments