use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use printobserver_core::{DEFAULT_DETECTOR_RESUME_GRACE, MAX_LOOK_WAIT_S};
use printobserver_obico::HANDLED_OVERWRITE;
use printobserver_server::DEFAULT_OBSERVATION_INTERVAL_S;
fn document(relative: &str) -> String {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../skills/printobserver")
.join(relative);
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("{} reads: {error}", path.display()));
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn spoken(seconds: u64) -> &'static str {
match seconds {
20 => "twenty seconds",
90 => "a minute and a half",
other => panic!(
"the skill says {other} seconds in words: spell it here and rewrite the skill's \
sentence to it"
),
}
}
#[test]
fn every_stated_look_wait_is_the_cores_limit() {
let limit = u64::from(MAX_LOOK_WAIT_S);
let figure = format!("at most {MAX_LOOK_WAIT_S}");
for (relative, passage) in [
(
"SKILL.md",
format!("can wait up to {} first", spoken(limit)),
),
("reference/api-and-clients.md", figure.clone()),
("reference/command-surface.md", figure.clone()),
(
"reference/command-surface.md",
format!("`refused` for a wait over {MAX_LOOK_WAIT_S}"),
),
("reference/common-operations.md", figure.clone()),
] {
assert!(
document(relative).contains(&passage),
"{relative} no longer says `{passage}`, which is how long the core lets a look wait"
);
}
}
#[test]
fn every_stated_resume_grace_is_the_cores() {
let grace = spoken(DEFAULT_DETECTOR_RESUME_GRACE.as_secs());
for (relative, passage) in [
(
"SKILL.md",
format!("resumes the print itself {grace} after your last such change"),
),
(
"reference/intervention-policy.md",
format!("{grace} after the agent's last such adjustment"),
),
] {
assert!(
document(relative).contains(&passage),
"{relative} no longer says `{passage}`, which is when the core resumes the print"
);
}
}
#[test]
fn the_stated_overwrite_is_the_one_obico_is_sent() {
let passage = format!("with the overwrite `{HANDLED_OVERWRITE}`");
assert!(
document("reference/intervention-policy.md").contains(&passage),
"the intervention policy no longer says `{passage}`, which is what the adapter sends"
);
}
#[test]
fn every_stated_observation_interval_is_the_servers_default() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let turns = 8 * 60 * 60 / DEFAULT_OBSERVATION_INTERVAL_S;
for relative in [
"scripts/install-service.sh",
"scripts/install-service.ps1",
"tests/real-prints/service-config.toml",
] {
let path = root.join(relative);
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("{} reads: {error}", path.display()));
let folded = text
.lines()
.map(|line| line.trim_start_matches('#').trim())
.collect::<Vec<_>>()
.join(" ");
for passage in [
format!("observation_interval_s = {DEFAULT_OBSERVATION_INTERVAL_S}"),
format!("Left out, every {DEFAULT_OBSERVATION_INTERVAL_S} seconds"),
format!("an eight-hour print is {turns} turns"),
] {
assert!(
folded.contains(&passage),
"{relative} does not say `{passage}`"
);
}
}
}
#[test]
fn every_configuration_shows_the_camera_calibration() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let key = printobserver_server::ConfigField::CameraCalibration.key();
let (table, field) = key.split_once('.').expect("a dotted key");
for relative in [
"scripts/install-service.sh",
"scripts/install-service.ps1",
"scripts/install.sh",
"scripts/install.ps1",
"tests/real-prints/service-config.toml",
] {
let path = root.join(relative);
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("{} reads: {error}", path.display()));
let lines: Vec<&str> = text
.lines()
.map(|line| line.trim_start_matches('#').trim())
.collect();
let in_table = lines
.iter()
.position(|line| *line == format!("[{table}]"))
.unwrap_or_else(|| panic!("{relative} shows no [{table}] table"));
assert!(
lines[in_table..]
.iter()
.take_while(|line| !line.starts_with('[') || **line == format!("[{table}]"))
.any(|line| line.starts_with(&format!("{field} = "))),
"{relative} shows no `{field}` in its [{table}] table"
);
let folded = lines.join(" ");
assert!(
folded.contains("calibration file") && folded.contains("refuses to start"),
"{relative} does not say what `{key}` names and that an invalid file is refused"
);
}
let readme = std::fs::read_to_string(root.join("README.md")).expect("the README reads");
assert!(
readme.contains(&format!("`{key}` is optional")) && readme.contains("refuses to start"),
"the README does not describe `{key}`"
);
}
fn counted(count: usize) -> &'static str {
const WORDS: [&str; 50] = [
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
"Twenty",
"Twenty-one",
"Twenty-two",
"Twenty-three",
"Twenty-four",
"Twenty-five",
"Twenty-six",
"Twenty-seven",
"Twenty-eight",
"Twenty-nine",
"Thirty",
"Thirty-one",
"Thirty-two",
"Thirty-three",
"Thirty-four",
"Thirty-five",
"Thirty-six",
"Thirty-seven",
"Thirty-eight",
"Thirty-nine",
"Forty",
"Forty-one",
"Forty-two",
"Forty-three",
"Forty-four",
"Forty-five",
"Forty-six",
"Forty-seven",
"Forty-eight",
"Forty-nine",
];
WORDS.get(count).copied().unwrap_or_else(|| {
panic!("{count} kinds: spell the count here and in the architecture reference")
})
}
fn declared_kinds() -> BTreeMap<String, BTreeSet<String>> {
let schemas = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../schemas");
let mut found: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
for crate_dir in std::fs::read_dir(&schemas).expect("the schemas read") {
let crate_dir = crate_dir.expect("a schema directory").path();
let Some(owner) = crate_dir.file_name().and_then(|name| name.to_str()) else {
continue;
};
let Ok(files) = std::fs::read_dir(&crate_dir) else {
continue;
};
for file in files {
let path = file.expect("a schema file").path();
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("{} reads: {error}", path.display()));
let Ok(schema) = serde_json::from_str::<serde_json::Value>(&text) else {
continue;
};
if let Some(kind) = schema[printobserver_types::EVENT_KIND_MARKER].as_str() {
found
.entry(owner.to_owned())
.or_default()
.insert(kind.to_owned());
}
}
}
found
}
fn quoted(clause: &str) -> Vec<String> {
clause
.split('`')
.skip(1)
.step_by(2)
.map(str::to_owned)
.collect()
}
#[test]
fn the_stated_event_kinds_are_the_ones_the_schemas_declare() {
let declared = declared_kinds();
let total: usize = declared.values().map(BTreeSet::len).sum();
let text = document("reference/architecture.md");
let opening = format!(
"{} kinds are declared today, each by its owner: ",
counted(total)
);
let start = text
.find(&opening)
.unwrap_or_else(|| panic!("the architecture reference no longer says `{opening}`"))
+ opening.len();
let passage = &text[start..];
let passage = &passage[..passage
.find(". ")
.expect("the inventory ends with its sentence")];
let stated: BTreeMap<String, BTreeSet<String>> = passage
.split(';')
.filter_map(|clause| {
let mut names = quoted(clause).into_iter();
let owner = names.next()?;
Some((owner, names.collect()))
})
.collect();
assert_eq!(
stated, declared,
"the architecture reference's event kinds are not the ones the schemas declare"
);
}