Skip to content

Commit 5eaa146

Browse files
committed
test: share channel health helpers
1 parent 944a2c9 commit 5eaa146

File tree

2 files changed

+56
-75
lines changed

2 files changed

+56
-75
lines changed

src/gateway/channel-health-monitor.test.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ function createSlackSnapshotManager(
106106
);
107107
}
108108

109+
function createBusyDisconnectedManager(lastRunActivityAt: number): ChannelManager {
110+
const now = Date.now();
111+
return createSnapshotManager({
112+
discord: {
113+
default: {
114+
running: true,
115+
connected: false,
116+
enabled: true,
117+
configured: true,
118+
lastStartAt: now - 300_000,
119+
activeRuns: 1,
120+
busy: true,
121+
lastRunActivityAt,
122+
},
123+
},
124+
});
125+
}
126+
109127
async function expectRestartedChannel(
110128
manager: ChannelManager,
111129
channel: ChannelId,
@@ -250,39 +268,13 @@ describe("channel-health-monitor", () => {
250268

251269
it("restarts busy channels when run activity is stale", async () => {
252270
const now = Date.now();
253-
const manager = createSnapshotManager({
254-
discord: {
255-
default: {
256-
running: true,
257-
connected: false,
258-
enabled: true,
259-
configured: true,
260-
lastStartAt: now - 300_000,
261-
activeRuns: 1,
262-
busy: true,
263-
lastRunActivityAt: now - 26 * 60_000,
264-
},
265-
},
266-
});
271+
const manager = createBusyDisconnectedManager(now - 26 * 60_000);
267272
await expectRestartedChannel(manager, "discord");
268273
});
269274

270275
it("restarts disconnected channels when busy flags are inherited from a prior lifecycle", async () => {
271276
const now = Date.now();
272-
const manager = createSnapshotManager({
273-
discord: {
274-
default: {
275-
running: true,
276-
connected: false,
277-
enabled: true,
278-
configured: true,
279-
lastStartAt: now - 300_000,
280-
activeRuns: 1,
281-
busy: true,
282-
lastRunActivityAt: now - 301_000,
283-
},
284-
},
285-
});
277+
const manager = createBusyDisconnectedManager(now - 301_000);
286278
await expectRestartedChannel(manager, "discord");
287279
});
288280

src/gateway/channel-health-policy.test.ts

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import { describe, expect, it } from "vitest";
22
import { evaluateChannelHealth, resolveChannelRestartReason } from "./channel-health-policy.js";
33

4+
function evaluateDiscordHealth(
5+
account: Record<string, unknown>,
6+
now = 100_000,
7+
channelId = "discord",
8+
) {
9+
return evaluateChannelHealth(account, {
10+
channelId,
11+
now,
12+
channelConnectGraceMs: 10_000,
13+
staleEventThresholdMs: 30_000,
14+
});
15+
}
16+
417
describe("evaluateChannelHealth", () => {
518
it("treats disabled accounts as healthy unmanaged", () => {
619
const evaluation = evaluateChannelHealth(
@@ -144,67 +157,47 @@ describe("evaluateChannelHealth", () => {
144157
});
145158

146159
it("skips stale-socket detection for channels in webhook mode", () => {
147-
const evaluation = evaluateChannelHealth(
148-
{
149-
running: true,
150-
connected: true,
151-
enabled: true,
152-
configured: true,
153-
lastStartAt: 0,
154-
lastEventAt: 0,
155-
mode: "webhook",
156-
},
157-
{
158-
channelId: "discord",
159-
now: 100_000,
160-
channelConnectGraceMs: 10_000,
161-
staleEventThresholdMs: 30_000,
162-
},
163-
);
160+
const evaluation = evaluateDiscordHealth({
161+
running: true,
162+
connected: true,
163+
enabled: true,
164+
configured: true,
165+
lastStartAt: 0,
166+
lastEventAt: 0,
167+
mode: "webhook",
168+
});
164169
expect(evaluation).toEqual({ healthy: true, reason: "healthy" });
165170
});
166171

167172
it("does not flag stale sockets for channels without event tracking", () => {
168-
const evaluation = evaluateChannelHealth(
169-
{
170-
running: true,
171-
connected: true,
172-
enabled: true,
173-
configured: true,
174-
lastStartAt: 0,
175-
lastEventAt: null,
176-
},
177-
{
178-
channelId: "discord",
179-
now: 100_000,
180-
channelConnectGraceMs: 10_000,
181-
staleEventThresholdMs: 30_000,
182-
},
183-
);
173+
const evaluation = evaluateDiscordHealth({
174+
running: true,
175+
connected: true,
176+
enabled: true,
177+
configured: true,
178+
lastStartAt: 0,
179+
lastEventAt: null,
180+
});
184181
expect(evaluation).toEqual({ healthy: true, reason: "healthy" });
185182
});
186183

187184
it("does not flag stale sockets without an active connected socket", () => {
188-
const evaluation = evaluateChannelHealth(
185+
const evaluation = evaluateDiscordHealth(
189186
{
190187
running: true,
191188
enabled: true,
192189
configured: true,
193190
lastStartAt: 0,
194191
lastEventAt: 0,
195192
},
196-
{
197-
channelId: "slack",
198-
now: 75_000,
199-
channelConnectGraceMs: 10_000,
200-
staleEventThresholdMs: 30_000,
201-
},
193+
75_000,
194+
"slack",
202195
);
203196
expect(evaluation).toEqual({ healthy: true, reason: "healthy" });
204197
});
205198

206199
it("ignores inherited event timestamps from a previous lifecycle", () => {
207-
const evaluation = evaluateChannelHealth(
200+
const evaluation = evaluateDiscordHealth(
208201
{
209202
running: true,
210203
connected: true,
@@ -213,12 +206,8 @@ describe("evaluateChannelHealth", () => {
213206
lastStartAt: 50_000,
214207
lastEventAt: 10_000,
215208
},
216-
{
217-
channelId: "slack",
218-
now: 75_000,
219-
channelConnectGraceMs: 10_000,
220-
staleEventThresholdMs: 30_000,
221-
},
209+
75_000,
210+
"slack",
222211
);
223212
expect(evaluation).toEqual({ healthy: true, reason: "healthy" });
224213
});

0 commit comments

Comments
 (0)