42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import type { FindingRecord, RecordField } from '../../domain/finding';
|
|
|
|
const normalizedLabel = (label: string) => label.trim().toLocaleLowerCase('en-US').replace(/[\s_-]+/g, ' ');
|
|
|
|
function field(record: FindingRecord, label: string): RecordField | undefined {
|
|
return record.fields.find(item => normalizedLabel(item.label) === label);
|
|
}
|
|
|
|
/** Maester carries an ID-prefixed Name beside the actual Title. Work uses the Title. */
|
|
export function maesterFindingTitle(record: FindingRecord, fallback: string): string {
|
|
return field(record, 'title')?.value.trim() || fallback;
|
|
}
|
|
|
|
export function maesterTestResultText(status: string, detail = ''): string {
|
|
if (detail.trim()) return detail.trim();
|
|
if (/^pass(?:ed)?$/i.test(status)) return 'Test passed.';
|
|
if (/^fail(?:ed)?$/i.test(status)) return 'Test failed.';
|
|
if (/^investigate$/i.test(status)) return 'Test requires investigation.';
|
|
if (/^(?:skip(?:ped)?|not\s*run)$/i.test(status)) return 'Test skipped.';
|
|
return status.trim();
|
|
}
|
|
|
|
export function maesterTagText(value: string): string {
|
|
try {
|
|
const parsed = JSON.parse(value) as unknown;
|
|
if (Array.isArray(parsed) && parsed.every(item => typeof item === 'string')) {
|
|
return parsed.map(item => item.trim()).filter(Boolean).join(' · ');
|
|
}
|
|
} catch { /* Preserve the unchanged source text when it is not JSON. */ }
|
|
return value.trim();
|
|
}
|
|
|
|
export function maesterExecutionErrorText(value: string): string {
|
|
try {
|
|
const parsed = JSON.parse(value) as unknown;
|
|
if (Array.isArray(parsed) && parsed.every(item => typeof item === 'string')) {
|
|
return parsed.map(item => item.trim()).filter(Boolean).join('\n');
|
|
}
|
|
} catch { /* Preserve the unchanged source text when it is not JSON. */ }
|
|
return value.trim();
|
|
}
|