f9faebfa91
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 51s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 2m20s
The in-game hint badge re-fetched on entry and, for a game where it was the player's turn, showed a too-high count that "reset" (e.g. back to 11) — the wallet hint spent in another game was not reflected. Root cause: the server sends one hints_remaining = per-game allowance + global wallet, and the client cached that combined number per game. The wallet is global, so spending a wallet hint in one game left every other game's cached count stale (a my-turn game holds the stalest value: the opponent-moved delta preserves the old number, whereas a game you just moved in re-cached a fresh one). The backend allowance-then-wallet spend order was already correct. Fix: split the two. StateView/HintResult gain a trailing wallet_balance field (the global wallet alone); the client derives the per-game allowance as hints_remaining - wallet_balance (stable, cacheable) and reads the wallet live from the profile, refreshing it from every state/hint response. The badge is allowance + live wallet, so a wallet hint anywhere updates every game at once. - wire: scrabble.fbs StateView/HintResult + pkg/wire.BuildStateView (the single encoder for both the gateway transcode and the backend's event StateView), gateway encode + resp structs, regen. - backend: game StateView/HintResult + service (GameState/Hint) + eventwire + notify PlayerState/encode + server DTOs. - ui: lib/hints.ts (pure hintsLeft), Game.svelte (badge + syncWallet on load/hint, carry wallet_balance through applyMoveResult), codec/model, mock. - docs: ARCHITECTURE §Hint. Tests: hints.ts unit (incl. the staleness case), TestHintPolicy extended (wallet_balance + allowance-first), gateway state/hint round-trips.
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { GameView, StateView } from './model';
|
|
|
|
// Mock the gateway singleton so preload's fan-out is observed without a transport.
|
|
const mocks = vi.hoisted(() => ({
|
|
gameState: vi.fn(),
|
|
gameHistory: vi.fn(),
|
|
draftGet: vi.fn(),
|
|
}));
|
|
vi.mock('./gateway', () => ({
|
|
gateway: { gameState: mocks.gameState, gameHistory: mocks.gameHistory, draftGet: mocks.draftGet },
|
|
}));
|
|
|
|
import { preloadGames } from './preload';
|
|
import { clearGameCache, getCachedGame, setCachedGame } from './gamecache';
|
|
|
|
function gameView(id: string, status: GameView['status'] = 'active'): GameView {
|
|
return {
|
|
id,
|
|
variant: 'scrabble_en',
|
|
dictVersion: 'v1',
|
|
vsAi: false,
|
|
unreadChat: false,
|
|
status,
|
|
players: 2,
|
|
toMove: 0,
|
|
turnTimeoutSecs: 300,
|
|
multipleWordsPerTurn: true,
|
|
moveCount: 0,
|
|
endReason: '',
|
|
lastActivityUnix: 0,
|
|
seats: [],
|
|
};
|
|
}
|
|
|
|
function stateView(id: string): StateView {
|
|
return { game: gameView(id), seat: 0, rack: ['A', 'B'], bagLen: 50, hintsRemaining: 1, walletBalance: 0 };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
clearGameCache();
|
|
mocks.gameState.mockReset().mockImplementation((id: string) => Promise.resolve(stateView(id)));
|
|
mocks.gameHistory.mockReset().mockImplementation((id: string) => Promise.resolve({ gameId: id, moves: [] }));
|
|
mocks.draftGet.mockReset().mockImplementation((id: string) => Promise.resolve(id === 'g1' ? 'DRAFT1' : ''));
|
|
});
|
|
|
|
describe('preloadGames', () => {
|
|
it('warms ongoing, uncached games with state, history and draft', async () => {
|
|
await preloadGames([gameView('g1'), gameView('g2')]);
|
|
expect(getCachedGame('g1')?.view.game.id).toBe('g1');
|
|
expect(getCachedGame('g1')?.draft).toBe('DRAFT1');
|
|
expect(getCachedGame('g2')?.draft).toBe('');
|
|
});
|
|
|
|
it('skips finished games', async () => {
|
|
await preloadGames([gameView('done', 'finished')]);
|
|
expect(getCachedGame('done')).toBeUndefined();
|
|
expect(mocks.gameState).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('skips games already in the cache (kept fresh by the live stream)', async () => {
|
|
setCachedGame('g1', stateView('g1'), [], 'KEEP');
|
|
await preloadGames([gameView('g1'), gameView('g2')]);
|
|
expect(mocks.gameState).toHaveBeenCalledTimes(1);
|
|
expect(mocks.gameState).toHaveBeenCalledWith('g2', expect.any(Boolean));
|
|
expect(getCachedGame('g1')?.draft).toBe('KEEP'); // untouched
|
|
});
|
|
|
|
it('does nothing for an empty list', async () => {
|
|
await preloadGames([]);
|
|
expect(mocks.gameState).not.toHaveBeenCalled();
|
|
});
|
|
});
|