From f8e4be9b36a16969ac53bd9ce12ce8064be10196 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Sun, 4 Jan 2026 17:43:59 +0900
Subject: refactor(client): migrate state management from React Context to
 Jotai
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Replace AuthProvider and SyncProvider with Jotai atoms for more granular
state management and better performance. This migration:

- Creates atoms for auth, sync, decks, cards, noteTypes, and study state
- Uses atomFamily for parameterized state (e.g., cards by deckId)
- Introduces StoreInitializer component for subscription initialization
- Updates all components and pages to use useAtomValue/useSetAtom
- Updates all tests to use Jotai Provider with createStore pattern

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---
 src/client/stores/auth.test.tsx | 160 ----------------------------------------
 1 file changed, 160 deletions(-)
 delete mode 100644 src/client/stores/auth.test.tsx

(limited to 'src/client/stores/auth.test.tsx')

diff --git a/src/client/stores/auth.test.tsx b/src/client/stores/auth.test.tsx
deleted file mode 100644
index 1769011..0000000
--- a/src/client/stores/auth.test.tsx
+++ /dev/null
@@ -1,160 +0,0 @@
-/**
- * @vitest-environment jsdom
- */
-import { act, renderHook, waitFor } from "@testing-library/react";
-import type { ReactNode } from "react";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { apiClient } from "../api/client";
-import { AuthProvider, useAuth } from "./auth";
-
-// Mock the apiClient
-vi.mock("../api/client", () => ({
-	apiClient: {
-		login: vi.fn(),
-		logout: vi.fn(),
-		isAuthenticated: vi.fn(),
-		getTokens: vi.fn(),
-		onSessionExpired: vi.fn(() => vi.fn()),
-	},
-	ApiClientError: class ApiClientError extends Error {
-		constructor(
-			message: string,
-			public status: number,
-			public code?: string,
-		) {
-			super(message);
-			this.name = "ApiClientError";
-		}
-	},
-}));
-
-const wrapper = ({ children }: { children: ReactNode }) => (
-	<AuthProvider>{children}</AuthProvider>
-);
-
-describe("useAuth", () => {
-	beforeEach(() => {
-		vi.clearAllMocks();
-		vi.mocked(apiClient.getTokens).mockReturnValue(null);
-		vi.mocked(apiClient.isAuthenticated).mockReturnValue(false);
-	});
-
-	afterEach(() => {
-		vi.restoreAllMocks();
-	});
-
-	it("throws error when used outside AuthProvider", () => {
-		// Suppress console.error for this test
-		const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
-
-		expect(() => {
-			renderHook(() => useAuth());
-		}).toThrow("useAuth must be used within an AuthProvider");
-
-		consoleSpy.mockRestore();
-	});
-
-	it("returns initial unauthenticated state", async () => {
-		const { result } = renderHook(() => useAuth(), { wrapper });
-
-		await waitFor(() => {
-			expect(result.current.isLoading).toBe(false);
-		});
-
-		expect(result.current.user).toBeNull();
-		expect(result.current.isAuthenticated).toBe(false);
-	});
-
-	it("returns authenticated state when tokens exist", async () => {
-		vi.mocked(apiClient.getTokens).mockReturnValue({
-			accessToken: "test-access-token",
-			refreshToken: "test-refresh-token",
-		});
-		vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
-		const { result } = renderHook(() => useAuth(), { wrapper });
-
-		await waitFor(() => {
-			expect(result.current.isLoading).toBe(false);
-		});
-
-		expect(result.current.isAuthenticated).toBe(true);
-	});
-
-	describe("login", () => {
-		it("logs in and sets user", async () => {
-			const mockUser = { id: "user-1", username: "testuser" };
-			vi.mocked(apiClient.login).mockResolvedValue({
-				accessToken: "access-token",
-				refreshToken: "refresh-token",
-				user: mockUser,
-			});
-			vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
-			const { result } = renderHook(() => useAuth(), { wrapper });
-
-			await waitFor(() => {
-				expect(result.current.isLoading).toBe(false);
-			});
-
-			await act(async () => {
-				await result.current.login("testuser", "password123");
-			});
-
-			expect(apiClient.login).toHaveBeenCalledWith("testuser", "password123");
-			expect(result.current.user).toEqual(mockUser);
-		});
-
-		it("propagates login errors", async () => {
-			vi.mocked(apiClient.login).mockRejectedValue(
-				new Error("Invalid credentials"),
-			);
-
-			const { result } = renderHook(() => useAuth(), { wrapper });
-
-			await waitFor(() => {
-				expect(result.current.isLoading).toBe(false);
-			});
-
-			await expect(
-				act(async () => {
-					await result.current.login("testuser", "wrongpassword");
-				}),
-			).rejects.toThrow("Invalid credentials");
-		});
-	});
-
-	describe("logout", () => {
-		it("logs out and clears user", async () => {
-			const mockUser = { id: "user-1", username: "testuser" };
-			vi.mocked(apiClient.login).mockResolvedValue({
-				accessToken: "access-token",
-				refreshToken: "refresh-token",
-				user: mockUser,
-			});
-			vi.mocked(apiClient.isAuthenticated).mockReturnValue(true);
-
-			const { result } = renderHook(() => useAuth(), { wrapper });
-
-			await waitFor(() => {
-				expect(result.current.isLoading).toBe(false);
-			});
-
-			// Login first
-			await act(async () => {
-				await result.current.login("testuser", "password123");
-			});
-
-			expect(result.current.user).toEqual(mockUser);
-
-			// Now logout
-			vi.mocked(apiClient.isAuthenticated).mockReturnValue(false);
-			act(() => {
-				result.current.logout();
-			});
-
-			expect(apiClient.logout).toHaveBeenCalled();
-			expect(result.current.user).toBeNull();
-		});
-	});
-});
-- 
cgit v1.2.3-70-g09d2

