From 35a4cbe6738043deb9444bd68854bf4883abe41a Mon Sep 17 00:00:00 2001 From: iceteaSA <171169159+iceteaSA@users.noreply.github.com> Date: Fri, 4 Dec 2026 15:02:17 +0000 Subject: [PATCH] test(opencode-v2): cover 22 untested fast-read cases Mutation testing of the OpenCode 2 fast-read path (cargo-mutants, 193 mutants) found 22 changes that no test detected. Each new or extended test passes on the current code and fails with its mutant. --- .../src/opencode_v2_source.rs | 461 ++++++++++++++++++ 1 file changed, 461 insertions(+) diff --git a/crates/astrocyte-module/src/opencode_v2_source.rs b/crates/astrocyte-module/src/opencode_v2_source.rs index 95fe4a0..065fe57 100644 --- a/crates/astrocyte-module/src/opencode_v2_source.rs +++ b/crates/astrocyte-module/src/opencode_v2_source.rs @@ -4503,6 +4503,467 @@ pub(crate) mod tests { assert_eq!(quantity(&state, "output"), 52); } + #[test] + fn v2_idle_fast_polls_keep_the_rewind_boundary() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + + let append_users = |rows: std::ops::RangeInclusive, prefix: &str| { + for rowid in rows { + conn.execute( + "INSERT INTO session_message(rowid,id,session_id,type,seq,time_created,time_updated,data) + VALUES (?1,?2,'session','user',?1,20,20,'{}')", + params![rowid, format!("{prefix}-{rowid}")], + ) + .unwrap(); + } + }; + set_page_limits_for_tests(Some((20, 0))); + source.drain_fast(&state, 110, &STOP).unwrap(); + append_users(2..=21, "original"); + source.drain_fast(&state, 120, &STOP).unwrap(); + append_users(22..=41, "original"); + source.drain_fast(&state, 130, &STOP).unwrap(); + for now in 131..131 + i64::try_from(TAIL_ANCHOR_CAP).expect("anchor cap fits i64") { + source.drain_fast(&state, now, &STOP).unwrap(); + } + conn.execute("DELETE FROM session_message WHERE rowid > 21", []) + .unwrap(); + append_users(22..=41, "replacement"); + source.drain_fast(&state, 200, &STOP).unwrap(); + set_page_limits_for_tests(None); + + assert_eq!(source.sweep_probe().tail, Some((1, 41))); + } + + #[test] + fn v2_missing_boundary_with_higher_top_rewinds_to_the_first_row() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + insert(&conn, "boundary", 20, &payload(20, true)); + conn.execute( + "UPDATE session_message SET rowid=20 WHERE id='boundary'", + [], + ) + .unwrap(); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 2); + set_page_limits_for_tests(Some((1, 0))); + source.drain_fast(&state, 110, &STOP).unwrap(); + conn.execute("DELETE FROM session_message WHERE id='boundary'", []) + .unwrap(); + insert(&conn, "replacement", 120, &payload(120, true)); + conn.execute_batch( + "DELETE FROM session_message WHERE id='first'; + INSERT INTO session_message(rowid,id,session_id,type,seq,time_created,time_updated,data) + VALUES (21,'higher','session','user',21,121,121,'{}');", + ) + .unwrap(); + source.drain_fast(&state, 200, &STOP).unwrap(); + set_page_limits_for_tests(None); + + assert_eq!( + count(&state, "usage_event WHERE producer_ref='replacement'"), + 1 + ); + } + + #[test] + fn v2_idle_poll_does_not_accept_a_replaced_row_as_a_rewind_anchor() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + conn.execute_batch( + "WITH RECURSIVE n(x) AS (SELECT 2 UNION ALL SELECT x+1 FROM n WHERE x<6) + INSERT INTO session_message SELECT 'user-'||x,'session','user',x,20,20,'{}' FROM n", + ) + .unwrap(); + set_page_limits_for_tests(Some((2, 0))); + for now in 110..113 { + source.drain_fast(&state, now, &STOP).unwrap(); + } + conn.execute( + "UPDATE session_message SET id='replacement',type='assistant',time_updated=120,data=?1 WHERE rowid=4", + [payload(120, true).to_string()], + ) + .unwrap(); + source.drain_fast(&state, 130, &STOP).unwrap(); + conn.execute_batch( + "DELETE FROM session_message WHERE rowid>=5; + INSERT INTO session_message(rowid,id,session_id,type,seq,time_created,time_updated,data) + VALUES (7,'new-7','session','user',7,140,140,'{}'),(8,'new-8','session','user',8,140,140,'{}');", + ) + .unwrap(); + source.drain_fast(&state, 150, &STOP).unwrap(); + set_page_limits_for_tests(None); + + assert_eq!( + count(&state, "usage_event WHERE producer_ref='replacement'"), + 1 + ); + } + + #[test] + fn v2_stateless_tail_rereads_a_pending_call_at_the_start_boundary() { + let (_temp, path, conn) = database(); + insert(&conn, "finished", 10, &payload(10, true)); + let page = opencode_v2::read_tail( + &path, + &OpenCodeTailRequest { + after_rowid: Some(1), + seed_rows: 0, + seed_at_most: None, + open_rowids: &[1], + limit: 1, + }, + 100, + &STOP, + ) + .unwrap(); + + assert_eq!(page.map_or(0, |read| read.messages.len()), 1); + } + + #[test] + fn v2_tail_replay_returns_the_shared_page_boundary_once() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + insert(&conn, "boundary", 20, &payload(20, true)); + let request = TailPageRequest { + scan: OpenCodeTailRequest { + after_rowid: Some(1), + seed_rows: 0, + seed_at_most: None, + open_rowids: &[], + limit: 1, + }, + replay_target: Some(2), + previous: &[], + recent: &[], + verified: None, + pending: &Default::default(), + }; + let page = TailReader::open(&path, &STOP) + .unwrap() + .unwrap() + .read_with_sessions(&path, &request, 100, &STOP, true, &|_| { + SessionPolicy::default() + }) + .unwrap() + .unwrap(); + + assert_eq!(page.messages.len(), 1); + } + + #[test] + fn v2_full_sweep_does_not_treat_a_moved_front_as_pruned() { + let (_temp, path, conn) = database(); + insert(&conn, "old-front", 10, &payload(10, true)); + insert(&conn, "new-front", 20, &payload(20, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 2); + assert_eq!(runtime(&state).0, "old-front"); + source.drain_fast(&state, 110, &STOP).unwrap(); + assert_eq!(source.sweep_probe().tail, Some((0, 2))); + + conn.execute( + "UPDATE session_message SET rowid=3 WHERE id='old-front'", + [], + ) + .unwrap(); + source.require_full_pass(); + source.drain(&state, 200, &STOP).unwrap(); + + assert_eq!(runtime(&state).0, "new-front"); + assert_eq!(source.sweep_probe().tail, None); + } + + #[test] + fn v2_fast_coverage_stays_pending_when_only_an_evicted_call_is_open() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + conn.execute( + "WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x2 AND rowid<(SELECT MAX(rowid) FROM session_message); + UPDATE session_message SET type='user' WHERE rowid=(SELECT MAX(rowid) FROM session_message);", + ) + .unwrap(); + source.drain_fast(&state, 140, &STOP).unwrap(); + set_page_limits_for_tests(None); + + let coverage = state + .with_conn(|conn| { + conn.query_row( + "SELECT pending_unknown,multiplier_covered_through_ms FROM source_runtime WHERE source_key=?1", + [SOURCE_KEY], + |row| Ok((row.get::<_, i64>(0)?, row.get::<_, Option>(1)?)), + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + assert_eq!(coverage, (1, None)); + } + + #[test] + fn v2_rewind_does_not_use_an_open_row_at_the_reused_slot_as_a_floor() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + insert(&conn, "replacement", 20, &payload(20, false)); + let request = TailPageRequest { + scan: OpenCodeTailRequest { + after_rowid: Some(3), + seed_rows: 0, + seed_at_most: None, + open_rowids: &[], + limit: 2, + }, + replay_target: None, + previous: &[], + recent: &[ + SweepMark { + rowid: 2, + message_id: "before-replacement".into(), + }, + SweepMark { + rowid: 3, + message_id: "deleted-boundary".into(), + }, + ], + verified: None, + pending: &[( + "replacement".into(), + TailPending { + rowid: 2, + open: true, + }, + )] + .into(), + }; + let page = TailReader::open(&path, &STOP) + .unwrap() + .unwrap() + .read_with_sessions(&path, &request, 100, &STOP, true, &|_| { + SessionPolicy::default() + }) + .unwrap() + .unwrap(); + + assert_eq!(page.start_rowid, 0); + } + + #[test] + fn v2_rewind_uses_the_earliest_open_row_not_the_first_pending_key() { + let (_temp, path, conn) = database(); + insert(&conn, "z-earlier", 10, &payload(10, false)); + insert(&conn, "a-later", 20, &payload(20, false)); + insert(&conn, "replacement", 30, &payload(30, true)); + let pending = [("a-later", 2), ("z-earlier", 1)] + .map(|(id, rowid)| (id.into(), TailPending { rowid, open: true })) + .into(); + let request = TailPageRequest { + scan: OpenCodeTailRequest { + after_rowid: Some(3), + seed_rows: 0, + seed_at_most: None, + open_rowids: &[], + limit: 3, + }, + replay_target: None, + previous: &[], + recent: &[SweepMark { + rowid: 3, + message_id: "deleted-boundary".into(), + }], + verified: None, + pending: &pending, + }; + let page = TailReader::open(&path, &STOP) + .unwrap() + .unwrap() + .read_with_sessions(&path, &request, 100, &STOP, true, &|_| { + SessionPolicy::default() + }) + .unwrap() + .unwrap(); + + assert_eq!(page.start_rowid, 1); + } + + #[test] + fn v2_fast_rewind_leaves_the_position_when_a_known_front_changes() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + source.drain_fast(&state, 110, &STOP).unwrap(); + assert_eq!(source.sweep_probe().tail, Some((0, 1))); + + conn.execute( + "UPDATE session_message SET id='new-front' WHERE rowid=1", + [], + ) + .unwrap(); + insert(&conn, "after-front", 120, &payload(120, true)); + source.drain_fast(&state, 130, &STOP).unwrap(); + + assert_eq!(source.sweep_probe().tail, Some((0, 1))); + assert_eq!( + count(&state, "usage_event WHERE producer_ref='after-front'"), + 1 + ); + } + + #[test] + fn v2_sweep_at_shrink_time_does_not_clear_unknown_coverage() { + let (_temp, path, conn) = database(); + insert(&conn, "front", 100, &payload(100, true)); + conn.execute_batch( + "WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x<32) + INSERT INTO session_message SELECT 'newest-'||x,'session','user',x+1,100,100,'{}' FROM n", + ) + .unwrap(); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + drain_full_sweep(&source, &state, 200); + source.drain_fast(&state, 201, &STOP).unwrap(); + conn.execute("DELETE FROM session_message WHERE rowid>1", []) + .unwrap(); + insert(&conn, "regrowth", 210, &payload(210, true)); + source.drain_fast(&state, 220, &STOP).unwrap(); + + source.require_full_pass(); + assert_eq!(drain_full_sweep(&source, &state, 220).recorded, 1); + let pending = state + .with_conn(|conn| { + conn.query_row( + "SELECT pending_unknown FROM source_runtime WHERE source_key=?1", + [SOURCE_KEY], + |row| row.get::<_, i64>(0), + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + assert_eq!(pending, 1); + } + + #[test] + fn v2_contiguous_fast_page_clears_coverage_until_it_catches_up() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + insert(&conn, "second", 110, &payload(110, true)); + insert(&conn, "third", 110, &payload(110, true)); + set_page_limits_for_tests(Some((1, 0))); + let recorded = source + .drain_in_lane(&state, 120, &STOP, DrainMode::Fast) + .unwrap() + .recorded; + set_page_limits_for_tests(None); + assert_eq!(recorded, 1); + + let covered = state + .with_conn(|conn| { + conn.query_row( + "SELECT multiplier_covered_through_ms FROM source_runtime WHERE source_key=?1", + [SOURCE_KEY], + |row| row.get::<_, Option>(0), + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + assert_eq!(covered, None); + } + + #[test] + fn v2_open_rows_at_capacity_do_not_block_fast_coverage_after_removal() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + conn.execute( + "WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x1 AND rowid<(SELECT MAX(rowid) FROM session_message); + UPDATE session_message SET type='user' WHERE rowid=(SELECT MAX(rowid) FROM session_message);", + ) + .unwrap(); + source.drain_fast(&state, 140, &STOP).unwrap(); + set_page_limits_for_tests(None); + + let coverage = state + .with_conn(|conn| { + conn.query_row( + "SELECT pending_unknown,multiplier_covered_through_ms FROM source_runtime WHERE source_key=?1", + [SOURCE_KEY], + |row| Ok((row.get::<_, i64>(0)?, row.get::<_, Option>(1)?)), + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + assert_eq!(coverage, (0, Some(100))); + } + + #[test] + fn v2_fast_tail_leaves_both_evicted_calls_for_the_full_sweep() { + let (_temp, path, conn) = database(); + insert(&conn, "first", 10, &payload(10, true)); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(source.drain(&state, 100, &STOP).unwrap().recorded, 1); + conn.execute( + "WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x