|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { GitPoller } from "../src/history/git-poller.js"; |
| 3 | +import { CommitIngester } from "../src/history/commit-ingest.js"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Stub CommitIngester so tests don't touch the real DB / git log |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +vi.mock("../src/history/commit-ingest.js", () => ({ |
| 10 | + CommitIngester: { |
| 11 | + ingestLatest: vi.fn(), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../src/core/logger.js", () => ({ |
| 16 | + RuntimeLogger: { info: vi.fn(), debug: vi.fn(), error: vi.fn() }, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../src/core/dashboard.js", () => ({ |
| 20 | + CommandCenterDashboard: { log: vi.fn() }, |
| 21 | +})); |
| 22 | + |
| 23 | +const ingestLatest = vi.mocked(CommitIngester.ingestLatest); |
| 24 | + |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | +// Suite |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | + |
| 29 | +describe("GitPoller", () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.useFakeTimers(); |
| 32 | + ingestLatest.mockResolvedValue(0); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.useRealTimers(); |
| 37 | + vi.clearAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + // ------------------------------------------------------------------------- |
| 41 | + // AUTO-03: poll() calls CommitIngester.ingestLatest |
| 42 | + // ------------------------------------------------------------------------- |
| 43 | + |
| 44 | + it("poll() calls CommitIngester.ingestLatest with the repo path (AUTO-03)", async () => { |
| 45 | + const poller = new GitPoller("/repo"); |
| 46 | + await poller.poll(); |
| 47 | + expect(ingestLatest).toHaveBeenCalledWith("/repo", 50); |
| 48 | + }); |
| 49 | + |
| 50 | + // ------------------------------------------------------------------------- |
| 51 | + // AUTO-04: emits "commits" when new commits found |
| 52 | + // ------------------------------------------------------------------------- |
| 53 | + |
| 54 | + it("emits 'commits' event when ingestLatest returns > 0 (AUTO-04)", async () => { |
| 55 | + ingestLatest.mockResolvedValue(3); |
| 56 | + const poller = new GitPoller("/repo"); |
| 57 | + const handler = vi.fn(); |
| 58 | + poller.on("commits", handler); |
| 59 | + |
| 60 | + await poller.poll(); |
| 61 | + |
| 62 | + expect(handler).toHaveBeenCalledWith(3); |
| 63 | + }); |
| 64 | + |
| 65 | + it("does NOT emit 'commits' when ingestLatest returns 0", async () => { |
| 66 | + ingestLatest.mockResolvedValue(0); |
| 67 | + const poller = new GitPoller("/repo"); |
| 68 | + const handler = vi.fn(); |
| 69 | + poller.on("commits", handler); |
| 70 | + |
| 71 | + await poller.poll(); |
| 72 | + |
| 73 | + expect(handler).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + // ------------------------------------------------------------------------- |
| 77 | + // poll() return value |
| 78 | + // ------------------------------------------------------------------------- |
| 79 | + |
| 80 | + it("poll() returns the count from ingestLatest", async () => { |
| 81 | + ingestLatest.mockResolvedValue(7); |
| 82 | + const poller = new GitPoller("/repo"); |
| 83 | + const count = await poller.poll(); |
| 84 | + expect(count).toBe(7); |
| 85 | + }); |
| 86 | + |
| 87 | + it("poll() returns 0 and does not throw when ingestLatest rejects", async () => { |
| 88 | + ingestLatest.mockRejectedValue(new Error("git failure")); |
| 89 | + const poller = new GitPoller("/repo"); |
| 90 | + const count = await poller.poll(); |
| 91 | + expect(count).toBe(0); |
| 92 | + }); |
| 93 | + |
| 94 | + // ------------------------------------------------------------------------- |
| 95 | + // AUTO-03: start() triggers polling on interval |
| 96 | + // ------------------------------------------------------------------------- |
| 97 | + |
| 98 | + it("start() triggers poll at the configured interval", async () => { |
| 99 | + const poller = new GitPoller("/repo", 1000); |
| 100 | + poller.start(); |
| 101 | + |
| 102 | + await vi.advanceTimersByTimeAsync(2100); |
| 103 | + poller.stop(); |
| 104 | + |
| 105 | + expect(ingestLatest.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 106 | + }); |
| 107 | + |
| 108 | + it("start() is idempotent — calling twice does not double the interval", async () => { |
| 109 | + const poller = new GitPoller("/repo", 1000); |
| 110 | + poller.start(); |
| 111 | + poller.start(); |
| 112 | + |
| 113 | + await vi.advanceTimersByTimeAsync(1000); |
| 114 | + poller.stop(); |
| 115 | + |
| 116 | + // One interval fired exactly once |
| 117 | + expect(ingestLatest.mock.calls.length).toBe(1); |
| 118 | + }); |
| 119 | + |
| 120 | + // ------------------------------------------------------------------------- |
| 121 | + // stop() clears the interval |
| 122 | + // ------------------------------------------------------------------------- |
| 123 | + |
| 124 | + it("stop() prevents further interval polls", async () => { |
| 125 | + const poller = new GitPoller("/repo", 1000); |
| 126 | + poller.start(); |
| 127 | + |
| 128 | + await vi.advanceTimersByTimeAsync(1000); |
| 129 | + const callsBeforeStop = ingestLatest.mock.calls.length; |
| 130 | + poller.stop(); |
| 131 | + |
| 132 | + await vi.advanceTimersByTimeAsync(5000); |
| 133 | + expect(ingestLatest.mock.calls.length).toBe(callsBeforeStop); |
| 134 | + }); |
| 135 | +}); |
0 commit comments