testsAndMisc-archive/poker-stakes/src/stakes/storage.ts
Krzysztof kuhy Rudnicki bf6f94a75b Track poker-stakes so the file-length cap can reach it
The directory has existed on disk with 58 untracked files, four of them over
the 250-line cap. An untracked file is invisible to every pre-commit hook, so
those violations could not be fixed or gated while it stayed that way -- and
its CI workflow was untracked too, so nothing ran against it.

Landed with its already-written evidence artifact and a contract, since this
crosses the 4-file threshold.

Verified before staging: 58 plain text source/config files, no binaries, no
node_modules, no build output. npm run check passes -- typecheck, eslint
--max-warnings 0, and 220 tests at 100% statements/branches/functions/lines.

Splitting its four oversized files is deliberately a separate commit.

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

49 lines
1.4 KiB
TypeScript

import type { SettledDebt } from './types'
/** Exported so tests can seed a scripted ledger without duplicating the key string. */
export const DEBTS_STORAGE_KEY = 'poker-stakes:settledDebts'
export type DebtLoad = { status: 'ok'; debts: SettledDebt[] } | { status: 'unreadable' }
const isSettledDebt = (value: unknown): value is SettledDebt => {
if (typeof value !== 'object' || value === null) {
return false
}
const v = value as Record<string, unknown>
return (
typeof v.id === 'string' &&
typeof v.taskTypeId === 'string' &&
typeof v.owedUnits === 'number' &&
(v.reason === 'cashOut' || v.reason === 'bust') &&
typeof v.settledAt === 'number' &&
typeof v.paidUnits === 'number'
)
}
/** Fail-closed: any read/parse/shape error yields 'unreadable', never a silently-empty list. */
export const loadDebts = (storage: Storage): DebtLoad => {
let raw: string | null
try {
raw = storage.getItem(DEBTS_STORAGE_KEY)
} catch {
return { status: 'unreadable' }
}
if (raw === null) {
return { status: 'ok', debts: [] }
}
let parsed: unknown
try {
parsed = JSON.parse(raw)
} catch {
return { status: 'unreadable' }
}
if (!Array.isArray(parsed) || !parsed.every(isSettledDebt)) {
return { status: 'unreadable' }
}
return { status: 'ok', debts: parsed }
}
export const saveDebts = (storage: Storage, debts: SettledDebt[]): void => {
storage.setItem(DEBTS_STORAGE_KEY, JSON.stringify(debts))
}