Skip to content

Commit 5efab82

Browse files
committed
test: cover prompt cache zero-request guard
1 parent cf2e6a9 commit 5efab82

2 files changed

Lines changed: 45 additions & 27 deletions

File tree

index.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,21 +2331,22 @@ while (attempted.size < Math.max(1, accountCount)) {
23312331
}
23322332

23332333
try {
2334-
// Request metrics are tracked at the fetch boundary, so retries and account
2335-
// rotation are counted consistently. This is in-memory only: no filesystem I/O,
2336-
// no token persistence, and prompt-cache keys stay redacted in doctor output.
2337-
runtimeMetrics.totalRequests++;
2338-
if (promptCacheKey) {
2339-
runtimeMetrics.promptCacheEnabledRequests++;
2340-
} else {
2341-
runtimeMetrics.promptCacheMissingRequests++;
2342-
}
2343-
response = await fetch(url, {
2344-
...requestInit,
2345-
headers,
2334+
// Request metrics are tracked at the fetch boundary, so retries and
2335+
// account rotation are counted consistently. These increments are
2336+
// in-memory only and run on Node's single-threaded event loop, so no
2337+
// filesystem locking or token-redaction concerns are introduced here.
2338+
runtimeMetrics.totalRequests++;
2339+
if (promptCacheKey) {
2340+
runtimeMetrics.promptCacheEnabledRequests++;
2341+
} else {
2342+
runtimeMetrics.promptCacheMissingRequests++;
2343+
}
2344+
response = await fetch(url, {
2345+
...requestInit,
2346+
headers,
23462347
signal: fetchController.signal,
23472348
});
2348-
} catch (networkError) {
2349+
} catch (networkError) {
23492350
const errorMsg = networkError instanceof Error ? networkError.message : String(networkError);
23502351
logWarn(`Network error for account ${account.index + 1}: ${errorMsg}`);
23512352
if (
@@ -2378,21 +2379,21 @@ while (attempted.size < Math.max(1, accountCount)) {
23782379
accountManager.refundToken(account, modelFamily, model);
23792380
accountManager.recordFailure(account, modelFamily, model);
23802381
break;
2381-
} finally {
2382-
clearTimeout(fetchTimeoutId);
2383-
if (abortSignal && onUserAbort) {
2384-
abortSignal.removeEventListener("abort", onUserAbort);
2385-
}
2382+
} finally {
2383+
clearTimeout(fetchTimeoutId);
2384+
if (abortSignal && onUserAbort) {
2385+
abortSignal.removeEventListener("abort", onUserAbort);
23862386
}
2387-
const fetchLatencyMs = Math.round(performance.now() - fetchStart);
2388-
2389-
logRequest(LOG_STAGES.RESPONSE, {
2390-
status: response.status,
2391-
ok: response.ok,
2392-
statusText: response.statusText,
2393-
latencyMs: fetchLatencyMs,
2394-
headers: Object.fromEntries(response.headers.entries()),
2395-
});
2387+
}
2388+
const fetchLatencyMs = Math.round(performance.now() - fetchStart);
2389+
2390+
logRequest(LOG_STAGES.RESPONSE, {
2391+
status: response.status,
2392+
ok: response.ok,
2393+
statusText: response.statusText,
2394+
latencyMs: fetchLatencyMs,
2395+
headers: Object.fromEntries(response.headers.entries()),
2396+
});
23962397

23972398
if (!response.ok) {
23982399
const contextOverflowResult = await handleContextOverflow(response, model);

test/beginner-ui.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ describe("buildBeginnerDoctorFindings", () => {
154154

155155
expect(findings.some((f) => f.code === "prompt-cache-inconsistent")).toBe(true);
156156
});
157+
158+
it("does not flag cache issues when no requests have been made", () => {
159+
const findings = buildBeginnerDoctorFindings({
160+
accounts: [buildAccount()],
161+
now,
162+
runtime: {
163+
...healthyRuntime,
164+
totalRequests: 0,
165+
promptCacheEnabledRequests: 0,
166+
promptCacheMissingRequests: 3,
167+
lastPromptCacheKey: null,
168+
},
169+
});
170+
171+
expect(findings.some((f) => f.code === "prompt-cache-missing")).toBe(false);
172+
expect(findings.some((f) => f.code === "prompt-cache-inconsistent")).toBe(false);
173+
});
157174
});
158175

159176
describe("recommendBeginnerNextAction", () => {

0 commit comments

Comments
 (0)