use super::*;
fn tmp() -> tempfile::TempDir {
tempfile::tempdir().unwrap()
}
#[test]
fn test_a_chunk_recorded_by_a_previous_turn_cannot_be_claimed() {
let dir = tmp();
let root = dir.path();
let mut prior = SessionLedger::load(root, "sess-1");
prior.record(&[chunk_key("src/a.rs", 10, 20)], "inj-prompt");
let mut turn = InjectionTurn::open(root, "sess-1", true, "some error");
assert!(
!turn.claim("src/a.rs", 10, 20),
"claimed a chunk the prompt path already injected"
);
assert!(
turn.claim("src/b.rs", 1, 5),
"a fresh chunk must be claimable"
);
assert_eq!(turn.len(), 1);
}
#[test]
fn test_a_chunk_cannot_be_claimed_twice_in_one_turn() {
let dir = tmp();
let mut turn = InjectionTurn::open(dir.path(), "sess-1", true, "q");
assert!(turn.claim("src/a.rs", 1, 9));
assert!(!turn.claim("src/a.rs", 1, 9));
assert_eq!(turn.len(), 1);
}
#[test]
fn test_within_turn_dedup_applies_even_when_reducing_is_disabled() {
let dir = tmp();
let mut turn = InjectionTurn::open(dir.path(), "sess-1", false, "q");
assert!(turn.claim("src/a.rs", 1, 9));
assert!(!turn.claim("src/a.rs", 1, 9));
}
#[test]
fn test_reducing_disabled_still_yields_an_identifier_and_no_cross_turn_filtering() {
let dir = tmp();
let root = dir.path();
let mut prior = SessionLedger::load(root, "sess-1");
prior.record(&[chunk_key("src/a.rs", 10, 20)], "inj-prompt");
let mut turn = InjectionTurn::open(root, "sess-1", false, "q");
assert!(
turn.claim("src/a.rs", 10, 20),
"reducing is off; prior turns must not filter"
);
assert!(turn.injection_id().starts_with("inj-"));
}
#[test]
fn test_no_session_id_degrades_to_in_memory_but_keeps_an_identifier() {
let dir = tmp();
let mut turn = InjectionTurn::open(dir.path(), "", true, "q");
assert!(turn.injection_id().starts_with("inj-"));
assert!(turn.claim("src/a.rs", 1, 2));
assert!(!turn.claim("src/a.rs", 1, 2));
turn.commit(); }
#[test]
fn test_commit_makes_chunks_visible_to_the_next_turn() {
let dir = tmp();
let root = dir.path();
let mut first = InjectionTurn::open(root, "sess-1", true, "q");
assert!(first.claim("src/a.rs", 1, 9));
first.commit();
let mut second = InjectionTurn::open(root, "sess-1", true, "q");
assert!(
!second.claim("src/a.rs", 1, 9),
"the next turn re-injected what this one recorded"
);
}
#[test]
fn test_an_uncommitted_turn_leaves_the_ledger_untouched() {
let dir = tmp();
let root = dir.path();
let mut abandoned = InjectionTurn::open(root, "sess-1", true, "q");
assert!(abandoned.claim("src/a.rs", 1, 9));
drop(abandoned);
let mut next = InjectionTurn::open(root, "sess-1", true, "q");
assert!(
next.claim("src/a.rs", 1, 9),
"chunks were suppressed by a turn that never emitted them"
);
}
#[test]
fn test_committing_an_empty_turn_is_a_no_op() {
let dir = tmp();
let root = dir.path();
let mut turn = InjectionTurn::open(root, "sess-1", true, "q");
assert!(turn.is_empty());
turn.commit();
let ledger = SessionLedger::load(root, "sess-1");
assert_eq!(ledger.turn, 0, "an empty commit advanced the turn counter");
}
#[test]
fn test_file_claims_collide_with_the_complementary_expansion_marker() {
let dir = tmp();
let root = dir.path();
let mut prior = SessionLedger::load(root, "sess-1");
prior.record(&[chunk_key("src/a.rs", 0, 0)], "inj-complementary");
let mut turn = InjectionTurn::open(root, "sess-1", true, "q");
assert!(
!turn.claim_file("src/a.rs"),
"re-suggested a file complementary expansion already suggested"
);
}
#[test]
fn test_a_file_marker_does_not_suppress_a_chunk_from_that_file() {
let dir = tmp();
let root = dir.path();
let mut prior = SessionLedger::load(root, "sess-1");
prior.record(&[chunk_key("src/a.rs", 0, 0)], "inj-post-tool-use");
let mut turn = InjectionTurn::open(root, "sess-1", true, "q");
assert!(
turn.claim("src/a.rs", 40, 60),
"a file suggestion wrongly suppressed injecting that file's contents"
);
}
#[test]
fn test_claimed_files_dedupes_preserves_order_and_handles_colons_in_paths() {
let dir = tmp();
let mut turn = InjectionTurn::open(dir.path(), "sess-1", true, "q");
assert!(turn.claim("src/b.rs", 1, 5));
assert!(turn.claim("src/a.rs", 1, 5));
assert!(turn.claim("src/b.rs", 20, 25)); assert!(turn.claim("C:/win/path.rs", 1, 2));
assert_eq!(
turn.claimed_files(),
vec![
"src/b.rs".to_string(),
"src/a.rs".to_string(),
"C:/win/path.rs".to_string(),
],
);
}
#[test]
fn test_turns_get_distinct_identifiers() {
let dir = tmp();
let a = InjectionTurn::open(dir.path(), "sess-1", true, "query one");
let b = InjectionTurn::open(dir.path(), "sess-1", true, "query two");
assert_ne!(a.injection_id(), b.injection_id());
}