wsb-daemon 0.275.7622

wsb's daemon: the workbench, the GitHub mirror's schedule and peer continuity, for every workspace on the machine
use super::*;
use wsb::test_environment::Environment;

fn git(dir: &Path, args: &[&str]) {
    let output = wsb::git_backend::git_command()
        .arg("-C")
        .arg(dir)
        .args(args)
        .output()
        .expect("git runs");
    assert!(output.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&output.stderr));
}

fn manage(root: &Path) {
    wsb::settings::RepoSettings {
        name: "names".to_string(),
        alias: "names".to_string(),
        templates_root: ".".to_string(),
        version: wsb::settings::VersionSettings::at_major(0),
    }
    .save(root)
    .expect("managed");
}

/// TEST20124: a file's notes are the wsb notes linked to it and the
/// worktree's documents that mention it, and the explorer's count for it is
/// both; a note about another file, or about this path in another project,
/// is not counted; a worktree whose project has no GitHub name still shows
/// its documents and says why it has no notes.
#[test]
fn test20124_a_path_shows_its_linked_notes_and_the_documents_that_mention_it() {
    let prj = tempfile::tempdir().expect("a workshop");
    let machine = tempfile::tempdir().expect("a machine folder");
    let root = wsb::platform::canonicalize(prj.path()).expect("resolves");
    let _env = Environment::set(&[
        ("PRJ", &root.to_string_lossy()),
        (wsb::daemon::MACHINE_DIR_ENV, &machine.path().to_string_lossy()),
    ]);
    let main = root.join("names/main-worktree");
    std::fs::create_dir_all(main.join("src")).expect("a worktree");
    std::fs::create_dir_all(main.join("docs")).expect("docs");
    git(&main, &["init", "--quiet"]);
    git(&main, &["remote", "add", "origin", "git@github.com:owner/names.git"]);
    manage(&main);
    std::fs::write(main.join("src/lib.rs"), "").expect("a file");
    std::fs::write(main.join("docs/design.md"), "See [the library](src/lib.rs).\n").expect("a document");
    let about_file = |file: &str| wsb::cli::annotations::Targets { refs: vec![file.to_string()], ..Default::default() };
    let (note, _) =
        wsb::cli::annotations::note_add(&main, "Why lazy", "", &about_file("path:owner/names/src/lib.rs"), "bench")
            .expect("a note about the file");
    wsb::cli::annotations::note_add(&main, "Elsewhere", "", &about_file("path:owner/names/src/main.rs"), "bench")
        .expect("a note about another file");
    wsb::cli::annotations::note_add(&main, "Other project", "", &about_file("path:owner/other/src/lib.rs"), "bench")
        .expect("a note about the same path in another project");

    let answer = about(&main, "src/lib.rs").expect("the file's notes");
    assert_eq!(answer["about"], "path:owner/names/src/lib.rs");
    let linked = answer["notes"].as_array().expect("notes");
    assert_eq!(linked.len(), 1, "{answer}");
    assert_eq!(linked[0]["id"], note.id.as_str());
    let mentioned = answer["mentioned_in"].as_array().expect("documents");
    assert_eq!(mentioned.len(), 1, "{answer}");
    assert_eq!(mentioned[0]["path"], "docs/design.md");

    let tally = counts(&main).expect("the counts");
    assert_eq!(tally["counts"]["src/lib.rs"], 2, "a linked note and a document: {tally}");
    assert_eq!(tally["counts"]["src/main.rs"], 1, "{tally}");
    assert_eq!(tally["counts"].as_object().map(|counts| counts.len()), Some(2), "{tally}");

    let nameless = root.join("loose/main-worktree");
    std::fs::create_dir_all(nameless.join("docs")).expect("a worktree");
    git(&nameless, &["init", "--quiet"]);
    manage(&nameless);
    std::fs::write(nameless.join("docs/a.md"), "[x](src/lib.rs)\n").expect("a document");
    let answer = about(&nameless, "src/lib.rs").expect("documents without a project name");
    assert!(answer["about"].is_null(), "{answer}");
    assert_eq!(answer["mentioned_in"].as_array().map(Vec::len), Some(1), "{answer}");
    assert!(answer["problems"][0].as_str().is_some_and(|said| said.contains("no origin")), "{answer}");
}