From 629fd1652d84d64f10a96ef9022a88425b2caf1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Guti=C3=A9rrez?= <35082514+alezmad@users.noreply.github.com> Date: Tue, 8 Sep 2026 23:21:53 +0100 Subject: [PATCH] test(cli): de-flake close-flush assertion (poll for the server close event) Co-Authored-By: Claude Fable 5.1 --- apps/cli/tests/unit/ws-lifecycle.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/cli/tests/unit/ws-lifecycle.test.ts b/apps/cli/tests/unit/ws-lifecycle.test.ts index bacffcb..3ff5c58 100644 --- a/apps/cli/tests/unit/ws-lifecycle.test.ts +++ b/apps/cli/tests/unit/ws-lifecycle.test.ts @@ -200,10 +200,16 @@ describe("createWsLifecycle — close() flushes the close handshake (spec addend }); const { handle } = makeClient(broker.url); await handle.ready; + const t0 = Date.now(); await handle.close(); - // The broker side has already seen the close frame when close() resolves. + // close() waited for the handshake: the client socket is fully CLOSED + // (not merely CLOSING) and the broker's own close event lands within a + // few ms — well inside CLOSE_FLUSH_MS, not after a process.exit. + expect(handle.ws?.readyState).toBe(3 /* CLOSED */); + for (let i = 0; i < 30 && serverCloses.length === 0; i++) await sleep(10); expect(serverCloses).toHaveLength(1); expect(serverCloses[0]).toBe(1005); // close() without a code → no status + expect(Date.now() - t0).toBeLessThan(1_000); expect(handle.status).toBe("closed"); }); });