use tilo_resource::{Replica, Resource, ResourceKind, state_from_reading};
use crate::golden_path::Board;
tilo_test_env::scenario! {
fn a_reading_rebuilds_the_state_or_is_refused;
phase the_reading_a_host_publishes_rebuilds_the_state_its_view_reads;
{
let mut replica = Replica::<Board>::new().in_app("so.tilo.model", "Model");
replica.apply(Board::RENAME.with("Launch")).unwrap();
replica.apply(Board::ADD_CARD.with("Ship it")).unwrap();
let host_state = replica.state().unwrap();
let reading = Board::state_json(&host_state).expect("a board has a JSON form");
let kind = ResourceKind::of::<Board>();
let by_kind = kind
.to_json(&host_state)
.expect("the kind answers the same reading");
assert_eq!(reading, by_kind, "a record's reading is its model's JSON");
let rebuilt: Board = state_from_reading::<Board>(&reading).expect("the reading is the board");
assert_eq!(rebuilt, host_state, "the reading names the host's state");
assert_eq!(rebuilt.cards.len(), 1, "the card the host's action made");
let card = rebuilt.cards.values().next().expect("the card");
assert_eq!(card.text, "Ship it");
}
phase a_reading_that_is_not_the_record_is_refused;
{
let refused = state_from_reading::<Board>(&serde_json::json!("not a board"));
assert!(refused.is_err(), "a reading of another shape is refused");
}
}