@@ -40,6 +40,7 @@ async fn created_starter_channels_are_reported_when_metadata_is_unavailable() {
4040}
4141
4242const LIVE_GENERAL_ID : & str = "0b9f4c1e-6a51-4f7e-9d38-2f4b7f0e3c11" ;
43+ const LIVE_WELCOME_ID : & str = "5d2a7e90-3c4b-4f18-8e6d-91a0b2c4d5e6" ;
4344
4445/// A kind:39000 open stream, relay-signed like production.
4546fn channel_event ( id : & str , name : & str ) -> serde_json:: Value {
@@ -63,10 +64,13 @@ fn channel_event(id: &str, name: &str) -> serde_json::Value {
6364/// `renamed_starter`, a live channel under that name holds `welcome-everyone`'s
6465/// deterministic id and is served when metadata is read by id. Returns the
6566/// result, the kinds of the events the relay accepted, and whether that id is
66- /// still marked as pending-owned.
67+ /// still marked as pending-owned. With `late_welcome`, a live
68+ /// `welcome-everyone` under another id joins the directory after the first
69+ /// read.
6770async fn ensure_with_unusable_welcome (
6871 create_refusal : ( axum:: http:: StatusCode , & ' static str ) ,
6972 renamed_starter : Option < & ' static str > ,
73+ late_welcome : bool ,
7074) -> ( Result < Vec < ChannelInfo > , String > , Vec < u64 > , bool ) {
7175 use std:: sync:: { Arc , Mutex } ;
7276
@@ -83,6 +87,8 @@ async fn ensure_with_unusable_welcome(
8387 . to_string ( ) ;
8488
8589 let general = channel_event ( LIVE_GENERAL_ID , "general" ) ;
90+ let welcome = channel_event ( LIVE_WELCOME_ID , "welcome-everyone" ) ;
91+ let directory_reads = Arc :: new ( Mutex :: new ( 0 ) ) ;
8692 let renamed = renamed_starter. map ( |name| channel_event ( & starter_id, name) ) ;
8793 let accepted = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
8894 let accepted_kinds = accepted. clone ( ) ;
@@ -93,7 +99,15 @@ async fn ensure_with_unusable_welcome(
9399 let filter = filters[ 0 ] . clone ( ) ;
94100 let events = match ( filter[ "kinds" ] [ 0 ] . as_u64 ( ) , filter. get ( "#d" ) ) {
95101 // The open directory lists live channels only.
96- ( Some ( 39000 ) , None ) => vec ! [ general. clone( ) ] ,
102+ ( Some ( 39000 ) , None ) => {
103+ let mut reads = directory_reads. lock ( ) . unwrap ( ) ;
104+ * reads += 1 ;
105+ if late_welcome && * reads > 1 {
106+ vec ! [ general. clone( ) , welcome. clone( ) ]
107+ } else {
108+ vec ! [ general. clone( ) ]
109+ }
110+ }
97111 // Metadata by id.
98112 ( Some ( 39000 ) , Some ( ids) ) => renamed
99113 . iter ( )
@@ -156,6 +170,7 @@ async fn a_starter_name_held_by_another_channel_is_skipped() {
156170 "invalid: an open channel named \" welcome-everyone\" already exists" ,
157171 ) ,
158172 None ,
173+ false ,
159174 )
160175 . await ;
161176
@@ -175,6 +190,7 @@ async fn a_starter_id_held_by_an_archived_channel_is_skipped() {
175190 "invalid: channel is archived" ,
176191 ) ,
177192 None ,
193+ false ,
178194 )
179195 . await ;
180196
@@ -189,6 +205,7 @@ async fn a_starter_id_held_by_a_renamed_channel_is_skipped() {
189205 let ( result, accepted, still_pending) = ensure_with_unusable_welcome (
190206 ( status_ok ( ) , "duplicate: channel already exists" ) ,
191207 Some ( "intros" ) ,
208+ false ,
192209 )
193210 . await ;
194211
@@ -206,9 +223,12 @@ async fn a_starter_id_held_by_an_unseen_channel_is_skipped() {
206223 let _serial = crate :: relay_admission:: TEST_SERIAL . lock ( ) . await ;
207224 // A deleted channel keeps its id, so the relay answers `duplicate`, but
208225 // never serves its metadata.
209- let ( result, accepted, still_pending) =
210- ensure_with_unusable_welcome ( ( status_ok ( ) , "duplicate: channel already exists" ) , None )
211- . await ;
226+ let ( result, accepted, still_pending) = ensure_with_unusable_welcome (
227+ ( status_ok ( ) , "duplicate: channel already exists" ) ,
228+ None ,
229+ false ,
230+ )
231+ . await ;
212232
213233 let channels = result. expect ( "a deleted starter must not fail setup" ) ;
214234 assert ! ( joined_live_general( & channels) ) ;
@@ -228,6 +248,7 @@ async fn other_create_refusals_still_fail_setup() {
228248 "invalid: channel name too long" ,
229249 ) ,
230250 None ,
251+ false ,
231252 )
232253 . await ;
233254
@@ -237,3 +258,31 @@ async fn other_create_refusals_still_fail_setup() {
237258 ) ;
238259 assert ! ( accepted. is_empty( ) ) ;
239260}
261+
262+ #[ tokio:: test]
263+ async fn a_starter_name_held_by_a_live_starter_the_first_read_missed_is_joined ( ) {
264+ let _serial = crate :: relay_admission:: TEST_SERIAL . lock ( ) . await ;
265+ let ( result, accepted, _) = ensure_with_unusable_welcome (
266+ (
267+ axum:: http:: StatusCode :: BAD_REQUEST ,
268+ "invalid: an open channel named \" welcome-everyone\" already exists" ,
269+ ) ,
270+ None ,
271+ true ,
272+ )
273+ . await ;
274+
275+ let channels = result. expect ( "a live starter must not fail setup" ) ;
276+ assert ! ( joined_live_general( & channels) ) ;
277+ assert ! (
278+ channels
279+ . iter( )
280+ . any( |channel| channel. id == LIVE_WELCOME_ID && channel. is_member) ,
281+ "the live welcome-everyone must be joined"
282+ ) ;
283+ assert_eq ! (
284+ accepted,
285+ vec![ 9021 , 9021 ] ,
286+ "joins to general and welcome-everyone"
287+ ) ;
288+ }
0 commit comments