mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 12:15:57 +00:00
- Add centralized mock infrastructure in tests/mocks/: - mock-fetch.ts: Bun native fetch interception - mock-http-server.ts: Fake server responses (no ports) - fixtures/responses.ts: Preset response constants - types.ts: Mock type definitions - Refactor https-tunnel-proxy.test.ts: - Remove TEST_CERT constant (~50 lines) - Remove real HTTPS server creation - Use invalid hosts for error path testing - 26/26 tests pass in ~430ms - Refactor remote-token-uploader.test.ts: - Replace 7 real HTTP servers with mockFetch - Reduce from 422 to 289 lines - 18/18 tests pass in ~120ms - Add global test timeout (10s) in bunfig.toml Test suite: 10+ min → 14 seconds
138 lines
3.7 KiB
TypeScript
138 lines
3.7 KiB
TypeScript
/**
|
|
* Preset Mock Responses
|
|
*
|
|
* Common response fixtures for CCS CLI tests.
|
|
* Import these instead of duplicating response shapes.
|
|
*/
|
|
|
|
import type { MockResponse, MockFetchHandler } from '../types';
|
|
|
|
// ============================================================================
|
|
// CLIProxy API Responses
|
|
// ============================================================================
|
|
|
|
/** Successful health check response */
|
|
export const HEALTH_OK: MockResponse = {
|
|
status: 200,
|
|
body: { healthy: true, version: '1.0.0' },
|
|
};
|
|
|
|
/** Failed health check response */
|
|
export const HEALTH_FAIL: MockResponse = {
|
|
status: 503,
|
|
body: { healthy: false, error: 'Service unavailable' },
|
|
};
|
|
|
|
/** Successful file upload response */
|
|
export const UPLOAD_SUCCESS: MockResponse = {
|
|
status: 200,
|
|
body: { status: 'ok', id: 'uploaded-123' },
|
|
};
|
|
|
|
/** Unauthorized response */
|
|
export const UNAUTHORIZED: MockResponse = {
|
|
status: 401,
|
|
body: { error: 'Unauthorized' },
|
|
};
|
|
|
|
/** Forbidden response */
|
|
export const FORBIDDEN: MockResponse = {
|
|
status: 403,
|
|
body: { error: 'Forbidden' },
|
|
};
|
|
|
|
/** Not found response */
|
|
export const NOT_FOUND: MockResponse = {
|
|
status: 404,
|
|
body: { error: 'Not Found' },
|
|
};
|
|
|
|
/** Internal server error response */
|
|
export const SERVER_ERROR: MockResponse = {
|
|
status: 500,
|
|
body: { error: 'Internal Server Error' },
|
|
};
|
|
|
|
// ============================================================================
|
|
// Remote Proxy Responses
|
|
// ============================================================================
|
|
|
|
/** Remote proxy connection success */
|
|
export const REMOTE_PROXY_OK: MockResponse = {
|
|
status: 200,
|
|
body: { connected: true, latency: 50 },
|
|
};
|
|
|
|
/** Remote proxy connection timeout */
|
|
export const REMOTE_PROXY_TIMEOUT: MockResponse = {
|
|
status: 408,
|
|
body: { error: 'Request Timeout' },
|
|
delay: 100,
|
|
};
|
|
|
|
// ============================================================================
|
|
// Token Upload Responses
|
|
// ============================================================================
|
|
|
|
/** Token upload success */
|
|
export const TOKEN_UPLOAD_OK: MockResponse = {
|
|
status: 200,
|
|
body: { status: 'ok', id: 'token-456', type: 'gemini' },
|
|
};
|
|
|
|
/** Token upload conflict (already exists) */
|
|
export const TOKEN_UPLOAD_CONFLICT: MockResponse = {
|
|
status: 409,
|
|
body: { error: 'Token already exists', id: 'existing-789' },
|
|
};
|
|
|
|
// ============================================================================
|
|
// Mock Fetch Handler Presets
|
|
// ============================================================================
|
|
|
|
/** Health endpoint handler */
|
|
export const HEALTH_HANDLER: MockFetchHandler = {
|
|
url: /\/health$/,
|
|
method: 'GET',
|
|
response: { healthy: true },
|
|
};
|
|
|
|
/** Upload endpoint handler */
|
|
export const UPLOAD_HANDLER: MockFetchHandler = {
|
|
url: /\/v0\/management\/auth-files/,
|
|
method: 'POST',
|
|
response: { status: 'ok', id: 'uploaded-123' },
|
|
status: 200,
|
|
};
|
|
|
|
/** Messages endpoint handler (Claude API mock) */
|
|
export const MESSAGES_HANDLER: MockFetchHandler = {
|
|
url: /\/v1\/messages/,
|
|
method: 'POST',
|
|
response: {
|
|
id: 'msg-123',
|
|
type: 'message',
|
|
role: 'assistant',
|
|
content: [{ type: 'text', text: 'Mock response' }],
|
|
},
|
|
status: 200,
|
|
};
|
|
|
|
// ============================================================================
|
|
// Helper Functions
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Create a delayed response for timeout testing
|
|
*/
|
|
export function createDelayedResponse(base: MockResponse, delayMs: number): MockResponse {
|
|
return { ...base, delay: delayMs };
|
|
}
|
|
|
|
/**
|
|
* Create error response with custom message
|
|
*/
|
|
export function createErrorResponse(status: number, message: string): MockResponse {
|
|
return { status, body: { error: message } };
|
|
}
|