use super::*;
use serde_json::json;
#[test]
fn a_route_names_its_component_and_its_segments_are_props() {
let routes = BTreeMap::from([
("#/".to_string(), "page-shell".to_string()),
("#/ticket/:id".to_string(), "ticket-page".to_string()),
("#/ticket/new".to_string(), "ticket-new".to_string()),
("#/:epic/:id".to_string(), "epic-ticket".to_string()),
]);
let r = Router::new(&routes);
let at = |h: &str| r.resolve(h).map(|x| (x.component, Value::Object(x.props)));
assert_eq!(at("#/ticket/T-1"), Some(("ticket-page".into(), json!({ "id": "T-1" }))));
assert_eq!(at("#/ticket/new"), Some(("ticket-new".into(), json!({}))), "the literal pattern wins over :id");
assert_eq!(at("#/kiln/T-1"), Some(("epic-ticket".into(), json!({ "epic": "kiln", "id": "T-1" }))));
assert_eq!(at(""), Some(("page-shell".into(), json!({}))), "an empty hash is #/");
assert_eq!(at("#/"), Some(("page-shell".into(), json!({}))));
assert_eq!(at("#/a/b/c"), None);
}
#[test]
fn a_segment_is_percent_decoded() {
let routes = BTreeMap::from([("#/f/:filter".to_string(), "board-layout".to_string())]);
let r = Router::new(&routes);
let at = |h: &str| r.resolve(h).map(|x| Value::Object(x.props));
assert_eq!(at("#/f/status%20Ready"), Some(json!({ "filter": "status Ready" })));
assert_eq!(at("#/f/a%2Fb%25c"), Some(json!({ "filter": "a/b%c" })));
assert_eq!(at("#/f/100%"), Some(json!({ "filter": "100%" })));
assert_eq!(at("#/f/%E2%86%92"), Some(json!({ "filter": "→" })));
}
#[test]
fn a_route_row_draws_over_the_manifest() {
let routes = BTreeMap::from([
("#/".to_string(), "board-layout".to_string()),
("#/ticket/:id".to_string(), "ticket-page".to_string()),
("#/gone".to_string(), "old-page".to_string()),
]);
let rows = BTreeMap::from([
("#/".to_string(), "two-col".to_string()),
("#/two/:filter".to_string(), "two-col".to_string()),
("#/ticket/new".to_string(), "ticket-new".to_string()),
("#/gone".to_string(), "not-held".to_string()),
]);
let held = ["board-layout", "ticket-page", "old-page", "two-col", "ticket-new"];
let r = Router::merged(&routes, &rows, |c| held.contains(&c));
let at = |h: &str| r.resolve(h).map(|x| (x.component, Value::Object(x.props)));
assert_eq!(at("#/"), Some(("two-col".into(), json!({}))), "a row over the manifest's own");
assert_eq!(at("#/two/open"), Some(("two-col".into(), json!({ "filter": "open" }))), "a new pattern, its segment a prop");
assert_eq!(at("#/ticket/new"), Some(("ticket-new".into(), json!({}))), "a row's literal over the manifest's :id");
assert_eq!(at("#/ticket/T-1"), Some(("ticket-page".into(), json!({ "id": "T-1" }))), "the manifest's own, untouched");
assert_eq!(at("#/gone"), Some(("old-page".into(), json!({}))), "a row whose component is not held is passed over");
let none = Router::merged(&routes, &BTreeMap::new(), |_| true);
assert_eq!(none.resolve("#/").map(|x| x.component), Some("board-layout".into()), "no rows: the manifest's");
}