export const requirementKinds = ['round', 'matches', 'populated-minutes', 'player-hours', 'population', 'sustained-population', 'healthy-minutes', 'failure-budget', 'restart-budget', 'clean-logs', 'recovery', 'feature', 'manual'] as const; export type RequirementKind = typeof requirementKinds[number]; export interface BetaRequirement { id: string; kind: RequirementKind; target: number; label: string; minRound: number; minPlayers: number; minMinutes: number; event: string; } export interface BetaPlan { version: 1; requirements: BetaRequirement[]; scope: 'all' | 'quorum' | 'map' | 'game' | 'map-game'; quorum: number; timeoutHours: number; failureAction: 'fail' | 'pause' | 'rollback'; rollbackRestart: 'manual' | 'when-empty'; waveSize: number; // 0 deploys all selected servers; otherwise pass each wave before expanding. } export const requirementLabels: Record = { round: 'Reach round', matches: 'Complete qualifying matches', 'populated-minutes': 'Populated minutes', 'player-hours': 'Player-hours', population: 'Reach player count', 'sustained-population': 'Sustained population (minutes)', 'healthy-minutes': 'Continuous healthy minutes', 'failure-budget': 'Maximum health outages', 'restart-budget': 'Maximum unexpected restarts', 'clean-logs': 'Maximum script errors', recovery: 'Successful recovery cycles', feature: 'Feature event count', manual: 'Tester checklist' }; export function requirement(kind: RequirementKind, id: string): BetaRequirement { return { id, kind, label: requirementLabels[kind], target: kind === 'round' ? 25 : ['failure-budget', 'restart-budget', 'clean-logs'].includes(kind) ? 0 : 1, minRound: 0, minPlayers: 1, minMinutes: 0, event: '' }; } export function defaultPlan(): BetaPlan { return { version: 1, requirements: [requirement('healthy-minutes', 'health')], scope: 'all', quorum: 1, timeoutHours: 24, failureAction: 'fail', rollbackRestart: 'when-empty', waveSize: 0 }; } export const builtInPlans: Array<{ name: string; plan: BetaPlan }> = [ { name: 'Quick smoke test', plan: defaultPlan() }, { name: 'Gameplay validation', plan: { ...defaultPlan(), requirements: [ { ...requirement('matches', 'matches'), target: 2, minRound: 25 }, { ...requirement('populated-minutes', 'playtime'), target: 90 }, requirement('failure-budget', 'outages') ] } }, { name: 'Long soak', plan: { ...defaultPlan(), timeoutHours: 72, requirements: [ { ...requirement('healthy-minutes', 'health'), target: 360 }, { ...requirement('player-hours', 'participation'), target: 10 }, requirement('clean-logs', 'logs') ] } } ]; /** Strict allow-list validation at every boundary (including stored presets). */ export function parseBetaPlan(value: unknown): BetaPlan { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Test plan must be an object'); const p = value as BetaPlan; if (p.version !== 1 || !['all', 'quorum', 'map', 'game', 'map-game'].includes(p.scope)) throw new Error('Invalid test plan version or scope'); if (!['fail', 'pause', 'rollback'].includes(p.failureAction) || !['manual', 'when-empty'].includes(p.rollbackRestart)) throw new Error('Invalid failure policy'); for (const [key, min, max] of [['quorum', 1, 1000], ['timeoutHours', 0.01, 8760], ['waveSize', 0, 1000]] as const) { if (typeof p[key] !== 'number' || !Number.isFinite(p[key]) || p[key] < min || p[key] > max) throw new Error(`Invalid ${key}`); } if (!Number.isInteger(p.quorum) || !Number.isInteger(p.waveSize)) throw new Error('Quorum and wave size must be integers'); if (!Array.isArray(p.requirements) || !p.requirements.length || p.requirements.length > 30) throw new Error('Choose 1–30 requirements'); const ids = new Set(); const requirements = p.requirements.map(r => { if (!r || typeof r !== 'object' || !requirementKinds.includes(r.kind)) throw new Error('Unknown requirement'); if (typeof r.id !== 'string' || !/^[a-zA-Z0-9_-]{1,64}$/.test(r.id) || r.id in Object.prototype || ids.has(r.id)) throw new Error('Requirement IDs must be unique, non-reserved identifiers'); ids.add(r.id); if (typeof r.label !== 'string' || !r.label.trim() || r.label.length > 200) throw new Error('Requirement needs a label (up to 200 characters)'); for (const key of ['target', 'minRound', 'minPlayers', 'minMinutes'] as const) { if (typeof r[key] !== 'number' || !Number.isFinite(r[key]) || r[key] < 0 || r[key] > 1_000_000) throw new Error(`Invalid requirement ${key}`); } if (!['failure-budget', 'restart-budget', 'clean-logs'].includes(r.kind) && r.target <= 0) throw new Error('Requirement target must be positive'); if (['round', 'matches', 'population', 'failure-budget', 'restart-budget', 'clean-logs', 'recovery', 'feature', 'manual'].includes(r.kind) && !Number.isInteger(r.target)) throw new Error('Count targets must be integers'); if (!Number.isInteger(r.minRound) || !Number.isInteger(r.minPlayers)) throw new Error('Round and player qualifiers must be integers'); if (r.kind === 'manual' && r.target !== 1) throw new Error('Add a separate requirement for each checklist item'); if (typeof r.event !== 'string' || r.event.length > 120 || (r.kind === 'feature' && !/^[a-zA-Z0-9_.:-]+$/.test(r.event))) throw new Error('Feature requirements need an event name'); return { id: r.id, kind: r.kind, label: r.label.trim(), target: r.target, minRound: r.minRound, minPlayers: r.minPlayers, minMinutes: r.minMinutes, event: r.event }; }); return { version: 1, requirements, scope: p.scope, quorum: p.quorum, timeoutHours: p.timeoutHours, failureAction: p.failureAction, rollbackRestart: p.rollbackRestart, waveSize: p.waveSize }; } export interface RequirementProgress { id: string; label: string; current: number; target: number; passed: boolean; failed: boolean; detail: string } export interface BetaTestView { plan: BetaPlan; outcome: string; reason: string | null; servers: Array<{ server: string; runId: string; phase: number; passed: boolean; progress: RequirementProgress[] }>; evidence: Array<{ id: number; server: string; received_at: string; kind: string; payload: string }>; }