test: share channel health helpers

This commit is contained in:
Peter Steinberger 2026-03-14 00:25:24 +00:00
parent 944a2c93e3
commit 5eaa14687f
2 changed files with 56 additions and 75 deletions

View File

@ -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");
});

View File

@ -1,6 +1,19 @@
import { describe, expect, it } from "vitest";
import { evaluateChannelHealth, resolveChannelRestartReason } from "./channel-health-policy.js";
function evaluateDiscordHealth(
account: Record<string, unknown>,
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,8 +157,7 @@ describe("evaluateChannelHealth", () => {
});
it("skips stale-socket detection for channels in webhook mode", () => {
const evaluation = evaluateChannelHealth(
{
const evaluation = evaluateDiscordHealth({
running: true,
connected: true,
enabled: true,
@ -153,39 +165,24 @@ describe("evaluateChannelHealth", () => {
lastStartAt: 0,
lastEventAt: 0,
mode: "webhook",
},
{
channelId: "discord",
now: 100_000,
channelConnectGraceMs: 10_000,
staleEventThresholdMs: 30_000,
},
);
});
expect(evaluation).toEqual({ healthy: true, reason: "healthy" });
});
it("does not flag stale sockets for channels without event tracking", () => {
const evaluation = evaluateChannelHealth(
{
const evaluation = evaluateDiscordHealth({
running: true,
connected: true,
enabled: true,
configured: true,
lastStartAt: 0,
lastEventAt: null,
},
{
channelId: "discord",
now: 100_000,
channelConnectGraceMs: 10_000,
staleEventThresholdMs: 30_000,
},
);
});
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" });
});