From 9a4d61cc84a51653b71d214812b39044df632efa Mon Sep 17 00:00:00 2001 From: F88 <685250+F88@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:31:54 +0900 Subject: [PATCH] fix(game): refetch the deck after the repository changes Saving the token again creates a repository with an empty snapshot, but lastApiParams was kept, so the empty snapshot was reused. Refs: #80 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 +++ src/hooks/use-game-setup.test.ts | 59 ++++++++++++++++++++++++++++++++ src/hooks/use-game-setup.ts | 8 +++++ 3 files changed, 72 insertions(+) create mode 100644 src/hooks/use-game-setup.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1af38f8..18d0b92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [CalVer](https://calver.org/). ## [Unreleased] +### Fixed + +- A deck no longer ends up with 0 cards when it is selected after saving the + token again. The deck is now fetched for the new repository. + ## [2026.11.09] - 2026-11-09 ### Changed diff --git a/src/hooks/use-game-setup.test.ts b/src/hooks/use-game-setup.test.ts new file mode 100644 index 0000000..fa25d51 --- /dev/null +++ b/src/hooks/use-game-setup.test.ts @@ -0,0 +1,59 @@ +import { act, renderHook } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +import { DeckManager } from '@/lib/karuta/deck/deck-manager'; +import { ETO_RECIPES } from '@/lib/karuta/deck/deck-recipe-eto'; + +import { useGameSetup } from './use-game-setup'; + +import type { ProtopediaInMemoryRepository } from 'promidas'; + +const createRepository = () => + ({ + getAllFromSnapshot: vi.fn().mockResolvedValue([]), + }) as unknown as ProtopediaInMemoryRepository; + +describe('useGameSetup', () => { + // These recipes share the same apiParams, so the snapshot can be reused + const [firstRecipe, secondRecipe] = ETO_RECIPES; + + const render = (repository: ProtopediaInMemoryRepository) => + renderHook( + (props: { repository: ProtopediaInMemoryRepository }) => + useGameSetup({ ...props, onGameStateCreated: vi.fn() }), + { initialProps: { repository } }, + ); + + beforeEach(() => { + sessionStorage.clear(); + vi.restoreAllMocks(); + vi.spyOn(DeckManager, 'getPrototypesFromRecipe').mockResolvedValue([]); + }); + + it('reuses the snapshot when apiParams are unchanged', async () => { + const repository = createRepository(); + const { result } = render(repository); + + await act(() => result.current.selectDeckRecipe(firstRecipe)); + await act(() => result.current.selectDeckRecipe(secondRecipe)); + + expect(DeckManager.getPrototypesFromRecipe).toHaveBeenCalledTimes(1); + expect(repository.getAllFromSnapshot).toHaveBeenCalledTimes(1); + }); + + it('fetches again after the repository is replaced', async () => { + const replaced = createRepository(); + const { result, rerender } = render(createRepository()); + + await act(() => result.current.selectDeckRecipe(firstRecipe)); + await act(async () => rerender({ repository: replaced })); + await act(() => result.current.selectDeckRecipe(secondRecipe)); + + expect(DeckManager.getPrototypesFromRecipe).toHaveBeenCalledTimes(2); + expect(DeckManager.getPrototypesFromRecipe).toHaveBeenLastCalledWith( + secondRecipe, + replaced, + ); + expect(replaced.getAllFromSnapshot).not.toHaveBeenCalled(); + }); +}); diff --git a/src/hooks/use-game-setup.ts b/src/hooks/use-game-setup.ts index 4ab694c..7a35586 100644 --- a/src/hooks/use-game-setup.ts +++ b/src/hooks/use-game-setup.ts @@ -316,6 +316,14 @@ export function useGameSetup({ // Track last used apiParams to avoid redundant API calls const [lastApiParams, setLastApiParams] = useState(null); + const [prevRepository, setPrevRepository] = useState(repository); + + // A new repository starts with its own snapshot, so the last apiParams no + // longer describe what it holds + if (repository !== prevRepository) { + setPrevRepository(repository); + setLastApiParams(null); + } // Stack state - 生成済みStackを保持(正確な枚数表示のため) const [generatedStack, setGeneratedStack] = useState(null);