|
| 1 | +import { |
| 2 | + beforeEach, |
| 3 | + describe, |
| 4 | + expect, |
| 5 | + it, |
| 6 | + vi, |
| 7 | +} from "vitest"; |
| 8 | +import { settings } from "./settings.svelte.js"; |
| 9 | +import * as api from "../api/client.js"; |
| 10 | +import { ApiError } from "../api/client.js"; |
| 11 | + |
| 12 | +vi.mock("../api/client.js", async (importOriginal) => { |
| 13 | + const orig = |
| 14 | + await importOriginal<typeof import("../api/client.js")>(); |
| 15 | + return { |
| 16 | + ...orig, |
| 17 | + getSettings: vi.fn(), |
| 18 | + updateSettings: vi.fn(), |
| 19 | + setAuthToken: vi.fn(), |
| 20 | + isRemoteConnection: vi.fn(), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + settings.agentDirs = {}; |
| 27 | + settings.githubConfigured = false; |
| 28 | + settings.terminal = { mode: "auto" }; |
| 29 | + settings.host = ""; |
| 30 | + settings.port = 0; |
| 31 | + settings.authToken = ""; |
| 32 | + settings.requireAuth = false; |
| 33 | + settings.loading = false; |
| 34 | + settings.saving = false; |
| 35 | + settings.error = null; |
| 36 | + settings.needsAuth = false; |
| 37 | +}); |
| 38 | + |
| 39 | +describe("SettingsStore.load auth handling", () => { |
| 40 | + it("prompts for a token on 401 responses", async () => { |
| 41 | + vi.mocked(api.getSettings).mockRejectedValue( |
| 42 | + new ApiError(401, "Unauthorized"), |
| 43 | + ); |
| 44 | + |
| 45 | + await settings.load(); |
| 46 | + |
| 47 | + expect(settings.needsAuth).toBe(true); |
| 48 | + expect(settings.error).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("does not prompt for a token on non-auth 403 responses", async () => { |
| 52 | + vi.mocked(api.getSettings).mockRejectedValue( |
| 53 | + new ApiError(403, "Forbidden"), |
| 54 | + ); |
| 55 | + |
| 56 | + await settings.load(); |
| 57 | + |
| 58 | + expect(settings.needsAuth).toBe(false); |
| 59 | + expect(settings.error).toBe("Forbidden"); |
| 60 | + }); |
| 61 | +}); |
0 commit comments