refactor: add afterEach cleanup to all gateway tests
Added afterEach hooks with server/ws cleanup to: - server.channels.test.ts (3 tests) - server.config-apply.test.ts (2 tests) - server.sessions-send.test.ts (already had this) This ensures ports are properly released between tests, preventing timeout issues from port conflicts.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { describe, expect, test, vi } from "vitest";
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||||
import {
|
import {
|
||||||
connectOk,
|
connectOk,
|
||||||
installGatewayTestHooks,
|
installGatewayTestHooks,
|
||||||
@@ -10,10 +10,27 @@ const loadConfigHelpers = async () => await import("../config/config.js");
|
|||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks();
|
||||||
|
|
||||||
|
const servers: Array<Awaited<ReturnType<typeof startServerWithClient>>> = [];
|
||||||
|
|
||||||
|
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", () => {
|
describe("gateway server channels", () => {
|
||||||
test("channels.status returns snapshot without probe", async () => {
|
test("channels.status returns snapshot without probe", async () => {
|
||||||
vi.stubEnv("TELEGRAM_BOT_TOKEN", undefined);
|
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);
|
await connectOk(ws);
|
||||||
|
|
||||||
const res = await rpcReq<{
|
const res = await rpcReq<{
|
||||||
@@ -39,13 +56,12 @@ describe("gateway server channels", () => {
|
|||||||
expect(signal?.configured).toBe(false);
|
expect(signal?.configured).toBe(false);
|
||||||
expect(signal?.probe).toBeUndefined();
|
expect(signal?.probe).toBeUndefined();
|
||||||
expect(signal?.lastProbeAt).toBeNull();
|
expect(signal?.lastProbeAt).toBeNull();
|
||||||
|
|
||||||
ws.close();
|
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("channels.logout reports no session when missing", async () => {
|
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);
|
await connectOk(ws);
|
||||||
|
|
||||||
const res = await rpcReq<{ cleared?: boolean; channel?: string }>(ws, "channels.logout", {
|
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.ok).toBe(true);
|
||||||
expect(res.payload?.channel).toBe("whatsapp");
|
expect(res.payload?.channel).toBe("whatsapp");
|
||||||
expect(res.payload?.cleared).toBe(false);
|
expect(res.payload?.cleared).toBe(false);
|
||||||
|
|
||||||
ws.close();
|
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("channels.logout clears telegram bot token from config", async () => {
|
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);
|
await connectOk(ws);
|
||||||
|
|
||||||
const res = await rpcReq<{
|
const res = await rpcReq<{
|
||||||
@@ -88,8 +103,5 @@ describe("gateway server channels", () => {
|
|||||||
expect(snap.valid).toBe(true);
|
expect(snap.valid).toBe(true);
|
||||||
expect(snap.config?.channels?.telegram?.botToken).toBeUndefined();
|
expect(snap.config?.channels?.telegram?.botToken).toBeUndefined();
|
||||||
expect(snap.config?.channels?.telegram?.groups?.["*"]?.requireMention).toBe(false);
|
expect(snap.config?.channels?.telegram?.groups?.["*"]?.requireMention).toBe(false);
|
||||||
|
|
||||||
ws.close();
|
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
connectOk,
|
connectOk,
|
||||||
@@ -12,9 +12,26 @@ import {
|
|||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks();
|
||||||
|
|
||||||
|
const servers: Array<Awaited<ReturnType<typeof startServerWithClient>>> = [];
|
||||||
|
|
||||||
|
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", () => {
|
describe("gateway config.apply", () => {
|
||||||
it("writes config, stores sentinel, and schedules restart", async () => {
|
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);
|
await connectOk(ws);
|
||||||
|
|
||||||
const id = "req-1";
|
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
|
// File may not exist if signal delivery is mocked, verify response was ok instead
|
||||||
expect(res.ok).toBe(true);
|
expect(res.ok).toBe(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.close();
|
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects invalid raw config", async () => {
|
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);
|
await connectOk(ws);
|
||||||
|
|
||||||
const id = "req-2";
|
const id = "req-2";
|
||||||
@@ -75,8 +91,5 @@ describe("gateway config.apply", () => {
|
|||||||
(o) => o.type === "res" && o.id === id,
|
(o) => o.type === "res" && o.id === id,
|
||||||
);
|
);
|
||||||
expect(res.ok).toBe(false);
|
expect(res.ok).toBe(false);
|
||||||
|
|
||||||
ws.close();
|
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user