Browse Source

test(smartfetch): stub retry delay and remove unused mock helper

Addresses @greptile-apps feedback:
- Extract _testConfig.deleteRetryDelayMs so tests can zero the delay
  instead of sleeping for real (~1.5s → ~12ms)
- Remove unused _deleteCallCount helper from mock client
dhaern 1 month ago
parent
commit
6711849db1

+ 7 - 2
src/tools/smartfetch/secondary-model.test.ts

@@ -1,5 +1,5 @@
 import { afterEach, describe, expect, mock, test } from 'bun:test';
-import { runSecondaryModelWithFallback } from './secondary-model';
+import { runSecondaryModelWithFallback, _testConfig } from './secondary-model';
 import type { SecondaryModel } from './types';
 
 type PromptStep = {
@@ -36,7 +36,6 @@ function createMockClient(steps: PromptStep[], deleteBehavior?: {
         }
         return {};
       }),
-      _deleteCallCount: () => deleteCallCount,
     },
     tool: {
       ids: mock(async () => ({ data: ['read', 'bash'] })),
@@ -98,6 +97,8 @@ describe('smartfetch/secondary-model', () => {
     const originalWarn = console.warn;
     const warnCalls: unknown[][] = [];
     console.warn = (...args: unknown[]) => warnCalls.push(args);
+    const originalDelay = _testConfig.deleteRetryDelayMs;
+    _testConfig.deleteRetryDelayMs = 0;
     try {
       const client = createMockClient(
         [{ text: 'Answer' }],
@@ -118,6 +119,7 @@ describe('smartfetch/secondary-model', () => {
       expect(warnCalls.length).toBe(0);
     } finally {
       console.warn = originalWarn;
+      _testConfig.deleteRetryDelayMs = originalDelay;
     }
   });
 
@@ -125,6 +127,8 @@ describe('smartfetch/secondary-model', () => {
     const originalWarn = console.warn;
     const warnCalls: unknown[][] = [];
     console.warn = (...args: unknown[]) => warnCalls.push(args);
+    const originalDelay = _testConfig.deleteRetryDelayMs;
+    _testConfig.deleteRetryDelayMs = 0;
     try {
       const client = createMockClient(
         [{ text: 'Answer' }],
@@ -145,6 +149,7 @@ describe('smartfetch/secondary-model', () => {
       expect(String(warnCalls[0][0])).toContain('smartfetch');
     } finally {
       console.warn = originalWarn;
+      _testConfig.deleteRetryDelayMs = originalDelay;
     }
   });
 

+ 9 - 1
src/tools/smartfetch/secondary-model.ts

@@ -160,6 +160,14 @@ const SESSION_DELETE_RETRIES = 3;
 const SESSION_DELETE_RETRY_DELAY_MS = 500;
 const SECONDARY_MODEL_TIMEOUT_MS = 30_000;
 
+/**
+ * Exposed for tests so they can avoid real wall-clock sleeps.
+ * Not part of the public API.
+ */
+export const _testConfig = {
+  deleteRetryDelayMs: SESSION_DELETE_RETRY_DELAY_MS,
+};
+
 /**
  * Delete a temporary secondary-model session with retry.
  *
@@ -191,7 +199,7 @@ async function deleteSessionSafely(
         return;
       }
       await new Promise((resolve) =>
-        setTimeout(resolve, SESSION_DELETE_RETRY_DELAY_MS),
+        setTimeout(resolve, _testConfig.deleteRetryDelayMs),
       );
     }
   }