use super::*;
#[cfg(unix)]
#[test]
fn shep_log_json_makes_the_daemons_own_records_json() {
let dir = tempfile::tempdir().unwrap();
let log = daemon_log_after_a_missed_handshake(&dir, &[("SHEP_LOG_JSON", "1")]);
let lines: Vec<&str> = log.lines().filter(|line| !line.trim().is_empty()).collect();
assert!(
!lines.is_empty(),
"the daemon must have written something to read: {log:?}"
);
let records: Vec<serde_json::Value> = lines
.iter()
.map(|line| {
serde_json::from_str(line).unwrap_or_else(|err| {
panic!("every line of shepd.err.log must be JSON under log_json: {line:?} ({err})")
})
})
.collect();
assert!(
records.iter().any(|record| {
record["level"] == "WARN"
&& record["fields"]["message"]
.as_str()
.is_some_and(|message| message.contains(READINESS_RECORD))
}),
"the readiness record must survive as a JSON object with its level and \
message intact: {records:?}"
);
}
#[cfg(unix)]
#[test]
fn the_daemons_own_log_carries_no_ansi_escapes() {
let dir = tempfile::tempdir().unwrap();
let log = daemon_log_after_a_missed_handshake(&dir, &[]);
assert!(
log.contains(READINESS_RECORD),
"precondition: the daemon must have written a record to colour: {log:?}"
);
assert!(
!log.contains('\x1b'),
"a log file is not a terminal: {log:?}"
);
}
#[cfg(unix)]
#[test]
fn shep_log_level_decides_which_of_the_daemons_records_survive() {
let at_default = tempfile::tempdir().unwrap();
let default_log = daemon_log_after_a_missed_handshake(&at_default, &[]);
assert!(
default_log.contains(READINESS_RECORD),
"a warn-level record must reach the log at the default level: {default_log:?}"
);
let at_error = tempfile::tempdir().unwrap();
let error_log = daemon_log_after_a_missed_handshake(&at_error, &[("SHEP_LOG_LEVEL", "error")]);
assert!(
!error_log.contains(READINESS_RECORD),
"SHEP_LOG_LEVEL=error must filter out the same warn-level record the \
default level lets through: {error_log:?}"
);
}