99//! It tests the actual data flow from simulation → processing → shared state → API endpoints.
1010
1111use anyhow:: Result ;
12+ use rand:: rand_core:: le;
1213use rust_photoacoustic:: {
1314 config:: Config ,
1415 daemon:: launch_daemon:: Daemon ,
@@ -136,7 +137,7 @@ async fn test_real_world_peak_detection_endpoints() -> Result<()> {
136137 serde_json:: to_string_pretty( & computing_data) ?
137138 ) ;
138139
139- // Verify the structure exists
140+ // Verify the structure exists (legacy fields for compatibility)
140141 assert ! (
141142 computing_data. get( "peak_frequency" ) . is_some( ) ,
142143 "Should have peak_frequency field"
@@ -150,18 +151,109 @@ async fn test_real_world_peak_detection_endpoints() -> Result<()> {
150151 "Should have concentration_ppm field"
151152 ) ;
152153
153- // Check if we have real data (non-null values)
154- let peak_frequency = computing_data[ "peak_frequency" ] . as_f64 ( ) ;
155- let peak_amplitude = computing_data[ "peak_amplitude" ] . as_f64 ( ) ;
154+ // Verify new structure exists
155+ assert ! (
156+ computing_data. get( "active_node_ids" ) . is_some( ) ,
157+ "Should have active_node_ids field"
158+ ) ;
159+ assert ! (
160+ computing_data. get( "peak_results" ) . is_some( ) ,
161+ "Should have peak_results field"
162+ ) ;
163+
164+ // Get active node IDs
165+ let active_node_ids = computing_data[ "active_node_ids" ]
166+ . as_array ( )
167+ . expect ( "active_node_ids should be an array" ) ;
168+
169+ println ! ( "Active node IDs: {:?}" , active_node_ids) ;
170+
171+ // Check if we have peak_detector in active nodes
172+ let peak_detector_id = active_node_ids
173+ . iter ( )
174+ . find ( |id| id. as_str ( ) == Some ( "peak_detector" ) )
175+ . expect ( "Should have peak_detector in active_node_ids" ) ;
176+
177+ println ! ( "✓ Found active peak detector: {}" , peak_detector_id) ;
156178
157- println ! ( "Peak frequency: {:?}" , peak_frequency) ;
158- println ! ( "Peak amplitude: {:?}" , peak_amplitude) ;
179+ // Get peak results for the active peak detector
180+ let peak_results = computing_data[ "peak_results" ]
181+ . as_object ( )
182+ . expect ( "peak_results should be an object" ) ;
183+
184+ let peak_detector_result = peak_results
185+ . get ( "peak_detector" )
186+ . expect ( "Should have peak_detector in peak_results" ) ;
187+
188+ println ! (
189+ "Peak detector result: {}" ,
190+ serde_json:: to_string_pretty( peak_detector_result) ?
191+ ) ;
192+
193+ // Extract data from the peak detector result
194+ let peak_frequency = peak_detector_result[ "frequency" ] . as_f64 ( ) ;
195+ let peak_amplitude = peak_detector_result[ "amplitude" ] . as_f64 ( ) ;
196+ let peak_concentration = peak_detector_result[ "concentration_ppm" ] . as_f64 ( ) ;
197+
198+ println ! ( "Peak frequency from peak_detector: {:?}" , peak_frequency) ;
199+ println ! ( "Peak amplitude from peak_detector: {:?}" , peak_amplitude) ;
200+ println ! (
201+ "Peak concentration from peak_detector: {:?} ppm" ,
202+ peak_concentration
203+ ) ;
204+ assert ! (
205+ peak_frequency. is_some( ) ,
206+ "Peak frequency should not be null"
207+ ) ;
208+ // peak frequency should be between 1900 and 2200 Hz
209+ assert ! (
210+ peak_frequency. unwrap( ) >= 1900.0 && peak_frequency. unwrap( ) <= 2200.0 ,
211+ "Peak frequency should be in the range [1900, 2200] Hz"
212+ ) ;
213+ assert ! (
214+ peak_amplitude. is_some( ) ,
215+ "Peak amplitude should not be null"
216+ ) ;
217+ // peak amplitude should be 45 and 55
218+ assert ! (
219+ peak_amplitude. unwrap( ) >= 45.0 && peak_amplitude. unwrap( ) <= 55.0 ,
220+ "Peak amplitude should be in the range [45, 55]"
221+ ) ;
222+ // peak concentration should be between 0 and 100 ppm
223+ assert ! (
224+ peak_concentration. is_some( ) ,
225+ "Peak concentration should not be null"
226+ ) ;
227+ assert ! (
228+ peak_concentration. unwrap( ) >= 0.0 && peak_concentration. unwrap( ) <= 100.0 ,
229+ "Peak concentration should be in the range [0, 100] ppm"
230+ ) ;
231+
232+ // Check concentration calculation result
233+ let concentration_ppm = computing_data[ "concentration_ppm" ] . as_f64 ( ) ;
234+ println ! ( "Calculated concentration: {:?} ppm" , concentration_ppm) ;
235+
236+ // Check polynomial coefficients
237+ let polynomial_coefficients = computing_data[ "polynomial_coefficients" ]
238+ . as_array ( )
239+ . expect ( "Should have polynomial_coefficients array" ) ;
240+ println ! ( "Polynomial coefficients: {:?}" , polynomial_coefficients) ;
159241
160242 // The main test: verify we get real data, not null values
161243 if peak_frequency. is_some ( ) && peak_amplitude. is_some ( ) {
162244 println ! ( "✓ SUCCESS: Peak detection is working - got real values!" ) ;
163245 println ! ( " Peak frequency: {} Hz" , peak_frequency. unwrap( ) ) ;
164246 println ! ( " Peak amplitude: {}" , peak_amplitude. unwrap( ) ) ;
247+
248+ if concentration_ppm. is_some ( ) {
249+ println ! (
250+ " Calculated concentration: {} ppm" ,
251+ concentration_ppm. unwrap( )
252+ ) ;
253+ println ! ( "✓ SUCCESS: Concentration calculation is also working!" ) ;
254+ } else {
255+ println ! ( "⚠ WARNING: Concentration calculation returning null" ) ;
256+ }
165257 } else {
166258 println ! ( "⚠ WARNING: Peak detection returning null values" ) ;
167259 println ! (
@@ -184,8 +276,16 @@ async fn test_real_world_peak_detection_endpoints() -> Result<()> {
184276 serde_json:: to_string_pretty( & retry_data) ?
185277 ) ;
186278
187- let retry_peak_frequency = retry_data[ "peak_frequency" ] . as_f64 ( ) ;
188- let retry_peak_amplitude = retry_data[ "peak_amplitude" ] . as_f64 ( ) ;
279+ let retry_peak_results = retry_data[ "peak_results" ]
280+ . as_object ( )
281+ . expect ( "peak_results should be an object" ) ;
282+
283+ let retry_peak_detector_result = retry_peak_results
284+ . get ( "peak_detector" )
285+ . expect ( "Should have peak_detector in peak_results" ) ;
286+
287+ let retry_peak_frequency = retry_peak_detector_result[ "frequency" ] . as_f64 ( ) ;
288+ let retry_peak_amplitude = retry_peak_detector_result[ "amplitude" ] . as_f64 ( ) ;
189289
190290 if retry_peak_frequency. is_some ( ) && retry_peak_amplitude. is_some ( ) {
191291 println ! ( "✓ SUCCESS on retry: Peak detection working!" ) ;
0 commit comments