test(cliproxy): stabilize routing strategy config scope

This commit is contained in:
Tam Nhu Tran committed 2026-04-16 14:06:27 -04:00
1 parent 637a591dca
commit 4845b797de
1 file changed
+17 -14
+17 -14
View File
@@ -5,8 +5,8 @@ import * as path from 'path';
describe('cliproxy routing strategy service', () => { describe('cliproxy routing strategy service', () => {
let tempHome = ''; let tempHome = '';
let originalCcsHome: string | undefined; let scopedConfigDir = '';
let setGlobalConfigDir: (dir: string | undefined) => void; let runWithScopedConfigDir: <T>(ccsDir: string, fn: () => Promise<T> | T) => Promise<T>;
let routingTarget = { let routingTarget = {
host: '127.0.0.1', host: '127.0.0.1',
port: 8317, port: 8317,
@@ -16,29 +16,24 @@ describe('cliproxy routing strategy service', () => {
let responseFactory: (() => Promise<Response>) | null = null; let responseFactory: (() => Promise<Response>) | null = null;
beforeEach(async () => { beforeEach(async () => {
originalCcsHome = process.env.CCS_HOME;
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-routing-strategy-')); tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-routing-strategy-'));
process.env.CCS_HOME = tempHome; scopedConfigDir = path.join(tempHome, '.ccs');
({ setGlobalConfigDir } = await import('../../../src/utils/config-manager')); ({ runWithScopedConfigDir } = await import('../../../src/utils/config-manager'));
setGlobalConfigDir(path.join(tempHome, '.ccs'));
}); });
afterEach(() => { afterEach(() => {
mock.restore(); mock.restore();
setGlobalConfigDir(undefined);
if (originalCcsHome !== undefined) {
process.env.CCS_HOME = originalCcsHome;
} else {
delete process.env.CCS_HOME;
}
if (tempHome && fs.existsSync(tempHome)) { if (tempHome && fs.existsSync(tempHome)) {
fs.rmSync(tempHome, { recursive: true, force: true }); fs.rmSync(tempHome, { recursive: true, force: true });
} }
}); });
async function withScopedConfig<T>(fn: () => Promise<T> | T): Promise<T> {
return await runWithScopedConfigDir(scopedConfigDir, fn);
}
async function loadRoutingModule() { async function loadRoutingModule() {
mock.module('../../../src/cliproxy/routing-strategy-http', () => ({ mock.module('../../../src/cliproxy/routing-strategy-http', () => ({
getCliproxyRoutingTarget: () => routingTarget, getCliproxyRoutingTarget: () => routingTarget,
@@ -58,6 +53,7 @@ describe('cliproxy routing strategy service', () => {
} }
it('normalizes canonical and shorthand strategy values', async () => { it('normalizes canonical and shorthand strategy values', async () => {
await withScopedConfig(async () => {
const mod = await loadRoutingModule(); const mod = await loadRoutingModule();
expect(mod.normalizeCliproxyRoutingStrategy('round-robin')).toBe('round-robin'); expect(mod.normalizeCliproxyRoutingStrategy('round-robin')).toBe('round-robin');
@@ -66,8 +62,10 @@ describe('cliproxy routing strategy service', () => {
expect(mod.normalizeCliproxyRoutingStrategy('ff')).toBe('fill-first'); expect(mod.normalizeCliproxyRoutingStrategy('ff')).toBe('fill-first');
expect(mod.normalizeCliproxyRoutingStrategy('nope')).toBeNull(); expect(mod.normalizeCliproxyRoutingStrategy('nope')).toBeNull();
}); });
});
it('falls back to the saved local default when live CLIProxy is unavailable', async () => { it('falls back to the saved local default when live CLIProxy is unavailable', async () => {
await withScopedConfig(async () => {
const { mutateUnifiedConfig } = await import('../../../src/config/unified-config-loader'); const { mutateUnifiedConfig } = await import('../../../src/config/unified-config-loader');
mutateUnifiedConfig((config) => { mutateUnifiedConfig((config) => {
if (config.cliproxy) { if (config.cliproxy) {
@@ -83,21 +81,25 @@ describe('cliproxy routing strategy service', () => {
expect(state.target).toBe('local'); expect(state.target).toBe('local');
expect(state.reachable).toBe(false); expect(state.reachable).toBe(false);
}); });
});
it('persists the local startup default even when the live proxy is down', async () => { it('persists the local startup default even when the live proxy is down', async () => {
await withScopedConfig(async () => {
const mod = await loadRoutingModule(); const mod = await loadRoutingModule();
const result = await mod.applyCliproxyRoutingStrategy('fill-first'); const result = await mod.applyCliproxyRoutingStrategy('fill-first');
expect(result.applied).toBe('config-only'); expect(result.applied).toBe('config-only');
expect(result.strategy).toBe('fill-first'); expect(result.strategy).toBe('fill-first');
const configPath = path.join(tempHome, '.ccs', 'cliproxy', 'config.yaml'); const configPath = path.join(scopedConfigDir, 'cliproxy', 'config.yaml');
const configContent = fs.readFileSync(configPath, 'utf8'); const configContent = fs.readFileSync(configPath, 'utf8');
expect(configContent).toContain('routing:'); expect(configContent).toContain('routing:');
expect(configContent).toContain('strategy: fill-first'); expect(configContent).toContain('strategy: fill-first');
}); });
});
it('reads and writes remote strategy without mutating the local default', async () => { it('reads and writes remote strategy without mutating the local default', async () => {
await withScopedConfig(async () => {
routingTarget = { routingTarget = {
host: 'remote.example.com', host: 'remote.example.com',
port: 8080, port: 8080,
@@ -125,3 +127,4 @@ describe('cliproxy routing strategy service', () => {
expect(methodCount).toBe(2); expect(methodCount).toBe(2);
}); });
}); });
});