use crate::model::{status, TestResultRow};
pub fn functional_row(component: &str, check: &str, ok: bool, detail: &str) -> TestResultRow {
TestResultRow {
run_id: String::new(),
repo: String::new(),
suite: component.to_string(),
test_name: check.to_string(),
status: if ok { status::PASS } else { status::FAIL }.to_string(),
duration_ms: 0.0,
ts_micros: 0,
message: detail.to_string(),
aspect: "functional".to_string(),
metric: if ok { 0.0 } else { 1.0 },
}
}
#[cfg(feature = "testmatrix")]
mod recorder {
use super::functional_row;
use crate::model::TestResultRow;
use std::sync::{Mutex, OnceLock};
fn buffer() -> &'static Mutex<Vec<TestResultRow>> {
static BUF: OnceLock<Mutex<Vec<TestResultRow>>> = OnceLock::new();
BUF.get_or_init(|| Mutex::new(Vec::new()))
}
pub fn record(component: &str, check: &str, ok: bool, detail: &str) {
let row = functional_row(component, check, ok, detail);
let mut buf = buffer().lock().unwrap_or_else(|p| p.into_inner());
buf.push(row);
}
pub fn drain() -> Vec<TestResultRow> {
let mut buf = buffer().lock().unwrap_or_else(|p| p.into_inner());
std::mem::take(&mut *buf)
}
}
#[cfg(feature = "testmatrix")]
pub fn functional_status(component: &str, check: &str, ok: bool, detail: &str) {
recorder::record(component, check, ok, detail);
}
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn functional_status(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
#[cfg(feature = "testmatrix")]
pub fn drain_functional_rows() -> Vec<TestResultRow> {
recorder::drain()
}
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn drain_functional_rows() -> Vec<TestResultRow> {
Vec::new()
}
#[cfg(all(test, feature = "testmatrix"))]
pub(crate) fn test_lock() -> std::sync::MutexGuard<'static, ()> {
use std::sync::{Mutex, OnceLock};
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
.lock()
.unwrap_or_else(|p| p.into_inner())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::ASPECT_UNIT;
#[test]
fn functional_row_maps_ok_false_to_red_fail() {
let r = functional_row(
"facett-map",
"basemap_rendered",
false,
"0 ways, blank framebuffer",
);
assert_eq!(r.suite, "facett-map", "component โ suite");
assert_eq!(r.test_name, "basemap_rendered", "check โ test_name");
assert_eq!(r.status, status::FAIL, "ok=false is a fail");
assert!(
status::is_red(&r.status),
"and it reads as RED for the matrix"
);
assert_eq!(r.message, "0 ways, blank framebuffer", "detail preserved");
assert_eq!(r.aspect, "functional");
assert_eq!(r.metric, 1.0, "one red check");
assert_ne!(r.aspect, ASPECT_UNIT);
}
#[test]
fn functional_row_maps_ok_true_to_green_pass() {
let r = functional_row("facett-map", "basemap_rendered", true, "1024 ways drawn");
assert_eq!(r.status, status::PASS, "ok=true is a pass");
assert!(status::is_green(&r.status), "GREEN for the matrix");
assert!(!status::is_red(&r.status));
assert_eq!(r.metric, 0.0, "no red checks");
assert_eq!(r.message, "1024 ways drawn");
}
#[cfg(feature = "testmatrix")]
#[test]
fn emit_red_then_drain_yields_red_row_and_clears() {
let _guard = super::test_lock();
let _ = drain_functional_rows();
functional_status("facett-map", "drain_red_check", false, "blank framebuffer");
let rows = drain_functional_rows();
let mine: Vec<_> = rows
.iter()
.filter(|r| r.test_name == "drain_red_check")
.collect();
assert_eq!(mine.len(), 1, "exactly the one I emitted");
assert_eq!(mine[0].status, status::FAIL);
assert!(status::is_red(&mine[0].status), "broken render = RED row");
assert_eq!(mine[0].message, "blank framebuffer");
let again = drain_functional_rows();
assert!(
!again.iter().any(|r| r.test_name == "drain_red_check"),
"drain clears the buffer"
);
}
#[cfg(feature = "testmatrix")]
#[test]
fn emit_green_drains_as_green_row() {
let _guard = super::test_lock();
let _ = drain_functional_rows();
functional_status("facett-map", "drain_green_check", true, "1024 ways");
let rows = drain_functional_rows();
let mine: Vec<_> = rows
.iter()
.filter(|r| r.test_name == "drain_green_check")
.collect();
assert_eq!(mine.len(), 1);
assert_eq!(mine[0].status, status::PASS);
assert!(status::is_green(&mine[0].status));
}
#[cfg(not(feature = "testmatrix"))]
#[test]
fn release_path_is_a_noop_drain_is_empty() {
functional_status("facett-map", "basemap_rendered", false, "would be red");
functional_status("facett-map", "anything", true, "");
assert!(
drain_functional_rows().is_empty(),
"release build records + drains NOTHING โ zero matrix write"
);
}
}