Skip to content

Commit e4b4643

Browse files
JohnMcLearclaude
andcommitted
test: address Qodo review on smoke test
- Guard close() call (client?.close?.()): connect() assigns close only after its async bootstrap resolves, so the method may be undefined when the finally-block cleanup runs. - Clear the withTimeout setTimeout in a finally so a fast-resolving promise no longer leaves a dangling timer keeping the test process alive. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent de9d8bc commit e4b4643

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

test/smoke.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,21 @@ const apiCall = async (
4949
return res.body as {code: number; message: string; data: unknown};
5050
};
5151

52-
const withTimeout = <T>(p: Promise<T>, ms: number, label: string): Promise<T> =>
53-
Promise.race([
54-
p,
55-
new Promise<T>((_, reject) =>
56-
setTimeout(() => reject(new Error(`timeout: ${label}`)), ms)),
57-
]);
52+
const withTimeout = async <T>(p: Promise<T>, ms: number, label: string): Promise<T> => {
53+
let timer: ReturnType<typeof setTimeout> | undefined;
54+
try {
55+
return await Promise.race([
56+
p,
57+
new Promise<T>((_, reject) => {
58+
timer = setTimeout(() => reject(new Error(`timeout: ${label}`)), ms);
59+
}),
60+
]);
61+
} finally {
62+
// Clear the pending timer so a fast-resolving promise doesn't leave a
63+
// dangling timeout keeping the test process alive until it fires.
64+
if (timer) clearTimeout(timer);
65+
}
66+
};
5867

5968
test('live wire round-trip via USER_CHANGES', async (t) => {
6069
if (!API_KEY) {
@@ -108,7 +117,10 @@ test('live wire round-trip via USER_CHANGES', async (t) => {
108117

109118
assert.ok(roundTripped, `appended text did not round-trip; pad contained: ${JSON.stringify(lastText)}`);
110119
} finally {
111-
client?.close();
120+
// connect() assigns close() only after its async bootstrap resolves, so
121+
// the method may still be undefined here — guard the call itself, not just
122+
// the (always-defined) client reference.
123+
client?.close?.();
112124
// Best-effort cleanup.
113125
try {
114126
await apiCall('deletePad', {padID: padId});

0 commit comments

Comments
 (0)