From 7ca9941982a7d7a4c126d215770ce71ad2f7f427 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Sat, 2 May 2026 11:11:53 +0900
Subject: feat(client): read decks/cards/study from IndexedDB first

Switch deckAtom, cardsByDeckAtomFamily, noteTypesAtom, and studyDataAtom
to a stale-while-revalidate pattern: read from IndexedDB synchronously,
trigger sync in the background, and refetch on sync_complete. When local
is empty, await a single bootstrap pull before deciding there's no data.

Add study-builder to assemble StudyCards from LocalCard + Note + NoteType
+ field values, replacing the server /study endpoint dependency. The
study session can now run end-to-end offline.

Disable submit on all write modals when offline since writes still
require the server. Add a "Showing cached data" hint to the sync status
indicator. Drop cacheStudyCards (cards arrive via regular sync pull now)
and update page tests to reflect that lists no longer refresh by hitting
the GET API.
---
 src/client/pages/NoteTypesPage.test.tsx | 49 ++++-----------------------------
 1 file changed, 6 insertions(+), 43 deletions(-)

(limited to 'src/client/pages/NoteTypesPage.test.tsx')

diff --git a/src/client/pages/NoteTypesPage.test.tsx b/src/client/pages/NoteTypesPage.test.tsx
index 612cf16..1a41185 100644
--- a/src/client/pages/NoteTypesPage.test.tsx
+++ b/src/client/pages/NoteTypesPage.test.tsx
@@ -267,7 +267,7 @@ describe("NoteTypesPage", () => {
 			).toBeDefined();
 		});
 
-		it("creates note type and refreshes list", async () => {
+		it("submits the new note type via the create endpoint", async () => {
 			const user = userEvent.setup();
 			const newNoteType = {
 				id: "note-type-new",
@@ -279,35 +279,22 @@ describe("NoteTypesPage", () => {
 				updatedAt: "2024-01-03T00:00:00Z",
 			};
 
-			// Mock the POST response and subsequent GET after reload
 			mockNoteTypesPost.mockResolvedValue({
 				ok: true,
 				json: async () => ({ noteType: newNoteType }),
 			});
-			mockNoteTypesGet.mockResolvedValue({ noteTypes: [newNoteType] });
 
 			renderWithProviders({ initialNoteTypes: [] });
 
-			// Open modal
 			await user.click(screen.getByRole("button", { name: /New Note Type/i }));
-
-			// Fill in form
 			await user.type(screen.getByLabelText("Name"), "New Note Type");
-
-			// Submit
 			await user.click(screen.getByRole("button", { name: "Create" }));
 
-			// Modal should close
 			await waitFor(() => {
 				expect(screen.queryByRole("dialog")).toBeNull();
 			});
 
-			// Note type list should be refreshed with new note type
-			await waitFor(() => {
-				expect(
-					screen.getByRole("heading", { name: "New Note Type" }),
-				).toBeDefined();
-			});
+			expect(mockNoteTypesPost).toHaveBeenCalledTimes(1);
 		});
 	});
 
@@ -364,7 +351,7 @@ describe("NoteTypesPage", () => {
 			});
 		});
 
-		it("edits note type and refreshes list", async () => {
+		it("submits the edited note type via the update endpoint", async () => {
 			const user = userEvent.setup();
 			const mockNoteTypeWithFields = {
 				...mockNoteTypes[0],
@@ -395,42 +382,29 @@ describe("NoteTypesPage", () => {
 				ok: true,
 				json: async () => ({ noteType: updatedNoteType }),
 			});
-			mockNoteTypesGet.mockResolvedValue({
-				noteTypes: [updatedNoteType, mockNoteTypes[1]],
-			});
 
 			renderWithProviders({ initialNoteTypes: mockNoteTypes });
 
-			// Click Edit on first note type
 			const editButtons = screen.getAllByRole("button", {
 				name: "Edit note type",
 			});
 			await user.click(editButtons.at(0) as HTMLElement);
 
-			// Wait for the editor to load
 			await waitFor(() => {
 				expect(screen.getByLabelText("Name")).toHaveProperty("value", "Basic");
 			});
 
-			// Update name
 			const nameInput = screen.getByLabelText("Name");
 			await user.clear(nameInput);
 			await user.type(nameInput, "Updated Basic");
 
-			// Save
 			await user.click(screen.getByRole("button", { name: "Save Changes" }));
 
-			// Modal should close
 			await waitFor(() => {
 				expect(screen.queryByRole("dialog")).toBeNull();
 			});
 
-			// Note type list should be refreshed with updated name
-			await waitFor(() => {
-				expect(
-					screen.getByRole("heading", { name: "Updated Basic" }),
-				).toBeDefined();
-			});
+			expect(mockNoteTypePut).toHaveBeenCalledTimes(1);
 		});
 	});
 
@@ -463,29 +437,25 @@ describe("NoteTypesPage", () => {
 			expect(dialog.textContent).toContain("Basic");
 		});
 
-		it("deletes note type and refreshes list", async () => {
+		it("submits the note type delete via the delete endpoint", async () => {
 			const user = userEvent.setup();
 
 			mockNoteTypeDelete.mockResolvedValue({
 				ok: true,
 				json: async () => ({ success: true }),
 			});
-			mockNoteTypesGet.mockResolvedValue({ noteTypes: [mockNoteTypes[1]] });
 
 			renderWithProviders({ initialNoteTypes: mockNoteTypes });
 
-			// Click Delete on first note type
 			const deleteButtons = screen.getAllByRole("button", {
 				name: "Delete note type",
 			});
 			await user.click(deleteButtons.at(0) as HTMLElement);
 
-			// Wait for modal to appear
 			await waitFor(() => {
 				expect(screen.getByRole("dialog")).toBeDefined();
 			});
 
-			// Confirm deletion
 			const dialog = screen.getByRole("dialog");
 			const dialogButtons = dialog.querySelectorAll("button");
 			const deleteButton = Array.from(dialogButtons).find(
@@ -493,18 +463,11 @@ describe("NoteTypesPage", () => {
 			);
 			await user.click(deleteButton as HTMLElement);
 
-			// Modal should close
 			await waitFor(() => {
 				expect(screen.queryByRole("dialog")).toBeNull();
 			});
 
-			// Note type list should be refreshed without deleted note type
-			await waitFor(() => {
-				expect(screen.queryByRole("heading", { name: "Basic" })).toBeNull();
-			});
-			expect(
-				screen.getByRole("heading", { name: "Basic (and reversed card)" }),
-			).toBeDefined();
+			expect(mockNoteTypeDelete).toHaveBeenCalledTimes(1);
 		});
 	});
 });
-- 
cgit v1.3.1

