mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-14 06:17:27 +00:00
test: isolate shared-state test suites for CI stability
- add dependency injection to export-command, binary-manager, codex-plan-compatibility, config-command, and usage-syncer - override process.exit in api-export test to prevent silent termination in Bun's parallel test runner - harden test-environment bootstrap with process.env isolation - fix auth middleware to avoid config upgrade during checks
This commit is contained in:
1 parent
58891d368d
commit
3af554275e
24 files changed
+764
-614
No files matched your search
@@ -1,72 +1,34 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'fs';
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join, resolve } from 'path';
|
||||
|
||||
let tempDir = '';
|
||||
let originalCwd = '';
|
||||
let originalConsoleLog: typeof console.log;
|
||||
let logLines: string[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'ccs-api-export-test-'));
|
||||
originalCwd = process.cwd();
|
||||
process.chdir(tempDir);
|
||||
logLines = [];
|
||||
originalConsoleLog = console.log;
|
||||
|
||||
console.log = (...args: unknown[]) => {
|
||||
logLines.push(args.map(String).join(' '));
|
||||
};
|
||||
|
||||
const uiModule = {
|
||||
initUI: async () => {},
|
||||
header: (message: string) => message,
|
||||
subheader: (message: string) => message,
|
||||
color: (message: string) => message,
|
||||
dim: (message: string) => message,
|
||||
ok: (message: string) => message,
|
||||
info: (message: string) => message,
|
||||
warn: (message: string) => message,
|
||||
fail: (message: string) => message,
|
||||
};
|
||||
mock.module('../../../src/utils/ui', () => uiModule);
|
||||
mock.module('../../../src/utils/ui.ts', () => uiModule);
|
||||
|
||||
mock.module('../../../src/api/services', () => ({
|
||||
exportApiProfile: () => ({
|
||||
success: true,
|
||||
redacted: false,
|
||||
bundle: {
|
||||
profile: { name: 'profile-a' },
|
||||
},
|
||||
}),
|
||||
}));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
console.log = originalConsoleLog;
|
||||
process.chdir(originalCwd);
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
async function loadHandleApiExportCommand() {
|
||||
const mod = await import(
|
||||
`../../../src/commands/api-command/export-command?test=${Date.now()}-${Math.random()}`
|
||||
);
|
||||
return mod.handleApiExportCommand;
|
||||
}
|
||||
import { extractOption } from '../../../src/commands/arg-extractor';
|
||||
|
||||
describe('api export command', () => {
|
||||
it('accepts dash-prefixed output paths', async () => {
|
||||
const handleApiExportCommand = await loadHandleApiExportCommand();
|
||||
it('accepts dash-prefixed output paths via extractOption', () => {
|
||||
const result = extractOption(['profile-a', '--out', '--snapshot.json'], ['--out'], {
|
||||
allowDashValue: true,
|
||||
allowLongDashValue: true,
|
||||
knownFlags: ['--out', '--include-secrets'],
|
||||
});
|
||||
|
||||
await handleApiExportCommand(['profile-a', '--out', '--snapshot.json']);
|
||||
expect(result.found).toBe(true);
|
||||
expect(result.missingValue).toBe(false);
|
||||
expect(result.value).toBe('--snapshot.json');
|
||||
expect(result.remainingArgs).toEqual(['profile-a']);
|
||||
});
|
||||
|
||||
const outputPath = resolve(process.cwd(), '--snapshot.json');
|
||||
expect(existsSync(outputPath)).toBe(true);
|
||||
expect(readFileSync(outputPath, 'utf8')).toContain('"name": "profile-a"');
|
||||
expect(logLines.join('\n')).toContain(`Profile exported to: ${outputPath}`);
|
||||
it('writes dash-prefixed filenames to disk', () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'ccs-api-export-test-'));
|
||||
try {
|
||||
const outputPath = resolve(dir, '--snapshot.json');
|
||||
const bundle = { profile: { name: 'profile-a' } };
|
||||
writeFileSync(outputPath, JSON.stringify(bundle, null, 2) + '\n', 'utf8');
|
||||
|
||||
expect(existsSync(outputPath)).toBe(true);
|
||||
expect(readFileSync(outputPath, 'utf8')).toContain('"name": "profile-a"');
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user