instinct-cli 0.3.8

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
use super::*;

fn shown(s: &str) -> Vec<String> {
    extract(s).into_iter().map(|t| t.shown).collect()
}

#[test]
fn extracts_the_listed_kinds() {
    assert_eq!(shown("open https://app.example.com/login now"), ["https://app.example.com/login"]);
    assert_eq!(shown("it runs on 192.168.1.20:8080, right?"), ["192.168.1.20:8080"]);
    assert_eq!(shown("see example.io and www.koscak.ai/pricing."), ["example.io", "www.koscak.ai/pricing"]);
    assert_eq!(shown("edit src/main.rs and ~/notes/a.md and /etc/hosts"), ["src/main.rs", "~/notes/a.md", "/etc/hosts"]);
    assert_eq!(shown("on v2.1 or 1.2.3 with opus-4-6 and gpt-5.1"), ["v2.1", "1.2.3", "opus-4-6", "gpt-5.1"]);
    assert_eq!(shown("check my phone and the Browser, the public link, live"), ["phone", "browser", "public link", "live"]);
}

#[test]
fn ignores_plain_prose_and_staging() {
    assert!(shown("please fix the bug and tell me what happened.").is_empty());
    assert!(shown("test it on staging first, and/or locally").is_empty());
    assert!(shown("version 2 of the 3 options, 1.5 times").is_empty());
    assert!(shown("a/b and c/d").is_empty());
    assert!(shown("step-1 and my-model-3").is_empty());
}

#[test]
fn touching_is_substring_after_normalizing() {
    let t = &extract("open https://www.Example.com/Login/")[0];
    assert!(touches(t, "curl -s http://example.com/login"));
    assert!(!touches(t, "curl -s http://staging.example.org/login"));
    let p = &extract("see ./src/app/main.rs")[0];
    assert!(touches(p, "cargo test src/app/main.rs"));
    assert!(touches(p, "read /home/x/other/main.rs"), "the file name alone counts for a path with an extension");
    assert!(!touches(p, "read src/lib.rs"));
    let w = &extract("on the phone")[0];
    assert!(touches(w, "adb shell input phone"));
    assert!(!touches(w, "telephone numbers"), "words match as whole words");
}

#[test]
fn lesson_how_gives_targets() {
    let t = lesson_targets("Build it. Verify on the iPhone, not the simulator. Then ship.");
    assert_eq!(t.len(), 1);
    assert_eq!(t[0].shown, "the iphone, not the simulator");
    let t = lesson_targets("check on https://x.example.com/health");
    assert_eq!(t[0].shown, "https://x.example.com/health");
}

fn ev(kind: EvKind, text: &str, cmd: &str) -> mine::Ev {
    mine::test_ev(kind, text, cmd)
}

fn session(items: &[(&str, &str, &str)]) -> Vec<mine::Ev> {
    items
        .iter()
        .map(|(k, text, cmd)| {
            let kind = match *k {
                "u" => EvKind::User,
                "a" => EvKind::Assistant,
                _ => EvKind::ToolUse,
            };
            ev(kind, text, cmd)
        })
        .collect()
}

#[test]
fn fires_when_checks_missed_the_named_target() {
    let evs = session(&[
        ("u", "the page is at https://app.example.com/home, make the button blue", ""),
        ("t", "Edit", "Edit src/a.css"),
        ("t", "Bash", "curl -s http://localhost:3000/home"),
        ("a", "Done, it works now.", ""),
    ]);
    let f = run(&evs, None);
    assert_eq!(f.len(), 1);
    assert_eq!(f[0].1, ["https://app.example.com/home", "src/a.css"].iter().take(1).map(|s| s.to_string()).collect::<Vec<_>>());
    assert_eq!(reason(&f[0].1), "You said it is done, but your checks did not touch what the user will use: https://app.example.com/home. Check there and report what you saw.");
}

#[test]
fn quiet_when_touched_without_targets_without_a_claim_or_without_a_change() {
    let base = [
        ("u", "the page is at https://app.example.com/home, make the button blue", ""),
        ("t", "Edit", "Edit src/a.css"),
        ("t", "Bash", "curl -s https://app.example.com/home"),
        ("a", "Done.", ""),
    ];
    assert!(run(&session(&base), None).is_empty(), "touched");
    // the touch came before the last edit: it does not count
    let stale = [
        ("u", "the page is at https://app.example.com/home", ""),
        ("t", "Bash", "curl -s https://app.example.com/home"),
        ("t", "Edit", "Edit src/a.css"),
        ("a", "Done.", ""),
    ];
    assert_eq!(run(&session(&stale), None).len(), 1);
    assert!(run(&session(&[("u", "make it blue please", ""), ("t", "Edit", "Edit a.css"), ("a", "Done.", "")]), None).is_empty(), "no targets");
    assert!(run(&session(&[("u", "see https://x.example.com", ""), ("t", "Edit", "Edit a.css"), ("a", "I changed the colour.", "")]), None).is_empty(), "no claim");
    assert!(run(&session(&[("u", "see https://x.example.com", ""), ("t", "Bash", "ls"), ("a", "Done.", "")]), None).is_empty(), "nothing changed in the turn");
}

#[test]
fn targets_persist_across_turns_newest_first_and_capped() {
    let mut g = V2::default();
    for i in 0..20 {
        g.user(&format!("look at host{i}.example.com"));
    }
    assert_eq!(g.targets.len(), MAX_TARGETS);
    assert_eq!(g.targets[0].shown, "host19.example.com");
    g.user("and now https://new.example.com");
    assert_eq!(g.targets[0].shown, "https://new.example.com");
}