use crate::common::Drip;
use std::fs;
#[test]
fn session_persists_state_between_invocations() {
let drip = Drip::new();
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("notes.md");
fs::write(
&f,
"# Hello\n\nLine with enough repeated payload\n".repeat(20),
)
.unwrap();
let first = drip.read_stdout(&f);
assert!(first.contains("[DRIP: full read"));
let second = drip.read_stdout(&f);
assert!(second.contains("[DRIP: unchanged"));
}
#[test]
fn reset_clears_session() {
let drip = Drip::new();
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("a.txt");
fs::write(&f, "x\n").unwrap();
drip.read_stdout(&f);
drip.reset();
let out = drip.read_stdout(&f);
assert!(
out.contains("[DRIP: full read"),
"after reset, expected full read: {out}"
);
}
#[test]
fn distinct_sessions_do_not_share_state() {
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("shared.txt");
fs::write(&f, "shared\n").unwrap();
let mut a = Drip::new();
a.session_id = "session-A".to_string();
let mut b = Drip::new();
b.data_dir = tempfile::TempDir::new_in(a.data_dir.path().parent().unwrap()).unwrap();
let bin = a.bin.clone();
let data = a.data_dir.path().to_path_buf();
let run = |sid: &str| -> String {
let o = std::process::Command::new(&bin)
.arg("read")
.arg(&f)
.env("DRIP_DATA_DIR", &data)
.env("DRIP_SESSION_ID", sid)
.output()
.unwrap();
assert!(o.status.success());
String::from_utf8_lossy(&o.stdout).into_owned()
};
let a1 = run("session-A");
let b1 = run("session-B");
assert!(a1.contains("[DRIP: full read"));
assert!(
b1.contains("[DRIP: full read"),
"session B should see fresh state, got: {b1}"
);
}
#[test]
fn expired_session_announces_fresh_baseline_on_first_read() {
use std::process::Command;
let drip = Drip::new();
let dir = tempfile::tempdir().unwrap();
let f = dir.path().join("ttl.txt");
fs::write(&f, "v1\n").unwrap();
drip.read_stdout(&f);
let _ = Command::new(&drip.bin)
.arg("reset")
.env("DRIP_DATA_DIR", drip.data_dir.path())
.env("DRIP_SESSION_ID", &drip.session_id)
.output()
.unwrap();
let out = drip.read_stdout(&f);
assert!(
out.contains("session expired") || out.contains("[DRIP: full read"),
"expected expired notice or fresh full read, got: {out}"
);
}