|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { HealthSummary } from "../commands/health.js"; |
| 3 | + |
| 4 | +const cleanOldMediaMock = vi.fn(async () => {}); |
| 5 | + |
| 6 | +vi.mock("../media/store.js", async (importOriginal) => { |
| 7 | + const actual = await importOriginal<typeof import("../media/store.js")>(); |
| 8 | + return { |
| 9 | + ...actual, |
| 10 | + cleanOldMedia: cleanOldMediaMock, |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +describe("startGatewayMaintenanceTimers", () => { |
| 15 | + afterEach(() => { |
| 16 | + vi.useRealTimers(); |
| 17 | + vi.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("does not schedule recursive media cleanup unless ttl is configured", async () => { |
| 21 | + vi.useFakeTimers(); |
| 22 | + const { startGatewayMaintenanceTimers } = await import("./server-maintenance.js"); |
| 23 | + |
| 24 | + const timers = startGatewayMaintenanceTimers({ |
| 25 | + broadcast: () => {}, |
| 26 | + nodeSendToAllSubscribed: () => {}, |
| 27 | + getPresenceVersion: () => 1, |
| 28 | + getHealthVersion: () => 1, |
| 29 | + refreshGatewayHealthSnapshot: async () => ({ ok: true }) as HealthSummary, |
| 30 | + logHealth: { error: () => {} }, |
| 31 | + dedupe: new Map(), |
| 32 | + chatAbortControllers: new Map(), |
| 33 | + chatRunState: { abortedRuns: new Map() }, |
| 34 | + chatRunBuffers: new Map(), |
| 35 | + chatDeltaSentAt: new Map(), |
| 36 | + removeChatRun: () => undefined, |
| 37 | + agentRunSeq: new Map(), |
| 38 | + nodeSendToSession: () => {}, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(cleanOldMediaMock).not.toHaveBeenCalled(); |
| 42 | + expect(timers.mediaCleanup).toBeNull(); |
| 43 | + |
| 44 | + clearInterval(timers.tickInterval); |
| 45 | + clearInterval(timers.healthInterval); |
| 46 | + clearInterval(timers.dedupeCleanup); |
| 47 | + }); |
| 48 | + |
| 49 | + it("runs startup media cleanup and repeats it hourly", async () => { |
| 50 | + vi.useFakeTimers(); |
| 51 | + const { startGatewayMaintenanceTimers } = await import("./server-maintenance.js"); |
| 52 | + |
| 53 | + const timers = startGatewayMaintenanceTimers({ |
| 54 | + broadcast: () => {}, |
| 55 | + nodeSendToAllSubscribed: () => {}, |
| 56 | + getPresenceVersion: () => 1, |
| 57 | + getHealthVersion: () => 1, |
| 58 | + refreshGatewayHealthSnapshot: async () => ({ ok: true }) as HealthSummary, |
| 59 | + logHealth: { error: () => {} }, |
| 60 | + dedupe: new Map(), |
| 61 | + chatAbortControllers: new Map(), |
| 62 | + chatRunState: { abortedRuns: new Map() }, |
| 63 | + chatRunBuffers: new Map(), |
| 64 | + chatDeltaSentAt: new Map(), |
| 65 | + removeChatRun: () => undefined, |
| 66 | + agentRunSeq: new Map(), |
| 67 | + nodeSendToSession: () => {}, |
| 68 | + mediaCleanupTtlMs: 24 * 60 * 60_000, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(cleanOldMediaMock).toHaveBeenCalledWith(24 * 60 * 60_000, { |
| 72 | + recursive: true, |
| 73 | + pruneEmptyDirs: true, |
| 74 | + }); |
| 75 | + |
| 76 | + cleanOldMediaMock.mockClear(); |
| 77 | + await vi.advanceTimersByTimeAsync(60 * 60_000); |
| 78 | + expect(cleanOldMediaMock).toHaveBeenCalledWith(24 * 60 * 60_000, { |
| 79 | + recursive: true, |
| 80 | + pruneEmptyDirs: true, |
| 81 | + }); |
| 82 | + |
| 83 | + clearInterval(timers.tickInterval); |
| 84 | + clearInterval(timers.healthInterval); |
| 85 | + clearInterval(timers.dedupeCleanup); |
| 86 | + if (timers.mediaCleanup) { |
| 87 | + clearInterval(timers.mediaCleanup); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("skips overlapping media cleanup runs", async () => { |
| 92 | + vi.useFakeTimers(); |
| 93 | + let resolveCleanup = () => {}; |
| 94 | + let cleanupReady = false; |
| 95 | + cleanOldMediaMock.mockImplementation( |
| 96 | + () => |
| 97 | + new Promise<void>((resolve) => { |
| 98 | + resolveCleanup = resolve; |
| 99 | + cleanupReady = true; |
| 100 | + }), |
| 101 | + ); |
| 102 | + const { startGatewayMaintenanceTimers } = await import("./server-maintenance.js"); |
| 103 | + |
| 104 | + const timers = startGatewayMaintenanceTimers({ |
| 105 | + broadcast: () => {}, |
| 106 | + nodeSendToAllSubscribed: () => {}, |
| 107 | + getPresenceVersion: () => 1, |
| 108 | + getHealthVersion: () => 1, |
| 109 | + refreshGatewayHealthSnapshot: async () => ({ ok: true }) as HealthSummary, |
| 110 | + logHealth: { error: () => {} }, |
| 111 | + dedupe: new Map(), |
| 112 | + chatAbortControllers: new Map(), |
| 113 | + chatRunState: { abortedRuns: new Map() }, |
| 114 | + chatRunBuffers: new Map(), |
| 115 | + chatDeltaSentAt: new Map(), |
| 116 | + removeChatRun: () => undefined, |
| 117 | + agentRunSeq: new Map(), |
| 118 | + nodeSendToSession: () => {}, |
| 119 | + mediaCleanupTtlMs: 24 * 60 * 60_000, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(cleanOldMediaMock).toHaveBeenCalledTimes(1); |
| 123 | + |
| 124 | + await vi.advanceTimersByTimeAsync(60 * 60_000); |
| 125 | + expect(cleanOldMediaMock).toHaveBeenCalledTimes(1); |
| 126 | + |
| 127 | + if (cleanupReady) { |
| 128 | + resolveCleanup(); |
| 129 | + } |
| 130 | + await Promise.resolve(); |
| 131 | + |
| 132 | + await vi.advanceTimersByTimeAsync(60 * 60_000); |
| 133 | + expect(cleanOldMediaMock).toHaveBeenCalledTimes(2); |
| 134 | + |
| 135 | + clearInterval(timers.tickInterval); |
| 136 | + clearInterval(timers.healthInterval); |
| 137 | + clearInterval(timers.dedupeCleanup); |
| 138 | + if (timers.mediaCleanup) { |
| 139 | + clearInterval(timers.mediaCleanup); |
| 140 | + } |
| 141 | + }); |
| 142 | +}); |
0 commit comments