mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 06:16:37 +00:00
test(cliproxy): stabilize routing strategy config scope
This commit is contained in:
@@ -5,8 +5,8 @@ import * as path from 'path';
|
||||
|
||||
describe('cliproxy routing strategy service', () => {
|
||||
let tempHome = '';
|
||||
let originalCcsHome: string | undefined;
|
||||
let setGlobalConfigDir: (dir: string | undefined) => void;
|
||||
let scopedConfigDir = '';
|
||||
let runWithScopedConfigDir: <T>(ccsDir: string, fn: () => Promise<T> | T) => Promise<T>;
|
||||
let routingTarget = {
|
||||
host: '127.0.0.1',
|
||||
port: 8317,
|
||||
@@ -16,29 +16,24 @@ describe('cliproxy routing strategy service', () => {
|
||||
let responseFactory: (() => Promise<Response>) | null = null;
|
||||
|
||||
beforeEach(async () => {
|
||||
originalCcsHome = process.env.CCS_HOME;
|
||||
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'));
|
||||
setGlobalConfigDir(path.join(tempHome, '.ccs'));
|
||||
({ runWithScopedConfigDir } = await import('../../../src/utils/config-manager'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
setGlobalConfigDir(undefined);
|
||||
|
||||
if (originalCcsHome !== undefined) {
|
||||
process.env.CCS_HOME = originalCcsHome;
|
||||
} else {
|
||||
delete process.env.CCS_HOME;
|
||||
}
|
||||
|
||||
if (tempHome && fs.existsSync(tempHome)) {
|
||||
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() {
|
||||
mock.module('../../../src/cliproxy/routing-strategy-http', () => ({
|
||||
getCliproxyRoutingTarget: () => routingTarget,
|
||||
@@ -58,70 +53,78 @@ describe('cliproxy routing strategy service', () => {
|
||||
}
|
||||
|
||||
it('normalizes canonical and shorthand strategy values', async () => {
|
||||
const mod = await loadRoutingModule();
|
||||
await withScopedConfig(async () => {
|
||||
const mod = await loadRoutingModule();
|
||||
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('round-robin')).toBe('round-robin');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('RR')).toBe('round-robin');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('fillfirst')).toBe('fill-first');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('ff')).toBe('fill-first');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('nope')).toBeNull();
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('round-robin')).toBe('round-robin');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('RR')).toBe('round-robin');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('fillfirst')).toBe('fill-first');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('ff')).toBe('fill-first');
|
||||
expect(mod.normalizeCliproxyRoutingStrategy('nope')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to the saved local default when live CLIProxy is unavailable', async () => {
|
||||
const { mutateUnifiedConfig } = await import('../../../src/config/unified-config-loader');
|
||||
mutateUnifiedConfig((config) => {
|
||||
if (config.cliproxy) {
|
||||
config.cliproxy.routing = { strategy: 'fill-first' };
|
||||
}
|
||||
await withScopedConfig(async () => {
|
||||
const { mutateUnifiedConfig } = await import('../../../src/config/unified-config-loader');
|
||||
mutateUnifiedConfig((config) => {
|
||||
if (config.cliproxy) {
|
||||
config.cliproxy.routing = { strategy: 'fill-first' };
|
||||
}
|
||||
});
|
||||
|
||||
const mod = await loadRoutingModule();
|
||||
const state = await mod.readCliproxyRoutingState();
|
||||
|
||||
expect(state.strategy).toBe('fill-first');
|
||||
expect(state.source).toBe('config');
|
||||
expect(state.target).toBe('local');
|
||||
expect(state.reachable).toBe(false);
|
||||
});
|
||||
|
||||
const mod = await loadRoutingModule();
|
||||
const state = await mod.readCliproxyRoutingState();
|
||||
|
||||
expect(state.strategy).toBe('fill-first');
|
||||
expect(state.source).toBe('config');
|
||||
expect(state.target).toBe('local');
|
||||
expect(state.reachable).toBe(false);
|
||||
});
|
||||
|
||||
it('persists the local startup default even when the live proxy is down', async () => {
|
||||
const mod = await loadRoutingModule();
|
||||
const result = await mod.applyCliproxyRoutingStrategy('fill-first');
|
||||
await withScopedConfig(async () => {
|
||||
const mod = await loadRoutingModule();
|
||||
const result = await mod.applyCliproxyRoutingStrategy('fill-first');
|
||||
|
||||
expect(result.applied).toBe('config-only');
|
||||
expect(result.strategy).toBe('fill-first');
|
||||
expect(result.applied).toBe('config-only');
|
||||
expect(result.strategy).toBe('fill-first');
|
||||
|
||||
const configPath = path.join(tempHome, '.ccs', 'cliproxy', 'config.yaml');
|
||||
const configContent = fs.readFileSync(configPath, 'utf8');
|
||||
expect(configContent).toContain('routing:');
|
||||
expect(configContent).toContain('strategy: fill-first');
|
||||
const configPath = path.join(scopedConfigDir, 'cliproxy', 'config.yaml');
|
||||
const configContent = fs.readFileSync(configPath, 'utf8');
|
||||
expect(configContent).toContain('routing:');
|
||||
expect(configContent).toContain('strategy: fill-first');
|
||||
});
|
||||
});
|
||||
|
||||
it('reads and writes remote strategy without mutating the local default', async () => {
|
||||
routingTarget = {
|
||||
host: 'remote.example.com',
|
||||
port: 8080,
|
||||
protocol: 'http',
|
||||
isRemote: true,
|
||||
};
|
||||
await withScopedConfig(async () => {
|
||||
routingTarget = {
|
||||
host: 'remote.example.com',
|
||||
port: 8080,
|
||||
protocol: 'http',
|
||||
isRemote: true,
|
||||
};
|
||||
|
||||
let methodCount = 0;
|
||||
responseFactory = async () => {
|
||||
methodCount += 1;
|
||||
return new Response(JSON.stringify({ strategy: 'fill-first' }), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
};
|
||||
let methodCount = 0;
|
||||
responseFactory = async () => {
|
||||
methodCount += 1;
|
||||
return new Response(JSON.stringify({ strategy: 'fill-first' }), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
};
|
||||
|
||||
const mod = await loadRoutingModule();
|
||||
const readState = await mod.readCliproxyRoutingState();
|
||||
const writeState = await mod.applyCliproxyRoutingStrategy('fill-first');
|
||||
const mod = await loadRoutingModule();
|
||||
const readState = await mod.readCliproxyRoutingState();
|
||||
const writeState = await mod.applyCliproxyRoutingStrategy('fill-first');
|
||||
|
||||
expect(readState.strategy).toBe('fill-first');
|
||||
expect(readState.target).toBe('remote');
|
||||
expect(writeState.applied).toBe('live');
|
||||
expect(mod.getConfiguredCliproxyRoutingStrategy()).toBe('round-robin');
|
||||
expect(methodCount).toBe(2);
|
||||
expect(readState.strategy).toBe('fill-first');
|
||||
expect(readState.target).toBe('remote');
|
||||
expect(writeState.applied).toBe('live');
|
||||
expect(mod.getConfiguredCliproxyRoutingStrategy()).toBe('round-robin');
|
||||
expect(methodCount).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user