From bc214ffb46d4b8b6b14e6b37f5a75fdd2c33ce0c Mon Sep 17 00:00:00 2001 From: iceteaSA <171169159+iceteaSA@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:31:49 +0000 Subject: [PATCH] test(opencode-v2): cover 20 untested row-mapping and full-read cases Mutation testing of the OpenCode 2 row-to-record mapping and full-pass read (cargo-mutants) found 20 changes that no test detected. Each new test passes on the current code and fails with its mutant. --- .../src/opencode_v2_source_tests.rs | 338 ++++++++++++++++++ 1 file changed, 338 insertions(+) diff --git a/crates/astrocyte-module/src/opencode_v2_source_tests.rs b/crates/astrocyte-module/src/opencode_v2_source_tests.rs index 07092b4..66f6e39 100644 --- a/crates/astrocyte-module/src/opencode_v2_source_tests.rs +++ b/crates/astrocyte-module/src/opencode_v2_source_tests.rs @@ -1,3 +1,341 @@ +fn v2_creation_time_result(created: Value) -> (usize, Option, i64) { + let (_temp, path, conn) = database(); + let mut data = payload(10, true); + data["time"]["created"] = created; + insert(&conn, "creation-time", 11, &data); + + let state = test_state(); + let outcome = OpenCodeV2Source::at(path).drain(&state, 20, &STOP).unwrap(); + let occurred = state + .with_conn(|conn| { + conn.query_row("SELECT MAX(occurred_at_ms) FROM usage_event", [], |row| { + row.get(0) + }) + .map_err(|e| e.to_string()) + }) + .unwrap(); + ( + outcome.recorded, + occurred, + count(&state, "set_aside_record"), + ) +} + +#[test] +fn v2_creation_time_rejects_oversized_exponent_number() { + assert_eq!(v2_creation_time_result(json!(1e20)), (0, None, 1)); +} + +#[test] +fn v2_creation_time_rejects_negative_fraction_that_truncates_to_zero() { + assert_eq!(v2_creation_time_result(json!(-0.5)), (0, None, 1)); +} + +#[test] +fn v2_creation_time_rejects_negative_integer() { + assert_eq!(v2_creation_time_result(json!(-1)), (0, None, 1)); +} + +#[test] +fn v2_creation_time_records_the_epoch() { + assert_eq!(v2_creation_time_result(json!(0)), (1, Some(0), 0)); +} + +#[test] +fn v2_creation_time_rejects_the_next_value_above_the_float_limit() { + assert_eq!( + v2_creation_time_result(json!(9_223_372_036_854_777_856_u64)), + (0, None, 1) + ); +} + +#[test] +fn v2_creation_time_records_the_rounded_i64_limit() { + assert_eq!( + v2_creation_time_result(json!(9_223_372_036_854_775_808_u64)), + (1, Some(i64::MAX), 0) + ); +} + +#[test] +fn v2_idle_tail_returns_the_already_published_cutoff() { + let (_temp, path, conn) = database(); + insert(&conn, "open", 180, &payload(170, false)); + let state = test_state(); + let source = OpenCodeV2Source::at(path); + + drain_full_sweep(&source, &state, 200); + let first = poll_replay_tail(&source, &state, 201); + assert_eq!(first.multiplier_covered_through_ms, Some(169)); + + let idle = poll_replay_tail(&source, &state, 202); + assert_eq!( + (idle.recorded, idle.multiplier_covered_through_ms), + (0, Some(169)) + ); +} + +fn v2_declared_source_for_payer_test(path: &std::path::Path) -> OpenCodeV2Source { + OpenCodeV2Source::declared( + path.to_path_buf(), + CursorHome::Declared { + source_key: super::super::opencode_thalamus_source::SOURCE_KEY, + store: path.to_string_lossy().into_owned(), + layout: "opencode-2", + }, + ) +} + +#[test] +fn v2_replay_counts_the_queued_copy_already_recorded_by_another_reader() { + let (_temp, path, conn) = database(); + let state = test_state(); + let declared = v2_declared_source_for_payer_test(&path); + insert(&conn, "waiting", 10, &payload(10, true)); + let pending = declared + .drain_declared(&state, 20, &STOP, ReadOptions::default(), &|_| None) + .unwrap(); + assert_eq!((pending.recorded, declared.payer_waiting()), (0, 1)); + + insert(&conn, "another", 11, &payload(11, true)); + let other = OpenCodeV2Source::at(path).drain(&state, 21, &STOP).unwrap(); + assert_eq!(other.recorded, 2); + + declared.require_full_pass(); + let replay = declared + .drain_declared(&state, 22, &STOP, ReadOptions::default(), &|_| None) + .unwrap(); + assert_eq!( + ( + replay.recorded, + replay.skipped_recorded, + declared.payer_waiting() + ), + (0, 3, 0) + ); +} + +#[test] +fn v2_event_write_error_is_not_a_shutdown() { + let (_temp, path, conn) = database(); + let state = test_state(); + insert(&conn, "call", 10, &payload(10, true)); + state + .with_conn(|conn| { + conn.execute_batch( + "CREATE TRIGGER refuse_opencode_call BEFORE INSERT ON usage_event + BEGIN SELECT RAISE(ABORT, 'fixture refused the OpenCode call'); END;", + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + + let result = OpenCodeV2Source::at(path).drain(&state, 20, &STOP); + let kind = match result { + Err(SourceDrainError::Failed(reason)) + if reason.contains("fixture refused the OpenCode call") => + { + "write failure" + } + Err(SourceDrainError::Failed(_)) => "other failure", + Err(SourceDrainError::Stopped) => "stopped", + Err(SourceDrainError::Deferred) => "deferred", + Ok(_) => "committed", + }; + assert_eq!(kind, "write failure"); + assert_eq!(count(&state, "usage_event"), 0); +} + +#[test] +fn v2_stopped_payer_wait_drain_is_not_a_write_error() { + let (_temp, path, conn) = database(); + let state = test_state(); + let declared = v2_declared_source_for_payer_test(&path); + insert(&conn, "waiting", 10, &payload(10, true)); + let pending = declared + .drain_declared(&state, 20, &STOP, ReadOptions::default(), &|_| None) + .unwrap(); + assert_eq!((pending.recorded, declared.payer_waiting()), (0, 1)); + + let stop = AtomicBool::new(true); + assert_eq!( + declared.drain_payer_waits(&state, 21, &stop, DrainMode::Full), + Err(SourceDrainError::Stopped) + ); + assert_eq!( + (count(&state, "usage_event"), declared.payer_waiting()), + (0, 1) + ); +} + +fn v2_first_legacy_floor_payload_page(tied_users: i64, grow: bool) -> (usize, i64) { + let (_temp, path, conn) = database(); + conn.execute( + "WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x (usize, usize, i64) { + let (_temp, path, conn) = database(); + conn.execute( + "INSERT INTO session_message VALUES ('cursor','session','user',1,100,100,'{}')", + [], + ) + .unwrap(); + let state = test_state(); + let source = OpenCodeV2Source::at(path); + assert_eq!(drain_full_sweep(&source, &state, 110).recorded, 0); + assert_eq!(runtime(&state).1, 100); + + insert(&conn, "backdated", 50, &payload(50, true)); + for index in 0..limit { + insert(&conn, &format!("bad-{index}"), 60, &payload(60, true)); + } + conn.execute( + "UPDATE session_message SET time_updated='bad' WHERE id LIKE 'bad-%'", + [], + ) + .unwrap(); + source.require_full_pass(); + set_page_limits_for_tests(Some((limit, 0))); + let malformed = source.drain(&state, 120, &STOP); + let next = source.drain(&state, 121, &STOP); + set_page_limits_for_tests(None); + ( + malformed.unwrap().recorded, + next.unwrap().recorded, + count(&state, "usage_event WHERE producer_ref='backdated'"), + ) +} + +#[test] +fn v2_malformed_page_does_not_spend_the_zero_remaining_payload_slots() { + assert_eq!(v2_page_after_malformed_fills_limit(1), (0, 1, 1)); +} + +#[test] +fn v2_older_payload_waits_when_the_remaining_slot_count_equals_zero() { + assert_eq!(v2_page_after_malformed_fills_limit(2), (0, 1, 1)); +} + +fn v2_read_backdated_with_budget(steps: u64) -> (usize, u64) { + let (_temp, path, conn) = database(); + conn.execute( + "INSERT INTO session_message VALUES ('cursor','session','user',1,100,100,'{}')", + [], + ) + .unwrap(); + let state = test_state(); + let source = OpenCodeV2Source::at(&path); + assert_eq!(drain_full_sweep(&source, &state, 110).recorded, 0); + state + .with_conn(|conn| { + conn.execute( + "UPDATE source_runtime SET open_row_floor=NULL,open_row_floor_id=NULL, + sweep_mark_rowid=NULL,sweep_mark_id=NULL WHERE source_key=?1", + [SOURCE_KEY], + ) + .map_err(|error| error.to_string()) + }) + .unwrap(); + insert(&conn, "backdated", 50, &payload(50, true)); + + opencode_v2::set_sweep_limits_for_tests(Some((100, steps))); + let page = OpenCodeV2Source::at(path).drain(&state, 120, &STOP); + opencode_v2::set_sweep_limits_for_tests(None); + let page = page.unwrap(); + (page.recorded, page.cost.vm_steps) +} + +#[test] +fn v2_older_payload_waits_when_the_instruction_budget_is_exactly_spent() { + let (records, listing_steps) = v2_read_backdated_with_budget(1); + assert_eq!(records, 0); + assert_eq!(v2_read_backdated_with_budget(listing_steps).0, 0); +} + #[test] fn v2_fast_poll_records_a_tracked_call_after_the_first_session_is_removed() { tracked_call_at_changed_front(true);