Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions src/hooks/use-game-setup.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
8 changes: 8 additions & 0 deletions src/hooks/use-game-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ export function useGameSetup({
// Track last used apiParams to avoid redundant API calls
const [lastApiParams, setLastApiParams] =
useState<ListPrototypesParams | null>(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<number[] | null>(null);
Expand Down
Loading