@@ -471,6 +471,22 @@ async function settleAfterRefreshDebounce() {
471471 await settle ( ) ;
472472}
473473
474+ /**
475+ * Advance past the cross-snapshot notify window, then settle.
476+ *
477+ * Alerts are queued per snapshot and delivered on a trailing quiet window, so
478+ * a burst spanning several intermediate 13534s produces one notification
479+ * instead of one per snapshot. Anything asserting that a notification WAS
480+ * delivered has to outwait that window; anything asserting an absence should
481+ * outwait it too, or it proves only that delivery is deferred.
482+ */
483+ async function settleAfterNotifyWindow ( ) {
484+ await act ( async ( ) => {
485+ await new Promise ( ( r ) => setTimeout ( r , 1_700 ) ) ;
486+ } ) ;
487+ await settle ( ) ;
488+ }
489+
474490function ledgerKeys ( ) {
475491 return [ ...storage . keys ( ) ] . filter ( ( key ) =>
476492 key . startsWith ( "buzz-community-join-seen.v1" ) ,
@@ -514,7 +530,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
514530 assert . equal ( ledgerKeys ( ) . length , 1 , "the seed must be persisted" ) ;
515531
516532 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-2" } ) ) ;
517- await settle ( ) ;
533+ await settleAfterNotifyWindow ( ) ;
518534
519535 assert . equal ( notifications . length , 1 , "a genuine join must alert once" ) ;
520536 assert . match ( notifications [ 0 ] . title , / C o m m u n i t y A / ) ;
@@ -605,7 +621,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
605621 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE ] ) ) ;
606622 await settle ( ) ;
607623 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-2" } ) ) ;
608- await settle ( ) ;
624+ await settleAfterNotifyWindow ( ) ;
609625 assert . equal ( notifications . length , 1 , "precondition: one join alerted" ) ;
610626
611627 const before = relay . counts ( ) . fetchFirstEventCalls ;
@@ -616,6 +632,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
616632 ) ;
617633 relay . emitReconnect ( ) ;
618634 await settleAfterRefreshDebounce ( ) ;
635+ await settleAfterNotifyWindow ( ) ;
619636
620637 assert . ok (
621638 relay . counts ( ) . fetchFirstEventCalls > before ,
@@ -636,6 +653,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
636653 ) ;
637654 relay . emitReconnect ( ) ;
638655 await settleAfterRefreshDebounce ( ) ;
656+ await settleAfterNotifyWindow ( ) ;
639657
640658 assert . equal (
641659 notifications . length ,
@@ -707,6 +725,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
707725 sig : "s" . repeat ( 128 ) ,
708726 } ) ;
709727 await settleAfterRefreshDebounce ( ) ;
728+ await settleAfterNotifyWindow ( ) ;
710729
711730 assert . equal ( notifications . length , 1 ) ;
712731
@@ -743,7 +762,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
743762 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE ] ) ) ;
744763 await settle ( ) ;
745764 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-2" } ) ) ;
746- await settle ( ) ;
765+ await settleAfterNotifyWindow ( ) ;
747766 assert . equal ( notifications . length , 1 , "precondition: A alerted once" ) ;
748767 assert . deepEqual ( ledgerKeys ( ) , [ joinAlertStorageKey ( COMMUNITY_A , VIEWER ) ] ) ;
749768
@@ -808,7 +827,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
808827 createdAt : 5000 ,
809828 } ) ,
810829 ) ;
811- await settle ( ) ;
830+ await settleAfterNotifyWindow ( ) ;
812831 assert . equal ( notifications . length , 2 ) ;
813832
814833 // Switching back must not re-alert A's roster: its ledger persisted.
@@ -817,7 +836,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
817836 relay . emitSnapshot (
818837 snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-a-return" , createdAt : 6000 } ) ,
819838 ) ;
820- await settle ( ) ;
839+ await settleAfterNotifyWindow ( ) ;
821840 assert . equal (
822841 notifications . length ,
823842 2 ,
@@ -853,7 +872,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
853872 // Origin quota is exhausted and cache eviction cannot free enough.
854873 storageFull = true ;
855874 relay . emitSnapshot ( snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-full" } ) ) ;
856- await settle ( ) ;
875+ await settleAfterNotifyWindow ( ) ;
857876
858877 assert . equal (
859878 notifications . length ,
@@ -867,7 +886,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
867886 relay . emitSnapshot (
868887 snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-recovered" , createdAt : 2000 } ) ,
869888 ) ;
870- await settle ( ) ;
889+ await settleAfterNotifyWindow ( ) ;
871890
872891 assert . equal (
873892 notifications . length ,
@@ -879,7 +898,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
879898 relay . emitSnapshot (
880899 snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-after" , createdAt : 3000 } ) ,
881900 ) ;
882- await settle ( ) ;
901+ await settleAfterNotifyWindow ( ) ;
883902 assert . equal ( notifications . length , 1 ) ;
884903
885904 await unmount ( ) ;
@@ -938,7 +957,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
938957 relay . emitSnapshot (
939958 snapshot ( [ VIEWER , ALICE , BOB ] , { id : "snap-join" , viewerRole : "admin" } ) ,
940959 ) ;
941- await settle ( ) ;
960+ await settleAfterNotifyWindow ( ) ;
942961 assert . equal ( notifications . length , 1 , "precondition: admin still alerts" ) ;
943962
944963 const ledgerBefore = storage . get ( joinAlertStorageKey ( COMMUNITY_A , VIEWER ) ) ;
@@ -951,7 +970,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
951970 viewerRole : "member" ,
952971 } ) ,
953972 ) ;
954- await settle ( ) ;
973+ await settleAfterNotifyWindow ( ) ;
955974
956975 assert . equal (
957976 notifications . length ,
@@ -994,7 +1013,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
9941013 content : "" ,
9951014 sig : "s" . repeat ( 128 ) ,
9961015 } ) ;
997- await settle ( ) ;
1016+ await settleAfterNotifyWindow ( ) ;
9981017
9991018 assert . equal (
10001019 notifications . length ,
@@ -1027,7 +1046,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
10271046 relay . emitSnapshot (
10281047 snapshot ( [ VIEWER , ALICE , ...bulk ] , { id : "snap-bulk" , createdAt : 6000 } ) ,
10291048 ) ;
1030- await settle ( 10 ) ;
1049+ await settleAfterNotifyWindow ( ) ;
10311050
10321051 assert . equal ( notifications . length , 1 , "one summary, not one per member" ) ;
10331052 assert . equal ( notifications [ 0 ] . body , "40 new members joined" ) ;
@@ -1055,7 +1074,7 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
10551074 createdAt : 7000 ,
10561075 } ) ,
10571076 ) ;
1058- await settle ( 10 ) ;
1077+ await settleAfterNotifyWindow ( ) ;
10591078
10601079 assert . equal ( notifications . length , 2 , "two joins, two named alerts" ) ;
10611080 assert . ok (
@@ -1109,4 +1128,173 @@ describe("useCommunityJoinAlerts — mounted subscription behaviour", () => {
11091128
11101129 await unmount ( ) ;
11111130 } ) ;
1131+
1132+ /**
1133+ * Max's live 50-join storm at `fdeda44f0`: 10 banners, not 1.
1134+ *
1135+ * The per-snapshot cap answers "one snapshot, many keys". The relay answers
1136+ * back "one burst, many snapshots" — it republishes the whole 13534 as each
1137+ * concurrent add commits, so a storm arrives as several growing rosters and
1138+ * each one independently emitted its own capped batch. The batch sizes below
1139+ * are Max's observed live values (6, 17, 4, 4, 4, 4, 11 = 50).
1140+ */
1141+ it ( "collapses a burst spanning several snapshots into one alert" , async ( ) => {
1142+ const relay = installRelayStub ( ) ;
1143+ const { render, unmount } = mountHook ( ) ;
1144+
1145+ await render ( ) ;
1146+ await settle ( ) ;
1147+
1148+ relay . emitSnapshot ( snapshot ( [ VIEWER ] ) ) ;
1149+ await settleAfterNotifyWindow ( ) ;
1150+ assert . equal ( notifications . length , 0 , "precondition: seeded silently" ) ;
1151+
1152+ const roster = [ VIEWER ] ;
1153+ let minted = 0 ;
1154+ let snapIndex = 0 ;
1155+ for ( const size of [ 6 , 17 , 4 , 4 , 4 , 4 , 11 ] ) {
1156+ for ( let i = 0 ; i < size ; i ++ ) {
1157+ minted += 1 ;
1158+ roster . push ( minted . toString ( 16 ) . padStart ( 2 , "0" ) . repeat ( 32 ) ) ;
1159+ }
1160+ snapIndex += 1 ;
1161+ relay . emitSnapshot (
1162+ snapshot ( [ ...roster ] , {
1163+ id : `storm-${ snapIndex } ` ,
1164+ createdAt : 9000 + snapIndex ,
1165+ } ) ,
1166+ ) ;
1167+ await settle ( ) ;
1168+ }
1169+ await settleAfterNotifyWindow ( ) ;
1170+
1171+ assert . equal ( minted , 50 , "fixture must mint Max's 50 joins" ) ;
1172+ assert . equal (
1173+ notifications . length ,
1174+ 1 ,
1175+ "a burst spanning 7 snapshots must produce one alert, not one per snapshot" ,
1176+ ) ;
1177+ assert . equal ( notifications [ 0 ] . body , "50 new members joined" ) ;
1178+
1179+ await unmount ( ) ;
1180+ } ) ;
1181+
1182+ /**
1183+ * Wren's arm 3, and the reason batching is not free: deferring delivery
1184+ * reopens his disclosure as a DELAYED one unless revocation also drops what
1185+ * is already queued. Measured failing before the clearPending() call existed
1186+ * — the queued batch flushed "5 new members joined" after the demotion.
1187+ */
1188+ it ( "drops queued alerts when a later snapshot demotes the viewer" , async ( ) => {
1189+ const relay = installRelayStub ( ) ;
1190+ const { render, unmount } = mountHook ( { role : "admin" } ) ;
1191+
1192+ await render ( ) ;
1193+ await settle ( ) ;
1194+
1195+ relay . emitSnapshot ( snapshot ( [ VIEWER ] , { viewerRole : "admin" } ) ) ;
1196+ await settle ( ) ;
1197+
1198+ // Joins land and are queued, but the flush window has not elapsed.
1199+ const roster = [ VIEWER , ALICE , BOB , CAROL ] ;
1200+ relay . emitSnapshot (
1201+ snapshot ( [ ...roster ] , {
1202+ id : "queued-joins" ,
1203+ createdAt : 10_000 ,
1204+ viewerRole : "admin" ,
1205+ } ) ,
1206+ ) ;
1207+ await settle ( ) ;
1208+ assert . equal (
1209+ notifications . length ,
1210+ 0 ,
1211+ "precondition: delivery is still pending on the window" ,
1212+ ) ;
1213+
1214+ // Demotion arrives before the timer fires.
1215+ relay . emitSnapshot (
1216+ snapshot ( [ ...roster ] , {
1217+ id : "queued-demote" ,
1218+ createdAt : 10_001 ,
1219+ viewerRole : "member" ,
1220+ } ) ,
1221+ ) ;
1222+ await settleAfterNotifyWindow ( ) ;
1223+
1224+ assert . equal (
1225+ notifications . length ,
1226+ 0 ,
1227+ "a demotion before the flush must cancel the queued batch, not delay it" ,
1228+ ) ;
1229+
1230+ await unmount ( ) ;
1231+ } ) ;
1232+
1233+ /**
1234+ * Wren's arm 5. The window must batch a burst without swallowing legitimate
1235+ * later joins — otherwise the fix trades 10 spurious alerts for a silently
1236+ * dropped one.
1237+ */
1238+ it ( "still alerts separately for joins beyond the batching window" , async ( ) => {
1239+ const relay = installRelayStub ( ) ;
1240+ const { render, unmount } = mountHook ( ) ;
1241+
1242+ await render ( ) ;
1243+ await settle ( ) ;
1244+
1245+ relay . emitSnapshot ( snapshot ( [ VIEWER ] ) ) ;
1246+ await settleAfterNotifyWindow ( ) ;
1247+
1248+ relay . emitSnapshot (
1249+ snapshot ( [ VIEWER , ALICE ] , { id : "join-1" , createdAt : 11_000 } ) ,
1250+ ) ;
1251+ await settleAfterNotifyWindow ( ) ;
1252+ assert . equal ( notifications . length , 1 , "first join alerts on its own" ) ;
1253+
1254+ relay . emitSnapshot (
1255+ snapshot ( [ VIEWER , ALICE , BOB ] , { id : "join-2" , createdAt : 12_000 } ) ,
1256+ ) ;
1257+ await settleAfterNotifyWindow ( ) ;
1258+
1259+ assert . equal (
1260+ notifications . length ,
1261+ 2 ,
1262+ "a join after the window closed must get its own alert, not be suppressed" ,
1263+ ) ;
1264+
1265+ await unmount ( ) ;
1266+ } ) ;
1267+
1268+ /**
1269+ * Teardown must drop the queued batch, not just its timer. On a community
1270+ * switch the effect re-keys, and keys accumulated for the old community must
1271+ * never flush against the new one.
1272+ */
1273+ it ( "does not deliver a queued batch after unmount" , async ( ) => {
1274+ const relay = installRelayStub ( ) ;
1275+ const { render, unmount } = mountHook ( ) ;
1276+
1277+ await render ( ) ;
1278+ await settle ( ) ;
1279+
1280+ relay . emitSnapshot ( snapshot ( [ VIEWER ] ) ) ;
1281+ await settle ( ) ;
1282+ relay . emitSnapshot (
1283+ snapshot ( [ VIEWER , ALICE , BOB , CAROL ] , {
1284+ id : "queued-at-teardown" ,
1285+ createdAt : 13_000 ,
1286+ } ) ,
1287+ ) ;
1288+ await settle ( ) ;
1289+ assert . equal ( notifications . length , 0 , "precondition: still queued" ) ;
1290+
1291+ await unmount ( ) ;
1292+ await settleAfterNotifyWindow ( ) ;
1293+
1294+ assert . equal (
1295+ notifications . length ,
1296+ 0 ,
1297+ "a torn-down mount must not fire its pending batch" ,
1298+ ) ;
1299+ } ) ;
11121300} ) ;
0 commit comments