use super::run;
use crate::render::json_string;
use std::path::{Path, PathBuf};
#[path = "adapter_bytes_test.rs"]
mod bytes;
#[path = "adapter_output_test.rs"]
mod output;
fn tree(name: &str, files: &[(&str, &str)]) -> Option<PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join(name);
std::fs::create_dir_all(&root).ok()?;
for (file, text) in files {
std::fs::write(root.join(file), text).ok()?;
}
Some(root)
}
fn read_payload(cwd: &Path, file: &str) -> String {
let cwd = json_string(&cwd.to_string_lossy());
let path = json_string(file);
format!(
"{{\"session_id\":\"s\",\"transcript_path\":\"/t.jsonl\",\
\"cwd\":{cwd},\"permission_mode\":\"default\",\
\"hook_event_name\":\"PreToolUse\",\"tool_name\":\"Read\",\
\"tool_input\":{{\"file_path\":{path}}},\
\"tool_use_id\":\"toolu_1\"}}"
)
}
fn output_payload(tool: &str, response: &str) -> String {
format!(
"{{\"session_id\":\"s\",\"cwd\":\"/nowhere\",\
\"hook_event_name\":\"PostToolUse\",\"tool_name\":\"{tool}\",\
\"tool_input\":{{\"url\":\"https://example.com\"}},\
\"tool_response\":{response}}}"
)
}
fn answer(stdin: &str) -> String {
answer_in(stdin, Path::new("."))
}
fn answer_in(stdin: &str, root: &Path) -> String {
let got = run(stdin.as_bytes(), root);
assert!(got.is_ok(), "{got:?}");
got.unwrap_or_default()
}
fn read_of(name: &str, files: &[(&str, &str)], file: &str) -> String {
let Some(root) = tree(name, files) else {
return String::from("(disk refused)");
};
let full = root.join(file);
answer_in(&read_payload(&root, &full.to_string_lossy()), &root)
}
#[test]
fn a_read_of_a_clean_file_passes_in_silence() {
let got = read_of("ctrm-guard-clean", &[("a.md", "plain\n")], "a.md");
assert_eq!(got, "");
}
#[test]
fn a_read_of_a_bidi_control_is_denied_by_name() {
let files = [("trojan.rs", "ok\nx\u{202E}y\u{2066}\n")];
let got = read_of("ctrm-guard-bidi", &files, "trojan.rs");
assert!(got.contains(r#""permissionDecision":"deny""#), "{got}");
let row = "trojan.rs:2:2 U+202E bidi-control -- 2 bidi override or tag";
assert!(got.contains(row), "{got}");
assert!(got.is_ascii(), "{got}");
}
#[test]
fn a_denied_read_names_the_next_step() {
let files = [("t.md", "a\u{E0041}\n")];
let got = read_of("ctrm-guard-next-step", &files, "t.md");
let step = "Read denied: run `ctrm fix ";
assert!(got.contains(step), "{got}");
assert!(
got.contains("t.md` to remove them, then Read again."),
"{got}"
);
}
#[test]
fn a_read_of_ordinary_hazards_passes_with_a_note() {
let files = [("a.c", "Donau\u{00AD}dampf \u{1B}[1m\u{0C}\n")];
let got = read_of("ctrm-guard-tier-note", &files, "a.c");
assert!(!got.contains("permissionDecision"), "{got}");
let note = "a.c:1:6 U+00AD invisible -- 3 invisible formatting or \
control character(s)";
assert!(got.contains(note), "{got}");
assert!(got.contains("do not change the file unless asked"), "{got}");
}
#[test]
fn a_read_of_an_em_dash_passes_with_a_note() {
let files = [("a.md", "a\u{2014}b\u{2014}c\n")];
let got = read_of("ctrm-guard-dash", &files, "a.md");
assert!(got.contains(r#""additionalContext":"#), "{got}");
assert!(got.contains("(outside-set: 2)"), "{got}");
assert!(!got.contains("permissionDecision"), "{got}");
}
#[test]
fn a_read_is_judged_against_the_rules_in_cwd() {
let files = [(".ctrm", "*.md ascii+typography\n"), ("a.md", "-\n")];
let got = read_of("ctrm-guard-granted", &files, "a.md");
assert_eq!(got, "");
let files = [(".ctrm", "*.md any\n"), ("a.md", "\u{2014}\n")];
let got = read_of("ctrm-guard-granted-any", &files, "a.md");
assert_eq!(got, "");
}
#[test]
fn the_note_fires_at_warn_and_is_silent_at_allow() {
let files = [(".ctrm", "* ascii !warn\n"), ("a.md", "a\u{2014}b\n")];
let got = read_of("ctrm-guard-note-warn", &files, "a.md");
assert!(got.contains("(outside-set: 1)"), "{got}");
let files = [(".ctrm", "* ascii !allow\n"), ("a.md", "a\u{2014}b\n")];
let got = read_of("ctrm-guard-note-allow", &files, "a.md");
assert_eq!(got, "");
}
#[test]
fn a_broken_rules_file_still_blocks_a_hazard() {
let files = [(".ctrm", "*.md nosuchset\n"), ("a.md", "a\u{202E}\n")];
let got = read_of("ctrm-guard-broken", &files, "a.md");
assert!(got.contains("a.md:1:2 U+202E bidi-control"), "{got}");
let files = [(".ctrm", "*.md nosuchset\n"), ("b.md", "fine\n")];
let got = read_of("ctrm-guard-broken-clean", &files, "b.md");
assert!(got.contains("judged for hazards only"), "{got}");
}
#[test]
fn a_read_of_a_missing_file_passes() {
let got = read_of("ctrm-guard-missing", &[], "absent.md");
assert_eq!(got, "");
}
#[test]
fn a_payload_without_cwd_resolves_against_the_root() {
let files = [("t.rs", "\u{202E}\n")];
let root = tree("ctrm-guard-nocwd", &files).unwrap_or_default();
let stdin = r#"{"hook_event_name":"PreToolUse","tool_name":"Read",
"tool_input":{"file_path":"t.rs"}}"#;
let got = answer_in(stdin, &root);
assert!(got.contains("t.rs:1:1 U+202E bidi-control"), "{got}");
}
#[test]
fn malformed_input_is_a_named_error() {
for bad in ["", "{", "not json", r#"{"tool_name":"Read"}"#] {
let why = run(bad.as_bytes(), Path::new("."))
.err()
.unwrap_or_default();
assert!(why.starts_with("hook input"), "{bad:?} -> {why}");
}
}