testsAndMisc-archive/poker-stakes/src/ui/useGame.reducer.test.tsx
Krzysztof kuhy Rudnicki caee1d7067 Split poker-stakes' four oversized files under the 250-line cap
useGame.test.tsx (494) splits by describe-block into reducer/session/debt
files; handEval.test.ts (269) splits at the evaluate7 boundary; useGame.ts
(268) sheds its state and reducer into gameState.ts; handFlow.ts (252) sheds
its betting-state helpers into handFlowBetting.ts.

useGame.ts and handFlow.ts re-export what they moved, so no importer changed.

The shared test helpers live in src/test/ rather than beside the source they
exercise. That is load-bearing: vite.config.ts already excludes src/test/ from
coverage, so no new exclusion entry was needed. Putting them next to the source
first dropped coverage to 98.57% because v8 counted the fixtures' unused
branches as production code -- fixed by moving them, not by excluding them.

Verified: 220 tests still pass, coverage back to 100% on statements, branches,
functions and lines, and npm run build succeeds. poker-stakes violations 4 -> 0;
repo total 196 -> 192.

invariants.test.ts hit its 5s timeout once mid-work. It passes standalone in
2.56s and on re-run, so it is a pre-existing contention flake under parallel
coverage load rather than a regression here -- noted, not papered over.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 15:26:18 +02:00

76 lines
2.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { gameReducer, hasOutstandingDebt } from './useGame'
import type { GameState } from './useGame'
import { debt, initialState } from '../test/useGameFixtures'
describe('hasOutstandingDebt', () => {
it('blocks when storage was unreadable', () => {
expect(hasOutstandingDebt({ status: 'unreadable' })).toBe(true)
})
it('does not block an empty debt list', () => {
expect(hasOutstandingDebt({ status: 'ok', debts: [] })).toBe(false)
})
it('does not block a fully paid debt', () => {
expect(hasOutstandingDebt({ status: 'ok', debts: [debt({ owedUnits: 50, paidUnits: 50 })] })).toBe(false)
})
it('does not block a debt that owes zero units (doubled-up cash-out)', () => {
expect(hasOutstandingDebt({ status: 'ok', debts: [debt({ owedUnits: 0, paidUnits: 0 })] })).toBe(false)
})
it('blocks a partially paid debt', () => {
expect(hasOutstandingDebt({ status: 'ok', debts: [debt({ owedUnits: 100, paidUnits: 40 })] })).toBe(true)
})
})
describe('gameReducer', () => {
it('buyIn sets buyIn/session and clears handPhase', () => {
const session: GameState['session'] = {
stacks: { player: 100, opponent: 100 },
buttonSeat: 'player',
handsPlayed: 0,
outcome: null,
}
const next = gameReducer(initialState, {
type: 'buyIn',
buyIn: { taskTypeId: 'pushups', chips: 100 },
session,
})
expect(next).toEqual({
debtLoad: { status: 'ok', debts: [] },
buyIn: { taskTypeId: 'pushups', chips: 100 },
session,
handPhase: null,
})
})
it('debtSettled clears the live session and resets to the buy-in gate', () => {
const mid: GameState = {
...initialState,
buyIn: { taskTypeId: 'pushups', chips: 100 },
session: {
stacks: { player: 0, opponent: 200 },
buttonSeat: 'player',
handsPlayed: 3,
outcome: { reason: 'bust', finalChips: 0 },
},
handPhase: { kind: 'betweenHands', lastResult: null },
}
const settled = [debt({ owedUnits: 200 })]
const next = gameReducer(mid, { type: 'debtSettled', debts: settled })
expect(next).toEqual({
debtLoad: { status: 'ok', debts: settled },
buyIn: null,
session: null,
handPhase: null,
})
})
it('debtProgressLogged updates debtLoad only', () => {
const next = gameReducer(initialState, { type: 'debtProgressLogged', debts: [debt({ paidUnits: 20 })] })
expect(next.debtLoad).toEqual({ status: 'ok', debts: [debt({ paidUnits: 20 })] })
})
})