KiThe 0.3.6

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#[cfg(test)]
mod tests {
    use crate::Thermodynamics::DBhandlers::NIST_parser::{
        NistError, NistInput, NistParser, Phase, SearchType,
    };
    use reqwest::blocking::Client;
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    };
    use std::thread;
    use std::time::Duration;
    // use std::collections::HashMap;
    /*
       // Mock HTTP client for testing
       #[derive(Default)]
       struct MockHttpClient {
           responses: HashMap<String, Result<String, reqwest::Error>>,
       }

       impl MockHttpClient {
           fn new() -> Self {
               Self {
                   responses: HashMap::new(),
               }
           }

           fn mock_response(&mut self, url: &str, response: Result<String, reqwest::Error>) {
               self.responses.insert(url.to_string(), response);
           }
       }

       impl HttpClient for MockHttpClient {
           fn get_text(&self, url: &str) -> Result<String, reqwest::Error> {
               let resp = self.responses.get(url).unwrap().clone()?;
               match resp {
                   Ok(text) => Ok(text.clone()),
                   Err(err) => Err(err),
               }
           }
       }
    */

    fn build_timeout_client(timeout_ms: u64) -> Client {
        Client::builder()
            .connect_timeout(Duration::from_millis(timeout_ms))
            .timeout(Duration::from_millis(timeout_ms))
            .build()
            .expect("test client should be constructible")
    }

    fn http_response(status: &str, body: &str) -> String {
        format!(
            "HTTP/1.1 {}\r\nContent-Length: {}\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n{}",
            status,
            body.len(),
            body
        )
    }

    fn spawn_response_server(response: String) -> (String, Arc<AtomicUsize>) {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
        let addr = listener.local_addr().expect("local addr");
        let hits = Arc::new(AtomicUsize::new(0));
        let hits_for_thread = Arc::clone(&hits);

        thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                hits_for_thread.fetch_add(1, Ordering::SeqCst);
                let mut buf = [0_u8; 1024];
                let _ = stream.read(&mut buf);
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.flush();
            }
        });

        (format!("http://{}", addr), hits)
    }

    fn spawn_hanging_server() -> (String, Arc<AtomicUsize>) {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
        let addr = listener.local_addr().expect("local addr");
        let hits = Arc::new(AtomicUsize::new(0));
        let hits_for_thread = Arc::clone(&hits);

        thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                hits_for_thread.fetch_add(1, Ordering::SeqCst);
                let mut buf = [0_u8; 1024];
                let _ = stream.read(&mut buf);
                thread::sleep(Duration::from_secs(2));
            }
        });

        (format!("http://{}", addr), hits)
    }

    fn spawn_threshold_server(
        success_response: String,
        success_requests: usize,
    ) -> (String, Arc<AtomicUsize>) {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
        let addr = listener.local_addr().expect("local addr");
        let hits = Arc::new(AtomicUsize::new(0));
        let hits_for_thread = Arc::clone(&hits);
        let success_response = Arc::new(success_response);
        let success_response_for_thread = Arc::clone(&success_response);

        thread::spawn(move || {
            for _ in 0..32 {
                if let Ok((mut stream, _)) = listener.accept() {
                    let current = hits_for_thread.fetch_add(1, Ordering::SeqCst) + 1;
                    let mut buf = [0_u8; 1024];
                    let _ = stream.read(&mut buf);
                    let response = if current <= success_requests {
                        success_response_for_thread.as_str().to_string()
                    } else {
                        http_response("404 Not Found", "missing")
                    };
                    let _ = stream.write_all(response.as_bytes());
                    let _ = stream.flush();
                } else {
                    break;
                }
            }
        });

        (format!("http://{}", addr), hits)
    }

    #[test]
    fn test_url_construction() {
        let parser = NistParser::new();

        // Test CAS number
        let cas_url = parser.construct_url("7732-18-5").unwrap();
        assert_eq!(
            cas_url.as_str(),
            "https://webbook.nist.gov/cgi/cbook.cgi?ID=7732-18-5&Units=SI"
        );

        // Test chemical formula
        let formula_url = parser.construct_url("H2O").unwrap();
        assert_eq!(
            formula_url.as_str(),
            "https://webbook.nist.gov/cgi/cbook.cgi?Formula=H2O&NoIon=on&Units=SI"
        );

        // Test chemical name
        let name_url = parser.construct_url("Water").unwrap();
        assert_eq!(
            name_url.as_str(),
            "https://webbook.nist.gov/cgi/cbook.cgi?Name=Water&Units=SI"
        );

        // Test space handling
        let space_url = parser.construct_url("Carbon dioxide").unwrap();
        assert_eq!(
            space_url.as_str(),
            "https://webbook.nist.gov/cgi/cbook.cgi?Name=Carbondioxide&Units=SI"
        );
    }

    #[test]
    fn test_phase_conversion() {
        assert_eq!(Phase::Gas.as_str(), "gas");
        assert_eq!(Phase::Solid.as_str(), "solid");
    }

    #[test]
    fn test_fetch_page_rejects_http_error_status() {
        let (base_url, hits) = spawn_response_server(http_response("404 Not Found", "missing"));
        let client = build_timeout_client(250);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let url = url::Url::parse(&format!("{}/anything", base_url)).unwrap();

        let result = parser.fetch_page(&url);

        assert_eq!(hits.load(Ordering::SeqCst), 1);
        assert!(matches!(result, Err(NistError::HttpStatus(status)) if status.as_u16() == 404));
    }

    #[test]
    fn test_fetch_page_rejects_plain_text_body() {
        let (base_url, _) = spawn_response_server(http_response("200 OK", "plain text only"));
        let client = build_timeout_client(250);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let url = url::Url::parse(&format!("{}/anything", base_url)).unwrap();

        let result = parser.fetch_page(&url);

        assert!(matches!(result, Err(NistError::InvalidDataFormat)));
    }

    #[test]
    fn test_fetch_page_rejects_oversized_body() {
        let body = format!(
            "<html><body>{}</body></html>",
            "x".repeat(2 * 1024 * 1024 + 64)
        );
        let (base_url, _) = spawn_response_server(http_response("200 OK", &body));
        let client = build_timeout_client(250);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let url = url::Url::parse(&format!("{}/anything", base_url)).unwrap();

        let result = parser.fetch_page(&url);

        assert!(matches!(result, Err(NistError::ResponseTooLarge { .. })));
    }

    #[test]
    fn test_fetch_page_times_out_on_hanging_server() {
        let (base_url, hits) = spawn_hanging_server();
        let client = build_timeout_client(100);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let url = url::Url::parse(&format!("{}/anything", base_url)).unwrap();

        let result = parser.fetch_page(&url);

        assert_eq!(hits.load(Ordering::SeqCst), 1);
        assert!(matches!(result, Err(NistError::NetworkError(err)) if err.is_timeout()));
    }

    #[test]
    fn test_get_data_batch_reports_partial_success() {
        let success_body = http_response(
            "200 OK",
            r#"
            <html>
              <body>
                <h1>Example substance</h1>
                <ol>
                  <li><a href="/cgi/cbook.cgi?ID=example-entry">Example result</a></li>
                </ol>
                <a href="/gas">Gas phase thermochemistry data</a>
                <li>Molecular weight: 16.043</li>
              </body>
            </html>
            "#,
        );
        let (base_url, hits) = spawn_threshold_server(success_body, 4);
        let client = build_timeout_client(250);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let requests = vec![
            ("CH4".to_string(), SearchType::MolarMass, Phase::Gas),
            ("H2O".to_string(), SearchType::MolarMass, Phase::Gas),
        ];

        let report = parser
            .get_data_batch(&requests)
            .expect("batch should partially succeed");

        assert!(hits.load(Ordering::SeqCst) >= 5);
        assert_eq!(report.entries.len(), 2);
        assert_eq!(report.successes(), 1);
        assert_eq!(report.failures(), 1);
        assert!(report.entries[0].result.is_ok());
        assert!(report.entries[1].result.is_err());
        let _ = base_url;
    }

    #[test]
    fn test_get_data_batch_fails_when_every_request_fails() {
        let bad_body = http_response("404 Not Found", "missing");
        let (base_url, hits) = spawn_threshold_server(bad_body, 0);
        let client = build_timeout_client(250);
        let parser =
            NistParser::with_client_and_base_url(client, url::Url::parse(&base_url).unwrap());
        let requests = vec![
            ("CH4".to_string(), SearchType::MolarMass, Phase::Gas),
            ("H2O".to_string(), SearchType::MolarMass, Phase::Gas),
        ];

        let result = parser.get_data_batch(&requests);

        assert!(hits.load(Ordering::SeqCst) >= 1);
        assert!(matches!(
            result,
            Err(NistError::BatchAllFailed { attempts, .. }) if attempts == requests.len()
        ));
        let _ = base_url;
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_real_substance_fetch() {
        let parser = NistParser::new();

        // Test methane data fetch
        let result = parser.get_data("CH4", SearchType::MolarMass, Phase::Gas);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.molar_mass.is_some());
            assert!(data.cp.is_none()); // We didn't request Cp
            assert!(data.dh.is_none()); // We didn't request dH
        }

        // Test water data fetch
        let result = parser.get_data("H2O", SearchType::Cp, Phase::Gas);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.cp.is_some());
            assert!(data.molar_mass.is_none()); // We didn't request molar mass
        }
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_nonexistent_substance() {
        let parser = NistParser::new();
        let result = parser.get_data("NonexistentSubstance123", SearchType::MolarMass, Phase::Gas);
        assert!(matches!(result, Err(NistError::SubstanceNotFound)));
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_thermodynamic_data() {
        let parser = NistParser::new();

        // Test enthalpy data for water
        let result = parser.get_data("H2O", SearchType::DeltaH, Phase::Gas);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.dh.is_some() || data.ds.is_some());
        }
    }
    #[test]
    #[ignore = "live NIST integration test"]
    fn test_simple_substance() {
        let parser = NistParser::new();

        // Test enthalpy data for water
        let result = parser.get_data("O2", SearchType::All, Phase::Gas);
        let dh = result.as_ref().unwrap().clone().dh.unwrap();
        println!("dh: {}", dh);
        assert_eq!(dh, 0.0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_data_structure_initialization() {
        let data = NistInput {
            cp: Some(vec![vec![1.0, 2.0, 3.0]]),
            T: Some(vec![vec![298.0, 1000.0]]),
            dh: Some(42.0),
            ds: Some(100.0),
            molar_mass: Some(18.015),
        };

        assert_eq!(data.cp.as_ref().unwrap()[0][0], 1.0);
        assert_eq!(data.dh.unwrap(), 42.0);
        assert_eq!(data.ds.unwrap(), 100.0);
        assert_eq!(data.molar_mass.unwrap(), 18.015);
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_ch4_gas() {
        let parser = NistParser::new();
        let substance = "CH4";
        let result = parser.get_data(substance, SearchType::All, Phase::Gas);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.cp.is_some());
            assert!(data.dh.is_some());
            assert!(data.ds.is_some());
            assert!(data.molar_mass.is_some());
            println!("CH4 Gas data parsed successfully");
        }
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_nacl_solid() {
        let parser = NistParser::new();
        let substance = "NaCl";
        let result = parser.get_data(substance, SearchType::All, Phase::Solid);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.cp.is_some());
            assert!(data.dh.is_some());
            assert!(data.ds.is_some());
            assert!(data.molar_mass.is_some());
            println!("NaCl Solid data parsed successfully");
        }
    }

    #[test]
    #[ignore = "live NIST integration test"]
    fn test_nacl_liquid() {
        let parser = NistParser::new();
        let substance = "NaCl";
        let result = parser.get_data(substance, SearchType::All, Phase::Liquid);
        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.cp.is_some());
            assert!(data.dh.is_some());
            assert!(data.ds.is_some());
            assert!(data.molar_mass.is_some());
            println!("NaCl Liquid data parsed successfully");
        }
    }

    /*
    #[test]
    fn test_mock_substance_fetch() {
        let mut mock_client = MockHttpClient::new();

        // Mock response for methane
        let mock_response = r#"
            <html>
                <body>
                    <h1 id="Top">Methane</h1>
                    <li>Molecular weight: 16.043</li>
                    <table aria-label="Constant pressure heat capacity of gas">
                        <tr><td>298.15</td><td>35.69</td></tr>
                    </table>
                </body>
            </html>
        "#;

        mock_client.mock_response(
            "https://webbook.nist.gov/cgi/cbook.cgi?Formula=CH4&NoIon=on&Units=SI",
            Ok(mock_response.to_string())
        );

        let parser = NistParser::with_client(mock_client);
        let result = parser.get_data("CH4", SearchType::MolarMass, Phase::Gas);

        assert!(result.is_ok());
        if let Ok(data) = result {
            assert!(data.molar_mass.is_some());
            if let Some(mass) = data.molar_mass {
                assert!((mass - 16.043).abs() < 1e-6);
            }
        }
    }

    #[test]
    fn test_mock_not_found() {
        let mut mock_client = MockHttpClient::new();

        // Mock response for non-existent substance
        let mock_response = r#"
            <html>
                <body>
                    <h1>Name Not Found</h1>
                </body>
            </html>
        "#;

        mock_client.mock_response(
            "https://webbook.nist.gov/cgi/cbook.cgi?Name=NonexistentSubstance&Units=SI",
            Ok(mock_response.to_string())
        );

        let parser = NistParser::with_client(mock_client);
        let result = parser.get_data("NonexistentSubstance", SearchType::MolarMass, Phase::Gas);

        assert!(matches!(result, Err(NistError::SubstanceNotFound)));
    }

    #[test]
    fn test_mock_network_error() {
        let mut mock_client = MockHttpClient::new();

        // Mock a network error
        mock_client.mock_response(
            "https://webbook.nist.gov/cgi/cbook.cgi?Formula=H2O&NoIon=on&Units=SI",
            Err(reqwest::Error::from(std::io::Error::new(
                std::io::ErrorKind::ConnectionRefused,
                "Connection refused"
            )))
        );

        let parser = NistParser::with_client(mock_client);
        let result = parser.get_data("H2O", SearchType::MolarMass, Phase::Gas);

        assert!(matches!(result, Err(NistError::NetworkError(_))));
    }
    */
}