diff --git a/src/gateway/channel-health-monitor.test.ts b/src/gateway/channel-health-monitor.test.ts index 6f7c8104874..32052af5745 100644 --- a/src/gateway/channel-health-monitor.test.ts +++ b/src/gateway/channel-health-monitor.test.ts @@ -106,6 +106,24 @@ function createSlackSnapshotManager( ); } +function createBusyDisconnectedManager(lastRunActivityAt: number): ChannelManager { + const now = Date.now(); + return createSnapshotManager({ + discord: { + default: { + running: true, + connected: false, + enabled: true, + configured: true, + lastStartAt: now - 300_000, + activeRuns: 1, + busy: true, + lastRunActivityAt, + }, + }, + }); +} + async function expectRestartedChannel( manager: ChannelManager, channel: ChannelId, @@ -250,39 +268,13 @@ describe("channel-health-monitor", () => { it("restarts busy channels when run activity is stale", async () => { const now = Date.now(); - const manager = createSnapshotManager({ - discord: { - default: { - running: true, - connected: false, - enabled: true, - configured: true, - lastStartAt: now - 300_000, - activeRuns: 1, - busy: true, - lastRunActivityAt: now - 26 * 60_000, - }, - }, - }); + const manager = createBusyDisconnectedManager(now - 26 * 60_000); await expectRestartedChannel(manager, "discord"); }); it("restarts disconnected channels when busy flags are inherited from a prior lifecycle", async () => { const now = Date.now(); - const manager = createSnapshotManager({ - discord: { - default: { - running: true, - connected: false, - enabled: true, - configured: true, - lastStartAt: now - 300_000, - activeRuns: 1, - busy: true, - lastRunActivityAt: now - 301_000, - }, - }, - }); + const manager = createBusyDisconnectedManager(now - 301_000); await expectRestartedChannel(manager, "discord"); }); diff --git a/src/gateway/channel-health-policy.test.ts b/src/gateway/channel-health-policy.test.ts index 0a2c34604fa..359190cd8c6 100644 --- a/src/gateway/channel-health-policy.test.ts +++ b/src/gateway/channel-health-policy.test.ts @@ -1,6 +1,19 @@ import { describe, expect, it } from "vitest"; import { evaluateChannelHealth, resolveChannelRestartReason } from "./channel-health-policy.js"; +function evaluateDiscordHealth( + account: Record, + now = 100_000, + channelId = "discord", +) { + return evaluateChannelHealth(account, { + channelId, + now, + channelConnectGraceMs: 10_000, + staleEventThresholdMs: 30_000, + }); +} + describe("evaluateChannelHealth", () => { it("treats disabled accounts as healthy unmanaged", () => { const evaluation = evaluateChannelHealth( @@ -144,48 +157,32 @@ describe("evaluateChannelHealth", () => { }); it("skips stale-socket detection for channels in webhook mode", () => { - const evaluation = evaluateChannelHealth( - { - running: true, - connected: true, - enabled: true, - configured: true, - lastStartAt: 0, - lastEventAt: 0, - mode: "webhook", - }, - { - channelId: "discord", - now: 100_000, - channelConnectGraceMs: 10_000, - staleEventThresholdMs: 30_000, - }, - ); + const evaluation = evaluateDiscordHealth({ + running: true, + connected: true, + enabled: true, + configured: true, + lastStartAt: 0, + lastEventAt: 0, + mode: "webhook", + }); expect(evaluation).toEqual({ healthy: true, reason: "healthy" }); }); it("does not flag stale sockets for channels without event tracking", () => { - const evaluation = evaluateChannelHealth( - { - running: true, - connected: true, - enabled: true, - configured: true, - lastStartAt: 0, - lastEventAt: null, - }, - { - channelId: "discord", - now: 100_000, - channelConnectGraceMs: 10_000, - staleEventThresholdMs: 30_000, - }, - ); + const evaluation = evaluateDiscordHealth({ + running: true, + connected: true, + enabled: true, + configured: true, + lastStartAt: 0, + lastEventAt: null, + }); expect(evaluation).toEqual({ healthy: true, reason: "healthy" }); }); it("does not flag stale sockets without an active connected socket", () => { - const evaluation = evaluateChannelHealth( + const evaluation = evaluateDiscordHealth( { running: true, enabled: true, @@ -193,18 +190,14 @@ describe("evaluateChannelHealth", () => { lastStartAt: 0, lastEventAt: 0, }, - { - channelId: "slack", - now: 75_000, - channelConnectGraceMs: 10_000, - staleEventThresholdMs: 30_000, - }, + 75_000, + "slack", ); expect(evaluation).toEqual({ healthy: true, reason: "healthy" }); }); it("ignores inherited event timestamps from a previous lifecycle", () => { - const evaluation = evaluateChannelHealth( + const evaluation = evaluateDiscordHealth( { running: true, connected: true, @@ -213,12 +206,8 @@ describe("evaluateChannelHealth", () => { lastStartAt: 50_000, lastEventAt: 10_000, }, - { - channelId: "slack", - now: 75_000, - channelConnectGraceMs: 10_000, - staleEventThresholdMs: 30_000, - }, + 75_000, + "slack", ); expect(evaluation).toEqual({ healthy: true, reason: "healthy" }); });