summaryrefslogtreecommitdiff
path: root/tests/integration_tests.rs
blob: 7661e3285b4fcf784b3a46bea23a49942c1412fb (plain)
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
// tests/integration_tests.rs
// Comprehensive integration tests for the Morse decoder

use anyhow::Result;
use ditdah::{MorseDecoder, MorseGenerator};
use hound::{SampleFormat, WavReader};
use std::fs;
use std::io::Write;

const TARGET_SAMPLE_RATE: u32 = 12000;
const CHUNK_SIZE: usize = 4096;

#[derive(Debug)]
struct TestCase {
    name: &'static str,
    text: &'static str,
    frequency: f32,
    wpm: f32,
    sample_rate: u32,
    expected_accuracy: f32, // Minimum accuracy threshold (0.0 to 1.0)
}

const TEST_CASES: &[TestCase] = &[
    // Basic tests
    TestCase {
        name: "simple_sos",
        text: "SOS",
        frequency: 600.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.8,
    },
    TestCase {
        name: "hello_world",
        text: "HELLO WORLD",
        frequency: 600.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.7,
    },
    TestCase {
        name: "alphabet",
        text: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
        frequency: 600.0,
        wpm: 15.0,
        sample_rate: 12000,
        expected_accuracy: 0.6,
    },
    // Different frequencies
    TestCase {
        name: "low_freq",
        text: "TEST",
        frequency: 300.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.7,
    },
    TestCase {
        name: "high_freq",
        text: "TEST",
        frequency: 1000.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.7,
    },
    // Different WPM speeds
    TestCase {
        name: "slow_wpm",
        text: "SLOW",
        frequency: 600.0,
        wpm: 10.0,
        sample_rate: 12000,
        expected_accuracy: 0.8,
    },
    TestCase {
        name: "fast_wpm",
        text: "FAST",
        frequency: 600.0,
        wpm: 30.0,
        sample_rate: 12000,
        expected_accuracy: 0.6,
    },
    // Numbers
    TestCase {
        name: "numbers",
        text: "12345",
        frequency: 600.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.7,
    },
    // Mixed content
    TestCase {
        name: "mixed",
        text: "CQ DE W1AW",
        frequency: 600.0,
        wpm: 20.0,
        sample_rate: 12000,
        expected_accuracy: 0.6,
    },
    // Different sample rates
    TestCase {
        name: "different_sample_rate",
        text: "RATE",
        frequency: 600.0,
        wpm: 20.0,
        sample_rate: 44100,
        expected_accuracy: 0.7,
    },
];

#[test]
fn run_comprehensive_test_suite() -> Result<()> {
    println!("Running comprehensive Morse decoder test suite...");

    // Create test directory
    fs::create_dir_all("test_outputs")?;

    let mut results = Vec::new();
    let mut total_tests = 0;
    let mut passed_tests = 0;

    // Create a detailed report file
    let mut report_file = fs::File::create("test_outputs/test_report.txt")?;
    writeln!(report_file, "Morse Decoder Test Report")?;
    writeln!(report_file, "=========================")?;
    writeln!(report_file)?;

    for test_case in TEST_CASES {
        total_tests += 1;
        println!("Running test: {}", test_case.name);

        let result = run_single_test(test_case);
        let passed = result.is_ok();
        if passed {
            passed_tests += 1;
        }

        // Log detailed results
        match &result {
            Ok(test_result) => {
                println!(
                    "  ✓ PASSED - Accuracy: {:.1}%",
                    test_result.accuracy * 100.0
                );
                writeln!(
                    report_file,
                    "TEST: {} - PASSED\n  Expected: '{}'\n  Decoded: '{}'\n  Accuracy: {:.1}%\n  WPM: {}, Freq: {}Hz, SR: {}Hz\n",
                    test_case.name,
                    test_case.text,
                    test_result.decoded_text,
                    test_result.accuracy * 100.0,
                    test_case.wpm,
                    test_case.frequency,
                    test_case.sample_rate
                )?;
            }
            Err(e) => {
                println!("  ✗ FAILED - {}", e);
                writeln!(
                    report_file,
                    "TEST: {} - FAILED\n  Expected: '{}'\n  Error: {}\n  WPM: {}, Freq: {}Hz, SR: {}Hz\n",
                    test_case.name,
                    test_case.text,
                    e,
                    test_case.wpm,
                    test_case.frequency,
                    test_case.sample_rate
                )?;
            }
        }

        results.push((test_case, result));
    }

    // Summary
    let pass_rate = (passed_tests as f32 / total_tests as f32) * 100.0;
    println!("\nTest Summary:");
    println!("  Total tests: {}", total_tests);
    println!("  Passed: {}", passed_tests);
    println!("  Failed: {}", total_tests - passed_tests);
    println!("  Pass rate: {:.1}%", pass_rate);

    writeln!(report_file, "\nSUMMARY:")?;
    writeln!(report_file, "  Total tests: {}", total_tests)?;
    writeln!(report_file, "  Passed: {}", passed_tests)?;
    writeln!(report_file, "  Failed: {}", total_tests - passed_tests)?;
    writeln!(report_file, "  Pass rate: {:.1}%", pass_rate)?;

    // Analyze failure patterns
    let failed_tests: Vec<_> = results.iter().filter(|(_, r)| r.is_err()).collect();
    if !failed_tests.is_empty() {
        writeln!(report_file, "\nFAILURE ANALYSIS:")?;
        for (test_case, error) in failed_tests {
            writeln!(
                report_file,
                "  {} - {}",
                test_case.name,
                error.as_ref().unwrap_err()
            )?;
        }
    }

    // Clean up test directory
    std::fs::remove_dir_all("test_outputs").ok();

    // If overall pass rate is too low, fail the test
    if pass_rate < 50.0 {
        panic!("Test suite failed with pass rate of {:.1}%.", pass_rate);
    }

    Ok(())
}

