diff --git a/src/gateway/server.channels.test.ts b/src/gateway/server.channels.test.ts index bb30f4a45..4d0a969ce 100644 --- a/src/gateway/server.channels.test.ts +++ b/src/gateway/server.channels.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test, vi } from "vitest"; +import { afterEach, describe, expect, test, vi } from "vitest"; import { connectOk, installGatewayTestHooks, @@ -10,10 +10,27 @@ const loadConfigHelpers = async () => await import("../config/config.js"); installGatewayTestHooks(); +const servers: Array>> = []; + +afterEach(async () => { + for (const { server, ws } of servers) { + try { + ws.close(); + await server.close(); + } catch { + /* ignore */ + } + } + servers.length = 0; + await new Promise((resolve) => setTimeout(resolve, 50)); +}); + describe("gateway server channels", () => { test("channels.status returns snapshot without probe", async () => { vi.stubEnv("TELEGRAM_BOT_TOKEN", undefined); - const { server, ws } = await startServerWithClient(); + const result = await startServerWithClient(); + servers.push(result); + const { server, ws } = result; await connectOk(ws); const res = await rpcReq<{ @@ -39,13 +56,12 @@ describe("gateway server channels", () => { expect(signal?.configured).toBe(false); expect(signal?.probe).toBeUndefined(); expect(signal?.lastProbeAt).toBeNull(); - - ws.close(); - await server.close(); }); test("channels.logout reports no session when missing", async () => { - const { server, ws } = await startServerWithClient(); + const result = await startServerWithClient(); + servers.push(result); + const { server, ws } = result; await connectOk(ws); const res = await rpcReq<{ cleared?: boolean; channel?: string }>(ws, "channels.logout", { @@ -54,9 +70,6 @@ describe("gateway server channels", () => { expect(res.ok).toBe(true); expect(res.payload?.channel).toBe("whatsapp"); expect(res.payload?.cleared).toBe(false); - - ws.close(); - await server.close(); }); test("channels.logout clears telegram bot token from config", async () => { @@ -71,7 +84,9 @@ describe("gateway server channels", () => { }, }); - const { server, ws } = await startServerWithClient(); + const result = await startServerWithClient(); + servers.push(result); + const { server, ws } = result; await connectOk(ws); const res = await rpcReq<{ @@ -88,8 +103,5 @@ describe("gateway server channels", () => { expect(snap.valid).toBe(true); expect(snap.config?.channels?.telegram?.botToken).toBeUndefined(); expect(snap.config?.channels?.telegram?.groups?.["*"]?.requireMention).toBe(false); - - ws.close(); - await server.close(); }); }); diff --git a/src/gateway/server.config-apply.test.ts b/src/gateway/server.config-apply.test.ts index 0685bd4f1..af159ea18 100644 --- a/src/gateway/server.config-apply.test.ts +++ b/src/gateway/server.config-apply.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { connectOk, @@ -12,9 +12,26 @@ import { installGatewayTestHooks(); +const servers: Array>> = []; + +afterEach(async () => { + for (const { server, ws } of servers) { + try { + ws.close(); + await server.close(); + } catch { + /* ignore */ + } + } + servers.length = 0; + await new Promise((resolve) => setTimeout(resolve, 50)); +}); + describe("gateway config.apply", () => { it("writes config, stores sentinel, and schedules restart", async () => { - const { server, ws } = await startServerWithClient(); + const result = await startServerWithClient(); + servers.push(result); + const { server, ws } = result; await connectOk(ws); const id = "req-1"; @@ -50,13 +67,12 @@ describe("gateway config.apply", () => { // File may not exist if signal delivery is mocked, verify response was ok instead expect(res.ok).toBe(true); } - - ws.close(); - await server.close(); }); it("rejects invalid raw config", async () => { - const { server, ws } = await startServerWithClient(); + const result = await startServerWithClient(); + servers.push(result); + const { server, ws } = result; await connectOk(ws); const id = "req-2"; @@ -75,8 +91,5 @@ describe("gateway config.apply", () => { (o) => o.type === "res" && o.id === id, ); expect(res.ok).toBe(false); - - ws.close(); - await server.close(); }); });