import { expect, it, vi } from 'vitest'; import { render } from 'svelte/server'; import { writable } from 'svelte/store'; import { defaultPlan, requirement, type BetaTestView } from '../../src/lib/beta-plan.js'; import { betaStatus, type BetaDeploymentServer } from '../../src/lib/ui/betas.js'; import BetaTestProgress from '../../src/lib/ui/components/BetaTestProgress.svelte'; vi.mock('$app/stores', () => ({ page: writable({ url: new URL('http://localhost/betas/75') }) })); it('prioritizes lifecycle, failures and pauses over readiness without treating stage text as failure', () => { expect(betaStatus({ state: 'promoted', testOutcome: 'paused', servers: [] })).toMatchObject({ key: 'promoted', label: 'Promoted' }); expect(betaStatus({ state: 'rolled-back', testOutcome: 'failed', servers: [] })).toMatchObject({ key: 'rolled-back' }); expect(betaStatus({ state: 'ready-promote', testOutcome: 'failed', servers: [] })).toMatchObject({ key: 'attention', tone: 'danger' }); expect(betaStatus({ state: 'ready-promote', testOutcome: 'paused', servers: [] })).toMatchObject({ key: 'paused' }); expect(betaStatus({ state: 'ready-promote', testOutcome: 'passed', servers: [{ stage: 'ready' }] })).toMatchObject({ key: 'ready' }); expect(betaStatus({ state: 'running', servers: [{ stage: 'awaiting-empty' }] })).toMatchObject({ key: 'testing' }); expect(betaStatus({ state: 'running', servers: [{ stage: 'ready', server_deleted_at: '2026-10-06' }] })).toMatchObject({ key: 'attention' }); }); const deployment = (server: string, stage = 'ready'): BetaDeploymentServer => ({ server, stage, error: null, deployed_at: '2026-10-06', restarted_at: '2026-10-06', healthy_at: '2026-10-06', match_seen_at: null, provisioning_id: null }); const testRun = (server: string, passed = true, failed = false): BetaTestView['servers'][number] => ({ server, server_deleted_at: null, phase: 1, passed, observingAt: '2026-10-06', progress: [{ id: 'health', label: 'Health', current: passed ? 1 : 0, target: 1, passed, failed, detail: failed ? 'Health check failed' : 'Health observation' }] }); it('combines deployment and tests once per server, expands failures first, and keeps passing details collapsed', () => { const test: BetaTestView = { plan: defaultPlan(), outcome: 'failed', reason: null, servers: [testRun('passed'), testRun('failed', false, true)], evidence: [] }; const body = render(BetaTestProgress, { props: { test, betaId: 75, closed: false, deployments: [deployment('passed'), deployment('waiting', 'awaiting-empty'), deployment('failed')], onUpdate: async () => {} } }).body; expect(body).toContain('Deployment'); expect(body).toContain('Test result'); expect(body).toContain('Awaiting test run'); expect(body.indexOf('id="beta-server-75-failed"')).toBeLessThan(body.indexOf('id="beta-server-75-passed"')); expect(body).toMatch(/