Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tests/daemon/loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ const mockRunMonitorCycle = vi.fn();
const mockDeliverPendingAlerts = vi.fn();
const mockVacuumDatabase = vi.fn();
const mockAggregateDailyCostSnapshots = vi.fn();
const mockDaemonLogger = vi.hoisted(() => {
const logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
fatal: vi.fn(),
child: vi.fn(),
};
logger.child.mockReturnValue(logger);
return logger;
});

vi.mock("../../src/core/monitor.js", () => ({
runMonitorCycle: (...args: unknown[]) => mockRunMonitorCycle(...args),
Expand All @@ -34,6 +46,10 @@ vi.mock("../../src/db/repositories.js", async (importOriginal) => {
};
});

vi.mock("../../src/logging/index.js", () => ({
getLogger: () => mockDaemonLogger,
}));

import { startDaemon, stopDaemon } from "../../src/daemon/loop.js";

// ─── Helpers ─────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -532,4 +548,49 @@ describe("daemon loop", () => {
expect(onCycle).toHaveBeenNthCalledWith(2, result2, undefined);
});
});
// =========================================================================
// 10. ALERT DISPATCH
// =========================================================================
describe("Alert dispatch", () => {
it("triggers alert dispatch during daemon cycle", async () => {
mockRunMonitorCycle.mockResolvedValue(makeCycleResult());
mockDeliverPendingAlerts.mockResolvedValue({
attempted: 2,
delivered: 2,
failed: 0,
errors: [],
});

await startDaemon(db, "testnet", { intervalMs: 5000 });

expect(mockDeliverPendingAlerts).toHaveBeenCalledTimes(1);
expect(mockDeliverPendingAlerts).toHaveBeenCalledWith(db, "testnet");
expect(mockDaemonLogger.info).toHaveBeenCalledWith(
"Delivery — attempted: 2, delivered: 2, failed: 0",
);

await vi.advanceTimersByTimeAsync(5000);
expect(mockDeliverPendingAlerts).toHaveBeenCalledTimes(2);
expect(mockDeliverPendingAlerts).toHaveBeenLastCalledWith(db, "testnet");
});

it("survives and logs if alert dispatch throws unexpectedly", async () => {
mockRunMonitorCycle.mockResolvedValue(makeCycleResult());
mockDeliverPendingAlerts.mockRejectedValueOnce(new Error("Dispatcher exploded"));

// startDaemon should not throw, and the cycle should continue
await expect(startDaemon(db, "testnet", { intervalMs: 5000 })).resolves.not.toThrow();

expect(mockDeliverPendingAlerts).toHaveBeenCalledTimes(1);
expect(mockDaemonLogger.error).toHaveBeenCalledWith(
"deliverPendingAlerts threw unexpectedly",
expect.any(Error),
);

// The next interval should still trigger, showing the daemon didn't crash
await vi.advanceTimersByTimeAsync(5000);
expect(mockRunMonitorCycle).toHaveBeenCalledTimes(2);
expect(mockDeliverPendingAlerts).toHaveBeenCalledTimes(2);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
});
Loading