@@ -4,18 +4,19 @@ pub mod backend;
44pub mod common;
55pub mod constants;
66
7- use crate :: constants:: { GROUP_NOT_FOUND , UNABLE_TO_SET_GROUP_NAME , UNABLE_TO_GET_GROUP_NAME , TEST_GROUP_NAME , UNABLE_TO_STORE_KEYPAIR , FAILED_TO_LOAD_KEYPAIR , KEYPAIR_NOT_FOUND , FAILED_TO_DESERIALIZE_KEYPAIR } ;
7+ use crate :: constants:: { GROUP_NOT_FOUND , UNABLE_TO_SET_GROUP_NAME , UNABLE_TO_GET_GROUP_NAME , TEST_GROUP_NAME , UNABLE_TO_STORE_KEYPAIR , FAILED_TO_LOAD_KEYPAIR , KEYPAIR_NOT_FOUND , FAILED_TO_DESERIALIZE_KEYPAIR , ROUTE_ID_DHT_KEY } ;
88
99use crate :: backend:: Backend ;
1010use crate :: common:: { CommonKeypair , DHTEntity } ;
1111use veilid_core:: {
12- vld0_generate_keypair, TypedKey , CRYPTO_KIND_VLD0
12+ vld0_generate_keypair, TypedKey , CRYPTO_KIND_VLD0 , VeilidUpdate , VALID_CRYPTO_KINDS
1313} ;
1414
1515#[ cfg( test) ]
1616mod tests {
1717 use super :: * ;
1818 use tokio:: fs;
19+ use tokio:: sync:: mpsc;
1920 use tmpdir:: TmpDir ;
2021
2122 #[ tokio:: test]
@@ -37,7 +38,7 @@ mod tests {
3738 backend. stop ( ) . await . expect ( "Unable to stop" ) ;
3839
3940 backend. start ( ) . await . expect ( "Unable to restart" ) ;
40- let loaded_group = backend. get_group ( TypedKey :: new ( CRYPTO_KIND_VLD0 , group. id ( ) ) ) . await . expect ( GROUP_NOT_FOUND ) ;
41+ let mut loaded_group = backend. get_group ( TypedKey :: new ( CRYPTO_KIND_VLD0 , group. id ( ) ) ) . await . expect ( GROUP_NOT_FOUND ) ;
4142
4243 let protected_store = backend. get_protected_store ( ) . unwrap ( ) ;
4344 let keypair_data = protected_store. load_user_secret ( group. id ( ) . to_string ( ) )
@@ -48,16 +49,14 @@ mod tests {
4849
4950 // Check that the id matches group.id()
5051 assert_eq ! ( retrieved_keypair. id, group. id( ) ) ;
51-
52+
5253 // Check that the public_key matches the owner public key from the DHT record
5354 assert_eq ! ( retrieved_keypair. public_key, loaded_group. get_dht_record( ) . owner( ) . clone( ) ) ;
5455
5556 // Check that the secret and encryption keys match
5657 assert_eq ! ( retrieved_keypair. secret_key, group. get_secret_key( ) ) ;
5758 assert_eq ! ( retrieved_keypair. encryption_key, group. get_encryption_key( ) ) ;
5859
59- let mut loaded_group = backend. get_group ( TypedKey :: new ( CRYPTO_KIND_VLD0 , group. id ( ) ) ) . await . expect ( GROUP_NOT_FOUND ) ;
60-
6160 // Check if we can get group name
6261 let group_name = loaded_group. get_name ( ) . await . expect ( UNABLE_TO_GET_GROUP_NAME ) ;
6362 assert_eq ! ( group_name, TEST_GROUP_NAME ) ;
@@ -76,7 +75,7 @@ mod tests {
7675 assert_eq ! ( name, repo_name) ;
7776
7877 // Add repo to group
79- loaded_group. add_repo ( repo) . await . expect ( "Unable to add repo to group" ) ;
78+ loaded_group. add_repo ( repo. clone ( ) ) . await . expect ( "Unable to add repo to group" ) ;
8079
8180 // List known repos
8281 let repos = loaded_group. list_repos ( ) . await ;
@@ -89,6 +88,57 @@ mod tests {
8988 let retrieved_name = loaded_repo. get_name ( ) . await . expect ( "Unable to get repo name after restart" ) ;
9089 assert_eq ! ( retrieved_name, repo_name) ;
9190
91+ // Get the update receiver from the backend
92+ let update_rx = backend. subscribe_updates ( ) . expect ( "Failed to subscribe to updates" ) ;
93+
94+ // Set up a channel to receive AppMessage updates
95+ let ( message_tx, mut message_rx) = mpsc:: channel ( 1 ) ;
96+
97+ // Spawn a task to listen for updates
98+ tokio:: spawn ( async move {
99+ let mut rx = update_rx. resubscribe ( ) ;
100+ while let Ok ( update) = rx. recv ( ) . await {
101+ if let VeilidUpdate :: AppMessage ( app_message) = update {
102+ // Optionally, filter by route_id or other criteria
103+ message_tx. send ( app_message) . await . unwrap ( ) ;
104+ }
105+ }
106+ } ) ;
107+
108+ // Get VeilidAPI instance from backend
109+ let veilid_api = backend. get_veilid_api ( ) . expect ( "Failed to get VeilidAPI instance" ) ;
110+
111+ // Create a new private route
112+ let ( route_id, route_id_blob) = veilid_api
113+ . new_custom_private_route (
114+ & VALID_CRYPTO_KINDS ,
115+ veilid_core:: Stability :: Reliable ,
116+ veilid_core:: Sequencing :: PreferOrdered ,
117+ )
118+ . await
119+ . expect ( "Failed to create route" ) ;
120+
121+ // Store the route_id_blob in DHT
122+ loaded_repo
123+ . store_route_id_in_dht ( route_id_blob. clone ( ) )
124+ . await
125+ . expect ( "Failed to store route ID blob in DHT" ) ;
126+
127+ // Define the message to send
128+ let message = b"Test Message to Repo Owner" . to_vec ( ) ;
129+
130+ // Send the message
131+ loaded_repo
132+ . send_message_to_owner ( veilid_api, message. clone ( ) , ROUTE_ID_DHT_KEY )
133+ . await
134+ . expect ( "Failed to send message to repo owner" ) ;
135+
136+ // Receive the message
137+ let received_app_message = message_rx. recv ( ) . await . expect ( "Failed to receive message" ) ;
138+
139+ // Verify the message
140+ assert_eq ! ( received_app_message. message( ) , message. as_slice( ) ) ;
141+
92142 backend. stop ( ) . await . expect ( "Unable to stop" ) ;
93143 }
94144
0 commit comments