lib

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

batch_tests.rs (40246B)


      1 use super::*;
      2 use crate::{OpenMode, OpenOptions, Paths};
      3 use radroots_storage::{
      4     EventStore, ProjectionStore,
      5     event::SourceGeneration,
      6     memory::MemoryStorage,
      7     projection::{
      8         batch::ProjectionWriteScope,
      9         document_query::{ProjectionDocumentGenerations, ProjectionDocumentQuery},
     10     },
     11 };
     12 use tempfile::TempDir;
     13 
     14 fn id() -> ProjectionId {
     15     ProjectionId::parse("fixture").unwrap()
     16 }
     17 fn generation(byte: u8) -> ProjectionGeneration {
     18     ProjectionGeneration::new([byte; 32]).unwrap()
     19 }
     20 fn document(key: &str) -> ProjectionDocument {
     21     ProjectionDocument::new(key.into(), key.as_bytes().to_vec()).unwrap()
     22 }
     23 fn scope(
     24     source: EventSourceToken,
     25     active: Option<ProjectionGeneration>,
     26     target: u8,
     27 ) -> ProjectionWriteScope {
     28     ProjectionWriteScope::new(id(), generation(target), source, active).unwrap()
     29 }
     30 fn batch(scope: ProjectionWriteScope, keys: &[&str]) -> ProjectionDocumentBatch {
     31     ProjectionDocumentBatch::new(scope, keys.iter().map(|key| document(key)).collect()).unwrap()
     32 }
     33 fn checkpoint(target: u8, count: u64) -> ProjectionCheckpoint {
     34     ProjectionCheckpoint::new(id(), generation(target), None, count, target as u64).unwrap()
     35 }
     36 fn query(target: u8) -> ProjectionDocumentQuery {
     37     ProjectionDocumentQuery::new(
     38         id(),
     39         ProjectionDocumentGenerations::Exact(generation(target)),
     40         64,
     41     )
     42     .unwrap()
     43 }
     44 async fn open(temp: &TempDir) -> SqliteStorage {
     45     SqliteStorage::open(
     46         OpenOptions::new(
     47             Paths::from_directory(temp.path()).unwrap(),
     48             OpenMode::Create,
     49         )
     50         .with_source_generation(SourceGeneration::new([9; 32]).unwrap(), 9)
     51         .unwrap(),
     52     )
     53     .await
     54     .unwrap()
     55 }
     56 
     57 #[tokio::test]
     58 async fn projection_atomic_batch_and_promotion_match_memory_and_survive_reopen() {
     59     let temp = TempDir::new().unwrap();
     60     let store = open(&temp).await;
     61     let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
     62     let source = store.source_token().await.unwrap();
     63     for backend in [&store as &dyn ProjectionStore, &memory] {
     64         let first = scope(source, None, 1);
     65         backend
     66             .put_projection_batch(batch(first.clone(), &["a", "b"]))
     67             .await
     68             .unwrap();
     69         assert_eq!(backend.status(id()).await.unwrap(), None);
     70         assert_eq!(
     71             backend
     72                 .promote_projection_batch(batch(first.clone(), &["c"]), checkpoint(1, 2))
     73                 .await,
     74             Err(Error::ProjectionCheckpointMismatch)
     75         );
     76         assert_eq!(
     77             backend
     78                 .query_projection_documents(query(1))
     79                 .await
     80                 .unwrap()
     81                 .records()
     82                 .len(),
     83             2
     84         );
     85         backend
     86             .promote_projection_batch(batch(first, &["c"]), checkpoint(1, 3))
     87             .await
     88             .unwrap();
     89         assert_eq!(
     90             backend
     91                 .put_projection_document(id(), generation(1), document("a"))
     92                 .await,
     93             Err(Error::ProjectionRevisionConflict)
     94         );
     95         let second = scope(source, Some(generation(1)), 2);
     96         backend
     97             .put_projection_batch(batch(second.clone(), &["x"]))
     98             .await
     99             .unwrap();
    100         assert_eq!(
    101             backend
    102                 .put_projection_batch(batch(scope(source, Some(generation(1)), 3), &[]))
    103                 .await,
    104             Err(Error::ProjectionRevisionConflict)
    105         );
    106         assert_eq!(
    107             backend
    108                 .query_projection_documents_at(query(1), source, generation(1))
    109                 .await
    110                 .unwrap()
    111                 .records()
    112                 .len(),
    113             3
    114         );
    115     }
    116     store.close().await.unwrap();
    117     let store = open(&temp).await;
    118     assert_eq!(
    119         store
    120             .query_projection_documents_at(query(1), source, generation(1))
    121             .await
    122             .unwrap(),
    123         memory
    124             .query_projection_documents_at(query(1), source, generation(1))
    125             .await
    126             .unwrap()
    127     );
    128     for backend in [&store as &dyn ProjectionStore, &memory] {
    129         backend
    130             .promote_projection_batch(
    131                 batch(scope(source, Some(generation(1)), 2), &["y"]),
    132                 checkpoint(2, 2),
    133             )
    134             .await
    135             .unwrap();
    136         assert_eq!(
    137             backend
    138                 .query_projection_documents_at(query(1), source, generation(1))
    139                 .await,
    140             Err(Error::ProjectionRevisionConflict)
    141         );
    142         assert_eq!(
    143             backend
    144                 .query_projection_documents_at(query(2), source, generation(2))
    145                 .await
    146                 .unwrap()
    147                 .records()
    148                 .len(),
    149             2
    150         );
    151     }
    152     assert_eq!(
    153         store
    154             .query_projection_documents_at(query(2), source, generation(2))
    155             .await
    156             .unwrap(),
    157         memory
    158             .query_projection_documents_at(query(2), source, generation(2))
    159             .await
    160             .unwrap()
    161     );
    162     store.close().await.unwrap();
    163     let readonly = SqliteStorage::open(OpenOptions::new(
    164         Paths::from_directory(temp.path()).unwrap(),
    165         OpenMode::ReadOnly,
    166     ))
    167     .await
    168     .unwrap();
    169     assert_eq!(
    170         readonly
    171             .query_projection_documents_at(query(2), source, generation(2))
    172             .await
    173             .unwrap()
    174             .records()
    175             .len(),
    176         2
    177     );
    178     assert_eq!(
    179         readonly
    180             .put_projection_batch(batch(scope(source, Some(generation(2)), 3), &["z"]))
    181             .await,
    182         Err(Error::BackendUnavailable)
    183     );
    184     readonly.close().await.unwrap();
    185 }
    186 
    187 #[tokio::test]
    188 async fn projection_final_batch_fault_rolls_back_every_row_fence_and_status() {
    189     let temp = TempDir::new().unwrap();
    190     let store = open(&temp).await;
    191     let source = store.source_token().await.unwrap();
    192     store
    193         .promote_projection_batch(batch(scope(source, None, 1), &["old"]), checkpoint(1, 1))
    194         .await
    195         .unwrap();
    196     sqlx::raw_sql("CREATE TRIGGER fixture_projection_abort BEFORE INSERT ON radroots_runtime_projection_documents WHEN NEW.document_key = 'fail' BEGIN SELECT RAISE(ABORT,'fixture'); END;")
    197         .execute(store.pool()).await.unwrap();
    198     let next = scope(source, Some(generation(1)), 2);
    199     assert!(
    200         store
    201             .promote_projection_batch(batch(next.clone(), &["new", "fail"]), checkpoint(2, 2))
    202             .await
    203             .is_err()
    204     );
    205     assert!(
    206         store
    207             .query_projection_documents(query(2))
    208             .await
    209             .unwrap()
    210             .records()
    211             .is_empty()
    212     );
    213     assert_eq!(
    214         ProjectionStore::status(&store, id())
    215             .await
    216             .unwrap()
    217             .unwrap()
    218             .generation(),
    219         generation(1)
    220     );
    221     let count: i64 = sqlx::query_scalar(
    222         "SELECT count(*) FROM radroots_runtime_projection_generations WHERE phase='staging'",
    223     )
    224     .fetch_one(store.pool())
    225     .await
    226     .unwrap();
    227     assert_eq!(count, 0);
    228     sqlx::raw_sql("DROP TRIGGER fixture_projection_abort")
    229         .execute(store.pool())
    230         .await
    231         .unwrap();
    232     store
    233         .promote_projection_batch(batch(next, &["new", "fail"]), checkpoint(2, 2))
    234         .await
    235         .unwrap();
    236     assert_eq!(
    237         store
    238             .query_projection_documents_at(query(2), source, generation(2))
    239             .await
    240             .unwrap()
    241             .records()
    242             .len(),
    243         2
    244     );
    245     store.close().await.unwrap();
    246 }
    247 
    248 #[tokio::test]
    249 async fn projection_stale_source_epoch_and_cancelled_wait_cannot_publish() {
    250     let temp = TempDir::new().unwrap();
    251     let store = open(&temp).await;
    252     let source = store.source_token().await.unwrap();
    253     let mut all = Vec::new();
    254     for _ in 0..4 {
    255         all.push(store.pool().acquire().await.unwrap());
    256     }
    257     let next = scope(source, None, 1);
    258     let mut future = store.promote_projection_batch(batch(next.clone(), &["a"]), checkpoint(1, 1));
    259     assert!(matches!(
    260         future
    261             .as_mut()
    262             .poll(&mut std::task::Context::from_waker(std::task::Waker::noop())),
    263         std::task::Poll::Pending
    264     ));
    265     drop(future);
    266     drop(all);
    267     assert_eq!(ProjectionStore::status(&store, id()).await.unwrap(), None);
    268     assert!(
    269         store
    270             .query_projection_documents(query(1))
    271             .await
    272             .unwrap()
    273             .records()
    274             .is_empty()
    275     );
    276     store
    277         .put_projection_batch(batch(next.clone(), &["a"]))
    278         .await
    279         .unwrap();
    280     sqlx::query("UPDATE radroots_runtime_source_generations SET mutation_epoch = mutation_epoch + 1 WHERE generation = ?")
    281         .bind(source.generation().as_bytes().as_slice()).execute(store.pool()).await.unwrap();
    282     assert_eq!(
    283         store
    284             .promote_projection_batch(batch(next, &["b"]), checkpoint(1, 2))
    285             .await,
    286         Err(Error::SourceEpochChanged)
    287     );
    288     let changed = store.source_token().await.unwrap();
    289     assert_eq!(
    290         store
    291             .put_projection_batch(batch(scope(changed, None, 1), &["b"]))
    292             .await,
    293         Err(Error::ProjectionRevisionConflict)
    294     );
    295     assert_eq!(
    296         store
    297             .query_projection_documents(query(1))
    298             .await
    299             .unwrap()
    300             .records()
    301             .len(),
    302         1
    303     );
    304     assert_eq!(ProjectionStore::status(&store, id()).await.unwrap(), None);
    305     store.close().await.unwrap();
    306 }
    307 
    308 #[tokio::test]
    309 async fn projection_wal_readers_observe_complete_old_new_or_typed_stale() {
    310     let temp = TempDir::new().unwrap();
    311     let store = open(&temp).await;
    312     let source = store.source_token().await.unwrap();
    313     store
    314         .promote_projection_batch(
    315             batch(scope(source, None, 1), &["old.a", "old.b"]),
    316             checkpoint(1, 2),
    317         )
    318         .await
    319         .unwrap();
    320     for next in 2..18u8 {
    321         let old = next - 1;
    322         let (read, promoted) = tokio::join!(
    323             store.query_projection_documents_at(query(old), source, generation(old)),
    324             store.promote_projection_batch(
    325                 batch(
    326                     scope(source, Some(generation(old)), next),
    327                     &["new.a", "new.b"]
    328                 ),
    329                 checkpoint(next, 2)
    330             )
    331         );
    332         promoted.unwrap();
    333         match read {
    334             Ok(page) => assert_eq!(page.records().len(), 2),
    335             Err(Error::ProjectionRevisionConflict) => {}
    336             other => panic!("unexpected read {other:?}"),
    337         }
    338         assert_eq!(
    339             store
    340                 .query_projection_documents_at(query(next), source, generation(next))
    341                 .await
    342                 .unwrap()
    343                 .records()
    344                 .len(),
    345             2
    346         );
    347     }
    348     store.close().await.unwrap();
    349 }
    350 
    351 #[tokio::test]
    352 async fn projection_real_same_count_admission_change_invalidates_durable_staging() {
    353     use radroots_event::{SignedEvent, admission::RawEvent, wire::Nip01EventWire};
    354     use radroots_storage::{Storage, event::EventAdmission};
    355     use radroots_transport::{
    356         Target, TransportId,
    357         source::{EventProvenance, ObservedEvent},
    358     };
    359     struct FixtureVerifier;
    360     impl radroots_event::admission::SignatureVerifier for FixtureVerifier {
    361         fn verify_signature(
    362             &self,
    363             _event: &radroots_event::Event,
    364         ) -> Result<(), radroots_event::Error> {
    365             Ok(())
    366         }
    367     }
    368     let mut wire = Nip01EventWire {
    369         id: "0".repeat(64),
    370         pubkey: "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df".into(),
    371         created_at: 1,
    372         kind: 0,
    373         tags: vec![],
    374         content: r#"{"name":"fixture"}"#.into(),
    375         sig: "42".repeat(64),
    376         extra: Default::default(),
    377     };
    378     wire.id = wire.computed_event_id().unwrap().to_hex();
    379     let raw = serde_json::to_string(&wire).unwrap();
    380     let event = SignedEvent::from_wire_verified_id(wire, raw).unwrap();
    381     let target = Target::new(TransportId::NOSTR, "wss://fixture.example").unwrap();
    382     let observed = ObservedEvent::new(
    383         event.clone(),
    384         EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), 1).unwrap(),
    385     );
    386     let verified = RawEvent::new(event.envelope().clone())
    387         .verify_id()
    388         .unwrap()
    389         .verify_signature(&FixtureVerifier)
    390         .unwrap();
    391     let temp = TempDir::new().unwrap();
    392     let store = open(&temp).await;
    393     let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    394     for backend in [&store as &dyn Storage, &memory] {
    395         let empty = backend.source_token().await.unwrap();
    396         let receipt = backend
    397             .admit(EventAdmission::raw(observed.clone()))
    398             .await
    399             .unwrap();
    400         assert_eq!(
    401             backend
    402                 .put_projection_batch(batch(scope(empty, None, 1), &["a"]))
    403                 .await,
    404             Err(Error::SourceEpochChanged)
    405         );
    406         let source = backend.source_token().await.unwrap();
    407         let next = scope(source, None, 1);
    408         backend
    409             .put_projection_batch(batch(next.clone(), &["a"]))
    410             .await
    411             .unwrap();
    412         backend
    413             .admit(EventAdmission::raw(observed.clone()))
    414             .await
    415             .unwrap();
    416         assert_eq!(backend.source_token().await.unwrap(), source);
    417         backend
    418             .admit(EventAdmission::verified(observed.clone(), verified.clone()).unwrap())
    419             .await
    420             .unwrap();
    421         let changed = backend.source_token().await.unwrap();
    422         assert_eq!(changed.epoch(), source.epoch() + 1);
    423         let checkpoint =
    424             ProjectionCheckpoint::new(id(), generation(1), Some(receipt.position()), 2, 1).unwrap();
    425         assert_eq!(
    426             backend
    427                 .promote_projection_batch(batch(next, &["b"]), checkpoint)
    428                 .await,
    429             Err(Error::SourceEpochChanged)
    430         );
    431         assert_eq!(ProjectionStore::status(backend, id()).await.unwrap(), None);
    432         assert_eq!(
    433             backend
    434                 .query_projection_documents(query(1))
    435                 .await
    436                 .unwrap()
    437                 .records()
    438                 .len(),
    439             1
    440         );
    441     }
    442     store.close().await.unwrap();
    443 }
    444 
    445 #[tokio::test]
    446 async fn projection_max_batch_updates_and_exact_lookup_validate_before_hydration() {
    447     let temp = TempDir::new().unwrap();
    448     let store = open(&temp).await;
    449     let source = store.source_token().await.unwrap();
    450     let next = scope(source, None, 1);
    451     let documents: Vec<_> = (0..64).map(|i| document(&format!("k{i:02}"))).collect();
    452     store
    453         .put_projection_batch(ProjectionDocumentBatch::new(next.clone(), documents).unwrap())
    454         .await
    455         .unwrap();
    456     store
    457         .promote_projection_batch(batch(next, &["k00"]), checkpoint(1, 64))
    458         .await
    459         .unwrap();
    460     assert_eq!(
    461         store
    462             .query_projection_documents_at(query(1), source, generation(1))
    463             .await
    464             .unwrap()
    465             .records()
    466             .len(),
    467         64
    468     );
    469     assert_eq!(
    470         store
    471             .projection_document(id(), generation(1), "k00".into())
    472             .await
    473             .unwrap()
    474             .unwrap()
    475             .value(),
    476         b"k00"
    477     );
    478     assert_eq!(
    479         store
    480             .projection_document(id(), generation(1), "missing".into())
    481             .await
    482             .unwrap(),
    483         None
    484     );
    485     assert_eq!(
    486         store
    487             .projection_document(id(), generation(1), "bad\nkey".into())
    488             .await,
    489         Err(Error::InvalidProjectionDocument)
    490     );
    491     sqlx::query("UPDATE radroots_runtime_projection_documents SET value_sha256 = zeroblob(32) WHERE document_key='k00'").execute(store.pool()).await.unwrap();
    492     assert_eq!(
    493         store
    494             .projection_document(id(), generation(1), "k00".into())
    495             .await,
    496         Err(Error::CorruptProjectionDocument)
    497     );
    498     let plan=sqlx::query("EXPLAIN QUERY PLAN SELECT document_key FROM radroots_runtime_projection_documents WHERE projection_id=? AND generation=? AND document_key>? ORDER BY document_key LIMIT 65")
    499         .bind(id().as_str()).bind(generation(1).as_bytes().as_slice()).bind("k00").fetch_all(store.pool()).await.unwrap();
    500     let details: Vec<String> = plan.iter().map(|row| row.get("detail")).collect();
    501     assert!(
    502         details
    503             .iter()
    504             .any(|line| line.contains("PRIMARY KEY") && line.contains("document_key>"))
    505     );
    506     assert!(!details.iter().any(|line| line.contains("TEMP B-TREE")));
    507     store.close().await.unwrap();
    508 }
    509 
    510 #[tokio::test]
    511 async fn projection_foreign_sources_legacy_rows_and_invalidated_headers_refuse_without_writes() {
    512     let temp = TempDir::new().unwrap();
    513     let store = open(&temp).await;
    514     let source = store.source_token().await.unwrap();
    515     let foreign = EventSourceToken::new(SourceGeneration::new([8; 32]).unwrap(), 0);
    516     assert_eq!(
    517         store
    518             .put_projection_batch(batch(scope(foreign, None, 1), &["foreign"]))
    519             .await,
    520         Err(Error::SourceEpochChanged)
    521     );
    522     store
    523         .put_projection_document(id(), generation(1), document("legacy"))
    524         .await
    525         .unwrap();
    526     assert_eq!(
    527         store
    528             .put_projection_batch(batch(scope(source, None, 1), &[]))
    529             .await,
    530         Err(Error::ProjectionRevisionConflict)
    531     );
    532     store
    533         .promote_projection_batch(batch(scope(source, None, 2), &["active"]), checkpoint(2, 1))
    534         .await
    535         .unwrap();
    536     assert_eq!(
    537         store
    538             .query_projection_documents_at(query(1), source, generation(2))
    539             .await,
    540         Err(Error::InvalidProjectionDocument)
    541     );
    542     store
    543         .invalidate(
    544             radroots_storage::projection::ProjectionInvalidation::new(
    545                 id(),
    546                 generation(2),
    547                 generation(3),
    548                 radroots_storage::projection::InvalidationReason::OperatorRequested,
    549                 3,
    550             )
    551             .unwrap(),
    552         )
    553         .await
    554         .unwrap();
    555     assert_eq!(
    556         store
    557             .put_projection_batch(batch(scope(source, Some(generation(2)), 3), &["refused"]))
    558             .await,
    559         Err(Error::ProjectionRevisionConflict)
    560     );
    561     assert_eq!(
    562         store
    563             .query_projection_documents_at(query(2), source, generation(2))
    564             .await,
    565         Err(Error::ProjectionRevisionConflict)
    566     );
    567     assert!(
    568         store
    569             .query_projection_documents(query(3))
    570             .await
    571             .unwrap()
    572             .records()
    573             .is_empty()
    574     );
    575     store.close().await.unwrap();
    576 }
    577 
    578 #[tokio::test]
    579 async fn projection_retired_or_corrupt_staging_fences_cannot_be_rebound() {
    580     let temp = TempDir::new().unwrap();
    581     let store = open(&temp).await;
    582     let source = store.source_token().await.unwrap();
    583     let next = scope(source, None, 1);
    584     store
    585         .put_projection_batch(batch(next.clone(), &["retained"]))
    586         .await
    587         .unwrap();
    588     for statement in [
    589         "UPDATE radroots_runtime_projection_generations SET source_generation=zeroblob(32)",
    590         "UPDATE radroots_runtime_projection_generations SET source_epoch=1",
    591         "UPDATE radroots_runtime_projection_generations SET expected_active=zeroblob(32)",
    592         "UPDATE radroots_runtime_projection_generations SET phase='retired'",
    593     ] {
    594         sqlx::query(statement).execute(store.pool()).await.unwrap();
    595         assert_eq!(
    596             store
    597                 .put_projection_batch(batch(next.clone(), &["refused"]))
    598                 .await,
    599             Err(Error::ProjectionRevisionConflict)
    600         );
    601         assert_eq!(
    602             store
    603                 .query_projection_documents(query(1))
    604                 .await
    605                 .unwrap()
    606                 .records()
    607                 .len(),
    608             1
    609         );
    610         sqlx::query("UPDATE radroots_runtime_projection_generations SET source_generation=?,source_epoch=0,expected_active=NULL,phase='staging'")
    611             .bind(source.generation().as_bytes().as_slice()).execute(store.pool()).await.unwrap();
    612     }
    613     store
    614         .promote_projection_batch(batch(next, &[]), checkpoint(1, 1))
    615         .await
    616         .unwrap();
    617     store.close().await.unwrap();
    618 }
    619 
    620 #[tokio::test]
    621 async fn projection_nonempty_checkpoints_bind_exact_identity_generation_and_source_position() {
    622     use radroots_event::{SignedEvent, wire::Nip01EventWire};
    623     use radroots_storage::event::{EventAdmission, EventPosition, EventSequence};
    624     use radroots_transport::{
    625         Target, TransportId,
    626         source::{EventProvenance, ObservedEvent},
    627     };
    628     let temp = TempDir::new().unwrap();
    629     let store = open(&temp).await;
    630     let mut wire = Nip01EventWire {
    631         id: "0".repeat(64),
    632         pubkey: "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df".into(),
    633         created_at: 1,
    634         kind: 0,
    635         tags: vec![],
    636         content: r#"{"name":"fixture"}"#.into(),
    637         sig: "42".repeat(64),
    638         extra: Default::default(),
    639     };
    640     wire.id = wire.computed_event_id().unwrap().to_hex();
    641     let raw = serde_json::to_string(&wire).unwrap();
    642     let event = SignedEvent::from_wire_verified_id(wire, raw).unwrap();
    643     let target = Target::new(TransportId::NOSTR, "wss://fixture.example").unwrap();
    644     let observed = ObservedEvent::new(
    645         event,
    646         EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), 1).unwrap(),
    647     );
    648     let receipt = store.admit(EventAdmission::raw(observed)).await.unwrap();
    649     let source = store.source_token().await.unwrap();
    650     let next = scope(source, None, 1);
    651     store
    652         .put_projection_batch(batch(next.clone(), &["a"]))
    653         .await
    654         .unwrap();
    655     for checkpoint in [
    656         ProjectionCheckpoint::new(
    657             ProjectionId::parse("foreign").unwrap(),
    658             generation(1),
    659             Some(receipt.position()),
    660             2,
    661             1,
    662         )
    663         .unwrap(),
    664         ProjectionCheckpoint::new(id(), generation(2), Some(receipt.position()), 2, 1).unwrap(),
    665         ProjectionCheckpoint::new(id(), generation(1), None, 2, 1).unwrap(),
    666         ProjectionCheckpoint::new(
    667             id(),
    668             generation(1),
    669             Some(EventPosition::new(
    670                 SourceGeneration::new([8; 32]).unwrap(),
    671                 EventSequence::new(1).unwrap(),
    672             )),
    673             2,
    674             1,
    675         )
    676         .unwrap(),
    677         ProjectionCheckpoint::new(
    678             id(),
    679             generation(1),
    680             Some(EventPosition::new(
    681                 source.generation(),
    682                 EventSequence::new(2).unwrap(),
    683             )),
    684             2,
    685             1,
    686         )
    687         .unwrap(),
    688     ] {
    689         assert_eq!(
    690             store
    691                 .promote_projection_batch(batch(next.clone(), &["b"]), checkpoint)
    692                 .await,
    693             Err(Error::ProjectionCheckpointMismatch)
    694         );
    695         assert_eq!(
    696             store
    697                 .query_projection_documents(query(1))
    698                 .await
    699                 .unwrap()
    700                 .records()
    701                 .len(),
    702             1
    703         );
    704         assert_eq!(ProjectionStore::status(&store, id()).await.unwrap(), None);
    705     }
    706     store
    707         .promote_projection_batch(
    708             batch(next, &["b"]),
    709             ProjectionCheckpoint::new(id(), generation(1), Some(receipt.position()), 2, 1).unwrap(),
    710         )
    711         .await
    712         .unwrap();
    713     assert_eq!(
    714         store
    715             .query_projection_documents_at(query(1), source, generation(1))
    716             .await
    717             .unwrap()
    718             .records()
    719             .len(),
    720         2
    721     );
    722     store.close().await.unwrap();
    723 }
    724 
    725 #[tokio::test]
    726 async fn projection_exact_lookup_refuses_zero_and_oversized_stored_values_before_hydration() {
    727     let temp = TempDir::new().unwrap();
    728     let store = open(&temp).await;
    729     let source = store.source_token().await.unwrap();
    730     store
    731         .put_projection_document(id(), generation(1), document("retained"))
    732         .await
    733         .unwrap();
    734     for size in [0, 16_777_217] {
    735         let mut connection = store.pool().acquire().await.unwrap();
    736         sqlx::raw_sql("PRAGMA ignore_check_constraints=ON")
    737             .execute(&mut *connection)
    738             .await
    739             .unwrap();
    740         sqlx::query("UPDATE radroots_runtime_projection_documents SET value=zeroblob(?) WHERE document_key='retained'")
    741             .bind(size).execute(&mut *connection).await.unwrap();
    742         sqlx::raw_sql("PRAGMA ignore_check_constraints=OFF")
    743             .execute(&mut *connection)
    744             .await
    745             .unwrap();
    746         drop(connection);
    747         assert_eq!(
    748             store
    749                 .projection_document(id(), generation(1), "retained".into())
    750                 .await,
    751             Err(Error::CorruptProjectionDocument)
    752         );
    753         assert_eq!(store.source_token().await.unwrap(), source);
    754     }
    755     store.close().await.unwrap();
    756 }
    757 
    758 fn review_admission(verified: bool) -> radroots_storage::event::EventAdmission {
    759     use radroots_event::{SignedEvent, admission::RawEvent, wire::Nip01EventWire};
    760     use radroots_storage::event::EventAdmission;
    761     use radroots_transport::{
    762         Target, TransportId,
    763         source::{EventProvenance, ObservedEvent},
    764     };
    765     struct FixtureVerifier;
    766     impl radroots_event::admission::SignatureVerifier for FixtureVerifier {
    767         fn verify_signature(
    768             &self,
    769             _event: &radroots_event::Event,
    770         ) -> Result<(), radroots_event::Error> {
    771             Ok(())
    772         }
    773     }
    774     let mut wire = Nip01EventWire {
    775         id: "0".repeat(64),
    776         pubkey: "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df".into(),
    777         created_at: 17,
    778         kind: 0,
    779         tags: vec![],
    780         content: r#"{"name":"review"}"#.into(),
    781         sig: "42".repeat(64),
    782         extra: Default::default(),
    783     };
    784     wire.id = wire.computed_event_id().unwrap().to_hex();
    785     let raw = serde_json::to_string(&wire).unwrap();
    786     let event = SignedEvent::from_wire_verified_id(wire, raw).unwrap();
    787     let target = Target::new(TransportId::NOSTR, "wss://fixture.example").unwrap();
    788     let observed = ObservedEvent::new(
    789         event.clone(),
    790         EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), 1).unwrap(),
    791     );
    792     if verified {
    793         let verified = RawEvent::new(event.envelope().clone())
    794             .verify_id()
    795             .unwrap()
    796             .verify_signature(&FixtureVerifier)
    797             .unwrap();
    798         EventAdmission::verified(observed, verified).unwrap()
    799     } else {
    800         EventAdmission::raw(observed)
    801     }
    802 }
    803 
    804 #[tokio::test]
    805 async fn projection_promoted_source_binding_survives_real_insert_and_same_count_change() {
    806     use radroots_storage::Storage;
    807     let temp = TempDir::new().unwrap();
    808     let store = open(&temp).await;
    809     let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    810     for backend in [&memory as &dyn Storage, &store] {
    811         let empty = backend.source_token().await.unwrap();
    812         backend
    813             .promote_projection_batch(batch(scope(empty, None, 1), &["empty"]), checkpoint(1, 1))
    814             .await
    815             .unwrap();
    816         let receipt = backend.admit(review_admission(false)).await.unwrap();
    817         let raw = backend.source_token().await.unwrap();
    818         assert_eq!(
    819             backend
    820                 .query_projection_documents_at(query(1), empty, generation(1))
    821                 .await,
    822             Err(Error::SourceEpochChanged)
    823         );
    824         assert_eq!(
    825             backend
    826                 .query_projection_documents_at(query(1), raw, generation(1))
    827                 .await,
    828             Err(Error::SourceEpochChanged)
    829         );
    830         backend
    831             .promote_projection_batch(
    832                 batch(scope(raw, Some(generation(1)), 2), &["raw"]),
    833                 ProjectionCheckpoint::new(id(), generation(2), Some(receipt.position()), 1, 2)
    834                     .unwrap(),
    835             )
    836             .await
    837             .unwrap();
    838         backend.admit(review_admission(true)).await.unwrap();
    839         let verified = backend.source_token().await.unwrap();
    840         assert_eq!(verified.epoch(), raw.epoch() + 1);
    841         assert_eq!(
    842             backend
    843                 .query_projection_documents_at(query(2), raw, generation(2))
    844                 .await,
    845             Err(Error::SourceEpochChanged)
    846         );
    847         assert_eq!(
    848             backend
    849                 .query_projection_documents_at(query(2), verified, generation(2))
    850                 .await,
    851             Err(Error::SourceEpochChanged)
    852         );
    853         assert_eq!(
    854             backend
    855                 .query_projection_documents(query(2))
    856                 .await
    857                 .unwrap()
    858                 .records()
    859                 .len(),
    860             1
    861         );
    862         backend
    863             .promote_projection_batch(
    864                 batch(scope(verified, Some(generation(2)), 3), &["verified"]),
    865                 ProjectionCheckpoint::new(id(), generation(3), Some(receipt.position()), 1, 3)
    866                     .unwrap(),
    867             )
    868             .await
    869             .unwrap();
    870         assert_eq!(
    871             backend
    872                 .query_projection_documents_at(query(3), verified, generation(3))
    873                 .await
    874                 .unwrap()
    875                 .records()
    876                 .len(),
    877             1
    878         );
    879     }
    880     store.close().await.unwrap();
    881 }
    882 
    883 #[tokio::test]
    884 async fn projection_registered_checkpoint_and_atomic_admission_cannot_bypass_promotion() {
    885     use radroots_storage::{
    886         Storage,
    887         atomic::{
    888             AtomicCommit, AtomicCommitDigest, AtomicCommitId, AtomicWorkflow, CommitIngested,
    889         },
    890     };
    891     let temp = TempDir::new().unwrap();
    892     let store = open(&temp).await;
    893     let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    894     for backend in [&memory as &dyn Storage, &store] {
    895         let source = backend.source_token().await.unwrap();
    896         let staged = scope(source, None, 1);
    897         backend
    898             .put_projection_batch(batch(staged.clone(), &["partial"]))
    899             .await
    900             .unwrap();
    901         assert_eq!(
    902             backend.checkpoint(checkpoint(1, 999)).await,
    903             Err(Error::ProjectionRevisionConflict)
    904         );
    905         assert_eq!(ProjectionStore::status(backend, id()).await.unwrap(), None);
    906         let request = AtomicCommit::new(
    907             AtomicCommitId::new([1; 16]).unwrap(),
    908             AtomicCommitDigest::new([1; 32]),
    909             1,
    910             AtomicWorkflow::Ingested(Box::new(CommitIngested::new(
    911                 review_admission(false),
    912                 Some(checkpoint(1, 999)),
    913             ))),
    914         )
    915         .unwrap();
    916         assert_eq!(
    917             backend.commit(request.clone()).await,
    918             Err(Error::ProjectionRevisionConflict)
    919         );
    920         assert_eq!(backend.receipt(request.commit_id()).await.unwrap(), None);
    921         assert_eq!(backend.source_token().await.unwrap(), source);
    922         assert_eq!(ProjectionStore::status(backend, id()).await.unwrap(), None);
    923         backend
    924             .promote_projection_batch(batch(staged, &[]), checkpoint(1, 1))
    925             .await
    926             .unwrap();
    927         let prior = ProjectionStore::status(backend, id()).await.unwrap();
    928         assert_eq!(
    929             backend.checkpoint(checkpoint(1, 999)).await,
    930             Err(Error::ProjectionRevisionConflict)
    931         );
    932         assert_eq!(ProjectionStore::status(backend, id()).await.unwrap(), prior);
    933         assert_eq!(
    934             backend
    935                 .query_projection_documents_at(query(1), source, generation(1))
    936                 .await
    937                 .unwrap()
    938                 .records()
    939                 .len(),
    940             1
    941         );
    942         backend
    943             .promote_projection_batch(
    944                 batch(scope(source, Some(generation(1)), 2), &["new"]),
    945                 checkpoint(2, 1),
    946             )
    947             .await
    948             .unwrap();
    949         assert_eq!(
    950             backend.checkpoint(checkpoint(1, 999)).await,
    951             Err(Error::ProjectionRevisionConflict)
    952         );
    953         let legacy = ProjectionId::parse("legacy").unwrap();
    954         backend
    955             .put_projection_document(legacy.clone(), generation(9), document("legacy"))
    956             .await
    957             .unwrap();
    958         backend
    959             .checkpoint(
    960                 ProjectionCheckpoint::new(legacy.clone(), generation(9), None, 7, 9).unwrap(),
    961             )
    962             .await
    963             .unwrap();
    964         let legacy_query = ProjectionDocumentQuery::new(
    965             legacy,
    966             ProjectionDocumentGenerations::Exact(generation(9)),
    967             1,
    968         )
    969         .unwrap();
    970         assert_eq!(
    971             backend
    972                 .query_projection_documents_at(legacy_query.clone(), source, generation(9))
    973                 .await,
    974             Err(Error::ProjectionRevisionConflict)
    975         );
    976         assert_eq!(
    977             backend
    978                 .query_projection_documents(legacy_query)
    979                 .await
    980                 .unwrap()
    981                 .records()
    982                 .len(),
    983             1
    984         );
    985     }
    986     store.close().await.unwrap();
    987 }
    988 
    989 #[tokio::test]
    990 async fn projection_legacy_rebuild_cannot_publish_registered_staging_or_retired_generation() {
    991     use radroots_storage::projection::{
    992         InvalidationReason, ProjectionInvalidation, RawSourceDigest, RebuildTicket,
    993         RebuildTicketId, RebuildTransition,
    994     };
    995     for retired in [false, true] {
    996         let temp = TempDir::new().unwrap();
    997         let store = open(&temp).await;
    998         let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    999         for backend in [&memory as &dyn ProjectionStore, &store] {
   1000             let source = store.source_token().await.unwrap();
   1001             backend
   1002                 .promote_projection_batch(
   1003                     batch(scope(source, None, 1), &["first"]),
   1004                     checkpoint(1, 1),
   1005                 )
   1006                 .await
   1007                 .unwrap();
   1008             let second = batch(scope(source, Some(generation(1)), 2), &["second"]);
   1009             let (active, target) = if retired {
   1010                 backend
   1011                     .promote_projection_batch(second, checkpoint(2, 1))
   1012                     .await
   1013                     .unwrap();
   1014                 (2, 1)
   1015             } else {
   1016                 backend.put_projection_batch(second).await.unwrap();
   1017                 (1, 2)
   1018             };
   1019             let invalidation = ProjectionInvalidation::new(
   1020                 id(),
   1021                 generation(active),
   1022                 generation(target),
   1023                 InvalidationReason::OperatorRequested,
   1024                 10,
   1025             )
   1026             .unwrap();
   1027             backend.invalidate(invalidation.clone()).await.unwrap();
   1028             let ticket = RebuildTicket::requested(
   1029                 RebuildTicketId::new([target; 16]).unwrap(),
   1030                 invalidation,
   1031                 source.generation(),
   1032                 None,
   1033                 RawSourceDigest::new([1; 32]),
   1034             )
   1035             .unwrap();
   1036             backend.request_rebuild(ticket.clone()).await.unwrap();
   1037             let running = backend
   1038                 .transition_rebuild(RebuildTransition::start(
   1039                     ticket.ticket_id(),
   1040                     ticket.revision(),
   1041                     11,
   1042                 ))
   1043                 .await
   1044                 .unwrap();
   1045             let status = backend.status(id()).await.unwrap();
   1046             assert_eq!(
   1047                 backend
   1048                     .transition_rebuild(RebuildTransition::complete(
   1049                         running.ticket_id(),
   1050                         running.revision(),
   1051                         12,
   1052                         ProjectionCheckpoint::new(id(), generation(target), None, 999, 12).unwrap()
   1053                     ))
   1054                     .await,
   1055                 Err(Error::ProjectionRevisionConflict)
   1056             );
   1057             assert_eq!(backend.status(id()).await.unwrap(), status);
   1058             assert_eq!(
   1059                 backend.rebuild(ticket.ticket_id()).await.unwrap(),
   1060                 Some(running)
   1061             );
   1062             assert_eq!(
   1063                 backend
   1064                     .query_projection_documents(query(target))
   1065                     .await
   1066                     .unwrap()
   1067                     .records()
   1068                     .len(),
   1069                 1
   1070             );
   1071             assert_eq!(
   1072                 backend
   1073                     .query_projection_documents_at(query(target), source, generation(target))
   1074                     .await,
   1075                 Err(Error::ProjectionRevisionConflict)
   1076             );
   1077         }
   1078         store.close().await.unwrap();
   1079     }
   1080 }
   1081 
   1082 #[tokio::test]
   1083 async fn projection_source_mutation_wal_race_reopen_and_read_only_never_relabel_old_rows() {
   1084     for _ in 0..8 {
   1085         let temp = TempDir::new().unwrap();
   1086         let store = open(&temp).await;
   1087         let old = store.source_token().await.unwrap();
   1088         store
   1089             .promote_projection_batch(batch(scope(old, None, 1), &["old"]), checkpoint(1, 1))
   1090             .await
   1091             .unwrap();
   1092         let (mutation, read) = tokio::join!(
   1093             store.admit(review_admission(false)),
   1094             store.query_projection_documents_at(query(1), old, generation(1))
   1095         );
   1096         let receipt = mutation.unwrap();
   1097         match read {
   1098             Ok(page) => assert_eq!(page.records().len(), 1),
   1099             Err(error) => assert_eq!(error, Error::SourceEpochChanged),
   1100         }
   1101         let current = store.source_token().await.unwrap();
   1102         assert_ne!(current, old);
   1103         for token in [old, current] {
   1104             assert_eq!(
   1105                 store
   1106                     .query_projection_documents_at(query(1), token, generation(1))
   1107                     .await,
   1108                 Err(Error::SourceEpochChanged)
   1109             );
   1110         }
   1111         store.close().await.unwrap();
   1112         let readonly = SqliteStorage::open(OpenOptions::new(
   1113             Paths::from_directory(temp.path()).unwrap(),
   1114             OpenMode::ReadOnly,
   1115         ))
   1116         .await
   1117         .unwrap();
   1118         for token in [old, current] {
   1119             assert_eq!(
   1120                 readonly
   1121                     .query_projection_documents_at(query(1), token, generation(1))
   1122                     .await,
   1123                 Err(Error::SourceEpochChanged)
   1124             );
   1125         }
   1126         assert_eq!(
   1127             readonly
   1128                 .query_projection_documents(query(1))
   1129                 .await
   1130                 .unwrap()
   1131                 .records()
   1132                 .len(),
   1133             1
   1134         );
   1135         readonly.close().await.unwrap();
   1136         let reopened = open(&temp).await;
   1137         assert_eq!(
   1138             reopened
   1139                 .query_projection_documents_at(query(1), current, generation(1))
   1140                 .await,
   1141             Err(Error::SourceEpochChanged)
   1142         );
   1143         reopened
   1144             .promote_projection_batch(
   1145                 batch(scope(current, Some(generation(1)), 2), &["new"]),
   1146                 ProjectionCheckpoint::new(id(), generation(2), Some(receipt.position()), 1, 2)
   1147                     .unwrap(),
   1148             )
   1149             .await
   1150             .unwrap();
   1151         assert_eq!(
   1152             reopened
   1153                 .query_projection_documents_at(query(2), current, generation(2))
   1154                 .await
   1155                 .unwrap()
   1156                 .records()
   1157                 .len(),
   1158             1
   1159         );
   1160         reopened.close().await.unwrap();
   1161         let readonly = SqliteStorage::open(OpenOptions::new(
   1162             Paths::from_directory(temp.path()).unwrap(),
   1163             OpenMode::ReadOnly,
   1164         ))
   1165         .await
   1166         .unwrap();
   1167         assert_eq!(
   1168             readonly
   1169                 .query_projection_documents_at(query(2), current, generation(2))
   1170                 .await
   1171                 .unwrap()
   1172                 .records()
   1173                 .len(),
   1174             1
   1175         );
   1176         readonly.close().await.unwrap();
   1177     }
   1178 }