plexus-ui-engine 0.2.2

The plexus-ui engine — signals, live() over Plexus streams, the view tree over a renderer, Rhai folds, sections, hot-swap
Documentation
//! FRAME-17: what the route needs at boot — its snapshots as the mount
//! asks them, the largest page first, shared by the mount; what it reaches.

use super::super::*;
use crate::live::ManualClock;
use crate::transport::FakeWire;
use crate::tree::Recording;
use serde_json::json;

fn tree(name: &str, props: &[&str], state: Value, live: Value, uses: &[&str], body: Value) -> String {
    json!({ "name": name, "abi": "engine/1", "props": props, "state": state, "live": live, "uses": uses, "handlers": sha256_hex(""), "tree": body }).to_string()
}

/// `#/f/:filter` → `lay`: its `main` section `rows` (a page of 200 under
/// the filter, after the declared state; the extents), its `side` section
/// `pal` (a `live` held by `when`, one outside its `uses`); `lone` in the
/// bundle, on no route.
fn setup() -> (Recording, FakeWire, Rc<App<Recording, FakeWire, ManualClock>>) {
    let r = Recording::new();
    let wire = FakeWire::new();
    let app = App::new(&Runtime::new(), r.clone(), wire.clone(), ManualClock::new());
    let lay = tree("lay", &["filter"], json!({}), json!({}), &[], json!([
        { "el": "div", "children": [ { "slot": "main", "props": { "filter": { "expr": "filter" } } }, { "slot": "side", "props": {} } ] }
    ]));
    let rows = tree("rows", &["filter"], json!({ "after": null }), json!({
        "ext": { "snapshot": "tickets.nav.extents", "params": {}, "changes": "tickets.watch", "changes_params": {}, "key": "shortcode", "on": {} },
        "page": { "snapshot": "tickets.list", "params": { "limit": 200, "summary": true, "filter": { "expr": "filter" }, "after": { "expr": "state.after" } },
                  "changes": "tickets.watch", "changes_params": { "filter": { "expr": "filter" } }, "key": "id", "on": { "created": "upsert:ticket" } }
    }), &["tickets.list", "tickets.nav.extents", "tickets.watch"], json!([ { "el": "p", "children": [ { "text": { "expr": "page.status" } } ] } ]));
    let pal = tree("pal", &[], json!({ "q": "" }), json!({
        "found": { "snapshot": "tickets.find", "params": { "q": { "expr": "state.q" } }, "changes_params": {}, "key": "id", "on": {}, "when": { "expr": "state.q" } },
        "sneak": { "snapshot": "tickets.secret", "params": {}, "changes_params": {}, "key": "id", "on": {} }
    }), &["tickets.find"], json!([ { "el": "p", "children": [] } ]));
    let lone = tree("lone", &[], json!({}), json!({ "x": { "snapshot": "tickets.list", "params": { "limit": 9 }, "changes_params": {}, "key": "id", "on": {} } }), &["tickets.list"], json!([]));
    let mut files = Files::default();
    let mut m = Manifest { abi: "engine/1".into(), ..Manifest::default() };
    for (name, t, uses) in [("lay", &lay, vec![]), ("rows", &rows, vec!["tickets.list", "tickets.nav.extents", "tickets.watch"]), ("pal", &pal, vec!["tickets.find"]), ("lone", &lone, vec!["tickets.list"])] {
        files.put_version("v1", name, t, "");
        m.components.insert(name.into(), ComponentEntry { tree: sha256_hex(t), handlers: sha256_hex(""), uses: uses.into_iter().map(String::from).collect(), ..ComponentEntry::default() });
    }
    m.slots.insert("lay".into(), [("main".to_string(), "rows".to_string()), ("side".to_string(), "pal".to_string())].into());
    m.routes.insert("#/f/:filter".into(), "lay".into());
    app.provide(&files);
    app.load("v1", m);
    (r, wire, app)
}

