|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../../utils/logger", () => ({ |
| 4 | + logger: { |
| 5 | + scope: () => ({ |
| 6 | + info: vi.fn(), |
| 7 | + debug: vi.fn(), |
| 8 | + warn: vi.fn(), |
| 9 | + error: vi.fn(), |
| 10 | + }), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +import { |
| 15 | + EMPTY_HOME_SNAPSHOT, |
| 16 | + HomeEvent, |
| 17 | + type HomeSnapshot, |
| 18 | +} from "@shared/types/home-snapshot"; |
| 19 | +import type { AuthService } from "../auth/service"; |
| 20 | +import { HomeService } from "./service"; |
| 21 | + |
| 22 | +const POLL_INTERVAL_MS = 120_000; |
| 23 | + |
| 24 | +const SNAP_WITH: HomeSnapshot = { |
| 25 | + activeAgents: [ |
| 26 | + { |
| 27 | + taskId: "t1", |
| 28 | + title: "Fix bug", |
| 29 | + repoName: null, |
| 30 | + branch: null, |
| 31 | + status: "in_progress", |
| 32 | + lastActivityAt: 1, |
| 33 | + needsPermission: false, |
| 34 | + cloudPrUrl: null, |
| 35 | + }, |
| 36 | + ], |
| 37 | + needsAttention: [], |
| 38 | + inProgress: [], |
| 39 | +}; |
| 40 | + |
| 41 | +function res(status: number, body: unknown): Response { |
| 42 | + return { |
| 43 | + ok: status >= 200 && status < 300, |
| 44 | + status, |
| 45 | + json: async () => body, |
| 46 | + } as Response; |
| 47 | +} |
| 48 | + |
| 49 | +const fetchMock = vi.fn(); |
| 50 | +let projectId: string | null; |
| 51 | +const authService = { |
| 52 | + getState: () => ({ currentProjectId: projectId }), |
| 53 | + authenticatedProjectFetch: fetchMock, |
| 54 | +} as unknown as AuthService; |
| 55 | + |
| 56 | +let service: HomeService; |
| 57 | +let events: HomeSnapshot[]; |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + fetchMock.mockReset(); |
| 61 | + projectId = "proj_1"; |
| 62 | + events = []; |
| 63 | + service = new HomeService(authService); |
| 64 | + service.on(HomeEvent.SnapshotUpdated, (s) => events.push(s)); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("HomeService.getSnapshot", () => { |
| 68 | + it("returns EMPTY_HOME_SNAPSHOT without fetching when no project is selected", async () => { |
| 69 | + projectId = null; |
| 70 | + await expect(service.getSnapshot()).resolves.toEqual(EMPTY_HOME_SNAPSHOT); |
| 71 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns the parsed snapshot on a successful response", async () => { |
| 75 | + fetchMock.mockResolvedValue(res(200, SNAP_WITH)); |
| 76 | + await expect(service.getSnapshot()).resolves.toEqual(SNAP_WITH); |
| 77 | + expect(fetchMock).toHaveBeenCalledWith("code_home/", { method: "GET" }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns EMPTY_HOME_SNAPSHOT on a non-ok response", async () => { |
| 81 | + fetchMock.mockResolvedValue(res(503, {})); |
| 82 | + await expect(service.getSnapshot()).resolves.toEqual(EMPTY_HOME_SNAPSHOT); |
| 83 | + }); |
| 84 | + |
| 85 | + it("returns EMPTY_HOME_SNAPSHOT when the body fails schema validation", async () => { |
| 86 | + fetchMock.mockResolvedValue(res(200, { bogus: true })); |
| 87 | + await expect(service.getSnapshot()).resolves.toEqual(EMPTY_HOME_SNAPSHOT); |
| 88 | + }); |
| 89 | + |
| 90 | + it("returns EMPTY_HOME_SNAPSHOT when the fetch throws", async () => { |
| 91 | + fetchMock.mockRejectedValue(new Error("network down")); |
| 92 | + await expect(service.getSnapshot()).resolves.toEqual(EMPTY_HOME_SNAPSHOT); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe("HomeService.refresh", () => { |
| 97 | + it("POSTs the refresh endpoint then returns the latest snapshot", async () => { |
| 98 | + fetchMock |
| 99 | + .mockResolvedValueOnce(res(200, {})) |
| 100 | + .mockResolvedValueOnce(res(200, SNAP_WITH)); |
| 101 | + await expect(service.refresh()).resolves.toEqual(SNAP_WITH); |
| 102 | + expect(fetchMock).toHaveBeenNthCalledWith(1, "code_home/refresh/", { |
| 103 | + method: "POST", |
| 104 | + }); |
| 105 | + expect(fetchMock).toHaveBeenNthCalledWith(2, "code_home/", { |
| 106 | + method: "GET", |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("still returns a snapshot when the refresh POST fails", async () => { |
| 111 | + fetchMock |
| 112 | + .mockRejectedValueOnce(new Error("refresh failed")) |
| 113 | + .mockResolvedValueOnce(res(200, SNAP_WITH)); |
| 114 | + await expect(service.refresh()).resolves.toEqual(SNAP_WITH); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe("HomeService poll loop", () => { |
| 119 | + beforeEach(() => { |
| 120 | + vi.useFakeTimers(); |
| 121 | + }); |
| 122 | + |
| 123 | + afterEach(() => { |
| 124 | + service.dispose(); |
| 125 | + vi.useRealTimers(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("emits SnapshotUpdated when the snapshot changes between polls", async () => { |
| 129 | + fetchMock |
| 130 | + .mockResolvedValueOnce(res(200, SNAP_WITH)) |
| 131 | + .mockResolvedValueOnce(res(200, EMPTY_HOME_SNAPSHOT)); |
| 132 | + service.init(); |
| 133 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 134 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 135 | + expect(events).toEqual([SNAP_WITH, EMPTY_HOME_SNAPSHOT]); |
| 136 | + }); |
| 137 | + |
| 138 | + it("does not re-emit an unchanged snapshot", async () => { |
| 139 | + fetchMock.mockResolvedValue(res(200, SNAP_WITH)); |
| 140 | + service.init(); |
| 141 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 142 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 143 | + expect(events).toEqual([SNAP_WITH]); |
| 144 | + }); |
| 145 | + |
| 146 | + it("does not emit when a poll fails", async () => { |
| 147 | + fetchMock.mockResolvedValue(res(500, {})); |
| 148 | + service.init(); |
| 149 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 150 | + expect(events).toEqual([]); |
| 151 | + }); |
| 152 | + |
| 153 | + it("getSnapshot seeds the dedup state so a matching first poll does not emit", async () => { |
| 154 | + fetchMock.mockResolvedValue(res(200, SNAP_WITH)); |
| 155 | + await service.getSnapshot(); |
| 156 | + service.init(); |
| 157 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 158 | + expect(events).toEqual([]); |
| 159 | + }); |
| 160 | + |
| 161 | + it("dispose stops the timer", async () => { |
| 162 | + fetchMock.mockResolvedValue(res(200, SNAP_WITH)); |
| 163 | + service.init(); |
| 164 | + service.dispose(); |
| 165 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS * 3); |
| 166 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 167 | + expect(events).toEqual([]); |
| 168 | + }); |
| 169 | +}); |
0 commit comments