MCPcopy Index your code
hub / github.com/BlockRunAI/ClawRouter / startMockServer

Function startMockServer

test/fallback.ts:27–126  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

25
26// Mock BlockRun API server
27async function startMockServer(): Promise<{ port: number; close: () => Promise<void> }> {
28 const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
29 const chunks: Buffer[] = [];
30 for await (const chunk of req) {
31 chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
32 }
33 const body = Buffer.concat(chunks).toString();
34
35 try {
36 const parsed = JSON.parse(body) as { model?: string; messages?: Array<{ content: string }> };
37 const model = parsed.model || "unknown";
38 modelCalls.push(model);
39
40 console.log(` [MockAPI] Request for model: ${model}`);
41
42 // Simulate a slow model (per-model timeout test)
43 if (slowModels.includes(model) && slowModelDelayMs > 0) {
44 await new Promise((resolve) => setTimeout(resolve, slowModelDelayMs));
45 }
46
47 // Simulate 429 rate-limit on first call, succeed on retry (stepped backoff test)
48 if (rateLimitOnceModels.has(model)) {
49 const attempts = rateLimitAttempts.get(model) ?? 0;
50 rateLimitAttempts.set(model, attempts + 1);
51 if (attempts === 0) {
52 res.writeHead(429, { "Content-Type": "application/json" });
53 res.end(
54 JSON.stringify({
55 error: { message: "Rate limit exceeded", type: "rate_limited" },
56 }),
57 );
58 return;
59 }
60 // Second call succeeds — fall through to normal success path
61 }
62
63 // Simulate an invalid explicit model name that should be surfaced to the caller
64 // instead of being silently converted into a free-model success.
65 if (model.startsWith("invalid/")) {
66 res.writeHead(400, { "Content-Type": "application/json" });
67 res.end(
68 JSON.stringify({
69 error: {
70 message: `Unknown model: ${model}. Available models: moonshot/kimi-k2.6, moonshot/kimi-k2.5, nvidia/gpt-oss-120b`,
71 type: "provider_error",
72 },
73 }),
74 );
75 return;
76 }
77
78 // Simulate provider error for models in failModels list (or all models)
79 if (failAllModels || failModels.includes(model)) {
80 console.log(` [MockAPI] Simulating billing error for ${model}`);
81 res.writeHead(400, { "Content-Type": "application/json" });
82 res.end(
83 JSON.stringify({
84 error: {

Callers 1

runTestsFunction · 0.70

Calls 3

getMethod · 0.80
setMethod · 0.80
closeMethod · 0.80

Tested by

no test coverage detected