#[derive(Debug)]
struct TestResult {
    decoded_text: String,
    accuracy: f32,
}

fn run_single_test(test_case: &TestCase) -> Result<TestResult> {
    // Generate the test WAV file in a temporary location
    let generator = MorseGenerator::new(test_case.sample_rate, test_case.frequency, test_case.wpm);
    let wav_path = format!("test_outputs/{}.wav", test_case.name);
    generator.generate_wav_file(test_case.text, &wav_path)?;

    // Decode the WAV file
    let decoded_text = decode_wav_file(&wav_path)?;

    // Calculate accuracy
    let accuracy = calculate_accuracy(test_case.text, &decoded_text);

    let result = TestResult {
        decoded_text,
        accuracy,
    };

    // Clean up the temporary WAV file
    std::fs::remove_file(&wav_path).ok();

    // Check if accuracy meets threshold
    if accuracy >= test_case.expected_accuracy {
        Ok(result)
    } else {
        Err(anyhow::anyhow!(
            "Accuracy {:.1}% below threshold {:.1}%",
            accuracy * 100.0,
            test_case.expected_accuracy * 100.0
        ))
    }
}

fn decode_wav_file(path: &str) -> Result<String> {
    let mut reader = WavReader::open(path)?;
    let spec = reader.spec();

    if spec.sample_format != SampleFormat::Int && spec.sample_format != SampleFormat::Float {
        return Err(anyhow::anyhow!(
            "Unsupported sample format: {:?}",
            spec.sample_format
        ));
    }

    // Create decoder
    let mut decoder = MorseDecoder::new(spec.sample_rate, TARGET_SAMPLE_RATE)?;

    // Read and process audio
    let samples_f32: Vec<f32> = if spec.sample_format == SampleFormat::Int {
        reader
            .samples::<i16>()
            .map(|s| s.unwrap() as f32 / 32768.0)
            .collect()
    } else {
        reader.samples::<f32>().map(|s| s.unwrap()).collect()
    };

    // Convert to mono if necessary
    let mono_samples: Vec<f32> = if spec.channels > 1 {
        samples_f32
            .chunks_exact(spec.channels as usize)
            .map(|chunk| chunk.iter().sum::<f32>() / spec.channels as f32)
            .collect()
    } else {
        samples_f32
    };

    // Process in chunks
    for chunk in mono_samples.chunks(CHUNK_SIZE) {
        decoder.process(chunk)?;
    }

    // Finalize and get result
    decoder.finalize()
}

fn calculate_accuracy(expected: &str, actual: &str) -> f32 {
    if expected.is_empty() {
        return if actual.is_empty() { 1.0 } else { 0.0 };
    }

    let expected_clean = expected.to_uppercase().replace(" ", "");
    let actual_clean = actual.to_uppercase().replace(" ", "").replace("?", "");

    if expected_clean.is_empty() {
        return if actual_clean.is_empty() { 1.0 } else { 0.0 };
    }

    // Simple character-by-character comparison
    let expected_chars: Vec<char> = expected_clean.chars().collect();
    let actual_chars: Vec<char> = actual_clean.chars().collect();

    let max_len = expected_chars.len().max(actual_chars.len());
    let mut matches = 0;

    for i in 0..max_len {
        let expected_char = expected_chars.get(i);
        let actual_char = actual_chars.get(i);

        if expected_char == actual_char {
            matches += 1;
        }
    }

    matches as f32 / max_len as f32
}

#[test]
fn test_accuracy_calculation() {
    assert_eq!(calculate_accuracy("SOS", "SOS"), 1.0);
    assert_eq!(calculate_accuracy("SOS", "SO"), 2.0 / 3.0);
    assert_eq!(calculate_accuracy("SOS", "XOS"), 2.0 / 3.0);
    assert_eq!(calculate_accuracy("HELLO", "WORLD"), 1.0 / 5.0); // Only L matches
    assert_eq!(calculate_accuracy("", ""), 1.0);
    assert_eq!(calculate_accuracy("A", ""), 0.0);
}

#[test]
fn baseline_decoder_test() -> Result<()> {
    // Clean baseline test to establish current decoder status
    println!("=== DECODER BASELINE TEST ===");

    // Note: Set RUST_LOG=info environment variable to see decoder output during testing
    env_logger::try_init().ok();

    let generator = MorseGenerator::new(12000, 600.0, 20.0);

    let test_cases = [
        ("EEEE", "4 dots"),
        ("TTTT", "4 dashes"),
        ("ETET", "dot-dash-dot-dash"),
    ];

    for (i, (test_text, description)) in test_cases.iter().enumerate() {
        println!("\n--- Test {}: {} ({}) ---", i + 1, test_text, description);

        let temp_file = format!("baseline_test_{}.wav", i);
        generator.generate_wav_file(test_text, &temp_file)?;
        let decoded = decode_wav_file(&temp_file)?;
        println!(
            "Expected: {} | Decoded: {} | Success: {}",
            test_text,
            decoded,
            decoded == *test_text
        );

        // Clean up immediately
        std::fs::remove_file(&temp_file).ok();
    }

    // Summary
    println!("\n=== BASELINE SUMMARY ===");
    println!("This establishes our current decoder capabilities");
    println!("Focus on getting these 3 simple patterns working first");

    Ok(())
}