import { act, screen } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import { makeBudget, makeBudgetToday } from '../test/factories' import { makeJob, makeSpec, makeStatus } from '../test/fixtures' import { apiError, renderRouted, stubApi } from '../test/harness' import { Dashboard } from './Dashboard' const daemon = (fast_installs: unknown[] = []) => ({ state: 'running', started_at: null, pid: 1, journal_tail: [], restart_available_at: null, fast_installs, }) const bg3 = { app_id: 1086940, name: "Baldur's Gate 3", phase: 'downloading depot 3/8', percent: 46.9, elapsed_seconds: 312 } const base = (over: Record = {}) => ({ 'GET /api/status': makeStatus(), 'GET /api/daemon': daemon(), 'GET /api/budget': makeBudget(), 'GET /api/jobs': [], 'GET /api/commands': [makeSpec('done'), makeSpec('install'), makeSpec('pick'), makeSpec('scan'), makeSpec('check')], ...over, }) const show = (over: Record = {}) => { stubApi(base(over)) return renderRouted() } const fact = (label: string) => screen.getByText(label, { selector: 'dt' }).nextElementSibling! describe('Dashboard', () => { afterEach(() => vi.unstubAllGlobals()) it('offers the quick actions', async () => { show() expect(await screen.findByRole('button', { name: 'I got an achievement' })).toBeInTheDocument() for (const name of ['Install assigned', 'Pick next', 'Scan']) expect(screen.getByRole('button', { name })).toBeInTheDocument() }) describe('assignment', () => { it('summarises a healthy state', async () => { show() expect(await screen.findByText('Hollow Knight')).toBeInTheDocument() expect(fact('Installed')).toHaveTextContent('Yes') expect(fact('Games finished')).toHaveTextContent('3') expect(fact('Store')).toHaveTextContent('Blocked') expect(fact('Installed games')).toHaveTextContent('4') expect(fact('Total block')).toHaveTextContent('Off') expect(fact('Manual picks')).toHaveTextContent('None') }) it('warns about an uninstalled assignment, open store, total block and picks', async () => { show({ 'GET /api/status': makeStatus({ current_game_name: null, assigned_game_installed: false, store_blocked: false, total_block: { active: true, days_remaining: 5, until: null }, manual_picks: [{ app_id: 1, name: 'Alpha', age_days: 2 }, { app_id: 2, name: 'Beta', age_days: 9 }], }), }) expect(await screen.findByText('Nothing assigned')).toBeInTheDocument() expect(fact('Installed')).toHaveTextContent('No — install it from Library') expect(fact('Store')).toHaveTextContent('Unblocked') expect(fact('Total block')).toHaveTextContent('5d 0h left') expect(fact('Manual picks')).toHaveTextContent('Alpha (2 d), Beta (9 d)') }) it('counts the total block down from until, every second', async () => { vi.useFakeTimers({ shouldAdvanceTime: true, now: Date.parse('2026-10-23T10:00:00Z') }) try { show({ 'GET /api/status': makeStatus({ total_block: { active: true, days_remaining: 0, until: '2026-10-23T11:08:02Z' }, }), }) expect(await screen.findByText('1h 8m left')).toBeInTheDocument() // The jump alone re-renders nothing; the 1 s tick picks it up, then // every further tick takes exactly one second off. act(() => vi.setSystemTime(Date.parse('2026-10-23T11:07:58Z'))) expect(fact('Total block')).toHaveTextContent('1h 8m left') act(() => vi.advanceTimersByTime(1000)) const first = Number(/^(\d)s left$/.exec(fact('Total block').textContent ?? '')?.[1]) expect([3, 4]).toContain(first) act(() => vi.advanceTimersByTime(1000)) expect(fact('Total block')).toHaveTextContent(`${first - 1}s left`) } finally { vi.useRealTimers() } }) it('shows loading, then an error', async () => { show({ 'GET /api/status': () => new Promise(() => {}) }) expect(await screen.findByText(/Loading status/)).toBeInTheDocument() }) it('explains a failed load', async () => { show({ 'GET /api/status': apiError(500, 'op_failed', 'status broke') }) expect(await screen.findByText('status broke')).toBeInTheDocument() }) }) describe('fast install', () => { it('shows a daemon-started run with its depot percent', async () => { show({ 'GET /api/daemon': daemon([bg3]) }) expect(await screen.findByText("Fast-installing Baldur's Gate 3 — 46% (downloading depot 3/8)")).toBeInTheDocument() expect(screen.getByRole('progressbar', { name: "Baldur's Gate 3 download" })).toHaveAttribute('aria-valuenow', '46') expect(screen.getByText('5m elapsed')).toBeInTheDocument() }) it('shows the phase alone before a percent is known', async () => { show({ 'GET /api/daemon': daemon([{ ...bg3, phase: 'shutting Steam down', percent: null, elapsed_seconds: 12 }]) }) expect(await screen.findByText("Fast-installing Baldur's Gate 3 — shutting Steam down")).toBeInTheDocument() expect(screen.getByRole('progressbar', { name: "Baldur's Gate 3 download" })).toHaveClass('bar-indeterminate') }) it('keeps a failed daemon run on screen with its reason', async () => { const failed = { ...bg3, phase: 'failed', percent: null, elapsed_seconds: 0, error: 'Refusing to mark it as installed' } show({ 'GET /api/daemon': daemon([failed]) }) expect(await screen.findByText("Fast install of Baldur's Gate 3 failed: Refusing to mark it as installed")).toHaveClass('text-danger') expect(screen.getByText("Steam's own download took over.")).toBeInTheDocument() expect(screen.queryByRole('progressbar', { name: "Baldur's Gate 3 download" })).not.toBeInTheDocument() }) it('stays out of the way when nothing is installing', async () => { show() expect(await screen.findByText('Hollow Knight')).toBeInTheDocument() expect(screen.queryByText(/Fast-installing/)).not.toBeInTheDocument() }) }) describe('gaming today', () => { it('shows time left and what is billing', async () => { show() expect(await screen.findByText(/left of/)).toBeInTheDocument() expect(screen.getByText('Now: Hollow Knight')).toBeInTheDocument() expect(screen.getByRole('progressbar', { name: 'Budget used' }).className).toContain('bar-accent') expect(screen.getByRole('link', { name: 'Details' })).toHaveAttribute('href', '/gaming') }) it('says nothing is billing', async () => { show({ 'GET /api/budget': makeBudget({ session: { ...makeBudget().session, billing_label: '' } }) }) expect(await screen.findByText('Now: nothing billing')).toBeInTheDocument() }) it('warns when the day is almost used, and reports a block', async () => { show({ 'GET /api/budget': makeBudget({ today: makeBudgetToday({ fraction_used: 0.9 }) }) }) expect(await screen.findByRole('progressbar', { name: 'Budget used' })).toHaveClass('bar-warning') }) it('reports a blocked day', async () => { show({ 'GET /api/budget': makeBudget({ today: makeBudgetToday({ blocked: true, fraction_used: 1 }) }) }) expect(await screen.findByText('Blocked for today.')).toBeInTheDocument() expect(screen.getByRole('progressbar', { name: 'Budget used' })).toHaveClass('bar-danger') }) it('shows the budget error, or a default, when there is no today', async () => { show({ 'GET /api/budget': makeBudget({ today: null, error: 'state file unreadable' }) }) expect(await screen.findByText('state file unreadable')).toBeInTheDocument() }) it('has a default message too', async () => { show({ 'GET /api/budget': makeBudget({ today: null, error: null }) }) expect(await screen.findByText('Budget state unavailable.')).toBeInTheDocument() }) it('shows loading, then a request error', async () => { show({ 'GET /api/budget': () => new Promise(() => {}) }) expect(await screen.findByText(/Loading budget/)).toBeInTheDocument() }) it('explains a failed load', async () => { show({ 'GET /api/budget': apiError(500, 'op_failed', 'budget broke') }) expect(await screen.findByText('API returned 500 Error')).toBeInTheDocument() }) }) describe('recent jobs', () => { it('invites running one when there are none', async () => { show() expect(await screen.findByText(/No jobs yet/)).toBeInTheDocument() expect(screen.getByRole('link', { name: 'All jobs' })).toHaveAttribute('href', '/jobs') }) it('lists five with summary, progress or nothing', async () => { const jobs = [ makeJob({ id: 'a', command: 'scan', state: 'succeeded', summary: 'Scanned 12' }), makeJob({ id: 'b', command: 'check', progress: { step: 1, total: 2, label: 'Checking games' } }), makeJob({ id: 'c', command: 'done' }), makeJob({ id: 'd' }), makeJob({ id: 'e' }), makeJob({ id: 'f' }), ] show({ 'GET /api/jobs': jobs }) expect(await screen.findByText('Scanned 12')).toBeInTheDocument() expect(screen.getByText('Checking games')).toBeInTheDocument() expect(document.querySelectorAll('.rows .row-link')).toHaveLength(5) expect(document.querySelector('a[href="/jobs/a"]')).toHaveTextContent('Scan') }) it('explains a failed load', async () => { show({ 'GET /api/jobs': apiError(500, 'op_failed', 'jobs broke') }) expect(await screen.findByText('jobs broke')).toBeInTheDocument() }) }) })