//! The `via` binary on the fixture store: check, compile, explain, exit codes, and byte-identical output //! across separate processes (QA R-52). use std::path::PathBuf; use std::process::{Command, Output}; const NOW: &str = "2026-11-25T12:00:00+01:00"; fn home() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../roads/tests/fixtures/home") } fn via(store: &std::path::Path, args: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_via")) .arg("--store") .arg(store) .args(["--now", NOW]) .args(args) .output() .unwrap() } fn text(o: &Output) -> String { String::from_utf8_lossy(&o.stdout).into_owned() } #[test] fn check_passes_on_the_home_store_and_reports_the_inactive_roads() { let o = via(&home(), &["check"]); assert_eq!(o.status.code(), Some(0), "{}", text(&o)); let out = text(&o); assert!(out.contains("inactive: roads/m6v0hp-petr-git-expired.md:6: until: expired at 2026-11-01T00:00:00+01:00 [road.expired]"), "{out}"); assert!(out.contains("10 road files, 0 with errors"), "{out}"); } #[test] fn check_fails_on_a_bad_road_and_names_file_and_field() { let tmp = tempfile::tempdir().unwrap(); copy_store(tmp.path()); std::fs::write( tmp.path().join("roads/a00005-untill.md"), std::fs::read(home().join("../invalid/roads/a00005-untill.md")).unwrap(), ) .unwrap(); let o = via(tmp.path(), &["check"]); assert_eq!(o.status.code(), Some(1)); assert!( text(&o).contains("roads/a00005-untill.md:6: untill: unknown key [key.unknown]"), "{}", text(&o) ); } #[test] fn compile_prints_the_golden_plan_and_the_same_bytes_in_every_process() { let want = std::fs::read_to_string(home().join("expected/jana-phone.json")).unwrap(); let first = via(&home(), &["compile", "--device", "jana-phone"]); assert_eq!(first.status.code(), Some(0)); assert_eq!(text(&first), want); for _ in 0..4 { assert_eq!( via(&home(), &["compile", "--device", "jana-phone"]).stdout, first.stdout ); } let by_key = via( &home(), &[ "compile", "--device", "device:AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM", ], ); assert_eq!(by_key.stdout, first.stdout); } #[test] fn compile_cbor_is_a_valid_plan_message() { let o = via( &home(), &["compile", "--device", "router", "--format", "cbor"], ); assert_eq!(o.status.code(), Some(0)); let env = proto::Envelope::decode(&o.stdout).unwrap(); let proto::Body::Plan { plan, .. } = env.body else { panic!("not a plan message") }; assert_eq!(plan.name, "router"); assert!(plan.classic); } #[test] fn explain_allowed_and_denied() { let o = via( &home(), &["explain", "jana-phone", "service:photos.home.arpa"], ); assert_eq!(o.status.code(), Some(0)); let out = text(&o); assert!( out.starts_with("jana-phone can reach service:photos.home.arpa because of 2 roads:"), "{out}" ); assert!(out.contains("7tq0mz") && out.contains("k3f9q2")); let o = via(&home(), &["explain", "petr-phone", "service:git.home.arpa"]); assert_eq!(o.status.code(), Some(1)); let out = text(&o); assert!( out.starts_with("petr-phone cannot reach service:git.home.arpa: no road allows it."), "{out}" ); assert!( out.contains("m6v0hp") && out.contains("[road.expired]"), "the expired road is shown as granting nothing" ); } #[test] fn usage_and_unknown_devices_exit_2() { assert_eq!(via(&home(), &["frobnicate"]).status.code(), Some(2)); assert_eq!( via(&home(), &["compile", "--device", "nobody"]) .status .code(), Some(2) ); assert_eq!( via(&home(), &["explain", "jana-phone", "photos"]) .status .code(), Some(2) ); let o = Command::new(env!("CARGO_BIN_EXE_via")) .args(["--now", "tomorrow", "check"]) .output() .unwrap(); assert_eq!(o.status.code(), Some(2)); } #[test] fn a_broken_snapshot_compiles_nothing() { let tmp = tempfile::tempdir().unwrap(); copy_store(tmp.path()); std::fs::write( tmp.path().join("devices.json"), "{\"format\": 1, \"devices\": [], \"extra\": 1}", ) .unwrap(); let o = via(tmp.path(), &["compile", "--device", "jana-phone"]); assert_eq!(o.status.code(), Some(1)); assert!(o.stdout.is_empty(), "no plan is printed"); assert!(String::from_utf8_lossy(&o.stderr).contains("nothing compiles")); } fn copy_store(to: &std::path::Path) { std::fs::create_dir_all(to.join("roads")).unwrap(); for f in ["directory.json", "devices.json"] { std::fs::copy(home().join(f), to.join(f)).unwrap(); } for e in std::fs::read_dir(home().join("roads")).unwrap() { let e = e.unwrap(); std::fs::copy(e.path(), to.join("roads").join(e.file_name())).unwrap(); } }