lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

visibility_index_tests.rs (24547B)


      1 use super::tests::{observed, signed_event_with, unprepared_store as store, verified, visible};
      2 use super::*;
      3 use radroots_storage::event::VisibilityIndexBounds;
      4 
      5 fn generation() -> SourceGeneration {
      6     SourceGeneration::new([81; 32]).unwrap()
      7 }
      8 fn bounds(rows: u16) -> VisibilityIndexBounds {
      9     VisibilityIndexBounds::new(rows, 16 * 1024 * 1024).unwrap()
     10 }
     11 fn profile(name: &str, time: u64) -> SignedEvent {
     12     signed_event_with(&format!(r#"{{"name":"{name}"}}"#), false, time, 0, vec![])
     13 }
     14 fn raw(event: SignedEvent) -> EventAdmission {
     15     EventAdmission::raw(observed(event, 100, None))
     16 }
     17 fn admitted(event: SignedEvent) -> EventAdmission {
     18     EventAdmission::visible(observed(event.clone(), 100, None), visible(&event)).unwrap()
     19 }
     20 
     21 async fn finish(storage: &SqliteStorage) {
     22     for _ in 0..100 {
     23         if storage
     24             .maintain_visibility(bounds(2))
     25             .await
     26             .unwrap()
     27             .is_complete()
     28         {
     29             return;
     30         }
     31     }
     32     panic!("bounded fixture index did not finish");
     33 }
     34 
     35 async fn indexed_visible(storage: &SqliteStorage) -> Vec<EventId> {
     36     let values: Vec<Vec<u8>> = sqlx::query_scalar(
     37         "SELECT candidate.event_id FROM radroots_runtime_visibility_events AS candidate
     38          WHERE candidate.source_generation = ? AND candidate.admission_stage = 'visible'
     39          AND candidate.ephemeral = 0
     40          AND (candidate.head_coordinate IS NULL OR candidate.event_id = (
     41            SELECT head.event_id FROM radroots_runtime_visibility_events AS head
     42            WHERE head.source_generation = candidate.source_generation
     43            AND head.head_coordinate = candidate.head_coordinate
     44            ORDER BY head.created_at DESC, head.event_id ASC LIMIT 1))
     45          AND (candidate.deletion_request = 1 OR (
     46            NOT EXISTS (SELECT 1 FROM radroots_runtime_visibility_deletion_events AS deletion
     47              WHERE deletion.source_generation = candidate.source_generation
     48              AND deletion.target_event = candidate.event_id AND deletion.author = candidate.author)
     49            AND NOT EXISTS (SELECT 1 FROM radroots_runtime_visibility_deletion_addresses AS deletion
     50              WHERE deletion.source_generation = candidate.source_generation
     51              AND deletion.target_coordinate = candidate.deletion_coordinate
     52              AND deletion.author = candidate.author AND deletion.created_at >= candidate.created_at)))
     53          ORDER BY candidate.event_id",
     54     ).bind(storage.generation.as_bytes().as_slice()).fetch_all(&storage.pool).await.unwrap();
     55     let ids = values
     56         .into_iter()
     57         .map(|value| EventId::from_bytes(value.try_into().unwrap()))
     58         .collect::<Vec<_>>();
     59     let mut public_ids = storage
     60         .query_visible(EventQuery::all(EventQueryBounds::first(1000).unwrap()))
     61         .await
     62         .unwrap()
     63         .items()
     64         .iter()
     65         .map(|item| *item.event().id())
     66         .collect::<Vec<_>>();
     67     public_ids.sort();
     68     assert_eq!(public_ids, ids);
     69     ids
     70 }
     71 
     72 #[tokio::test]
     73 async fn visibility_index_backfill_and_changed_replay_survive_provider_reopen_without_restart() {
     74     let storage = store(generation()).await;
     75     let first = profile("first", 10);
     76     for event in [first.clone(), profile("second", 20), profile("third", 30)] {
     77         storage.admit(raw(event)).await.unwrap();
     78     }
     79     let before = storage.maintain_visibility(bounds(1)).await.unwrap();
     80     assert_eq!(before.source_sequence(), 1);
     81     assert_eq!(before.processed_rows(), 1);
     82     assert!(!before.is_complete());
     83     storage.admit(admitted(first.clone())).await.unwrap();
     84     storage.admit(raw(profile("fourth", 40))).await.unwrap();
     85     let reopened = SqliteStorage::new(
     86         storage.pool.clone(),
     87         generation(),
     88         EventStoreMode::ReadWrite,
     89     );
     90     let after = reopened.maintain_visibility(bounds(1)).await.unwrap();
     91     assert_eq!(after.source_sequence(), 2);
     92     assert!(!after.is_complete());
     93     let third = reopened.maintain_visibility(bounds(1)).await.unwrap();
     94     assert_eq!(third.source_sequence(), 3);
     95     assert!(!third.is_complete());
     96     finish(&reopened).await;
     97     let token = reopened.source_token().await.unwrap();
     98     let current = reopened.maintain_visibility(bounds(1)).await.unwrap();
     99     assert!(current.is_complete());
    100     assert_eq!(current.processed_rows(), 0);
    101     assert_eq!(current.source_sequence(), 3);
    102     assert_eq!(current.token(), token);
    103     let row = sqlx::query("SELECT admission_stage, last_mutation_epoch FROM radroots_runtime_visibility_events WHERE event_id = ?")
    104         .bind(first.id().as_bytes().as_slice()).fetch_one(&reopened.pool).await.unwrap();
    105     assert_eq!(row.get::<String, _>("admission_stage"), "visible");
    106     assert_eq!(row.get::<i64, _>("last_mutation_epoch"), 4);
    107     assert_eq!(indexed_visible(&reopened).await, vec![*first.id()]);
    108     let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM radroots_runtime_visibility_events")
    109         .fetch_one(&reopened.pool)
    110         .await
    111         .unwrap();
    112     assert_eq!(count, 4);
    113 }
    114 
    115 #[tokio::test]
    116 async fn visibility_index_matches_reference_winners_deletion_before_target_and_unsupported_head() {
    117     let storage = store(generation()).await;
    118     let old = profile("old", 10);
    119     let current = profile("current", 20);
    120     let deletion = signed_event_with(
    121         "delete",
    122         false,
    123         30,
    124         5,
    125         vec![vec!["e".to_owned(), current.id().to_hex()]],
    126     );
    127     for event in [deletion, old, current] {
    128         storage.admit(admitted(event)).await.unwrap();
    129     }
    130     finish(&storage).await;
    131     assert_eq!(
    132         indexed_visible(&storage).await,
    133         storage
    134             .rebuild_visibility()
    135             .await
    136             .unwrap()
    137             .visible_event_ids()
    138     );
    139     let newer = profile("unsupported", 40);
    140     storage
    141         .admit(
    142             EventAdmission::verified(observed(newer.clone(), 100, None), verified(&newer)).unwrap(),
    143         )
    144         .await
    145         .unwrap();
    146     assert!(
    147         sqlx::query_scalar::<_, bool>("SELECT ready FROM radroots_runtime_visibility_progress")
    148             .fetch_one(&storage.pool)
    149             .await
    150             .unwrap()
    151     );
    152     finish(&storage).await;
    153     assert_eq!(
    154         indexed_visible(&storage).await,
    155         storage
    156             .rebuild_visibility()
    157             .await
    158             .unwrap()
    159             .visible_event_ids()
    160     );
    161     let same_a = profile("same-a", 50);
    162     let same_b = profile("same-b", 50);
    163     storage.admit(admitted(same_a)).await.unwrap();
    164     storage.admit(admitted(same_b)).await.unwrap();
    165     storage
    166         .admit(raw(profile("forged raw later", 60)))
    167         .await
    168         .unwrap();
    169     let address_deletion = signed_event_with(
    170         "address delete",
    171         false,
    172         55,
    173         5,
    174         vec![vec![
    175             "a".to_owned(),
    176             "0:585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df:".to_owned(),
    177         ]],
    178     );
    179     storage.admit(admitted(address_deletion)).await.unwrap();
    180     finish(&storage).await;
    181     assert_eq!(
    182         indexed_visible(&storage).await,
    183         storage
    184             .rebuild_visibility()
    185             .await
    186             .unwrap()
    187             .visible_event_ids()
    188     );
    189 }
    190 
    191 #[tokio::test]
    192 async fn visibility_index_bounds_charge_before_payload_decode_and_commit_partial_progress() {
    193     let storage = store(generation()).await;
    194     let first = profile("first", 10);
    195     let size = first.raw_json().len() as u64;
    196     storage.admit(raw(first)).await.unwrap();
    197     storage
    198         .admit(raw(profile(&"x".repeat(1000), 20)))
    199         .await
    200         .unwrap();
    201     let one = 4096 + 2 * 128 + VisibilityIndexBounds::charge(size).unwrap();
    202     storage.work.take();
    203     let progress = storage
    204         .maintain_visibility(VisibilityIndexBounds::new(2, one).unwrap())
    205         .await
    206         .unwrap();
    207     assert_eq!(progress.charged_bytes(), one);
    208     assert_eq!(progress.processed_rows(), 1);
    209     assert_eq!(storage.work.take().codec_inputs, 1);
    210     assert!(!progress.is_complete());
    211     assert_eq!(
    212         storage
    213             .maintain_visibility(VisibilityIndexBounds::new(2, one).unwrap())
    214             .await,
    215         Err(Error::SpaceInsufficient)
    216     );
    217     assert_eq!(storage.work.take().codec_inputs, 0);
    218     assert_eq!(
    219         sqlx::query_scalar::<_, i64>(
    220             "SELECT backfill_sequence FROM radroots_runtime_visibility_progress"
    221         )
    222         .fetch_one(&storage.pool)
    223         .await
    224         .unwrap(),
    225         1
    226     );
    227     finish(&storage).await;
    228     storage.work.take();
    229     assert!(
    230         storage
    231             .maintain_visibility(bounds(64))
    232             .await
    233             .unwrap()
    234             .is_complete()
    235     );
    236     assert_eq!(storage.work.take().codec_inputs, 0);
    237 }
    238 
    239 #[tokio::test]
    240 async fn visibility_index_fault_rolls_back_all_metadata_and_progress_then_resumes() {
    241     let storage = store(generation()).await;
    242     storage.admit(raw(profile("first", 10))).await.unwrap();
    243     sqlx::raw_sql("CREATE TEMP TRIGGER visibility_test_fault AFTER INSERT ON radroots_runtime_visibility_events BEGIN SELECT RAISE(ABORT, 'index test fault'); END;")
    244         .execute(&storage.pool).await.unwrap();
    245     assert_eq!(
    246         storage.maintain_visibility(bounds(1)).await,
    247         Err(Error::BackendUnavailable)
    248     );
    249     for query in [
    250         "SELECT COUNT(*) FROM radroots_runtime_visibility_progress",
    251         "SELECT COUNT(*) FROM radroots_runtime_visibility_events",
    252     ] {
    253         assert_eq!(
    254             sqlx::query_scalar::<_, i64>(query)
    255                 .fetch_one(&storage.pool)
    256                 .await
    257                 .unwrap(),
    258             0
    259         );
    260     }
    261     sqlx::query("DROP TRIGGER visibility_test_fault")
    262         .execute(&storage.pool)
    263         .await
    264         .unwrap();
    265     assert!(
    266         storage
    267             .maintain_visibility(bounds(1))
    268             .await
    269             .unwrap()
    270             .is_complete()
    271     );
    272 }
    273 
    274 #[tokio::test]
    275 async fn visibility_index_direct_writer_invalidates_atomically_and_rollback_preserves_ready_state()
    276 {
    277     let storage = store(generation()).await;
    278     let event = profile("first", 10);
    279     storage.admit(raw(event)).await.unwrap();
    280     finish(&storage).await;
    281     let mut transaction = storage.pool.begin_with("BEGIN IMMEDIATE").await.unwrap();
    282     sqlx::query("UPDATE radroots_runtime_events SET admission_stage = 'verified'")
    283         .execute(&mut *transaction)
    284         .await
    285         .unwrap();
    286     let dirty: bool = sqlx::query_scalar("SELECT ready FROM radroots_runtime_visibility_progress")
    287         .fetch_one(&mut *transaction)
    288         .await
    289         .unwrap();
    290     assert!(!dirty);
    291     assert_eq!(
    292         sqlx::query_scalar::<_, i64>("SELECT last_mutation_epoch FROM radroots_runtime_events")
    293             .fetch_one(&mut *transaction)
    294             .await
    295             .unwrap(),
    296         2
    297     );
    298     transaction.rollback().await.unwrap();
    299     assert!(
    300         sqlx::query_scalar::<_, bool>("SELECT ready FROM radroots_runtime_visibility_progress")
    301             .fetch_one(&storage.pool)
    302             .await
    303             .unwrap()
    304     );
    305     sqlx::query("UPDATE radroots_runtime_events SET admission_stage = 'verified'")
    306         .execute(&storage.pool)
    307         .await
    308         .unwrap();
    309     assert!(
    310         !sqlx::query_scalar::<_, bool>("SELECT ready FROM radroots_runtime_visibility_progress")
    311             .fetch_one(&storage.pool)
    312             .await
    313             .unwrap()
    314     );
    315     let current = storage.maintain_visibility(bounds(1)).await.unwrap();
    316     assert!(current.is_complete());
    317     assert_eq!(current.processed_rows(), 1);
    318     sqlx::query("UPDATE radroots_runtime_events SET admission_stage = admission_stage")
    319         .execute(&storage.pool)
    320         .await
    321         .unwrap();
    322     assert_eq!(
    323         storage
    324             .maintain_visibility(bounds(1))
    325             .await
    326             .unwrap()
    327             .processed_rows(),
    328         0
    329     );
    330 }
    331 
    332 #[tokio::test]
    333 async fn visibility_index_corruption_and_retired_or_readonly_sources_refuse_completion() {
    334     let storage = store(generation()).await;
    335     storage.admit(raw(profile("first", 10))).await.unwrap();
    336     sqlx::query("DROP TRIGGER radroots_runtime_events_raw_update_guard")
    337         .execute(&storage.pool)
    338         .await
    339         .unwrap();
    340     sqlx::query("UPDATE radroots_runtime_events SET signed_event = X'7b'")
    341         .execute(&storage.pool)
    342         .await
    343         .unwrap();
    344     assert_eq!(
    345         storage.maintain_visibility(bounds(1)).await,
    346         Err(Error::CorruptStoredEvent)
    347     );
    348     assert_eq!(
    349         sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM radroots_runtime_visibility_progress")
    350             .fetch_one(&storage.pool)
    351             .await
    352             .unwrap(),
    353         0
    354     );
    355     let readonly = SqliteStorage::new(storage.pool.clone(), generation(), EventStoreMode::ReadOnly);
    356     assert_eq!(
    357         readonly.maintain_visibility(bounds(1)).await,
    358         Err(Error::BackendUnavailable)
    359     );
    360     sqlx::query(
    361         "UPDATE radroots_runtime_source_generations SET state = 'retired', retired_at_unix_ms = 2",
    362     )
    363     .execute(&storage.pool)
    364     .await
    365     .unwrap();
    366     assert_eq!(
    367         storage.maintain_visibility(bounds(1)).await,
    368         Err(Error::SourceGenerationChanged)
    369     );
    370 }
    371 
    372 #[tokio::test]
    373 async fn visibility_index_empty_source_is_complete_and_last_change_replay_uses_index_navigation() {
    374     let storage = store(generation()).await;
    375     let empty = storage.maintain_visibility(bounds(1)).await.unwrap();
    376     assert!(empty.is_complete());
    377     assert_eq!(empty.token().epoch(), 0);
    378     storage
    379         .admit(raw(profile("first", u64::MAX)))
    380         .await
    381         .unwrap();
    382     let progress = storage.maintain_visibility(bounds(1)).await.unwrap();
    383     assert!(progress.is_complete());
    384     assert_eq!(progress.source_sequence(), 0);
    385     let time: Vec<u8> =
    386         sqlx::query_scalar("SELECT created_at FROM radroots_runtime_visibility_events")
    387             .fetch_one(&storage.pool)
    388             .await
    389             .unwrap();
    390     assert_eq!(time, u64::MAX.to_be_bytes());
    391     let plan = sqlx::query("EXPLAIN QUERY PLAN SELECT source_sequence, last_mutation_epoch, length(signed_event) FROM radroots_runtime_events WHERE source_generation = ? AND (last_mutation_epoch, source_sequence) > (?, ?) ORDER BY last_mutation_epoch, source_sequence LIMIT ?")
    392         .bind(generation().as_bytes().as_slice()).bind(0i64).bind(0i64).bind(1i64)
    393         .fetch_all(&storage.pool).await.unwrap();
    394     assert!(plan.iter().any(|row| {
    395         row.get::<String, _>("detail")
    396             .contains("radroots_runtime_events_mutation_idx")
    397     }));
    398 }
    399 
    400 #[tokio::test]
    401 async fn visibility_index_real_database_close_and_reopen_preserves_bounded_progress() {
    402     let directory = tempfile::tempdir().unwrap();
    403     let paths = crate::Paths::from_directory(directory.path()).unwrap();
    404     let storage = SqliteStorage::open(
    405         crate::OpenOptions::new(paths.clone(), crate::OpenMode::Create)
    406             .with_source_generation(generation(), 100)
    407             .unwrap(),
    408     )
    409     .await
    410     .unwrap();
    411     // Retain a real file-backed unprepared-source fixture; fresh empty sources
    412     // otherwise initialize a ready index under forward20.
    413     sqlx::query("DELETE FROM radroots_runtime_visibility_progress WHERE source_generation = ?")
    414         .bind(generation().as_bytes().as_slice())
    415         .execute(&storage.pool)
    416         .await
    417         .unwrap();
    418     for event in [profile("first", 10), profile("second", 20)] {
    419         storage.admit(admitted(event)).await.unwrap();
    420     }
    421     let first = storage.maintain_visibility(bounds(1)).await.unwrap();
    422     assert_eq!(first.source_sequence(), 1);
    423     assert!(!first.is_complete());
    424     storage.close().await.unwrap();
    425     assert_eq!(
    426         storage.maintain_visibility(bounds(1)).await,
    427         Err(Error::BackendUnavailable)
    428     );
    429     let reopened = SqliteStorage::open(crate::OpenOptions::new(
    430         paths,
    431         crate::OpenMode::ReadWriteExisting,
    432     ))
    433     .await
    434     .unwrap();
    435     let next = reopened.maintain_visibility(bounds(1)).await.unwrap();
    436     assert_eq!(next.source_sequence(), 2);
    437     assert_eq!(next.processed_rows(), 1);
    438     assert!(next.is_complete());
    439     assert_eq!(
    440         indexed_visible(&reopened).await,
    441         reopened
    442             .rebuild_visibility()
    443             .await
    444             .unwrap()
    445             .visible_event_ids()
    446     );
    447     reopened.close().await.unwrap();
    448 }
    449 
    450 #[tokio::test]
    451 async fn visibility_index_forward_migration_retains_legacy_truth_and_refuses_corrupt_payloads() {
    452     use crate::migration::runtime::{MIGRATIONS, migration_sql};
    453     use sqlx::sqlite::SqlitePoolOptions;
    454     for state in [0, 1, 2] {
    455         let pool = SqlitePoolOptions::new()
    456             .max_connections(1)
    457             .connect("sqlite::memory:")
    458             .await
    459             .unwrap();
    460         for migration in &MIGRATIONS[..18] {
    461             sqlx::raw_sql(migration_sql(migration.version()).unwrap())
    462                 .execute(&pool)
    463                 .await
    464                 .unwrap();
    465         }
    466         sqlx::query("INSERT INTO radroots_runtime_source_generations (generation, sequence_head, state, created_at_unix_ms) VALUES (?, 0, 'active', 1)")
    467             .bind(generation().as_bytes().as_slice()).execute(&pool).await.unwrap();
    468         let storage = SqliteStorage::new(pool.clone(), generation(), EventStoreMode::ReadWrite);
    469         let event = profile("legacy", 10);
    470         if state != 0 {
    471             sqlx::query("UPDATE radroots_runtime_source_generations SET sequence_head = 1 WHERE generation = ?")
    472                 .bind(generation().as_bytes().as_slice()).execute(&pool).await.unwrap();
    473             sqlx::query(
    474                 "INSERT INTO radroots_runtime_events (source_generation, source_sequence, event_id,
    475                 admission_stage, signed_event, admitted_at_unix_ms, updated_at_unix_ms)
    476                 VALUES (?, 1, ?, 'raw', ?, 100, 100)",
    477             )
    478             .bind(generation().as_bytes().as_slice())
    479             .bind(event.id().as_bytes().as_slice())
    480             .bind(event.raw_json().as_bytes())
    481             .execute(&pool)
    482             .await
    483             .unwrap();
    484             if state == 2 {
    485                 sqlx::query("DROP TRIGGER radroots_runtime_events_raw_update_guard")
    486                     .execute(&pool)
    487                     .await
    488                     .unwrap();
    489                 sqlx::query("UPDATE radroots_runtime_events SET signed_event = X'7b'")
    490                     .execute(&pool)
    491                     .await
    492                     .unwrap();
    493             }
    494         }
    495         let token = storage.source_token().await.unwrap();
    496         assert_eq!(
    497             storage.maintain_visibility(bounds(1)).await,
    498             Err(Error::BackendUnavailable)
    499         );
    500         storage.work.take();
    501         sqlx::raw_sql(migration_sql(19).unwrap())
    502             .execute(&pool)
    503             .await
    504             .unwrap();
    505         sqlx::raw_sql(migration_sql(20).unwrap())
    506             .execute(&pool)
    507             .await
    508             .unwrap();
    509         assert_eq!(storage.source_token().await.unwrap(), token);
    510         assert_eq!(storage.work.take().codec_inputs, 0);
    511         if state == 2 {
    512             assert_eq!(
    513                 storage.maintain_visibility(bounds(1)).await,
    514                 Err(Error::CorruptStoredEvent)
    515             );
    516         } else {
    517             let result = storage.maintain_visibility(bounds(1)).await.unwrap();
    518             assert!(result.is_complete());
    519             assert_eq!(result.processed_rows(), u16::from(state == 1));
    520             if state == 1 {
    521                 assert_eq!(
    522                     sqlx::query_scalar::<_, Vec<u8>>(
    523                         "SELECT signed_event FROM radroots_runtime_events"
    524                     )
    525                     .fetch_one(&pool)
    526                     .await
    527                     .unwrap(),
    528                     event.raw_json().as_bytes()
    529                 );
    530             }
    531         }
    532     }
    533 }
    534 
    535 #[tokio::test]
    536 async fn visibility_index_cancellation_before_admission_leaves_no_progress_and_allows_retry() {
    537     let storage = store(generation()).await;
    538     storage.admit(raw(profile("first", 10))).await.unwrap();
    539     let held = storage.pool.acquire().await.unwrap();
    540     let mut future = storage.maintain_visibility(bounds(1));
    541     std::future::poll_fn(|context| {
    542         assert!(future.as_mut().poll(context).is_pending());
    543         std::task::Poll::Ready(())
    544     })
    545     .await;
    546     drop(future);
    547     drop(held);
    548     assert_eq!(
    549         sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM radroots_runtime_visibility_progress")
    550             .fetch_one(&storage.pool)
    551             .await
    552             .unwrap(),
    553         0
    554     );
    555     assert!(
    556         storage
    557             .maintain_visibility(bounds(1))
    558             .await
    559             .unwrap()
    560             .is_complete()
    561     );
    562 }
    563 
    564 #[tokio::test]
    565 async fn visibility_index_malformed_progress_or_missing_source_tail_cannot_claim_complete() {
    566     let storage = store(generation()).await;
    567     storage.admit(raw(profile("first", 10))).await.unwrap();
    568     finish(&storage).await;
    569     sqlx::query("PRAGMA ignore_check_constraints = ON")
    570         .execute(&storage.pool)
    571         .await
    572         .unwrap();
    573     sqlx::query("UPDATE radroots_runtime_visibility_progress SET schema_version = 2")
    574         .execute(&storage.pool)
    575         .await
    576         .unwrap();
    577     assert_eq!(
    578         storage.maintain_visibility(bounds(1)).await,
    579         Err(Error::CorruptStoredEvent)
    580     );
    581     sqlx::query("UPDATE radroots_runtime_visibility_progress SET schema_version = 1")
    582         .execute(&storage.pool)
    583         .await
    584         .unwrap();
    585     // Clear only derived membership before deliberately restarting this fixture.
    586     for table in [
    587         "radroots_runtime_visibility_heads",
    588         "radroots_runtime_visibility_members",
    589         "radroots_runtime_visibility_pending",
    590     ] {
    591         let sql = match table {
    592             "radroots_runtime_visibility_heads" => "DELETE FROM radroots_runtime_visibility_heads",
    593             "radroots_runtime_visibility_members" => {
    594                 "DELETE FROM radroots_runtime_visibility_members"
    595             }
    596             _ => "DELETE FROM radroots_runtime_visibility_pending",
    597         };
    598         sqlx::query(sql).execute(&storage.pool).await.unwrap();
    599     }
    600     sqlx::query("DELETE FROM radroots_runtime_visibility_progress")
    601         .execute(&storage.pool)
    602         .await
    603         .unwrap();
    604     sqlx::query("UPDATE radroots_runtime_source_generations SET sequence_head = 2")
    605         .execute(&storage.pool)
    606         .await
    607         .unwrap();
    608     assert!(
    609         !storage
    610             .maintain_visibility(bounds(1))
    611             .await
    612             .unwrap()
    613             .is_complete()
    614     );
    615     assert_eq!(
    616         storage.maintain_visibility(bounds(1)).await,
    617         Err(Error::CorruptStoredEvent)
    618     );
    619 }
    620 
    621 #[tokio::test]
    622 async fn visibility_index_memory_and_sqlite_progress_agree_across_same_count_promotion() {
    623     let storage = super::tests::store(generation()).await;
    624     let memory = radroots_storage::memory::MemoryStorage::new(generation());
    625     let first = profile("first", 10);
    626     let initial = [raw(first.clone()), raw(profile("large", 20))];
    627     for admission in initial {
    628         storage.admit(admission.clone()).await.unwrap();
    629         memory.admit(admission).await.unwrap();
    630     }
    631     for budget in [
    632         VisibilityIndexBounds::new(
    633             2,
    634             4096 + 2 * 128 + VisibilityIndexBounds::charge(first.raw_json().len() as u64).unwrap(),
    635         )
    636         .unwrap(),
    637         bounds(1),
    638     ] {
    639         assert_eq!(
    640             storage.maintain_visibility(budget).await.unwrap(),
    641             memory.maintain_visibility(budget).await.unwrap()
    642         );
    643     }
    644     for admission in [admitted(first.clone()), admitted(profile("later", 30))] {
    645         storage.admit(admission.clone()).await.unwrap();
    646         memory.admit(admission).await.unwrap();
    647     }
    648     for _ in 0..3 {
    649         let sql = storage.maintain_visibility(bounds(1)).await.unwrap();
    650         let reference = memory.maintain_visibility(bounds(1)).await.unwrap();
    651         assert_eq!(sql, reference);
    652         if sql.is_complete() {
    653             break;
    654         }
    655     }
    656     assert!(
    657         storage
    658             .maintain_visibility(bounds(1))
    659             .await
    660             .unwrap()
    661             .is_complete()
    662     );
    663     assert_eq!(
    664         storage.rebuild_visibility().await.unwrap(),
    665         memory.rebuild_visibility().await.unwrap()
    666     );
    667     assert_eq!(
    668         indexed_visible(&storage).await,
    669         memory
    670             .rebuild_visibility()
    671             .await
    672             .unwrap()
    673             .visible_event_ids()
    674     );
    675 }