From 7beae18a855c3a8f39066a39ed455420bdf0e2fa Mon Sep 17 00:00:00 2001 From: hauke Date: Thu, 5 Nov 2026 14:21:09 +0100 Subject: [PATCH] test #14740: assert wav header sizes after finalize Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/core/src/audio/wav.rs | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/core/src/audio/wav.rs b/crates/core/src/audio/wav.rs index a859d54..eb2fb66 100644 --- a/crates/core/src/audio/wav.rs +++ b/crates/core/src/audio/wav.rs @@ -169,6 +169,51 @@ mod tests { let data_size = std::fs::metadata(&path).unwrap().len() - HEADER_LEN; assert_eq!(data_size, 32_000 * 2); + + // The header itself (not just the file length) must carry the final + // sizes: `finalize` must have rewritten them, not just left whatever + // the last periodic flush wrote. + let bytes = std::fs::read(&path).unwrap(); + assert_eq!(&bytes[40..44], &64_000u32.to_le_bytes(), "data chunk size"); + assert_eq!( + &bytes[4..8], + &(36 + 64_000u32).to_le_bytes(), + "RIFF chunk size" + ); + } + + #[test] + fn finalize_rewrites_header_for_a_non_flush_aligned_length() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("track.wav"); + + let mut writer = TrackWriter::create(&path).unwrap(); + // Two separate `write` calls: the first exactly hits the periodic + // flush threshold (16 000 samples), the second (100 samples) does + // not reach another threshold on its own. The trailing 100 samples + // are only ever reflected in the on-disk header if `finalize` + // rewrites it again afterwards. + writer.write(&vec![0.0f32; 16_000]).unwrap(); + writer.write(&vec![0.0f32; 100]).unwrap(); + let duration = writer.finalize().unwrap(); + + assert!((duration.as_secs_f64() - 16_100.0 / SAMPLE_RATE as f64).abs() < 1e-9); + + let bytes = std::fs::read(&path).unwrap(); + let expected_data_size = 16_100u32 * 2; + assert_eq!( + &bytes[40..44], + &expected_data_size.to_le_bytes(), + "data chunk size" + ); + assert_eq!( + &bytes[4..8], + &(36 + expected_data_size).to_le_bytes(), + "RIFF chunk size" + ); + + let reader = hound::WavReader::open(&path).unwrap(); + assert_eq!(reader.into_samples::().count(), 16_100); } #[test]