@@ -79,6 +79,47 @@ async fn archive_and_unarchive_refusals_leave_no_state_or_notification_events()
7979 }
8080}
8181
82+ #[tokio::test]
83+ #[ignore = "requires Postgres"]
84+ async fn archive_replays_preserve_snapshot_identity_and_timestamp() {
85+ for websocket in [false, true] {
86+ let f = Fixture::new().await;
87+ let archive = archive_request(&f.user, 9035);
88+ let unarchive = archive_request(&f.user, 9036);
89+ for event in [&archive, &unarchive] {
90+ assert!(f.send(&f.user, event, websocket).await.0);
91+ let before = archive_snapshot(&f).await;
92+ for _ in 0..2 {
93+ assert!(f.send(&f.user, event, websocket).await.0);
94+ assert_eq!(
95+ archive_snapshot(&f).await,
96+ before,
97+ "duplicate must preserve every row, including snapshot id and created_at"
98+ );
99+ }
100+ }
101+ let before = archive_snapshot(&f).await;
102+ assert!(f.send(&f.user, &archive, websocket).await.0);
103+ assert_eq!(
104+ archive_snapshot(&f).await,
105+ before,
106+ "old archive replay must not replace the newer unarchive snapshot"
107+ );
108+ assert!(
109+ f.send(&f.user, &archive_request(&f.user, 9035), websocket)
110+ .await
111+ .0
112+ );
113+ let before = archive_snapshot(&f).await;
114+ assert!(f.send(&f.user, &unarchive, websocket).await.0);
115+ assert_eq!(
116+ archive_snapshot(&f).await,
117+ before,
118+ "old unarchive replay must not replace the newer archive snapshot"
119+ );
120+ }
121+ }
122+
82123#[tokio::test]
83124#[ignore = "requires Postgres"]
84125async fn archive_delta_failure_rolls_back_request_and_mutation() {
@@ -142,8 +183,15 @@ async fn archive_delta_failure_rolls_back_request_and_mutation() {
142183#[tokio::test]
143184#[ignore = "requires Postgres"]
144185async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
145- for websocket in [false, true] {
186+ for ( websocket, seed_snapshot) in [( false, false), (false, true), (true, false), (true, true) ] {
146187 let f = Fixture::new().await;
188+ if seed_snapshot {
189+ assert!(
190+ f.send(&f.peer, &archive_request(&f.peer, 9035), websocket)
191+ .await
192+ .0
193+ );
194+ }
147195 let archive = archive_request(&f.user, 9035);
148196 sqlx::raw_sql(
149197 "CREATE FUNCTION fail_archive_snapshot() RETURNS trigger LANGUAGE plpgsql AS $$
@@ -182,6 +230,26 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
182230 f.send(&f.user, &archive, websocket).await.0,
183231 "duplicate retry refreshes snapshot"
184232 );
233+ let tags: Value = sqlx::query_scalar(
234+ "SELECT tags FROM events WHERE community_id=$1 AND kind=13535 AND deleted_at IS NULL",
235+ )
236+ .bind(f.tenant.community().as_uuid())
237+ .fetch_one(&f.pool)
238+ .await
239+ .unwrap();
240+ assert!(
241+ tags.as_array()
242+ .unwrap()
243+ .contains(&serde_json::json!(["p", f.user.public_key().to_hex()])),
244+ "retry must repair a missing or stale snapshot"
245+ );
246+ let repaired = archive_snapshot(&f).await;
247+ assert!(f.send(&f.user, &archive, websocket).await.0);
248+ assert_eq!(
249+ archive_snapshot(&f).await,
250+ repaired,
251+ "retry after repair must leave the snapshot unchanged"
252+ );
185253 let unarchive = archive_request(&f.user, 9036);
186254 assert!(f.send(&f.user, &unarchive, websocket).await.0);
187255 assert!(f.send(&f.user, &archive, websocket).await.0);
@@ -200,7 +268,11 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
200268 .fetch_one(&f.pool)
201269 .await
202270 .unwrap();
203- assert_eq!(count, 2, "retries must not create new deltas");
271+ assert_eq!(
272+ count,
273+ if seed_snapshot { 3 } else { 2 },
274+ "retries must not create new deltas"
275+ );
204276 assert!(
205277 f.send(&f.user, &archive_request(&f.user, 9035), websocket)
206278 .await
@@ -218,6 +290,52 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
218290 }
219291}
220292
293+ #[tokio::test]
294+ #[ignore = "requires Postgres"]
295+ async fn archive_snapshot_noop_rechecks_concurrent_state_changes() {
296+ let f = Fixture::new().await;
297+ assert!(
298+ f.send(&f.user, &archive_request(&f.user, 9035), false)
299+ .await
300+ .0
301+ );
302+ let (reached, release) = super::side_effects::publish_test_hooks::arm(f.tenant.community());
303+ let state = f.state.clone();
304+ let tenant = f.tenant.clone();
305+ let publisher = tokio::spawn(async move {
306+ super::side_effects::publish_nipia_archival_list(&tenant, &state).await
307+ });
308+ tokio::time::timeout(Duration::from_secs(10), reached)
309+ .await
310+ .unwrap()
311+ .unwrap();
312+ // Keep the old matching snapshot while changing canonical state. A stale
313+ // no-op decision must not leave that snapshot in place.
314+ f.state
315+ .db
316+ .unarchive(f.tenant.community(), &f.user.public_key().to_hex())
317+ .await
318+ .unwrap();
319+ release.notify_one();
320+ tokio::time::timeout(Duration::from_secs(10), publisher)
321+ .await
322+ .unwrap()
323+ .unwrap()
324+ .unwrap();
325+ let tags: Value = sqlx::query_scalar(
326+ "SELECT tags FROM events WHERE community_id=$1 AND kind=13535 AND deleted_at IS NULL",
327+ )
328+ .bind(f.tenant.community().as_uuid())
329+ .fetch_one(&f.pool)
330+ .await
331+ .unwrap();
332+ assert_eq!(
333+ tags,
334+ serde_json::json!([["-"]]),
335+ "publisher must repair the snapshot after detecting canonical drift"
336+ );
337+ }
338+
221339#[tokio::test]
222340#[ignore = "requires Postgres"]
223341async fn archive_requests_wait_for_fence_commit_or_rollback() {
0 commit comments