use super::{Place, at, read, write};
use crate::test_support::Scratch;
use crate::ui::{Aim, Dragged, Engines};
fn aimed() -> Aim {
Aim {
channel: "(this box's own engine)".to_owned(),
address: "home".to_owned(),
}
}
fn pointed() -> Place {
Place {
aim: Some(aimed()),
engines: Engines::default(),
dragged: Dragged::default(),
}
}
#[test]
fn where_the_window_was_pointed_comes_back_and_so_does_nowhere() {
let scratch = Scratch::new();
write(scratch.path(), &pointed()).expect("written");
assert_eq!(read(scratch.path()), pointed());
write(scratch.path(), &Place::default()).expect("written");
assert_eq!(read(scratch.path()), Place::default());
}
#[test]
fn the_accordion_comes_back_with_the_aim() {
let scratch = Scratch::new();
let arranged = Place {
aim: Some(aimed()),
engines: Engines {
open: Some("lab".to_owned()),
opened: [("lab".to_owned(), 7), ("home".to_owned(), 3)]
.into_iter()
.collect(),
aimed: [("lab".to_owned(), "bench".to_owned())]
.into_iter()
.collect(),
},
dragged: Dragged::default(),
};
write(scratch.path(), &arranged).expect("written");
assert_eq!(read(scratch.path()), arranged);
}
#[test]
fn every_edge_the_operator_dragged_comes_back_beside_the_aim() {
let scratch = Scratch::new();
let laid_out = Place {
dragged: Dragged {
list: Some(360.5),
rows: Some(7),
},
..pointed()
};
write(scratch.path(), &laid_out).expect("written");
assert_eq!(read(scratch.path()), laid_out);
}
#[test]
fn a_file_that_names_no_edge_reads_back_as_no_drag_at_all() {
let scratch = Scratch::new();
std::fs::write(
at(scratch.path()),
r#"{"aim": {"channel": "(this box's own engine)", "address": "home"}}"#,
)
.expect("write");
assert_eq!(read(scratch.path()), pointed());
}
#[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, &pointed()).expect("written");
assert_eq!(read(&root), pointed());
}
#[test]
fn nothing_a_file_can_be_wrong_about_refuses() {
let scratch = Scratch::new();
assert_eq!(read(scratch.path()), Place::default(), "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"}}"#,
r#"{"open": 7, "opened": [], "aimed": "lab"}"#,
r#"{"list_width": "wide"}"#,
r#"{"list_width": null}"#,
r#"{"roster_width": 360.5, "convs_width": 240.0}"#,
r#"{"composer_rows": 900}"#,
r#"{"composer_rows": -1}"#,
r#"{"composer_rows": 3.5}"#,
] {
std::fs::write(at(scratch.path()), body).expect("write");
assert_eq!(read(scratch.path()), Place::default(), "{body}");
}
}
#[test]
fn a_row_this_build_cannot_read_costs_one_row_and_not_the_table() {
let scratch = Scratch::new();
std::fs::write(
at(scratch.path()),
r#"{"opened": {"lab": 7, "home": "whenever"},
"aimed": {"lab": "bench", "home": 3}}"#,
)
.expect("write");
let held = read(scratch.path());
assert_eq!(
held.engines.opened,
[("lab".to_owned(), 7)].into_iter().collect()
);
assert_eq!(
held.engines.aimed,
[("lab".to_owned(), "bench".to_owned())]
.into_iter()
.collect()
);
}
#[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()), pointed());
}
#[test]
fn the_place_is_a_projection_of_the_model_and_not_a_stored_copy() {
let mut model = crate::test_support::window::seated();
model.engines.open = Some("(this box's own engine)".to_owned());
let held = Place::of(&model);
assert_eq!(held.aim, model.aim);
assert_eq!(held.engines, model.engines);
assert_eq!(held.dragged, model.dragged);
}
#[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"), &Place::default()).expect_err("refused");
assert!(refusal.contains("under"), "{refusal}");
}