Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

tests/viz_knowledge.rs

//! Inject-and-assert test for the viz **Knowledge** tab (plan item C2, LAW #1).
//!
//! The map is the code room's (`Viz.Knowledge`: one bubble per local crate).
//! A workspace nothing was consumed into must SAY so — the reader's
//! `not_consumed`, with its sentence — never a `crates: 0` map that reads as
//! "this workspace has no crates"; and a workspace switch must re-scope it.
//! (The map itself, over a consumed workspace: `viz::knowledge::tests`.)
#![cfg(feature = "viz")]

use std::fs;

use nornir::viz::UrdrThreadsApp;

fn assert_not_consumed(kn: &serde_json::Value) {
    assert_eq!(kn["state"], "unavailable", "{kn}");
    let note = kn["note"].as_str().unwrap_or_default();
    assert!(note.contains("[not_consumed]") && note.contains("nothing of this workspace was consumed"), "the reader's reason, by state: {kn}");
    assert!(
        kn.get("crates").is_none() && kn.get("rows").is_none() && kn.get("bubbles").is_none(),
        "no empty count that could read as 'no crates': {kn}"
    );
}

#[test]
fn knowledge_tab_says_not_consumed_instead_of_an_empty_map() {
    let tmp = tempfile::tempdir().unwrap();
    let wh = tmp.path().join("warehouse");
    fs::create_dir_all(&wh).unwrap();
    let mut app = UrdrThreadsApp::with_repos(
        wh,
        "testws".to_string(),
        tmp.path().to_path_buf(),
        vec!["alpha".to_string(), "beta".to_string()],
    );
    assert_eq!(app.state_json()["knowledge"]["state"], "not_loaded", "nothing asked yet is its own state");

    app.knowledge_load_for_test();
    let kn = &app.state_json()["knowledge"];
    assert_not_consumed(kn);
    assert_eq!(kn["source"], "local");
    assert_eq!(kn["configured_repos"], serde_json::json!(["alpha", "beta"]));
}

#[test]
fn knowledge_switch_workspace_rescopes() {
    let tmp = tempfile::tempdir().unwrap();
    let wh = tmp.path().join("warehouse");
    fs::create_dir_all(&wh).unwrap();
    let mut app = UrdrThreadsApp::with_repos(
        wh,
        "testws".to_string(),
        tmp.path().to_path_buf(),
        vec!["alpha".to_string()],
    );
    assert_eq!(app.state_json()["knowledge"]["configured_repos"], serde_json::json!(["alpha"]));
    app.knowledge_load_for_test();

    app.knowledge_set_workspace_for_test(tmp.path().to_path_buf(), vec!["gamma".to_string()]);
    let kn = &app.state_json()["knowledge"];
    assert_eq!(kn["configured_repos"], serde_json::json!(["gamma"]), "re-scoped to new repos");
    app.knowledge_load_for_test();
    assert_not_consumed(&app.state_json()["knowledge"]);
}