use std::path::PathBuf;
use std::process::Command;
fn bin() -> &'static str {
env!("CARGO_BIN_EXE_instinct")
}
fn store(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("instinct-concepts-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir.join("bank.hxdb")
}
fn instinct(store: &PathBuf, concepts_off: bool, args: &[&str]) -> String {
let mut c = Command::new(bin());
for (k, _) in std::env::vars_os().filter(|(k, _)| k.to_string_lossy().starts_with("INSTINCT_")) {
c.env_remove(k);
}
c.env("INSTINCT_STORE", store).args(args);
if concepts_off {
c.env("INSTINCT_CONCEPTS", "0");
}
let out = c.output().unwrap();
assert!(out.status.success(), "{args:?}: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8(out.stdout).unwrap()
}
fn bank_pods(store: &PathBuf) {
instinct(
store,
false,
&[
"bank",
"--title=Set memory requests and limits",
"--when=Pods get killed for memory after a release",
"--found=No limits were set and a leak went unnoticed",
"--how=Set requests and limits and watch resident memory",
"--evidence=Three restarts in a week",
],
);
}
const PARAPHRASE: &str = "Containers keep getting killed for using too much RAM";
#[test]
fn paraphrase_finds_the_lesson_only_with_concepts() {
let s = store("para");
bank_pods(&s);
let on = instinct(&s, false, &["recall", &format!("--q={PARAPHRASE}"), "--k=1"]);
assert!(on.contains("Set memory requests and limits"), "concepts on: {on}");
let off = instinct(&s, true, &["recall", &format!("--q={PARAPHRASE}"), "--k=1"]);
assert!(!off.contains("Set memory requests and limits"), "concepts off: {off}");
}
#[test]
fn unrelated_query_stays_silent() {
let s = store("silent");
bank_pods(&s);
for q in ["Plan a three-day trip to Lisbon", "Write a haiku about autumn rain"] {
for off in [false, true] {
let out = instinct(&s, off, &["recall", &format!("--q={q}"), "--k=3"]);
assert!(!out.contains("Set memory requests and limits"), "{q} (concepts off: {off}): {out}");
}
}
}