mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-22 18:32:26 +01:00
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
120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { loadDebts, saveDebts } from './storage'
|
|
import type { SettledDebt } from './types'
|
|
|
|
const debt: SettledDebt = {
|
|
id: 'd1',
|
|
taskTypeId: 'pushups',
|
|
owedUnits: 100,
|
|
reason: 'cashOut',
|
|
settledAt: 1,
|
|
paidUnits: 0,
|
|
}
|
|
|
|
const makeStorage = (initial: Record<string, string> = {}): Storage => {
|
|
const store = new Map(Object.entries(initial))
|
|
return {
|
|
getItem: (key: string): string | null => store.get(key) ?? null,
|
|
setItem: (key: string, value: string): void => {
|
|
store.set(key, value)
|
|
},
|
|
removeItem: (key: string): void => {
|
|
store.delete(key)
|
|
},
|
|
clear: (): void => {
|
|
store.clear()
|
|
},
|
|
key: (): null => null,
|
|
get length(): number {
|
|
return store.size
|
|
},
|
|
}
|
|
}
|
|
|
|
const throwingStorage = (): Storage => ({
|
|
getItem: (): never => {
|
|
throw new Error('denied')
|
|
},
|
|
setItem: (): never => {
|
|
throw new Error('denied')
|
|
},
|
|
removeItem: (): undefined => undefined,
|
|
clear: (): undefined => undefined,
|
|
key: (): null => null,
|
|
length: 0,
|
|
})
|
|
|
|
describe('loadDebts', () => {
|
|
it('returns an empty ok list when nothing is stored yet', () => {
|
|
expect(loadDebts(makeStorage())).toEqual({ status: 'ok', debts: [] })
|
|
})
|
|
|
|
it('returns the stored debts when the shape is valid', () => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([debt]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'ok', debts: [debt] })
|
|
})
|
|
|
|
it('is unreadable when getItem throws (storage disabled/denied)', () => {
|
|
expect(loadDebts(throwingStorage())).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
it('is unreadable on malformed JSON', () => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': '{not json' })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
it('is unreadable when the stored value is not an array', () => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify({ not: 'an array' }) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
it('is unreadable when an array entry is missing required fields', () => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([{ id: 'd1' }]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
// Each field's guard is asserted in ISOLATION — all other fields stay valid. The
|
|
// missing-required-fields case above drops five fields at once, so it would still trip some
|
|
// other check if any single guard were deleted, leaving that guard effectively untested.
|
|
it.each([
|
|
['id', { id: 42 }],
|
|
['taskTypeId', { taskTypeId: null }],
|
|
['owedUnits', { owedUnits: 'ten' }],
|
|
['reason', { reason: 'quit' }],
|
|
['settledAt', { settledAt: '2026-01-01' }],
|
|
['paidUnits', { paidUnits: [] }],
|
|
])('is unreadable when only %s has the wrong type', (_field, override) => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([{ ...debt, ...override }]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
it.each(['id', 'taskTypeId', 'owedUnits', 'reason', 'settledAt', 'paidUnits'])(
|
|
'is unreadable when only %s is missing',
|
|
(field) => {
|
|
// Built by filtering rather than `delete`, which lint forbids on computed keys.
|
|
const partial = Object.fromEntries(Object.entries(debt).filter(([key]) => key !== field))
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([partial]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
},
|
|
)
|
|
|
|
it('is unreadable when an array entry has an invalid reason value', () => {
|
|
const bad = { ...debt, reason: 'quit' }
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([bad]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
|
|
it('is unreadable when an array entry is null', () => {
|
|
const storage = makeStorage({ 'poker-stakes:settledDebts': JSON.stringify([null]) })
|
|
expect(loadDebts(storage)).toEqual({ status: 'unreadable' })
|
|
})
|
|
})
|
|
|
|
describe('saveDebts', () => {
|
|
it('round-trips through loadDebts', () => {
|
|
const storage = makeStorage()
|
|
saveDebts(storage, [debt])
|
|
expect(loadDebts(storage)).toEqual({ status: 'ok', debts: [debt] })
|
|
})
|
|
})
|