use std::fmt::Write as _;
use std::fs::File;
use std::io::Write as _;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
const BINARY: &str = env!("CARGO_BIN_EXE_ids-le");
static COUNTER: AtomicUsize = AtomicUsize::new(0);
const LIMIT: Duration = Duration::from_secs(60);
const UUID_V1: &str = "c232ab00-9414-11ec-b3c8-9e6bdeced846";
const UUID_V7: &str = "019ff344-cc00-7abc-8def-0123456789ab";
const ULID: &str = "01KZSM9K00ABCDEFGH12345678";
const OBJECT_ID: &str = "6a7bb780a1b2c3d4e5f60718";
const SNOWFLAKE: &str = "1536886938009600000";
struct Tree {
root: PathBuf,
}
impl Tree {
fn new(name: &str) -> Self {
let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
let root = std::env::temp_dir().join(format!(
"ids-le-platform-{name}-{}-{unique}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("a temporary directory");
Self {
root: std::fs::canonicalize(&root).expect("a canonical directory"),
}
}
fn text(&self) -> String {
self.root.to_string_lossy().into_owned()
}
fn write(&self, relative: &str, contents: &str) -> PathBuf {
let target = self.root.join(relative);
if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent).expect("a parent directory");
}
std::fs::write(&target, contents).expect("a file");
target
}
}
impl Drop for Tree {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.root);
}
}
struct Run {
code: Option<i32>,
stdout: String,
}
fn execute(args: &[&str], timezone: Option<&str>) -> Run {
let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
let capture = std::env::temp_dir().join(format!(
"ids-le-platform-capture-{}-{unique}",
std::process::id()
));
std::fs::create_dir_all(&capture).expect("a capture directory");
let out = capture.join("stdout");
let mut command = Command::new(BINARY);
command
.args(args)
.stdin(Stdio::null())
.stdout(File::create(&out).expect("a stdout file"))
.stderr(Stdio::null());
match timezone {
Some(zone) => command.env("TZ", zone),
None => command.env_remove("TZ"),
};
let mut child = command.spawn().expect("the binary runs");
let started = Instant::now();
let status = loop {
match child.try_wait().expect("the child can be waited on") {
Some(status) => break status,
None if started.elapsed() >= LIMIT => {
let _ = child.kill();
let _ = child.wait();
panic!("the run hung past {LIMIT:?}: {args:?}");
}
None => std::thread::sleep(Duration::from_millis(5)),
}
};
let stdout = String::from_utf8_lossy(&std::fs::read(&out).unwrap_or_default()).into_owned();
let _ = std::fs::remove_dir_all(&capture);
Run {
code: status.code(),
stdout,
}
}
fn run(args: &[&str]) -> Run {
execute(args, Some("UTC"))
}
fn reports(run: &Run) -> Vec<serde_json::Value> {
run.stdout
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| serde_json::from_str(line).expect("stdout carries only JSON"))
.collect()
}
fn skipped(case: &str, why: &str) {
eprintln!("SKIPPED {case}: {why}");
}
fn decodable() -> String {
format!(
"service:\n legacy_id: {UUID_V1}\n request_id: {UUID_V7}\nsession:\n ulid: {ULID}\n\
documents:\n - _id: {OBJECT_ID}\ndiscord:\n channel_id: {SNOWFLAKE}\n"
)
}
fn nested(name: &str) -> Tree {
let tree = Tree::new(name);
tree.write("ids.yaml", &decodable());
tree.write(
"src/deep/session.json",
&format!("{{\"ulid\":\"{ULID}\"}}\n"),
);
tree.write("config/app.toml", &format!("id = \"{UUID_V7}\"\n"));
tree
}
#[test]
fn every_path_in_the_report_is_separated_by_forward_slashes() {
let tree = nested("separators");
let outcome = run(&[&tree.text()]);
assert_eq!(outcome.code, Some(0));
let scanned = reports(&outcome);
assert_eq!(scanned.len(), 3, "the whole tree was walked");
for report in &scanned {
let file = report["file"].as_str().expect("a file name");
assert!(
!file.contains('\\'),
"a backslash in a reported path: {file}"
);
assert!(
file.contains('/'),
"a nested path lost its separators: {file}"
);
}
}
#[test]
fn the_decoded_timestamps_do_not_depend_on_the_time_zone() {
let tree = nested("timezone");
let utc = execute(&[&tree.text()], Some("UTC"));
let unset = execute(&[&tree.text()], None);
let hostile = execute(&[&tree.text()], Some("Pacific/Kiritimati"));
assert_eq!(utc.stdout, unset.stdout, "TZ=UTC differs from TZ unset");
assert_eq!(utc.stdout, hostile.stdout, "the report moved with the zone");
assert_eq!(utc.code, unset.code);
assert_eq!(utc.code, hostile.code);
let decoded: Vec<String> = reports(&utc)
.iter()
.flat_map(|report| report["ids"].as_array().expect("rows").clone())
.filter_map(|row| row["timestamp"].as_str().map(str::to_string))
.collect();
assert!(
decoded.len() >= 5,
"the fixture stopped exercising the decoders: {decoded:?}"
);
assert!(
decoded.iter().all(|stamp| stamp.ends_with('Z')),
"a decode is not UTC: {decoded:?}"
);
for pinned in ["2022-02-22T19:22:22.000Z", "2026-08-12T00:00:00.000Z"] {
assert!(
decoded.iter().any(|stamp| stamp == pinned),
"{pinned} is not among the decodes: {decoded:?}"
);
}
}
#[test]
fn a_file_is_never_reported_twice_on_a_case_insensitive_filesystem() {
let tree = Tree::new("case");
tree.write("ids.yaml", &format!("id: {UUID_V7}\n"));
tree.write("IDS.YAML", &format!("id: {ULID}\n"));
let outcome = run(&[&tree.text()]);
let named: Vec<String> = reports(&outcome)
.iter()
.filter_map(|report| report["file"].as_str())
.map(str::to_string)
.collect();
if named.len() == 1 {
eprintln!("case-insensitive filesystem: the two names are one file");
}
let mut unique = named.clone();
unique.sort_unstable();
unique.dedup();
assert_eq!(
unique.len(),
named.len(),
"a file was reported twice: {named:?}"
);
assert!(
named.len() <= 2,
"more report lines than files written: {named:?}"
);
}
#[test]
fn the_walk_survives_the_reserved_windows_filenames() {
let tree = Tree::new("reserved");
tree.write("ids.yaml", &format!("id: {UUID_V7}\n"));
let mut made = Vec::new();
for reserved in ["CON", "PRN", "AUX", "NUL", "COM1"] {
match std::fs::write(tree.root.join(reserved), format!("id: {ULID}\n")) {
Ok(()) => made.push(reserved),
Err(_) => skipped(
&format!("a file named {reserved}"),
"this filesystem reserves the name",
),
}
}
let outcome = run(&[&tree.text()]);
let code = outcome.code.expect("an exit code, not a signal");
assert!((0..=2).contains(&code), "exit {code}");
let named: Vec<String> = reports(&outcome)
.iter()
.filter_map(|report| report["file"].as_str())
.map(|file| file.rsplit('/').next().unwrap_or(file).to_string())
.collect();
assert!(
named.iter().any(|file| file == "ids.yaml"),
"the reserved names took the rest of the tree with them: {named:?}\n\
created: {made:?}"
);
}
#[test]
fn windows_line_endings_do_not_change_the_report() {
let tree = Tree::new("crlf");
let document = decodable();
let unix = tree.write("unix.yaml", &document);
let windows = tree.write("windows.yaml", &document.replace('\n', "\r\n"));
let rows = |path: &PathBuf| -> serde_json::Value {
let outcome = run(&[&path.to_string_lossy()]);
assert_eq!(outcome.code, Some(0));
reports(&outcome)[0]["ids"].clone()
};
let expected = rows(&unix);
assert_eq!(
expected.as_array().expect("rows").len(),
5,
"the fixture stopped holding five identifiers"
);
assert_eq!(
rows(&windows),
expected,
"a carriage return moved the report"
);
let old_mac = tree.write("mac.yaml", &document.replace('\n', "\r"));
let single = rows(&old_mac);
assert!(
single
.as_array()
.expect("rows")
.iter()
.all(|row| row["line"] == 1),
"a lone carriage return started a line: {single}"
);
}
#[test]
fn a_child_that_refuses_before_reading_stdin_still_exits_two() {
let mut child = Command::new(BINARY)
.args(["--stdin", "unexpected.json"])
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("the binary runs");
let _ = child
.stdin
.as_mut()
.expect("stdin")
.write_all(&vec![b'x'; 1 << 20]);
drop(child.stdin.take());
assert_eq!(child.wait().expect("the child finishes").code(), Some(2));
}
#[test]
fn a_document_on_stdin_is_read_whole() {
let mut child = Command::new(BINARY)
.args(["--stdin", "--format", "yaml"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("the binary runs");
let mut document = String::new();
for row in 1..=10_000u32 {
let _ = writeln!(document, "k{row}: f47ac10b-58cc-4372-a567-{row:012x}");
}
child
.stdin
.as_mut()
.expect("stdin")
.write_all(document.as_bytes())
.expect("the child is still reading");
drop(child.stdin.take());
let output = child.wait_with_output().expect("the child finishes");
assert_eq!(output.status.code(), Some(0));
let report: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("stdout carries JSON");
assert_eq!(
report["summary"]["ids"], 10_000,
"a document arriving in pieces was read short"
);
assert_eq!(report["file"], "<stdin>");
}
#[test]
fn a_closed_stdin_is_an_empty_document() {
let mut child = Command::new(BINARY)
.arg("--stdin")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("the binary runs");
drop(child.stdin.take());
let output = child.wait_with_output().expect("the child finishes");
assert_eq!(output.status.code(), Some(1), "nothing found is exit 1");
let report: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("stdout carries JSON");
assert_eq!(
report["summary"],
serde_json::json!({ "ids": 0, "refused": 0 })
);
}
#[test]
fn the_mcp_server_exits_cleanly_when_stdin_closes_immediately() {
let mut child = Command::new(BINARY)
.arg("mcp")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("the binary runs");
drop(child.stdin.take());
let output = child.wait_with_output().expect("the child finishes");
assert_eq!(
output.status.code(),
Some(0),
"stdout was {:?}",
String::from_utf8_lossy(&output.stdout)
);
}
#[test]
fn a_refusal_names_the_path_the_caller_gave_it() {
let tree = Tree::new("named-path");
let missing = tree.root.join("not-here.json");
let given = missing.to_string_lossy().into_owned();
let output = Command::new(BINARY)
.arg(&given)
.stdin(Stdio::null())
.output()
.expect("the binary runs");
assert_eq!(output.status.code(), Some(2));
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(&given),
"the refusal rewrote the path it was given\n given: {given}\n said: {stderr}"
);
assert!(output.stdout.is_empty(), "a refusal wrote to stdout");
}
#[test]
fn a_nested_file_written_by_this_suite_is_found_by_the_walk() {
let tree = nested("sanity");
let found: Vec<String> = reports(&run(&[&tree.text()]))
.iter()
.filter_map(|report| report["file"].as_str())
.map(|file| file.rsplit('/').next().unwrap_or(file).to_string())
.collect();
for expected in ["ids.yaml", "session.json", "app.toml"] {
assert!(
found.iter().any(|file| file == expected),
"{expected} was written and not walked: {found:?}"
);
}
}
#[test]
fn awkward_file_names_are_walked() {
let tree = Tree::new("names");
let mut written = Vec::new();
for name in [
"with space.yaml",
"\u{fc}nicode.yaml",
"\u{1f389}.yaml",
"trailing.dots..yaml",
] {
match std::fs::write(tree.root.join(name), format!("id: {UUID_V7}\n")) {
Ok(()) => written.push(name),
Err(_) => skipped("an awkward file name", name),
}
}
assert!(
!written.is_empty(),
"this filesystem refused every awkward name"
);
let outcome = run(&[&tree.text()]);
assert_eq!(outcome.code, Some(0));
let named: Vec<String> = reports(&outcome)
.iter()
.filter_map(|report| report["file"].as_str())
.map(|file| file.rsplit('/').next().unwrap_or(file).to_string())
.collect();
for name in written {
assert!(named.iter().any(|file| file == name), "{name}: {named:?}");
}
}
#[test]
fn a_relative_path_is_reported_relative() {
let tree = Tree::new("relative");
tree.write("src/config.json", &format!("{{\"id\":\"{UUID_V7}\"}}\n"));
let output = Command::new(BINARY)
.arg("src")
.current_dir(&tree.root)
.stdin(Stdio::null())
.output()
.expect("the binary runs");
assert_eq!(output.status.code(), Some(0));
let report: serde_json::Value = serde_json::from_slice(
String::from_utf8_lossy(&output.stdout)
.lines()
.next()
.expect("a report line")
.as_bytes(),
)
.expect("stdout carries JSON");
assert_eq!(report["file"], "src/config.json");
}