Skip to content

Commit 40e2c07

Browse files
committed
🤖 refactor: DRY up sendMessage tests with shared workspace helper
1 parent baada8f commit 40e2c07

File tree

6 files changed

+120
-278
lines changed

6 files changed

+120
-278
lines changed

tests/ipcMain/sendMessage.basic.test.ts

Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import {
1818
readChatHistory,
1919
TEST_IMAGES,
2020
modelString,
21-
createTempGitRepo,
22-
cleanupTempGitRepo,
2321
configureTestRetries,
2422
} from "./helpers";
23+
import { createSharedRepo, cleanupSharedRepo, withSharedWorkspace } from "./sendMessageTestHelpers";
2524
import type { StreamDeltaEvent } from "../../src/common/types/stream";
2625
import { IPC_CHANNELS } from "../../src/common/constants/ipc-constants";
2726

@@ -47,17 +46,8 @@ const PROVIDER_CONFIGS: Array<[string, string]> = [
4746
// - Longer running tests (tool calls, multiple edits) can take up to 30s
4847
// - Test timeout values (in describe/test) should be 2-3x the expected duration
4948

50-
let sharedRepoPath: string;
51-
52-
beforeAll(async () => {
53-
sharedRepoPath = await createTempGitRepo();
54-
});
55-
56-
afterAll(async () => {
57-
if (sharedRepoPath) {
58-
await cleanupTempGitRepo(sharedRepoPath);
59-
}
60-
});
49+
beforeAll(createSharedRepo);
50+
afterAll(cleanupSharedRepo);
6151
describeIntegration("IpcMain sendMessage integration tests", () => {
6252
configureTestRetries(3);
6353

@@ -66,13 +56,7 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
6656
test.concurrent(
6757
"should successfully send message and receive response",
6858
async () => {
69-
// Setup test environment
70-
const { env, workspaceId, cleanup } = await setupWorkspace(
71-
provider,
72-
undefined,
73-
sharedRepoPath
74-
);
75-
try {
59+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
7660
// Send a simple message
7761
const result = await sendMessageWithModel(
7862
env.mockIpcRenderer,
@@ -94,23 +78,15 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
9478
// Verify we received deltas
9579
const deltas = collector.getDeltas();
9680
expect(deltas.length).toBeGreaterThan(0);
97-
} finally {
98-
await cleanup();
99-
}
81+
});
10082
},
10183
15000
10284
);
10385

10486
test.concurrent(
10587
"should interrupt streaming with interruptStream()",
10688
async () => {
107-
// Setup test environment
108-
const { env, workspaceId, cleanup } = await setupWorkspace(
109-
provider,
110-
undefined,
111-
sharedRepoPath
112-
);
113-
try {
89+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
11490
// Start a long-running stream with a bash command that takes time
11591
const longMessage = "Run this bash command: while true; do sleep 1; done";
11692
void sendMessageWithModel(
@@ -144,23 +120,15 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
144120
}, 5000);
145121

146122
expect(abortOrEndReceived).toBe(true);
147-
} finally {
148-
await cleanup();
149-
}
123+
});
150124
},
151125
15000
152126
);
153127

154128
test.concurrent(
155129
"should interrupt stream with pending bash tool call near-instantly",
156130
async () => {
157-
// Setup test environment
158-
const { env, workspaceId, cleanup } = await setupWorkspace(
159-
provider,
160-
undefined,
161-
sharedRepoPath
162-
);
163-
try {
131+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
164132
// Ask the model to run a long-running bash command
165133
// Use explicit instruction to ensure tool call happens
166134
const message = "Use the bash tool to run: sleep 60";
@@ -208,23 +176,15 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
208176
}, 5000);
209177

210178
expect(abortOrEndReceived).toBe(true);
211-
} finally {
212-
await cleanup();
213-
}
179+
});
214180
},
215181
25000
216182
);
217183

218184
test.concurrent(
219185
"should include tokens and timestamp in delta events",
220186
async () => {
221-
// Setup test environment
222-
const { env, workspaceId, cleanup } = await setupWorkspace(
223-
provider,
224-
undefined,
225-
sharedRepoPath
226-
);
227-
try {
187+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
228188
// Send a message that will generate text deltas
229189
// Disable reasoning for this test to avoid flakiness and encrypted content issues in CI
230190
void sendMessageWithModel(
@@ -287,23 +247,15 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
287247

288248
// Verify stream completed successfully
289249
assertStreamSuccess(collector);
290-
} finally {
291-
await cleanup();
292-
}
250+
});
293251
},
294252
30000 // Increased timeout for OpenAI models which can be slower in CI
295253
);
296254

297255
test.concurrent(
298256
"should include usage data in stream-abort events",
299257
async () => {
300-
// Setup test environment
301-
const { env, workspaceId, cleanup } = await setupWorkspace(
302-
provider,
303-
undefined,
304-
sharedRepoPath
305-
);
306-
try {
258+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
307259
// Start a stream that will generate some tokens
308260
const message = "Write a haiku about coding";
309261
void sendMessageWithModel(
@@ -353,9 +305,7 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
353305
expect(abortEvent.metadata.usage.outputTokens).toBeGreaterThanOrEqual(0);
354306
}
355307
}
356-
} finally {
357-
await cleanup();
358-
}
308+
});
359309
},
360310
15000
361311
);
@@ -368,12 +318,7 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
368318
return;
369319
}
370320

371-
const { env, workspaceId, cleanup } = await setupWorkspace(
372-
provider,
373-
undefined,
374-
sharedRepoPath
375-
);
376-
try {
321+
await withSharedWorkspace(provider, async ({ env, workspaceId }) => {
377322
// Start a stream with tool call that takes a long time
378323
void sendMessageWithModel(
379324
env.mockIpcRenderer,
@@ -456,9 +401,7 @@ describeIntegration("IpcMain sendMessage integration tests", () => {
456401

457402
// Note: If test completes quickly (~5s), abort signal worked and killed the loop
458403
// If test takes much longer, abort signal didn't work
459-
} finally {
460-
await cleanup();
461-
}
404+
});
462405
},
463406
15000
464407
);

0 commit comments

Comments
 (0)