Skip to content

Commit 85084bb

Browse files
committed
fix: validate configured region
1 parent 23722c3 commit 85084bb

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/config/loader.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { readFileSync, writeFileSync, renameSync, existsSync } from 'fs';
22
import { parseConfigFile, REGIONS, type Config, type ConfigFile, type Region } from './schema';
33
import { ensureConfigDir, getConfigPath } from './paths';
44
import { detectOutputFormat, type OutputFormat } from '../output/formatter';
5+
import { CLIError } from '../errors/base';
6+
import { ExitCode } from '../errors/codes';
57
import type { GlobalFlags } from '../types/flags';
68

79
export function readConfigFile(): ConfigFile {
@@ -33,6 +35,13 @@ export function loadConfig(flags: GlobalFlags): Config {
3335
const fileApiKey = file.api_key;
3436

3537
const explicitRegion = (flags.region as string) || process.env.MINIMAX_REGION || undefined;
38+
if (explicitRegion && !(explicitRegion in REGIONS)) {
39+
throw new CLIError(
40+
`Invalid region "${explicitRegion}". Valid values: ${Object.keys(REGIONS).join(', ')}`,
41+
ExitCode.USAGE,
42+
);
43+
}
44+
3645
const cachedRegion = file.region;
3746
const region = (explicitRegion || cachedRegion || 'global') as Region;
3847

test/config/loader.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)