import { MODULE_NAMES } from './agents'; const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const two = (n: number) => String(n).padStart(2, '0'); /** "11 Oct 09:47" in the viewer's time zone. */ export function shortWhen(iso?: string | null): string { if (!iso) return ''; const d = new Date(iso); if (Number.isNaN(d.getTime())) return ''; return `${d.getDate()} ${MONTHS[d.getMonth()]} ${two(d.getHours())}:${two(d.getMinutes())}`; } export interface RunLike { run_id: string; modules?: string[] | null; patch_id?: string | null; created_at?: string | null } /** "17 Oct 12:02:05": shortWhen with seconds, for event logs. */ export function shortWhenSec(iso?: string | null): string { if (!iso) return ''; const d = new Date(iso); if (Number.isNaN(d.getTime())) return ''; return `${shortWhen(iso)}:${two(d.getSeconds())}`; } /** Business area(s) of a test run: "Procure-to-Pay", "Procure-to-Pay + 2 more", "All areas". */ export function runAreas(r: RunLike): string { const mods = r.modules ?? []; if (!mods.length) return 'All areas'; const first = MODULE_NAMES[mods[0]] ?? mods[0]; return mods.length > 1 ? `${first} + ${mods.length - 1} more` : first; } /** Readable name of a test run: "Procure-to-Pay · 26C · 11 Oct 09:47". The machine id stays available as a copyable pill. */ export function runTitle(r: RunLike): string { return [runAreas(r), r.patch_id, shortWhen(r.created_at)].filter(Boolean).join(' · '); } /** Short form for places where the business area is already shown (the tree): "26C · 11 Oct 09:47". */ export function runShort(r: RunLike): string { return [r.patch_id, shortWhen(r.created_at)].filter(Boolean).join(' · ') || r.run_id; } /** Which Oracle cycle a patch name belongs to: quarterly (26C), monthly (2026-10, 26C-M2) or something else. Same rules as coworker/platform/patchcycle.py. */ export function patchCadence(patch: string | null | undefined): { cadence: 'quarterly' | 'monthly' | 'other' | 'none'; label: string } { const p = (patch ?? '').trim(); if (!p) return { cadence: 'none', label: 'No patch name' }; const q = /^(\d{2})\s*-?\s*([A-D])$/i.exec(p); if (q) return { cadence: 'quarterly', label: `${q[1]}${q[2].toUpperCase()} quarterly update` }; const m = /^(?:(20\d{2})[-/.](0[1-9]|1[0-2])|(\d{2})([A-D])[-_ ]?(?:M|MONTHLY)[-_ ]?(\d{1,2}))$/i.exec(p); if (m) return { cadence: 'monthly', label: m[1] ? `${m[1]}-${m[2]} monthly update` : `${m[3]}${m[4].toUpperCase()} monthly update ${m[5]}` }; return { cadence: 'other', label: p }; }