|
| 1 | +export const STATUS_VALUES = Object.freeze(["PASS", "WARN", "FAIL", "PENDING", "INFO", "SKIP"]); |
| 2 | + |
| 3 | +export function statusText(value, fallback = "not available") { |
| 4 | + const text = String(value ?? "").trim(); |
| 5 | + return text || fallback; |
| 6 | +} |
| 7 | + |
| 8 | +export function normalizeStatusValue(value, fallback = "PENDING") { |
| 9 | + const normalized = String(value || "").trim().toUpperCase(); |
| 10 | + return STATUS_VALUES.includes(normalized) ? normalized : fallback; |
| 11 | +} |
| 12 | + |
| 13 | +export function formatStatusMessage(status, message, options = {}) { |
| 14 | + const normalized = normalizeStatusValue(status, options.fallbackStatus || "PENDING"); |
| 15 | + const resolvedMessage = Object.hasOwn(options, "fallbackMessage") |
| 16 | + ? statusText(message, options.fallbackMessage) |
| 17 | + : String(message); |
| 18 | + return `${normalized}: ${resolvedMessage}`; |
| 19 | +} |
| 20 | + |
| 21 | +export function formatStatusReason(status, reason, options = {}) { |
| 22 | + const normalized = normalizeStatusValue(status, options.fallbackStatus || "PENDING"); |
| 23 | + return `${normalized}: ${statusText(reason, options.fallbackReason || "Safe server diagnostics did not provide a reason.")}`; |
| 24 | +} |
| 25 | + |
| 26 | +export function applyStatusNode(node, status, options = {}) { |
| 27 | + if (!node) { |
| 28 | + return ""; |
| 29 | + } |
| 30 | + const normalized = normalizeStatusValue(status, options.fallbackStatus || "PENDING"); |
| 31 | + node.textContent = normalized; |
| 32 | + node.dataset.healthStatus = normalized; |
| 33 | + if (normalized === "PASS" && !options.reason) { |
| 34 | + node.removeAttribute("title"); |
| 35 | + node.removeAttribute("aria-label"); |
| 36 | + return normalized; |
| 37 | + } |
| 38 | + const reason = statusText(options.reason, options.titleFallback || "Safe server diagnostics returned this non-PASS status."); |
| 39 | + node.setAttribute("title", `${options.titlePrefix || "Reason: "}${reason}`); |
| 40 | + node.setAttribute("aria-label", formatStatusReason(normalized, options.reason, options)); |
| 41 | + return normalized; |
| 42 | +} |
0 commit comments