|
| 1 | +/** |
| 2 | + * Pure helpers for run detail headings and labels. |
| 3 | + * |
| 4 | + * The API returns local and remote runs through the same shape, but remote |
| 5 | + * runs carry extra source identity (`source_label`, results repo). Keep that |
| 6 | + * presentation logic here so route components stay thin and tests can pin |
| 7 | + * the remote-context contract without rendering React. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { EvalResult, RunDetailResponse } from './types'; |
| 11 | + |
| 12 | +type RunSource = RunDetailResponse['source']; |
| 13 | + |
| 14 | +type HeaderResult = Pick<EvalResult, 'experiment' | 'target' | 'timestamp'>; |
| 15 | + |
| 16 | +export interface RunDetailHeaderInput { |
| 17 | + runId: string; |
| 18 | + results: readonly HeaderResult[]; |
| 19 | + source?: RunSource; |
| 20 | + sourceLabel?: string; |
| 21 | + remoteRepo?: string; |
| 22 | + formatTimestamp?: (timestamp: string) => string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface RunDetailHeaderContextItem { |
| 26 | + label: string; |
| 27 | + value: string; |
| 28 | +} |
| 29 | + |
| 30 | +export interface RunDetailHeader { |
| 31 | + heading: string; |
| 32 | + meta: string; |
| 33 | + sourceBadge?: 'Remote'; |
| 34 | + sourceLabel?: string; |
| 35 | + sourceContext: RunDetailHeaderContextItem[]; |
| 36 | +} |
| 37 | + |
| 38 | +export interface CategoryDisplay { |
| 39 | + label: string; |
| 40 | + mutedLabel?: string; |
| 41 | +} |
| 42 | + |
| 43 | +function nonDefaultExperiment(experiment: string | undefined): string | undefined { |
| 44 | + return experiment && experiment !== 'default' ? experiment : undefined; |
| 45 | +} |
| 46 | + |
| 47 | +function resultHeading(runId: string, firstResult: HeaderResult | undefined): string { |
| 48 | + const parts = [nonDefaultExperiment(firstResult?.experiment), firstResult?.target].filter( |
| 49 | + (part): part is string => Boolean(part), |
| 50 | + ); |
| 51 | + return parts.length > 0 ? parts.join(' · ') : runId; |
| 52 | +} |
| 53 | + |
| 54 | +function cleanOptional(value: string | undefined): string | undefined { |
| 55 | + const trimmed = value?.trim(); |
| 56 | + return trimmed ? trimmed : undefined; |
| 57 | +} |
| 58 | + |
| 59 | +export function buildRunDetailHeader(input: RunDetailHeaderInput): RunDetailHeader { |
| 60 | + const firstResult = input.results[0]; |
| 61 | + const sourceLabel = cleanOptional(input.sourceLabel); |
| 62 | + const isRemote = input.source === 'remote'; |
| 63 | + const heading = isRemote && sourceLabel ? sourceLabel : resultHeading(input.runId, firstResult); |
| 64 | + const formattedTimestamp = |
| 65 | + firstResult?.timestamp && input.formatTimestamp |
| 66 | + ? input.formatTimestamp(firstResult.timestamp) |
| 67 | + : firstResult?.timestamp; |
| 68 | + |
| 69 | + const metaItems = [ |
| 70 | + firstResult?.target, |
| 71 | + nonDefaultExperiment(firstResult?.experiment), |
| 72 | + formattedTimestamp, |
| 73 | + isRemote ? undefined : input.source, |
| 74 | + ].filter((item): item is string => Boolean(item)); |
| 75 | + |
| 76 | + const remoteRepo = cleanOptional(input.remoteRepo); |
| 77 | + const sourceContext: RunDetailHeaderContextItem[] = []; |
| 78 | + if (isRemote) { |
| 79 | + if (sourceLabel && sourceLabel !== heading) { |
| 80 | + sourceContext.push({ label: 'Source', value: sourceLabel }); |
| 81 | + } |
| 82 | + if (remoteRepo) { |
| 83 | + sourceContext.push({ label: 'Repo', value: remoteRepo }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + heading, |
| 89 | + meta: metaItems.join(' · '), |
| 90 | + ...(isRemote && { sourceBadge: 'Remote' as const }), |
| 91 | + ...(sourceLabel && { sourceLabel }), |
| 92 | + sourceContext, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function isTraversalLikeCategory(category: string): boolean { |
| 97 | + const normalized = category.replace(/\\/g, '/'); |
| 98 | + return ( |
| 99 | + normalized === '.' || |
| 100 | + normalized === '..' || |
| 101 | + normalized.startsWith('./') || |
| 102 | + normalized.startsWith('../') || |
| 103 | + normalized.startsWith('/') || |
| 104 | + /^[A-Za-z]:\//.test(normalized) || |
| 105 | + normalized.includes('/../') || |
| 106 | + normalized.includes('/./') |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function basenameFromCategory(category: string): string | undefined { |
| 111 | + const segment = category |
| 112 | + .split(/[\\/]+/) |
| 113 | + .filter((part) => part && part !== '.' && part !== '..') |
| 114 | + .at(-1) |
| 115 | + ?.trim(); |
| 116 | + return segment || undefined; |
| 117 | +} |
| 118 | + |
| 119 | +export function formatCategoryDisplay(category: string | undefined): CategoryDisplay { |
| 120 | + const raw = cleanOptional(category) ?? 'Uncategorized'; |
| 121 | + if (!isTraversalLikeCategory(raw)) { |
| 122 | + return { label: raw }; |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + label: basenameFromCategory(raw) ?? 'Uncategorized', |
| 127 | + mutedLabel: raw, |
| 128 | + }; |
| 129 | +} |
0 commit comments