#[test]
fn the_boot_asks_the_routes_snapshots_as_the_mount_will() {
    let (r, wire, app) = setup();
    let props = json!({ "filter": "open" }).as_object().cloned().unwrap();
    let compiled = crate::tree::expr::compiles();
    let specs = app.boot_specs("lay", props.clone());
    assert_eq!(crate::tree::expr::compiles(), compiled, "the boot reads its specs without Rhai: nothing compiled before the mount");
    let got: Vec<(&str, &Value)> = specs.iter().map(|s| (s.snapshot.as_str(), &s.params)).collect();
    assert_eq!(
        got,
        vec![("tickets.list", &json!({ "after": null, "filter": "open", "limit": 200, "summary": true })), ("tickets.nav.extents", &json!({}))],
        "the page of 200 first, under the route's filter and the declared state; the held find and the call outside uses not at all"
    );
    assert_eq!(specs[0].changes_params, json!({ "filter": "open" }));
    let early: Vec<_> = specs.iter().map(|s| app.cache().acquire(s)).collect();
    assert_eq!(wire.opened().iter().map(|o| o.method.as_str()).collect::<Vec<_>>(), ["tickets.watch", "tickets.list", "tickets.watch", "tickets.nav.extents"]);
    app.mount(&r.root(), "lay", props).expect("the layout mounts");
    drop(early);
    assert_eq!(wire.opened_of("tickets.list").len(), 1, "the mount shares the early page");
    assert_eq!(wire.opened_of("tickets.nav.extents").len(), 1, "and the extents");
    assert!(wire.opened_of("tickets.find").is_empty(), "the held find waits for its query");
    assert!(crate::marks::taken().iter().any(|m| m == "plexus-ui:handlers-compiled:rows"));
}

#[test]
fn the_boot_reaches_the_route_and_its_sections_only() {
    let (_r, _wire, app) = setup();
    let mut got = app.reachable("lay");
    got.sort();
    assert_eq!(got, ["lay", "pal", "rows"], "lone is on no route's first page");
    app.set_assignments(vec![Assignment { layout: "lay".into(), slot: "side".into(), component: "lone".into(), version: "v1".into() }]);
    let mut got = app.reachable("lay");
    got.sort();
    assert_eq!(got, ["lay", "lone", "rows"], "an assignment fills its section instead of the default");
}

/// FRAME-19: the early send derives only what it can read without running
/// a component — an rhx tree's `live` declarations. A module's specs are
/// made by its own code when it mounts (a Svelte palette's `live({ …,
/// when: () => q.length > 0 })`), so the derivation skips modules whole: a
/// section filled by a `svelte/1` module whose spec is held by `when`
/// adds nothing to the boot's calls, and its module is still imported
/// (reachable) so its mount asks — or holds — its own.
#[test]
fn the_early_send_skips_a_held_module_spec() {
    let r = Recording::new();
    let wire = FakeWire::new();
    let app = App::new(&Runtime::new(), r.clone(), wire.clone(), ManualClock::new());
    let lay = tree("lay", &[], json!({}), json!({}), &[], json!([
        { "el": "div", "children": [ { "slot": "main", "props": {} }, { "slot": "side", "props": {} } ] }
    ]));
    let rows = tree("rows", &[], json!({}), json!({
        "page": { "snapshot": "tickets.list", "params": { "limit": 200 }, "changes": "tickets.watch", "changes_params": {}, "key": "id", "on": {} }
    }), &["tickets.list", "tickets.watch"], json!([ { "el": "p", "children": [] } ]));
    let mut files = Files::default();
    let mut m = Manifest { abi: "engine/1".into(), ..Manifest::default() };
    for (name, t, uses) in [("lay", &lay, vec![]), ("rows", &rows, vec!["tickets.list", "tickets.watch"])] {
        files.put_version("v1", name, t, "");
        m.components.insert(name.into(), ComponentEntry { tree: sha256_hex(t), handlers: sha256_hex(""), uses: uses.into_iter().map(String::from).collect(), ..ComponentEntry::default() });
    }
    // A stale rhx tree under the module's name (an older version's, its find
    // unheld): only the profile keeps the derivation from reading it.
    let stale = tree("pal-sv", &[], json!({}), json!({ "f": { "snapshot": "tickets.find", "params": { "q": "" }, "changes_params": {}, "key": "id", "on": {} } }), &["tickets.find"], json!([]));
    files.put_version("v1", "pal-sv", &stale, "");
    // The palette: a svelte/1 module, its find held by `when` in its code.
    m.components.insert("pal-sv".into(), ComponentEntry { profile: "svelte/1".into(), module: sha256_hex("export function mount() {}"), uses: vec!["tickets.find".into()], ..ComponentEntry::default() });
    m.slots.insert("lay".into(), [("main".to_string(), "rows".to_string()), ("side".to_string(), "pal-sv".to_string())].into());
    m.routes.insert("#/".into(), "lay".into());
    app.provide(&files);
    app.load("v1", m);

    let specs = app.boot_specs("lay", Map::new());
    let got: Vec<&str> = specs.iter().map(|s| s.snapshot.as_str()).collect();
    assert_eq!(got, ["tickets.list"], "the rhx page alone: the module's held find is not derived");
    let mut reach = app.reachable("lay");
    reach.sort();
    assert_eq!(reach, ["lay", "pal-sv", "rows"], "the module is still the route's: imported, its own specs its mount's");
}