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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
"""
Tests for scripts/batch_harmonic_analysis.py

These tests exercise the batch driver against a tiny synthetic
:class:`Score` (no music21 corpus parsing, no disk corpus) so they run
in under a second. The music21-corpus path is smoke-tested separately
by the user via the CLI.

Structure:
    TestAnalyseScore                β€” pipeline correctness on a toy score
    TestJSONRoundTrip               β€” per-score JSON validates against schema
    TestRunBatchSkipAndForce        β€” idempotency + --force semantics
    TestRunBatchFailureRecovery     β€” one failing task doesn't abort the run
    TestManifest                    β€” manifest shape and totals
    TestCLI                         β€” argparse wiring
"""

from __future__ import annotations

import json
import sys
from fractions import Fraction
from pathlib import Path
from typing import List

import pytest

from src.core.data_structures import MusicalEvent, Score, TimeSignature

# Ensure backend/ is on sys.path so ``scripts.*`` imports work when pytest
# runs from the backend root (which is the normal configuration here).
_BACKEND_ROOT = Path(__file__).resolve().parent.parent.parent
if str(_BACKEND_ROOT) not in sys.path:
    sys.path.insert(0, str(_BACKEND_ROOT))

from scripts.batch_harmonic_analysis import (  # noqa: E402
    ANALYSIS_SCHEMA_VERSION,
    AnalysisResult,
    PipelineConfig,
    _ManifestEntry,
    _Task,
    analyse_score,
    main,
    run_batch,
    write_manifest,
)


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------


def _c_major_progression() -> Score:
    """Build a tiny synthetic C-major I–IV–V–I chorale.

    Every beat carries a full triad spelled out vertically so the chord
    classifier has obvious unambiguous material β€” we want to exercise
    the driver, not stress the analyzers.
    """

    def triad(root_pitch: int, third: int, fifth: int, onset: float) -> List[MusicalEvent]:
        return [
            MusicalEvent(
                pitch=p,
                onset=Fraction(onset),
                duration=Fraction(1),
                voice=v,
                beat_strength=1.0,
            )
            for v, p in enumerate([root_pitch, third, fifth])
        ]

    events: List[MusicalEvent] = []
    # I: C E G
    events += triad(48, 52, 55, 0)
    # IV: F A C
    events += triad(53, 57, 60, 1)
    # V: G B D
    events += triad(55, 59, 62, 2)
    # I: C E G
    events += triad(48, 52, 55, 3)

    return Score(
        events=events,
        time_signatures=[(Fraction(0), TimeSignature(4, 4))],
        title="Toy C-major",
        composer="TEST",
    )


@pytest.fixture
def toy_score() -> Score:
    return _c_major_progression()


# ---------------------------------------------------------------------------
# TestAnalyseScore
# ---------------------------------------------------------------------------


class TestAnalyseScore:
    def test_returns_analysis_result(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        assert isinstance(result, AnalysisResult)
        assert result.score_id == "toy"
        assert result.source == "test"
        assert result.pipeline_version == ANALYSIS_SCHEMA_VERSION

    def test_populates_global_key(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        gk = result.global_key
        assert gk["tonic"] == 0          # C
        assert gk["mode"] == "major"
        assert 0.0 <= float(gk["confidence"]) <= 1.0

    def test_produces_some_chords(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        assert result.stats["n_chords"] >= 1
        # RNs and chords line up one-to-one
        assert result.stats["n_roman_numerals"] == result.stats["n_chords"]

    def test_stats_sum_matches_lists(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        assert result.stats["n_chords"] == len(result.chords)
        assert result.stats["n_roman_numerals"] == len(result.roman_numerals)
        assert result.stats["n_cadences"] == len(result.cadences)
        assert result.stats["n_nct_annotations"] == len(result.nct_annotations)

    def test_records_runtime_ms(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        assert result.runtime_ms >= 0


# ---------------------------------------------------------------------------
# TestJSONRoundTrip
# ---------------------------------------------------------------------------


class TestJSONRoundTrip:
    def test_result_dict_is_json_serialisable(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        encoded = json.dumps(result.to_dict())
        decoded = json.loads(encoded)
        assert decoded["score_id"] == "toy"
        assert decoded["pipeline_version"] == ANALYSIS_SCHEMA_VERSION

    def test_fractions_render_as_strings(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        # Onsets and durations must be strings, not floats.
        assert isinstance(result.duration_ql, str)
        for c in result.chords:
            assert isinstance(c["onset"], str)
            assert isinstance(c["duration"], str)

    def test_cadence_indices_point_back_to_harmonic_events(self, toy_score: Score):
        result = analyse_score(
            toy_score,
            score_id="toy",
            source="test",
            input_path=None,
            config=PipelineConfig(),
        )
        for cad in result.cadences:
            # -1 is the sentinel for "not found" β€” our cadences always come
            # from the detector's own list so indices must be valid.
            assert 0 <= cad["penultimate_index"] < len(result.chords)
            assert 0 <= cad["final_index"] < len(result.chords)


# ---------------------------------------------------------------------------
# Helpers for batch-driver tests
# ---------------------------------------------------------------------------


class _StubLoader:
    """Minimal drop-in for ScoreLoader with just enough surface for the
    run_batch tests below. Maps (kind, payload) -> Score."""

    def __init__(self, mapping, failing: set = frozenset()):
        self._mapping = mapping
        self._failing = failing

    def load_corpus(self, bwv: str) -> Score:
        if bwv in self._failing:
            raise RuntimeError(f"synthetic failure for {bwv}")
        return self._mapping[("corpus", bwv)]

    def load(self, path) -> Score:
        if str(path) in self._failing:
            raise RuntimeError(f"synthetic failure for {path}")
        return self._mapping[("path", str(path))]


@pytest.fixture
def patched_loader(monkeypatch, toy_score):
    """Replace ScoreLoader inside the batch module with a stub that returns
    the toy score for any corpus id."""
    from scripts import batch_harmonic_analysis as mod

    class _AlwaysToy:
        def __init__(self, *_, **__):
            pass

        def load_corpus(self, bwv: str) -> Score:
            return toy_score

        def load(self, path) -> Score:
            return toy_score

    monkeypatch.setattr(mod, "ScoreLoader", _AlwaysToy)
    return _AlwaysToy


# ---------------------------------------------------------------------------
# TestRunBatchSkipAndForce
# ---------------------------------------------------------------------------


class TestRunBatchSkipAndForce:
    def test_first_run_writes_output(self, patched_loader, tmp_path: Path):
        tasks = [_Task(
            score_id="bwv66.6",
            source="corpus",
            loader_args=("corpus", "66.6"),
            relative_output=Path("corpus/bwv66.6.json"),
        )]
        entries = run_batch(
            tasks,
            output_dir=tmp_path,
            config=PipelineConfig(),
            force=False,
            fail_fast=False,
            progress=False,
        )
        assert len(entries) == 1
        assert entries[0].status == "ok"
        assert (tmp_path / "corpus" / "bwv66.6.json").exists()

    def test_second_run_skips_existing(self, patched_loader, tmp_path: Path):
        tasks = [_Task(
            score_id="bwv66.6",
            source="corpus",
            loader_args=("corpus", "66.6"),
            relative_output=Path("corpus/bwv66.6.json"),
        )]
        first = run_batch(
            tasks, output_dir=tmp_path, config=PipelineConfig(),
            force=False, fail_fast=False, progress=False,
        )
        assert first[0].status == "ok"

        second = run_batch(
            tasks, output_dir=tmp_path, config=PipelineConfig(),
            force=False, fail_fast=False, progress=False,
        )
        assert len(second) == 1
        assert second[0].status == "skipped"

    def test_force_re_runs_existing(self, patched_loader, tmp_path: Path):
        tasks = [_Task(
            score_id="bwv66.6",
            source="corpus",
            loader_args=("corpus", "66.6"),
            relative_output=Path("corpus/bwv66.6.json"),
        )]
        run_batch(
            tasks, output_dir=tmp_path, config=PipelineConfig(),
            force=False, fail_fast=False, progress=False,
        )
        forced = run_batch(
            tasks, output_dir=tmp_path, config=PipelineConfig(),
            force=True, fail_fast=False, progress=False,
        )
        assert forced[0].status == "ok"


# ---------------------------------------------------------------------------
# TestRunBatchFailureRecovery
# ---------------------------------------------------------------------------


class TestRunBatchFailureRecovery:
    def test_failing_task_does_not_abort_run(self, monkeypatch, tmp_path: Path, toy_score):
        from scripts import batch_harmonic_analysis as mod

        class _HalfFailing:
            def __init__(self, *_, **__):
                pass

            def load_corpus(self, bwv: str) -> Score:
                if bwv == "BAD":
                    raise RuntimeError("boom")
                return toy_score

            def load(self, path):  # pragma: no cover β€” not used
                return toy_score

        monkeypatch.setattr(mod, "ScoreLoader", _HalfFailing)

        tasks = [
            _Task("bwvGOOD1", "corpus", ("corpus", "GOOD1"), Path("corpus/g1.json")),
            _Task("bwvBAD", "corpus", ("corpus", "BAD"), Path("corpus/bad.json")),
            _Task("bwvGOOD2", "corpus", ("corpus", "GOOD2"), Path("corpus/g2.json")),
        ]
        entries = run_batch(
            tasks, output_dir=tmp_path, config=PipelineConfig(),
            force=False, fail_fast=False, progress=False,
        )
        statuses = [e.status for e in entries]
        assert statuses == ["ok", "failed", "ok"]
        # Failures log records the bad task
        log_path = tmp_path / "failures.jsonl"
        assert log_path.exists()
        lines = log_path.read_text().strip().splitlines()
        assert len(lines) == 1
        assert json.loads(lines[0])["score_id"] == "bwvBAD"

    def test_fail_fast_raises_on_first_error(self, monkeypatch, tmp_path: Path, toy_score):
        from scripts import batch_harmonic_analysis as mod

        class _AlwaysFail:
            def __init__(self, *_, **__):
                pass

            def load_corpus(self, bwv: str) -> Score:
                raise RuntimeError("boom")

            def load(self, path):  # pragma: no cover
                return toy_score

        monkeypatch.setattr(mod, "ScoreLoader", _AlwaysFail)

        tasks = [_Task("bwvX", "corpus", ("corpus", "X"), Path("corpus/x.json"))]
        with pytest.raises(RuntimeError, match="boom"):
            run_batch(
                tasks, output_dir=tmp_path, config=PipelineConfig(),
                force=False, fail_fast=True, progress=False,
            )


# ---------------------------------------------------------------------------
# TestManifest
# ---------------------------------------------------------------------------


class TestManifest:
    def test_manifest_totals_match_entries(self, tmp_path: Path):
        entries = [
            _ManifestEntry("a", "corpus", "ok", output="a.json", runtime_ms=1, n_cadences=0),
            _ManifestEntry("b", "corpus", "ok", output="b.json", runtime_ms=2, n_cadences=1),
            _ManifestEntry("c", "corpus", "failed", error="boom"),
            _ManifestEntry("d", "corpus", "skipped", output="d.json"),
        ]
        path = write_manifest(
            tmp_path, entries,
            source_description="unit-test",
            config=PipelineConfig(),
        )
        assert path.exists()
        data = json.loads(path.read_text())
        assert data["totals"] == {"ok": 2, "failed": 1, "skipped": 1, "total": 4}
        assert data["pipeline_version"] == ANALYSIS_SCHEMA_VERSION
        assert len(data["entries"]) == 4

    def test_manifest_records_pipeline_config(self, tmp_path: Path):
        cfg = PipelineConfig(separate_voices=True, max_voices=4)
        write_manifest(tmp_path, [], source_description="x", config=cfg)
        data = json.loads((tmp_path / "manifest.json").read_text())
        assert data["pipeline_config"]["separate_voices"] is True
        assert data["pipeline_config"]["max_voices"] == 4


# ---------------------------------------------------------------------------
# TestCLI
# ---------------------------------------------------------------------------


class TestCLI:
    def test_main_with_stubbed_loader(self, monkeypatch, tmp_path: Path, toy_score):
        from scripts import batch_harmonic_analysis as mod

        class _AlwaysToy:
            def __init__(self, *_, **__):
                pass

            def load_corpus(self, bwv: str) -> Score:
                return toy_score

            def load(self, path) -> Score:
                return toy_score

        monkeypatch.setattr(mod, "ScoreLoader", _AlwaysToy)

        rc = main([
            "--bwv", "999.9",
            "--output-dir", str(tmp_path),
            "--quiet",
        ])
        assert rc == 0
        assert (tmp_path / "manifest.json").exists()
        assert (tmp_path / "corpus" / "bwv999.9.json").exists()

    def test_main_requires_source(self):
        # argparse treats the mutually-exclusive group as required, so a
        # bare invocation should exit with code 2.
        with pytest.raises(SystemExit) as exc:
            main([])
        assert exc.value.code == 2

    def test_main_missing_path_returns_error(self, tmp_path: Path):
        missing = tmp_path / "does-not-exist"
        rc = main([
            "--path", str(missing),
            "--output-dir", str(tmp_path / "out"),
            "--quiet",
        ])
        assert rc == 2