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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
"""
Tests for :mod:`src.data.harmonic_encoder`.

Structure:
    TestLabelExtractors         β€” chord / rn / cadence label rendering
    TestBuildVocabulary         β€” frequency sort, ties, min-count filtering
    TestHarmonicVocabularyIO    β€” save β†’ load round-trip, schema-version guard
    TestEncoderAlignment        β€” chord/rn/cadence stream length invariants
    TestEncoderCadencePlacement β€” cadences land at final_index, NONE elsewhere
    TestEncoderUnk              β€” unseen labels fall back to UNK
    TestEncoderDecode           β€” decode renders specials + labels
    TestFromAnalyses            β€” in-memory construction shortcut
    TestEncodeFile              β€” load + encode JSON from disk
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Dict, List

import pytest

from src.data.harmonic_encoder import (
    BOS,
    CAD_NONE,
    EOS,
    EncodedSequence,
    HarmonicEncoder,
    HarmonicEncoderConfig,
    HarmonicEncoderError,
    HarmonicVocabulary,
    PAD,
    SchemaVersionMismatch,
    UNK,
    VOCAB_SCHEMA_VERSION,
    build_vocabulary,
    cadence_label,
    chord_label,
    rn_label,
)


# ---------------------------------------------------------------------------
# Fixtures β€” in-memory Stage 4 analysis dicts
# ---------------------------------------------------------------------------


def _chord(root: int, quality: str, onset: str = "0") -> Dict[str, object]:
    return {
        "onset": onset,
        "duration": "1",
        "root": root,
        "quality": quality,
        "pitches": [],
        "bass": root,
        "inversion": 0,
    }


def _rn(label: str) -> Dict[str, object]:
    return {
        "degree": 1,
        "quality": "major",
        "inversion": 0,
        "local_key_tonic": 0,
        "local_key_mode": "major",
        "is_secondary": False,
        "secondary_target": None,
        "function": "TONIC",
        "label": label,
    }


def _cad(cadence_type: str, penultimate_index: int, final_index: int) -> Dict[str, object]:
    return {
        "onset": str(final_index),
        "cadence_type": cadence_type,
        "label": cadence_type,
        "confidence": 1.0,
        "penultimate_index": penultimate_index,
        "final_index": final_index,
    }


@pytest.fixture
def c_major_analysis() -> Dict[str, object]:
    """I–IV–V–I in C major with a PAC at the end (Vβ†’I, indices 2β†’3)."""
    return {
        "score_id": "toy",
        "chords": [
            _chord(0, "major", "0"),   # C: I
            _chord(5, "major", "1"),   # F: IV
            _chord(7, "major", "2"),   # G: V
            _chord(0, "major", "3"),   # C: I
        ],
        "roman_numerals": [
            _rn("I"),
            _rn("IV"),
            _rn("V"),
            _rn("I"),
        ],
        "cadences": [
            _cad("authentic_perfect", 2, 3),
        ],
    }


@pytest.fixture
def minor_analysis() -> Dict[str, object]:
    """i–iv–V–i in A minor with a half cadence at index 2."""
    return {
        "score_id": "toy2",
        "chords": [
            _chord(9, "minor", "0"),
            _chord(2, "minor", "1"),
            _chord(4, "major", "2"),
            _chord(9, "minor", "3"),
        ],
        "roman_numerals": [
            _rn("i"),
            _rn("iv"),
            _rn("V"),
            _rn("i"),
        ],
        "cadences": [
            _cad("half", 1, 2),
        ],
    }


# ---------------------------------------------------------------------------
# TestLabelExtractors
# ---------------------------------------------------------------------------


class TestLabelExtractors:
    def test_chord_label_formats_root_and_quality(self):
        assert chord_label(_chord(0, "major")) == "C:major"
        assert chord_label(_chord(7, "minor7")) == "G:minor7"
        # Pitch-class 3 is "Eb" in the project table.
        assert chord_label(_chord(3, "diminished")) == "Eb:diminished"

    def test_rn_label_uses_stored_label_field(self):
        assert rn_label(_rn("V7")) == "V7"
        assert rn_label(_rn("V/V")) == "V/V"
        assert rn_label(_rn("ii\u00f865")) == "ii\u00f865"

    def test_cadence_label_uses_cadence_type(self):
        assert cadence_label(_cad("authentic_perfect", 0, 1)) == "authentic_perfect"
        assert cadence_label(_cad("phrygian_half", 0, 1)) == "phrygian_half"


# ---------------------------------------------------------------------------
# TestBuildVocabulary
# ---------------------------------------------------------------------------


class TestBuildVocabulary:
    def test_specials_get_canonical_ids(self, c_major_analysis):
        vocab = build_vocabulary([c_major_analysis], HarmonicEncoderConfig())
        assert vocab.chord_to_id["<pad>"] == PAD
        assert vocab.chord_to_id["<bos>"] == BOS
        assert vocab.chord_to_id["<eos>"] == EOS
        assert vocab.chord_to_id["<unk>"] == UNK
        # Cadence stream has an extra sentinel at CAD_NONE.
        assert vocab.cadence_to_id["<none>"] == CAD_NONE

    def test_chord_labels_are_sorted_by_frequency(self):
        # C:major appears 3x, F:major 1x β€” C should get a lower id.
        analysis = {
            "score_id": "freq",
            "chords": [
                _chord(0, "major"), _chord(0, "major"), _chord(0, "major"),
                _chord(5, "major"),
            ],
            "roman_numerals": [_rn("I"), _rn("I"), _rn("I"), _rn("IV")],
            "cadences": [],
        }
        vocab = build_vocabulary([analysis], HarmonicEncoderConfig())
        assert vocab.chord_to_id["C:major"] < vocab.chord_to_id["F:major"]

    def test_alphabetical_tiebreak(self):
        # A:major and B:major each appear once β€” ids should be alphabetical.
        analysis = {
            "score_id": "tie",
            "chords": [_chord(9, "major"), _chord(11, "major")],
            "roman_numerals": [_rn("VI"), _rn("VII")],
            "cadences": [],
        }
        vocab = build_vocabulary([analysis], HarmonicEncoderConfig())
        assert vocab.chord_to_id["A:major"] < vocab.chord_to_id["B:major"]

    def test_min_chord_count_filters_rare_labels(self):
        analysis = {
            "score_id": "filter",
            "chords": [
                _chord(0, "major"), _chord(0, "major"), _chord(0, "major"),
                _chord(5, "major"),  # appears once β€” should be filtered
            ],
            "roman_numerals": [_rn("I"), _rn("I"), _rn("I"), _rn("IV")],
            "cadences": [],
        }
        cfg = HarmonicEncoderConfig(min_chord_count=2, min_rn_count=1)
        vocab = build_vocabulary([analysis], cfg)
        assert "C:major" in vocab.chord_to_id
        assert "F:major" not in vocab.chord_to_id

    def test_min_rn_count_filters_rare_labels(self):
        analysis = {
            "score_id": "filter",
            "chords": [
                _chord(0, "major"), _chord(0, "major"), _chord(0, "major"),
            ],
            # "I" appears twice, "V/V" once β€” with min_rn_count=2, only
            # "I" should survive.
            "roman_numerals": [_rn("I"), _rn("I"), _rn("V/V")],
            "cadences": [],
        }
        cfg = HarmonicEncoderConfig(min_chord_count=1, min_rn_count=2)
        vocab = build_vocabulary([analysis], cfg)
        assert "I" in vocab.rn_to_id
        assert "V/V" not in vocab.rn_to_id

    def test_cadence_vocab_shape(self, c_major_analysis, minor_analysis):
        vocab = build_vocabulary(
            [c_major_analysis, minor_analysis], HarmonicEncoderConfig()
        )
        # 4 shared specials + <none> sentinel + 2 cadence types = 7.
        assert vocab.cadence_vocab_size == 7
        assert "authentic_perfect" in vocab.cadence_to_id
        assert "half" in vocab.cadence_to_id


# ---------------------------------------------------------------------------
# TestHarmonicVocabularyIO
# ---------------------------------------------------------------------------


class TestHarmonicVocabularyIO:
    def test_save_load_roundtrip(self, tmp_path: Path, c_major_analysis):
        vocab = build_vocabulary([c_major_analysis], HarmonicEncoderConfig())
        path = tmp_path / "vocab.json"
        vocab.save(path)
        loaded = HarmonicVocabulary.load(path)
        assert loaded.chord_to_id == vocab.chord_to_id
        assert loaded.rn_to_id == vocab.rn_to_id
        assert loaded.cadence_to_id == vocab.cadence_to_id

    def test_written_file_records_schema_version(self, tmp_path: Path):
        HarmonicVocabulary().save(tmp_path / "v.json")
        data = json.loads((tmp_path / "v.json").read_text())
        assert data["schema_version"] == VOCAB_SCHEMA_VERSION

    def test_load_rejects_schema_mismatch(self, tmp_path: Path):
        path = tmp_path / "v.json"
        path.write_text(json.dumps({
            "schema_version": "0.0.1",
            "chord_to_id": {},
            "rn_to_id": {},
            "cadence_to_id": {},
        }))
        with pytest.raises(SchemaVersionMismatch):
            HarmonicVocabulary.load(path)


# ---------------------------------------------------------------------------
# TestEncoderAlignment
# ---------------------------------------------------------------------------


class TestEncoderAlignment:
    def test_all_streams_same_length(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        # 4 chords + BOS + EOS = 6
        assert seq.length == 6
        assert len(seq.chord_tokens) == len(seq.rn_tokens) == len(seq.cadence_tokens)

    def test_sequence_wrapped_with_bos_eos(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        assert seq.chord_tokens[0] == BOS
        assert seq.rn_tokens[0] == BOS
        assert seq.cadence_tokens[0] == BOS
        assert seq.chord_tokens[-1] == EOS
        assert seq.rn_tokens[-1] == EOS
        assert seq.cadence_tokens[-1] == EOS

    def test_onset_strings_align_with_chords(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        # Specials have empty onset; body follows chord onsets.
        assert seq.onsets[0] == ""
        assert seq.onsets[-1] == ""
        assert seq.onsets[1:-1] == ["0", "1", "2", "3"]

    def test_mismatched_chord_rn_length_raises(self):
        bad = {
            "score_id": "bad",
            "chords": [_chord(0, "major"), _chord(5, "major")],
            "roman_numerals": [_rn("I")],   # only one!
            "cadences": [],
        }
        enc = HarmonicEncoder.from_analyses([bad])  # build_vocabulary tolerates mismatch
        with pytest.raises(HarmonicEncoderError):
            enc.encode(bad)


# ---------------------------------------------------------------------------
# TestEncoderCadencePlacement
# ---------------------------------------------------------------------------


class TestEncoderCadencePlacement:
    def test_cadence_lands_at_final_index(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        # final_index=3 (last chord) β€” position in stream is 3 + BOS offset = 4.
        cad_id = seq.cadence_tokens[4]
        assert cad_id == enc.vocabulary.cadence_id("authentic_perfect")

    def test_non_cadence_timesteps_are_none(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        # Indices 1, 2, 3 (body before PAC) should all be CAD_NONE.
        assert seq.cadence_tokens[1:4] == [CAD_NONE, CAD_NONE, CAD_NONE]

    def test_cadence_out_of_range_is_ignored(self):
        # Mimic a corrupt cadence with final_index pointing past the chord
        # list β€” encoder must silently fall back to NONE rather than crash.
        analysis = {
            "score_id": "corrupt",
            "chords": [_chord(0, "major")],
            "roman_numerals": [_rn("I")],
            "cadences": [_cad("authentic_perfect", 5, 9)],
        }
        enc = HarmonicEncoder.from_analyses([analysis])
        seq = enc.encode(analysis)
        # The single body timestep should be NONE since the cadence index
        # fell outside [0, 1).
        assert seq.cadence_tokens[1] == CAD_NONE


# ---------------------------------------------------------------------------
# TestEncoderUnk
# ---------------------------------------------------------------------------


class TestEncoderUnk:
    def test_unseen_chord_falls_back_to_unk(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        # Encode a brand-new analysis containing Bb:major, which was not in
        # the training set.
        new = {
            "score_id": "new",
            "chords": [_chord(10, "major")],     # Bb:major β€” unseen
            "roman_numerals": [_rn("bVII")],     # bVII β€” unseen
            "cadences": [],
        }
        seq = enc.encode(new)
        # Body timestep = UNK for both streams.
        assert seq.chord_tokens[1] == UNK
        assert seq.rn_tokens[1] == UNK


# ---------------------------------------------------------------------------
# TestEncoderDecode
# ---------------------------------------------------------------------------


class TestEncoderDecode:
    def test_decode_renders_specials_and_body(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        seq = enc.encode(c_major_analysis)
        decoded = enc.decode(seq)
        # Specials at boundaries.
        assert decoded["chord"][0] == "<bos>"
        assert decoded["chord"][-1] == "<eos>"
        # Body chord labels in the right order.
        assert decoded["chord"][1:5] == ["C:major", "F:major", "G:major", "C:major"]
        # Cadence renders CAD_NONE as "<none>" and the final cadence by name.
        assert decoded["cadence"][1] == "<none>"
        assert decoded["cadence"][4] == "authentic_perfect"


# ---------------------------------------------------------------------------
# TestFromAnalyses
# ---------------------------------------------------------------------------


class TestFromAnalyses:
    def test_from_analyses_builds_encoder_in_memory(self, c_major_analysis):
        enc = HarmonicEncoder.from_analyses([c_major_analysis])
        assert enc.vocabulary.chord_vocab_size > 4
        assert enc.vocabulary.rn_vocab_size > 4

    def test_from_analyses_respects_config(self):
        analysis = {
            "score_id": "cfg",
            "chords": [
                _chord(0, "major"), _chord(0, "major"),
                _chord(5, "major"),  # single occurrence β€” filtered
            ],
            "roman_numerals": [_rn("I"), _rn("I"), _rn("IV")],
            "cadences": [],
        }
        enc = HarmonicEncoder.from_analyses(
            [analysis],
            HarmonicEncoderConfig(min_chord_count=2, min_rn_count=1),
        )
        assert "F:major" not in enc.vocabulary.chord_to_id


# ---------------------------------------------------------------------------
# TestEncodeFile
# ---------------------------------------------------------------------------


class TestEncodeFile:
    def test_encode_file_reads_from_disk(self, tmp_path: Path, c_major_analysis):
        path = tmp_path / "toy.json"
        path.write_text(json.dumps(c_major_analysis))
        enc = HarmonicEncoder.from_cache_dir(tmp_path)
        seq = enc.encode_file(path)
        assert seq.score_id == "toy"
        assert seq.length == 6

    def test_from_cache_dir_ignores_manifest(
        self, tmp_path: Path, c_major_analysis
    ):
        (tmp_path / "manifest.json").write_text(json.dumps({
            "totals": {"ok": 1, "failed": 0, "skipped": 0, "total": 1},
            "entries": [],
        }))
        (tmp_path / "toy.json").write_text(json.dumps(c_major_analysis))
        enc = HarmonicEncoder.from_cache_dir(tmp_path)
        # Should have exactly the chord labels from the toy analysis β€” not
        # crash on the manifest.
        assert "C:major" in enc.vocabulary.chord_to_id

    def test_from_cache_dir_missing_raises(self, tmp_path: Path):
        missing = tmp_path / "no-such-dir"
        with pytest.raises(FileNotFoundError):
            HarmonicEncoder.from_cache_dir(missing)


# ---------------------------------------------------------------------------
# TestEncodedSequenceValidation
# ---------------------------------------------------------------------------


class TestEncodedSequenceValidation:
    def test_mismatched_lengths_raise(self):
        with pytest.raises(ValueError):
            EncodedSequence(
                score_id="x",
                chord_tokens=[BOS, EOS],
                rn_tokens=[BOS],                # wrong length
                cadence_tokens=[BOS, EOS],
                onsets=["", ""],
            )

    def test_to_dict_and_from_dict_roundtrip(self):
        seq = EncodedSequence(
            score_id="x",
            chord_tokens=[BOS, 5, EOS],
            rn_tokens=[BOS, 6, EOS],
            cadence_tokens=[BOS, CAD_NONE, EOS],
            onsets=["", "0", ""],
        )
        d = seq.to_dict()
        seq2 = EncodedSequence.from_dict(d)
        assert seq2 == seq