|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ChannelAccountSnapshot } from "../channels/plugins/types.public.js"; |
| 3 | +import { startChannelHealthMonitor } from "./channel-health-monitor.js"; |
| 4 | +import type { ChannelManager } from "./server-channels.js"; |
| 5 | + |
| 6 | +const STARTED_AT = 1_000_000; |
| 7 | +const CHECK_INTERVAL_MS = 1_000; |
| 8 | + |
| 9 | +function createChannelManager(running: boolean) { |
| 10 | + const account: ChannelAccountSnapshot = { |
| 11 | + accountId: "default", |
| 12 | + running, |
| 13 | + connected: running, |
| 14 | + enabled: true, |
| 15 | + configured: true, |
| 16 | + }; |
| 17 | + const snapshot = { |
| 18 | + channels: { discord: account }, |
| 19 | + channelAccounts: { discord: { default: account } }, |
| 20 | + }; |
| 21 | + const manager: ChannelManager = { |
| 22 | + getRuntimeSnapshot: vi.fn(() => snapshot), |
| 23 | + getPluginCommandCatalogAccounts: vi.fn(() => new Map()), |
| 24 | + startChannels: vi.fn(async () => {}), |
| 25 | + startChannel: vi.fn(async () => {}), |
| 26 | + stopChannel: vi.fn(async () => {}), |
| 27 | + setAutostartSuppression: vi.fn(), |
| 28 | + getAutostartSuppression: vi.fn(() => null), |
| 29 | + recoverAutostartSuppression: vi.fn(async () => false), |
| 30 | + setAmbientAutostartSuppressedChannelIds: vi.fn(), |
| 31 | + isAmbientAutostartSuppressed: vi.fn(() => false), |
| 32 | + markChannelLoggedOut: vi.fn(), |
| 33 | + isHealthMonitorEnabled: vi.fn(() => true), |
| 34 | + isManuallyStopped: vi.fn(() => false), |
| 35 | + isAutoRestartScheduled: vi.fn(() => false), |
| 36 | + resetRestartAttempts: vi.fn(), |
| 37 | + }; |
| 38 | + return { account, manager }; |
| 39 | +} |
| 40 | + |
| 41 | +describe("channel-health-monitor clock rollback", () => { |
| 42 | + beforeEach(() => { |
| 43 | + vi.useFakeTimers(); |
| 44 | + vi.setSystemTime(STARTED_AT); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + vi.useRealTimers(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("does not trap an existing monitor in startup grace after clock rollback", async () => { |
| 52 | + const { manager } = createChannelManager(false); |
| 53 | + const monitor = startChannelHealthMonitor({ |
| 54 | + channelManager: manager, |
| 55 | + checkIntervalMs: CHECK_INTERVAL_MS, |
| 56 | + timing: { monitorStartupGraceMs: CHECK_INTERVAL_MS }, |
| 57 | + }); |
| 58 | + |
| 59 | + vi.setSystemTime(STARTED_AT - 60_000); |
| 60 | + await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS); |
| 61 | + |
| 62 | + expect(manager.startChannel).toHaveBeenCalledWith("discord", "default"); |
| 63 | + monitor.stop(); |
| 64 | + }); |
| 65 | + |
| 66 | + it.each([ |
| 67 | + { name: "restart cooldown", maxRestartsPerHour: 10 }, |
| 68 | + { name: "hourly restart budget", maxRestartsPerHour: 1 }, |
| 69 | + ])("discards future $name from an existing monitor", async ({ maxRestartsPerHour }) => { |
| 70 | + const { account, manager } = createChannelManager(true); |
| 71 | + const monitor = startChannelHealthMonitor({ |
| 72 | + channelManager: manager, |
| 73 | + checkIntervalMs: CHECK_INTERVAL_MS, |
| 74 | + cooldownCycles: 0, |
| 75 | + maxRestartsPerHour, |
| 76 | + timing: { monitorStartupGraceMs: 0 }, |
| 77 | + }); |
| 78 | + |
| 79 | + await vi.advanceTimersByTimeAsync(5 * CHECK_INTERVAL_MS); |
| 80 | + account.running = false; |
| 81 | + account.connected = false; |
| 82 | + await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS); |
| 83 | + expect(manager.startChannel).toHaveBeenCalledTimes(1); |
| 84 | + |
| 85 | + vi.setSystemTime(STARTED_AT + 3 * CHECK_INTERVAL_MS); |
| 86 | + await vi.advanceTimersByTimeAsync(CHECK_INTERVAL_MS); |
| 87 | + |
| 88 | + expect(manager.startChannel).toHaveBeenCalledTimes(2); |
| 89 | + monitor.stop(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments