File size: 25,343 Bytes
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
"""
Contract tests for CLI commands

Tests that CLI interface contracts match specifications from contracts/cli-commands.md
"""

import json
import subprocess
from pathlib import Path

import pytest


class TestSeparateCLIContract:
    """Contract tests for 'voice-tools separate' command."""

    def test_separate_command_exists(self):
        """Test that separate command is registered."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        assert result.returncode == 0
        assert "separate" in result.stdout

    def test_separate_help_text(self):
        """Test that separate command has help documentation."""
        result = subprocess.run(
            ["voice-tools", "separate", "--help"], capture_output=True, text=True
        )

        assert result.returncode == 0
        help_text = result.stdout.lower()

        # Should document key parameters
        assert "input" in help_text or "file" in help_text
        assert "output" in help_text
        assert "speaker" in help_text

    def test_separate_required_argument(self):
        """Test that INPUT_FILE argument is required."""
        result = subprocess.run(["voice-tools", "separate"], capture_output=True, text=True)

        # Should fail without input file
        assert result.returncode != 0
        assert "required" in result.stderr.lower() or "missing" in result.stderr.lower()

    def test_separate_optional_arguments(self):
        """Test that optional arguments are accepted."""
        # Test --help to verify argument parsing without actual execution
        result = subprocess.run(
            ["voice-tools", "separate", "--help"], capture_output=True, text=True
        )

        help_text = result.stdout

        # Verify optional arguments from contract
        assert "--output-dir" in help_text or "-o" in help_text
        assert "--min-speakers" in help_text
        assert "--max-speakers" in help_text
        assert "--output-format" in help_text or "format" in help_text.lower()

    def test_separate_output_dir_default(self, tmp_path):
        """Test that default output directory is used when not specified."""
        # Create minimal test (will fail due to missing audio, but tests argument parsing)
        input_file = tmp_path / "test.m4a"
        input_file.touch()

        result = subprocess.run(
            ["voice-tools", "separate", str(input_file)],
            capture_output=True,
            text=True,
            cwd=str(tmp_path),
        )

        # Command should attempt to use default directory
        # (May fail on invalid audio, but should not fail on missing --output-dir)
        assert "--output-dir" not in result.stderr or "required" not in result.stderr.lower()

    def test_separate_exit_codes(self):
        """Test that appropriate exit codes are returned.

        Contract specifies:
        - 0: Success
        - 1: Invalid arguments or file not found
        - 2: Unsupported audio format
        - 3: Processing error
        - 4: Output write error
        """
        # Test exit code 1: File not found
        result = subprocess.run(
            ["voice-tools", "separate", "nonexistent.m4a"], capture_output=True, text=True
        )

        assert result.returncode == 1
        assert "not found" in result.stderr.lower() or "error" in result.stderr.lower()

    def test_separate_error_messages(self):
        """Test that error messages are informative."""
        # Test with nonexistent file
        result = subprocess.run(
            ["voice-tools", "separate", "nonexistent.m4a"], capture_output=True, text=True
        )

        assert result.returncode != 0

        # Error message should be clear
        error_msg = result.stderr.lower()
        assert "error" in error_msg or "failed" in error_msg
        assert "nonexistent.m4a" in error_msg or "not found" in error_msg

    def test_separate_min_max_speakers_validation(self):
        """Test that min/max speakers are validated."""
        # Test with min > max (should fail validation)
        result = subprocess.run(
            [
                "voice-tools",
                "separate",
                "test.m4a",
                "--min-speakers",
                "5",
                "--max-speakers",
                "2",
            ],
            capture_output=True,
            text=True,
        )

        # Should fail with validation error
        # (May fail on missing file first, but validates if file exists)
        assert result.returncode != 0

    @pytest.mark.integration
    def test_separate_output_structure(self, tmp_path):
        """Test that output follows specified structure.

        Contract specifies:
        - {output-dir}/speaker_00.m4a
        - {output-dir}/speaker_01.m4a
        - {output-dir}/separation_report.json
        """
        # This test requires actual audio processing
        # Skip if no test audio available
        test_audio = Path("audio_fixtures/multi_speaker/two_speakers.m4a")
        if not test_audio.exists():
            pytest.skip("Test audio not available")

        output_dir = tmp_path / "output"

        result = subprocess.run(
            ["voice-tools", "separate", str(test_audio), "--output-dir", str(output_dir)],
            capture_output=True,
            text=True,
        )

        if result.returncode == 0:
            # Verify output structure
            assert output_dir.exists()

            # Should have separation_report.json
            report_file = output_dir / "separation_report.json"
            assert report_file.exists()

            # Load and validate report structure
            with open(report_file) as f:
                report = json.load(f)

            assert "input_file" in report
            assert "speakers_detected" in report
            assert "processing_time_seconds" in report
            assert "output_files" in report

    @pytest.mark.integration
    def test_separate_progress_indicators(self, tmp_path):
        """Test that progress is displayed during processing."""
        test_audio = Path("audio_fixtures/multi_speaker/two_speakers.m4a")
        if not test_audio.exists():
            pytest.skip("Test audio not available")

        output_dir = tmp_path / "output"

        result = subprocess.run(
            ["voice-tools", "separate", str(test_audio), "--output-dir", str(output_dir)],
            capture_output=True,
            text=True,
        )

        # Should show progress in stdout
        if result.returncode == 0:
            output = result.stdout.lower()
            # Check for progress indicators
            assert "loading" in output or "processing" in output or "separating" in output


class TestCLICommonBehavior:
    """Test common CLI behavior across all commands."""

    def test_version_flag(self):
        """Test that --version flag works."""
        result = subprocess.run(["voice-tools", "--version"], capture_output=True, text=True)

        assert result.returncode == 0
        # Should show version number
        assert any(char.isdigit() for char in result.stdout)

    def test_help_flag(self):
        """Test that --help flag works."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        assert result.returncode == 0
        assert "usage" in result.stdout.lower()

    def test_command_list(self):
        """Test that all commands are listed in help."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        assert result.returncode == 0
        # Should list available commands
        assert "separate" in result.stdout.lower()
        # May also show other commands if they exist

    def test_verbose_flag(self):
        """Test that -v/--verbose flag is accepted."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        # Check if verbose flag is documented
        # (May not be implemented yet, so don't assert)
        has_verbose = "-v" in result.stdout or "--verbose" in result.stdout

    def test_quiet_flag(self):
        """Test that -q/--quiet flag is accepted."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        # Check if quiet flag is documented
        has_quiet = "-q" in result.stdout or "--quiet" in result.stdout


class TestExtractSpeakerCLIContract:
    """Contract tests for 'voice-tools extract-speaker' command (US2)."""

    def test_extract_speaker_command_exists(self):
        """Test that extract-speaker command is registered."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        assert result.returncode == 0
        assert "extract-speaker" in result.stdout or "extract_speaker" in result.stdout

    def test_extract_speaker_help_text(self):
        """Test that extract-speaker command has help documentation."""
        result = subprocess.run(
            ["voice-tools", "extract-speaker", "--help"], capture_output=True, text=True
        )

        assert result.returncode == 0
        help_text = result.stdout.lower()

        # Should document key parameters
        assert "reference" in help_text
        assert "target" in help_text
        assert "threshold" in help_text
        assert "output" in help_text

    def test_extract_speaker_required_arguments(self):
        """Test that REFERENCE_CLIP and TARGET_FILE arguments are required."""
        result = subprocess.run(
            ["voice-tools", "extract-speaker"], capture_output=True, text=True
        )

        # Should fail without required arguments
        assert result.returncode != 0
        assert "required" in result.stderr.lower() or "missing" in result.stderr.lower()

    def test_extract_speaker_optional_arguments(self):
        """Test that optional arguments are accepted."""
        result = subprocess.run(
            ["voice-tools", "extract-speaker", "--help"], capture_output=True, text=True
        )

        help_text = result.stdout

        # Verify optional arguments from contract
        assert "--output" in help_text or "-o" in help_text
        assert "--threshold" in help_text
        assert "--min-confidence" in help_text
        assert "--concatenate" in help_text or "--no-concatenate" in help_text

    def test_extract_speaker_threshold_validation(self):
        """Test that threshold is validated (must be between 0.0 and 1.0)."""
        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                "ref.m4a",
                "target.m4a",
                "--threshold",
                "1.5",  # Invalid: > 1.0
            ],
            capture_output=True,
            text=True,
        )

        # Should fail with validation error
        assert result.returncode != 0

    def test_extract_speaker_exit_codes(self):
        """Test that appropriate exit codes are returned.

        Contract specifies:
        - 0: Success (at least 1 segment found)
        - 1: Invalid arguments or file not found
        - 2: Unsupported audio format
        - 3: Processing error or no matches found
        - 4: Reference clip too short or poor quality
        - 5: Output write error
        """
        # Test exit code 1: File not found
        result = subprocess.run(
            ["voice-tools", "extract-speaker", "nonexistent.m4a", "target.m4a"],
            capture_output=True,
            text=True,
        )

        assert result.returncode in [1, 2, 3]  # Various error codes possible
        assert "not found" in result.stderr.lower() or "error" in result.stderr.lower()

    def test_extract_speaker_error_messages(self):
        """Test that error messages are informative."""
        # Test with nonexistent reference file
        result = subprocess.run(
            ["voice-tools", "extract-speaker", "nonexistent.m4a", "target.m4a"],
            capture_output=True,
            text=True,
        )

        assert result.returncode != 0

        # Error message should be clear
        error_msg = result.stderr.lower()
        assert "error" in error_msg or "failed" in error_msg
        assert "nonexistent.m4a" in error_msg or "not found" in error_msg

    @pytest.mark.integration
    def test_extract_speaker_output_structure_concatenated(self, tmp_path):
        """Test that output follows specified structure for concatenated mode.

        Contract specifies:
        - extracted_speaker.m4a
        - extraction_report.json
        """
        test_reference = Path("audio_fixtures/speaker_extraction/reference_speaker_a.m4a")
        test_target = Path("audio_fixtures/speaker_extraction/multi_speaker_conversation.m4a")

        if not test_reference.exists() or not test_target.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "extracted.m4a"

        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                str(test_reference),
                str(test_target),
                "--output",
                str(output_file),
            ],
            capture_output=True,
            text=True,
        )

        if result.returncode == 0:
            # Verify output file exists
            assert output_file.exists()

            # Verify report exists
            report_file = output_file.parent / "extraction_report.json"
            if report_file.exists():
                with open(report_file) as f:
                    report = json.load(f)

                # Validate report structure
                assert "reference_clip" in report
                assert "target_file" in report
                assert "threshold" in report
                assert "segments_found" in report
                assert "segments_included" in report
                assert "total_duration_seconds" in report
                assert "average_confidence" in report

    @pytest.mark.integration
    def test_extract_speaker_output_structure_separate(self, tmp_path):
        """Test that output follows specified structure for separate segments mode.

        Contract specifies:
        - {output_prefix}/segment_001.m4a
        - {output_prefix}/segment_002.m4a
        - {output_prefix}/extraction_report.json
        """
        test_reference = Path("audio_fixtures/speaker_extraction/reference_speaker_a.m4a")
        test_target = Path("audio_fixtures/speaker_extraction/multi_speaker_conversation.m4a")

        if not test_reference.exists() or not test_target.exists():
            pytest.skip("Test audio not available")

        output_dir = tmp_path / "segments"

        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                str(test_reference),
                str(test_target),
                "--no-concatenate",
                "--output",
                str(output_dir),
            ],
            capture_output=True,
            text=True,
        )

        if result.returncode == 0:
            # Verify output directory exists
            assert output_dir.exists()

            # Verify segment files exist
            segment_files = list(output_dir.glob("segment_*.m4a"))
            assert len(segment_files) > 0

            # Verify report exists
            report_file = output_dir / "extraction_report.json"
            if report_file.exists():
                with open(report_file) as f:
                    report = json.load(f)

                assert report["segments_included"] == len(segment_files)

    @pytest.mark.integration
    def test_extract_speaker_no_matches_warning(self, tmp_path):
        """Test that appropriate warning is shown when no matches found."""
        test_reference = Path("audio_fixtures/speaker_extraction/reference_speaker_a.m4a")
        test_target = Path("audio_fixtures/speaker_extraction/different_speaker_only.m4a")

        if not test_reference.exists() or not test_target.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "no_matches.m4a"

        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                str(test_reference),
                str(test_target),
                "--output",
                str(output_file),
            ],
            capture_output=True,
            text=True,
        )

        # Should return exit code 3 for no matches
        if result.returncode == 3:
            error_msg = result.stderr.lower() + result.stdout.lower()
            assert "no" in error_msg and ("match" in error_msg or "segment" in error_msg)

    @pytest.mark.integration
    def test_extract_speaker_reference_too_short(self, tmp_path):
        """Test that error is shown when reference clip is too short."""
        test_reference = Path("audio_fixtures/speaker_extraction/reference_too_short.m4a")
        test_target = Path("audio_fixtures/speaker_extraction/multi_speaker_conversation.m4a")

        if not test_reference.exists() or not test_target.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "extracted.m4a"

        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                str(test_reference),
                str(test_target),
                "--output",
                str(output_file),
            ],
            capture_output=True,
            text=True,
        )

        # Should return exit code 4 for reference clip issues
        if result.returncode == 4:
            error_msg = result.stderr.lower()
            assert "short" in error_msg or "minimum" in error_msg

    @pytest.mark.integration
    def test_extract_speaker_progress_indicators(self, tmp_path):
        """Test that progress is displayed during processing."""
        test_reference = Path("audio_fixtures/speaker_extraction/reference_speaker_a.m4a")
        test_target = Path("audio_fixtures/speaker_extraction/multi_speaker_conversation.m4a")

        if not test_reference.exists() or not test_target.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "extracted.m4a"

        result = subprocess.run(
            [
                "voice-tools",
                "extract-speaker",
                str(test_reference),
                str(test_target),
                "--output",
                str(output_file),
            ],
            capture_output=True,
            text=True,
        )

        # Should show progress in stdout
        if result.returncode == 0:
            output = result.stdout.lower()
            # Check for progress indicators
            assert (
                "loading" in output
                or "processing" in output
                or "extracting" in output
                or "matching" in output
            )

    def test_extract_speaker_concatenate_options(self):
        """Test that concatenation options are accepted."""
        result = subprocess.run(
            ["voice-tools", "extract-speaker", "--help"], capture_output=True, text=True
        )

        help_text = result.stdout.lower()

        # Should document concatenation options
        assert "--silence" in help_text or "silence" in help_text
        assert "--crossfade" in help_text or "crossfade" in help_text


class TestDenoiseCLIContract:
    """Contract tests for 'voice-tools denoise' command (US3)."""

    def test_denoise_command_exists(self):
        """Test that denoise command is registered."""
        result = subprocess.run(["voice-tools", "--help"], capture_output=True, text=True)

        assert result.returncode == 0
        assert "denoise" in result.stdout

    def test_denoise_help_text(self):
        """Test that denoise command has help documentation."""
        result = subprocess.run(
            ["voice-tools", "denoise", "--help"], capture_output=True, text=True
        )

        assert result.returncode == 0
        help_text = result.stdout.lower()

        # Should document key parameters
        assert "input" in help_text or "file" in help_text
        assert "output" in help_text
        assert "threshold" in help_text or "silence" in help_text

    def test_denoise_required_argument(self):
        """Test that INPUT_FILE argument is required."""
        result = subprocess.run(["voice-tools", "denoise"], capture_output=True, text=True)

        # Should fail without input file
        assert result.returncode != 0
        assert "required" in result.stderr.lower() or "missing" in result.stderr.lower()

    def test_denoise_optional_arguments(self):
        """Test that optional arguments are accepted."""
        result = subprocess.run(
            ["voice-tools", "denoise", "--help"], capture_output=True, text=True
        )

        help_text = result.stdout

        # Verify optional arguments from contract
        assert "--output" in help_text or "-o" in help_text
        assert "--vad-threshold" in help_text or "vad" in help_text.lower()
        assert "--silence-threshold" in help_text or "silence" in help_text.lower()
        assert "--crossfade" in help_text or "crossfade" in help_text.lower()

    def test_denoise_threshold_validation(self):
        """Test that VAD threshold is validated (must be between 0.0 and 1.0)."""
        result = subprocess.run(
            [
                "voice-tools",
                "denoise",
                "test.m4a",
                "--vad-threshold",
                "1.5",  # Invalid: > 1.0
            ],
            capture_output=True,
            text=True,
        )

        # Should fail with validation error
        assert result.returncode != 0

    def test_denoise_exit_codes(self):
        """Test that appropriate exit codes are returned.

        Contract specifies:
        - 0: Success (audio processed)
        - 1: Invalid arguments or file not found
        - 2: Unsupported audio format
        - 3: Processing error
        - 4: Output write error
        """
        # Test exit code 1: File not found
        result = subprocess.run(
            ["voice-tools", "denoise", "nonexistent.m4a"], capture_output=True, text=True
        )

        assert result.returncode == 1
        assert "not found" in result.stderr.lower() or "error" in result.stderr.lower()

    def test_denoise_error_messages(self):
        """Test that error messages are informative."""
        # Test with nonexistent file
        result = subprocess.run(
            ["voice-tools", "denoise", "nonexistent.m4a"], capture_output=True, text=True
        )

        assert result.returncode != 0

        # Error message should be clear
        error_msg = result.stderr.lower()
        assert "error" in error_msg or "failed" in error_msg
        assert "nonexistent.m4a" in error_msg or "not found" in error_msg

    @pytest.mark.integration
    def test_denoise_output_structure(self, tmp_path):
        """Test that output follows specified structure.

        Contract specifies:
        - denoised audio file at specified output path
        - denoising_report.json with metrics
        """
        test_audio = Path("audio_fixtures/noisy/background_noise.m4a")
        if not test_audio.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "denoised.m4a"

        result = subprocess.run(
            ["voice-tools", "denoise", str(test_audio), "--output", str(output_file)],
            capture_output=True,
            text=True,
        )

        if result.returncode == 0:
            # Verify output file exists
            assert output_file.exists()

            # Verify report exists
            report_file = output_file.parent / "denoising_report.json"
            if report_file.exists():
                with open(report_file) as f:
                    report = json.load(f)

                # Validate report structure
                assert "input_file" in report
                assert "segments_kept" in report
                assert "segments_removed" in report
                assert "original_duration" in report
                assert "output_duration" in report
                assert "compression_ratio" in report

    @pytest.mark.integration
    def test_denoise_progress_indicators(self, tmp_path):
        """Test that progress is displayed during processing."""
        test_audio = Path("audio_fixtures/noisy/background_noise.m4a")
        if not test_audio.exists():
            pytest.skip("Test audio not available")

        output_file = tmp_path / "denoised.m4a"

        result = subprocess.run(
            ["voice-tools", "denoise", str(test_audio), "--output", str(output_file)],
            capture_output=True,
            text=True,
        )

        # Should show progress in stdout
        if result.returncode == 0:
            output = result.stdout.lower()
            # Check for progress indicators
            assert (
                "loading" in output
                or "processing" in output
                or "denoising" in output
                or "detecting" in output
            )