From 540b36895fcd3d4ffc0b6f737460cc5fd4f252d0 Mon Sep 17 00:00:00 2001 From: NekoPunch Date: Fri, 18 Sep 2026 00:01:05 -0700 Subject: [PATCH] test(desktop): deflake Command Code browser login port release isPortFree previously called occupyPort, retaining an active net.Server in netServers until afterEach rather than probing and releasing immediately. Additionally, tests completing or settling attempts (notably the simultaneous starts test) did not await the winner's loopback port release before entering the next test, leading to EADDRINUSE races where the next test skipped the closing port and unexpectedly bound a higher port number. Generated-by: GitHub Copilot Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../commandcode-browser-login.test.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src/main/__tests__/commandcode-browser-login.test.ts b/apps/desktop/src/main/__tests__/commandcode-browser-login.test.ts index 0ecbed2d3b..eb714549d4 100644 --- a/apps/desktop/src/main/__tests__/commandcode-browser-login.test.ts +++ b/apps/desktop/src/main/__tests__/commandcode-browser-login.test.ts @@ -91,12 +91,15 @@ const APPROVED = { /** True when the port could be taken, i.e. no attempt is listening on it. */ async function isPortFree(port: number): Promise { for (let attempt = 0; attempt < 20; attempt += 1) { - try { - await occupyPort(port); - return true; - } catch { - await new Promise((resolve) => setTimeout(resolve, 10)); - } + const server = createNetServer(); + const free = await new Promise((resolve) => { + server.once('error', () => resolve(false)); + server.listen(port, '127.0.0.1', () => { + server.close(() => resolve(true)); + }); + }); + if (free) return true; + await new Promise((resolve) => setTimeout(resolve, 10)); } return false; } @@ -181,6 +184,7 @@ describe('CommandCodeBrowserLoginController', () => { reason: 'superseded', }); assert.deepEqual(await controller.complete('never-issued'), { ok: false, reason: 'superseded' }); + assert.equal(await isPortFree(started.port), true); }); test('a state mismatch is answered 403 and the attempt keeps waiting', async () => { @@ -197,6 +201,7 @@ describe('CommandCodeBrowserLoginController', () => { const approved = await post(started.callback, APPROVED); assert.equal(approved.status, 200); assert.equal((await completion).ok, true); + assert.equal(await isPortFree(started.port), true); }); test('a denial carrying the real state ends the attempt as denied', async () => { @@ -210,6 +215,7 @@ describe('CommandCodeBrowserLoginController', () => { }); assert.equal(response.status, 200); assert.deepEqual(await completion, { ok: false, reason: 'denied' }); + assert.equal(await isPortFree(started.port), true); }); test('the CORS preflight succeeds only for the Studio origins', async () => { @@ -297,6 +303,7 @@ describe('CommandCodeBrowserLoginController', () => { const approved = await post(started.callback, APPROVED); assert.equal(approved.status, 200); assert.equal((await completion).ok, true); + assert.equal(await isPortFree(started.port), true); }); test('the login window elapsing settles the attempt as timeout', async () => { @@ -307,6 +314,7 @@ describe('CommandCodeBrowserLoginController', () => { reason: 'timeout', }); await assert.rejects(post(started.callback, APPROVED)); + assert.equal(await isPortFree(started.port), true); }); test('cancel settles the attempt and releases the port', async () => { @@ -316,6 +324,7 @@ describe('CommandCodeBrowserLoginController', () => { controller.cancel(); assert.deepEqual(await completion, { ok: false, reason: 'cancelled' }); await assert.rejects(post(started.callback, APPROVED)); + assert.equal(await isPortFree(started.port), true); // Cancelling again, or a stranger's id, is a no-op. controller.cancel(started.attemptId); controller.cancel('unknown'); @@ -334,6 +343,7 @@ describe('CommandCodeBrowserLoginController', () => { const completion = controller.complete(second.attemptId); assert.equal((await post(second.callback, APPROVED)).status, 200); assert.equal((await completion).ok, true); + assert.equal(await isPortFree(second.port), true); }); test('two simultaneous starts leave one live attempt and free the loser\'s port', async () => { @@ -358,6 +368,7 @@ describe('CommandCodeBrowserLoginController', () => { const completion = controller.complete(winner.attemptId); assert.equal((await post(callback.toString(), APPROVED)).status, 200); assert.equal((await completion).ok, true); + assert.equal(await isPortFree(Number(callback.port)), true); }); test('an occupied port is skipped for the next one in the CLI range', async () => {