// SPDX-License-Identifier: AGPL-3.0-or-later import { ApiError, type DocwellClient, type User } from "@docwell/api-client"; import { createPinia, setActivePinia } from "pinia"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { useAuthStore } from "./auth"; const ann: User = { id: "u1", email: "ann@docwell.test", name: "Ann", avatar: "", locale: "", theme: "", status: "active", verified: false, created: "", role: "superadmin", organization: "", organizationName: "", }; function memoryTokens(initial: string | null = null) { let token = initial; return { get: () => token, set: (t: string | null) => (token = t) }; } function fakeApi(auth: Partial) { return { auth } as unknown as DocwellClient; } describe("auth store", () => { beforeEach(() => setActivePinia(createPinia())); it("login stores the token and user", async () => { const tokens = memoryTokens(); const store = useAuthStore(); store.attach(fakeApi({ login: vi.fn(async () => ({ token: "t1", user: ann })) }), tokens); await store.login("ann@docwell.test", "pw"); expect(store.user?.name).toBe("Ann"); expect(tokens.get()).toBe("t1"); store.logout(); expect(store.isSignedIn).toBe(false); expect(tokens.get()).toBeNull(); }); it("init refreshes a saved token", async () => { const tokens = memoryTokens("old"); const store = useAuthStore(); store.attach(fakeApi({ refresh: vi.fn(async () => ({ token: "new", user: ann })) }), tokens); await store.init(); expect(store.ready).toBe(true); expect(store.isSignedIn).toBe(true); expect(tokens.get()).toBe("new"); }); it("init drops an expired token", async () => { const tokens = memoryTokens("expired"); const store = useAuthStore(); store.attach( fakeApi({ refresh: vi.fn(async () => { throw new ApiError(401, { error: "unauthorized" }); }), }), tokens, ); await store.init(); expect(store.ready).toBe(true); expect(store.isSignedIn).toBe(false); expect(tokens.get()).toBeNull(); }); it("init keeps the token while the server is unreachable", async () => { const tokens = memoryTokens("saved"); const refresh = vi.fn(async () => { throw new ApiError(502, "Bad Gateway"); }); const store = useAuthStore(); store.attach(fakeApi({ refresh }), tokens); await store.init([0, 0]); expect(refresh).toHaveBeenCalledTimes(3); expect(store.ready).toBe(true); expect(store.isSignedIn).toBe(false); expect(tokens.get()).toBe("saved"); }); it("init retries a network error and signs in once the server answers", async () => { const tokens = memoryTokens("saved"); const refresh = vi .fn() .mockRejectedValueOnce(new TypeError("Failed to fetch")) .mockResolvedValueOnce({ token: "new", user: ann }); const store = useAuthStore(); store.attach(fakeApi({ refresh }), tokens); await store.init([0]); expect(store.isSignedIn).toBe(true); expect(tokens.get()).toBe("new"); }); it("init without a token does not call the server", async () => { const refresh = vi.fn(); const store = useAuthStore(); store.attach(fakeApi({ refresh }), memoryTokens()); await store.init(); expect(refresh).not.toHaveBeenCalled(); expect(store.ready).toBe(true); }); });