steam-backlog-enforcer/web/src/components/BudgetHistoryChart.test.tsx
Krzysztof kuhy Rudnicki 1faf211767 Show which games make up the gaming budget, and bill osu!lazer
The budget tab answered "how much did I play" but not "what did I play":
playtime_state.json and playtime_history.json each stored one aggregate
seconds figure per gaming day, so the 14-day chart was a row of featureless
bars. Separately, qualifying_pids had exactly two sources — the SteamAppId
env var and a hardcoded 16-launcher frozenset — so osu!lazer, which sets no
SteamAppId and matches no launcher name, was never billed at all.

qualifying_pids now returns pid -> attribution key instead of a bare pid set.
The app id was already in hand and thrown away on its first line; it now
survives into PlaytimeState.per_game, alongside a user-editable
counted_processes list in config.json seeded with osu!lazer.

Two things the parts/whole relationship forces:

  - per_game only ever sums to AT MOST seconds, never equals it. backdate
    refunds the idle grace on every engaged-to-paused edge, and
    _state_or_recover synthesises a full budget with no breakdown. The
    refund is debited from last_credited_key, and the remainder is rendered
    as "Unattributed" rather than stored — one path then covers legacy days,
    focus-probe failures and drift alike.

  - counted_processes is kept apart from LAUNCHER_PROCESS_NAMES. That
    frozenset is also the total-block kill list, and it routes through
    kill_and_uninstall_launchers, which pacman -R's the owning package.
    Counted entries are killed on total block, never uninstalled.

Both storage formats stay at schema 1: load_state already filters to
dataclass fields, so a record without per_game migrates for free, and the
history reader has to accept both a bare float and an object regardless, so
a version bump would distinguish nothing.

Chart segments use the shared design system's categorical ramp (--cat-1..6),
capped at six games plus "Other" because a seventh distinguishable hue does
not exist under simulated CVD. The red over-budget fill is gone: colour now
encodes which game, and the dashed budget line alone says over or under.

Snapshot persistence moves to _snapshot.py to keep config.py under the
250-line cap. Three modules bound config paths at import time, which
bypasses the conftest redirect — they now reach them through the module.

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

115 lines
4.0 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import type { BudgetHistoryDay, BudgetLegendEntry } from '../types'
import { BudgetHistoryChart } from './BudgetHistoryChart'
const LEGEND: BudgetLegendEntry[] = [
{ key: 'app:475150', label: 'Titan Quest' },
{ key: 'proc:osu-lazer', label: 'osu!lazer' },
{ key: 'unattributed', label: 'Unattributed' },
]
function day(over: Partial<BudgetHistoryDay> = {}): BudgetHistoryDay {
return {
day: '2026-08-28',
seconds: 14400,
segments: [{ key: 'app:475150', seconds: 14400 }],
...over,
}
}
describe('BudgetHistoryChart', () => {
it('explains itself when nothing has been recorded', () => {
render(<BudgetHistoryChart days={[]} legend={[]} budgetSeconds={28800} />)
expect(screen.getByText(/No budget history recorded yet/)).toBeInTheDocument()
expect(document.querySelector('svg')).toBeNull()
})
it('draws a single day without filling the plot', () => {
render(<BudgetHistoryChart days={[day()]} legend={LEGEND} budgetSeconds={28800} />)
const bars = document.querySelectorAll('rect.seg')
expect(bars).toHaveLength(1)
// Scaled against the budget, half a budget is half the plot height.
expect(Number(bars[0].getAttribute('height'))).toBeLessThan(
Number(document.querySelector('svg')?.getAttribute('viewBox')?.split(' ')[3]) / 2,
)
expect(screen.getByText('08-28')).toBeInTheDocument()
})
it('stacks one band per game and colours it by legend position', () => {
render(
<BudgetHistoryChart
days={[
day({
seconds: 21600,
segments: [
{ key: 'app:475150', seconds: 14400 },
{ key: 'proc:osu-lazer', seconds: 7200 },
],
}),
]}
legend={LEGEND}
budgetSeconds={28800}
/>,
)
expect(document.querySelectorAll('rect.seg')).toHaveLength(2)
expect(document.querySelector('rect.cat-1')).not.toBeNull()
expect(document.querySelector('rect.cat-2')).not.toBeNull()
// Bands abut: the second starts exactly where the first ends.
const [first, second] = document.querySelectorAll('rect.seg')
const firstTop = Number(first.getAttribute('y'))
const secondBottom =
Number(second.getAttribute('y')) + Number(second.getAttribute('height'))
expect(secondBottom).toBeCloseTo(firstTop, 5)
})
it('renders an unattributed day in its own neutral band', () => {
render(
<BudgetHistoryChart
days={[
day({
day: '2026-08-24',
seconds: 12000,
segments: [{ key: 'unattributed', seconds: 12000 }],
}),
]}
legend={LEGEND}
budgetSeconds={28800}
/>,
)
expect(document.querySelectorAll('rect.seg.unattributed')).toHaveLength(1)
// No ramp hue: an absence of information must not read as another game.
expect(document.querySelector('rect.cat-1')).toBeNull()
})
it('labels each band with its game in the tooltip and the legend', () => {
render(<BudgetHistoryChart days={[day()]} legend={LEGEND} budgetSeconds={28800} />)
expect(document.querySelector('rect.seg title')?.textContent).toContain('Titan Quest')
expect(document.querySelectorAll('.chart-legend li')).toHaveLength(LEGEND.length)
})
it('falls back to the raw key when the legend omits it', () => {
render(
<BudgetHistoryChart
days={[day({ segments: [{ key: 'app:999', seconds: 3600 }] })]}
legend={LEGEND}
budgetSeconds={28800}
/>,
)
expect(document.querySelector('rect.seg title')?.textContent).toContain('app:999')
})
it('survives a zero budget and a day that billed nothing', () => {
render(
<BudgetHistoryChart
days={[day({ seconds: 0, segments: [] })]}
legend={[]}
budgetSeconds={0}
/>,
)
// A day with no billed time draws no bands, but the axes still render.
expect(document.querySelectorAll('rect.seg')).toHaveLength(0)
expect(document.querySelector('svg')).not.toBeNull()
})
})