use super::{at, read, write};
use crate::test_support::Scratch;
use crate::ui::Aim;
fn aimed() -> Aim {
Aim {
channel: "(this box's own engine)".to_owned(),
address: "home".to_owned(),
}
}
#[test]
fn where_the_window_was_pointed_comes_back_and_so_does_nowhere() {
let scratch = Scratch::new();
write(scratch.path(), Some(aimed())).expect("written");
assert_eq!(read(scratch.path()), Some(aimed()));
write(scratch.path(), None).expect("written");
assert_eq!(read(scratch.path()), None);
}
#[test]
fn a_state_root_that_is_not_there_yet_is_made() {
let scratch = Scratch::new();
let root = scratch.join("never/made/before");
write(&root, Some(aimed())).expect("written");
assert_eq!(read(&root), Some(aimed()));
}
#[test]
fn nothing_a_file_can_be_wrong_about_refuses() {
let scratch = Scratch::new();
assert_eq!(read(scratch.path()), None, "no file at all");
for body in [
"",
"not json",
"[]",
"{}",
r#"{"aim": null}"#,
r#"{"aim": {}}"#,
r#"{"aim": {"channel": "own"}}"#,
r#"{"aim": {"address": "home"}}"#,
r#"{"aim": {"channel": 7, "address": "home"}}"#,
] {
std::fs::write(at(scratch.path()), body).expect("write");
assert_eq!(read(scratch.path()), None, "{body}");
}
}
#[test]
fn an_unknown_key_is_ignored_so_the_next_fact_is_a_key_and_not_a_format() {
let scratch = Scratch::new();
std::fs::write(
at(scratch.path()),
r#"{"aim": {"channel": "(this box's own engine)", "address": "home",
"scrolled_to": 42}, "draft": "not read yet"}"#,
)
.expect("write");
assert_eq!(read(scratch.path()), Some(aimed()));
}
#[test]
fn a_root_that_cannot_be_made_says_so_and_names_itself() {
let scratch = Scratch::new();
let blocked = scratch.join("a-file");
std::fs::write(&blocked, b"not a directory").expect("write");
let refusal = write(&blocked.join("under"), None).expect_err("refused");
assert!(refusal.contains("under"), "{refusal}");
}