| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import { getGitDiffSnapshot } from "../util/git.js"; |
| 4 | import { logger } from "../util/logger.js"; |
| 5 | |
| 6 | import { StorageSyncService } from "./StorageSyncService.js"; |
| 7 | |
| 8 | vi.mock("../util/logger.js", () => ({ |
| 9 | logger: { |
| 10 | debug: vi.fn(), |
| 11 | info: vi.fn(), |
| 12 | warn: vi.fn(), |
| 13 | error: vi.fn(), |
| 14 | }, |
| 15 | })); |
| 16 | |
| 17 | vi.mock("../env.js", () => ({ |
| 18 | env: { apiBase: "https://api.test/" }, |
| 19 | })); |
| 20 | |
| 21 | vi.mock("../util/git.js", () => ({ |
| 22 | getGitDiffSnapshot: vi.fn(), |
| 23 | })); |
| 24 | |
| 25 | describe("StorageSyncService", () => { |
| 26 | const fetchMock = vi.fn(); |
| 27 | const gitDiffMock = vi.mocked(getGitDiffSnapshot); |
| 28 | let service: StorageSyncService; |
| 29 | let originalFetch: typeof fetch | undefined; |
| 30 | let hadFetch = false; |
| 31 | |
| 32 | beforeEach(async () => { |
| 33 | vi.clearAllMocks(); |
| 34 | fetchMock.mockReset(); |
| 35 | hadFetch = "fetch" in globalThis; |
| 36 | originalFetch = globalThis.fetch; |
| 37 | globalThis.fetch = fetchMock as unknown as typeof fetch; |
| 38 | gitDiffMock.mockReset(); |
| 39 | |
| 40 | service = new StorageSyncService(); |
| 41 | await service.initialize(); |
| 42 | }); |
| 43 | |
| 44 | afterEach(() => { |
| 45 | service.stop(); |
| 46 | if (hadFetch) { |
| 47 | globalThis.fetch = originalFetch!; |
| 48 | } else { |
| 49 | delete (globalThis as { fetch?: typeof fetch }).fetch; |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | it("returns false when no storage option provided", async () => { |
| 54 | const result = await service.startFromOptions({ |
| 55 | storageOption: undefined, |
| 56 | accessToken: "token", |
| 57 | syncSessionHistory: vi.fn(), |
| 58 | getSessionSnapshot: vi.fn(), |
| 59 | }); |
| 60 | |
| 61 | expect(result).toBe(false); |
| 62 | expect(fetchMock).not.toHaveBeenCalled(); |
| 63 | }); |
| 64 | |
| 65 | it("warns when storage identifier trims to empty", async () => { |
| 66 | const warnSpy = vi.spyOn(logger, "warn"); |
| 67 | |
| 68 | const result = await service.startFromOptions({ |
| 69 | storageOption: " ", |
| 70 | accessToken: "token", |
| 71 | syncSessionHistory: vi.fn(), |
| 72 | getSessionSnapshot: vi.fn(), |
| 73 | }); |
| 74 | |
| 75 | expect(result).toBe(false); |
| 76 | expect(warnSpy).toHaveBeenCalledWith( |
| 77 | expect.stringContaining("storage identifier was empty"), |
| 78 | ); |
| 79 | warnSpy.mockRestore(); |
| 80 | }); |
| 81 | |
| 82 | it("warns when storage requested without access token", async () => { |
| 83 | const warnSpy = vi.spyOn(logger, "warn"); |
| 84 | |
| 85 | const result = await service.startFromOptions({ |
| 86 | storageOption: "session-123", |
| 87 | accessToken: null, |
| 88 | syncSessionHistory: vi.fn(), |
| 89 | getSessionSnapshot: vi.fn(), |
| 90 | }); |
| 91 | |
| 92 | expect(result).toBe(false); |
| 93 | expect(warnSpy).toHaveBeenCalledWith( |
| 94 | expect.stringContaining("no Continue API key"), |
| 95 | ); |
| 96 | warnSpy.mockRestore(); |
| 97 | }); |
| 98 | |
| 99 | it("starts syncing when presign succeeds", async () => { |
| 100 | const syncSessionHistory = vi.fn(); |
| 101 | const getSessionSnapshot = vi.fn().mockReturnValue({ foo: "bar" }); |
| 102 | |
| 103 | fetchMock.mockResolvedValueOnce({ |
| 104 | ok: true, |
| 105 | json: async () => ({ |
| 106 | session: { putUrl: "https://upload/session", key: "session.json" }, |
| 107 | diff: { putUrl: "https://upload/diff", key: "diff.txt" }, |
| 108 | }), |
| 109 | }); |
| 110 | fetchMock.mockResolvedValue({ ok: true }); |
| 111 | gitDiffMock.mockResolvedValue({ diff: "diff", repoFound: true }); |
| 112 | |
| 113 | const result = await service.startFromOptions({ |
| 114 | storageOption: "session-123", |
| 115 | accessToken: "token", |
| 116 | syncSessionHistory, |
| 117 | getSessionSnapshot, |
| 118 | isActive: () => true, |
| 119 | }); |
| 120 | |
| 121 | expect(result).toBe(true); |
| 122 | expect(syncSessionHistory).toHaveBeenCalledTimes(1); |
| 123 | expect(getSessionSnapshot).toHaveBeenCalledTimes(1); |
| 124 | expect(fetchMock).toHaveBeenCalledTimes(3); |
| 125 | const [presignUrl, presignOptions] = fetchMock.mock.calls[0]; |
| 126 | expect(presignUrl).toBeInstanceOf(URL); |
| 127 | expect((presignUrl as URL).toString()).toBe( |
| 128 | "https://api.test/agents/storage/presigned-url", |
| 129 | ); |
| 130 | expect(presignOptions).toMatchObject({ method: "POST" }); |
| 131 | |
| 132 | const [sessionUrl, sessionOptions] = fetchMock.mock.calls[1]; |
| 133 | expect(sessionUrl).toBe("https://upload/session"); |
| 134 | expect(sessionOptions).toMatchObject({ |
| 135 | method: "PUT", |
| 136 | body: expect.any(String), |
| 137 | }); |
| 138 | |
| 139 | service.stop(); |
| 140 | expect(service.getState().isEnabled).toBe(false); |
| 141 | }); |
| 142 | |
| 143 | it("returns false when presign request fails", async () => { |
| 144 | fetchMock.mockResolvedValueOnce({ |
| 145 | ok: false, |
| 146 | status: 500, |
| 147 | statusText: "Server Error", |
| 148 | }); |
| 149 | |
| 150 | const warnSpy = vi.spyOn(logger, "warn"); |
| 151 | const result = await service.startFromOptions({ |
| 152 | storageOption: "session-123", |
| 153 | accessToken: "token", |
| 154 | syncSessionHistory: vi.fn(), |
| 155 | getSessionSnapshot: vi.fn(), |
| 156 | }); |
| 157 | |
| 158 | expect(result).toBe(false); |
| 159 | expect(warnSpy).toHaveBeenCalledWith( |
| 160 | expect.stringContaining("presign request failed"), |
| 161 | ); |
| 162 | expect(service.getState().lastError).toBe( |
| 163 | "Failed to obtain presigned URLs", |
| 164 | ); |
| 165 | warnSpy.mockRestore(); |
| 166 | }); |
| 167 | |
| 168 | it("records upload error details when PUT fails", async () => { |
| 169 | fetchMock.mockResolvedValueOnce({ |
| 170 | ok: true, |
| 171 | json: async () => ({ |
| 172 | session: { putUrl: "https://upload/session", key: "session.json" }, |
| 173 | diff: { putUrl: "https://upload/diff", key: "diff.txt" }, |
| 174 | }), |
| 175 | }); |
| 176 | fetchMock.mockResolvedValueOnce({ |
| 177 | ok: false, |
| 178 | status: 403, |
| 179 | statusText: "Forbidden", |
| 180 | }); |
| 181 | gitDiffMock.mockResolvedValue({ diff: "", repoFound: true }); |
| 182 | |
| 183 | const warnSpy = vi.spyOn(logger, "warn"); |
| 184 | |
| 185 | const result = await service.startFromOptions({ |
| 186 | storageOption: "session-123", |
| 187 | accessToken: "token", |
| 188 | syncSessionHistory: vi.fn(), |
| 189 | getSessionSnapshot: vi.fn().mockReturnValue({}), |
| 190 | }); |
| 191 | |
| 192 | expect(result).toBe(true); |
| 193 | expect(warnSpy).toHaveBeenCalledWith( |
| 194 | expect.stringContaining("Storage upload failed"), |
| 195 | ); |
| 196 | const state = service.getState(); |
| 197 | expect(state.isEnabled).toBe(true); |
| 198 | expect(state.lastError).toContain("Storage upload failed"); |
| 199 | |
| 200 | service.stop(); |
| 201 | warnSpy.mockRestore(); |
| 202 | }); |
| 203 | }); |