kord 0.8.1

A tool to easily explore music theory principles.
Documentation
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
//! The WASM module.
//!
//! This module contains the WASM wrappers / bindings for the rest of the library.

use js_sys::{Array, Object, Reflect};
use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*};

use crate::core::{
    base::{HasDescription, HasName, HasPreciseName, HasStaticName, Parsable, Res},
    chord::{Chord, Chordable, HasChord, HasExtensions, HasInversion, HasIsCrunchy, HasModifiers, HasRoot, HasScale, HasSlash},
    interval::Interval,
    known_chord::{HasScaleCandidates, ScaleCandidate},
    mode::Mode,
    named_pitch::HasNamedPitch,
    note::{HasPrimaryHarmonicSeries, Note},
    octave::{HasOctave, Octave},
    pitch::HasFrequency,
    scale::Scale,
};

#[cfg(feature = "audio")]
use crate::core::base::PlaybackHandle;

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc<'_> = wee_alloc::WeeAlloc::INIT;

// Helper types.

/// The [`Result`] type for the WASM bindings.
pub type JsRes<T> = Result<T, JsValue>;

// [`Note`] ABI.

/// The [`Note`] wrapper.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct KordNote {
    inner: Note,
}

impl From<Note> for KordNote {
    fn from(note: Note) -> Self {
        KordNote { inner: note }
    }
}

impl From<KordNote> for Note {
    fn from(kord_note: KordNote) -> Self {
        kord_note.inner
    }
}

/// The [`Note`] impl.
#[wasm_bindgen]
impl KordNote {
    /// Creates a new [`Note`] from a frequency.
    #[wasm_bindgen]
    pub fn parse(name: String) -> JsRes<KordNote> {
        Ok(Self { inner: Note::parse(&name).to_js_error()? })
    }

    /// Returns [`Note`]s from audio data.
    #[cfg(feature = "analyze_base")]
    #[wasm_bindgen(js_name = fromAudio)]
    pub fn from_audio(data: &[f32], length_in_seconds: u8) -> JsRes<Array> {
        let notes = Note::try_from_audio(data, length_in_seconds).to_js_error()?.into_iter().map(KordNote::from);

        Ok(notes.into_js_array())
    }

    /// Returns [`Note`]s from audio data using the ML inference algorithm.
    #[cfg(all(feature = "ml_infer", feature = "analyze_base"))]
    #[wasm_bindgen(js_name = fromAudioMl)]
    pub fn from_audio_ml(data: &[f32], length_in_seconds: u8) -> JsRes<Array> {
        let notes = Note::try_from_audio_ml(data, length_in_seconds).to_js_error()?.into_iter().map(KordNote::from);

        Ok(notes.into_js_array())
    }

    /// Returns the [`Note`]'s friendly name.
    #[wasm_bindgen]
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the [`Note`] represented as a string (same as `name`).
    #[allow(clippy::inherent_to_string)]
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.inner.name()
    }

    /// Returns the [`Note`]'s [`NamedPitch`].
    #[wasm_bindgen]
    pub fn pitch(&self) -> String {
        self.inner.named_pitch().static_name().to_string()
    }

    /// Returns the [`Note`]'s [`Octave`].
    #[wasm_bindgen]
    pub fn octave(&self) -> u8 {
        self.inner.octave() as u8
    }

    /// Returns the [`Note`]'s frequency.
    #[wasm_bindgen]
    pub fn frequency(&self) -> f32 {
        self.inner.frequency()
    }

    /// Adds the given interval to the [`Note`], producing a new [`Note`] instance.
    #[wasm_bindgen(js_name = addInterval)]
    pub fn add_interval(&self, interval: Interval) -> KordNote {
        let note = self.inner + interval;

        Self { inner: note }
    }

    /// Subtracts the given interval from the [`Note`], producing a new [`Note`] instance.
    #[wasm_bindgen(js_name = subInterval)]
    pub fn subtract_interval(&self, interval: Interval) -> KordNote {
        let note = self.inner - interval;

        Self { inner: note }
    }

    /// Computes the [`Interval`] distance between the [`Note`] and the given [`Note`].
    #[wasm_bindgen(js_name = distanceTo)]
    pub fn distance_to(&self, other: KordNote) -> Interval {
        self.inner - other.inner
    }

    /// Returns the primary (first 13) harmonic series of the [`Note`].
    #[wasm_bindgen(js_name = harmonicSeries)]
    pub fn harmonic_series(&self) -> Array {
        let series = self.inner.primary_harmonic_series();

        series.into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the clone of the [`Note`].
    #[wasm_bindgen]
    pub fn copy(&self) -> KordNote {
        self.clone()
    }
}

// [`Mode`] ABI.

/// The [`Mode`] wrapper.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct KordMode {
    inner: Mode,
}

impl From<Mode> for KordMode {
    fn from(mode: Mode) -> Self {
        KordMode { inner: mode }
    }
}

impl From<KordMode> for Mode {
    fn from(kord_mode: KordMode) -> Self {
        kord_mode.inner
    }
}

/// The [`Mode`] impl.
#[wasm_bindgen]
impl KordMode {
    /// Creates a new [`Mode`] by parsing a string (e.g., "C dorian", "D lydian dominant").
    #[wasm_bindgen]
    pub fn parse(name: String) -> JsRes<KordMode> {
        Ok(Self { inner: Mode::parse(&name).to_js_error()? })
    }

    /// Returns the [`Mode`]'s friendly name.
    #[wasm_bindgen]
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the [`Mode`]'s precise name.
    #[wasm_bindgen(js_name = preciseName)]
    pub fn precise_name(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Mode`] as a string (same as `precise_name`).
    #[allow(clippy::inherent_to_string)]
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Mode`]'s description.
    #[wasm_bindgen]
    pub fn description(&self) -> String {
        self.inner.description().to_string()
    }

    /// Returns the [`Mode`]'s root note.
    #[wasm_bindgen]
    pub fn root(&self) -> String {
        self.inner.root().name()
    }

    /// Returns the [`Mode`]'s notes.
    #[wasm_bindgen]
    pub fn notes(&self) -> Array {
        self.inner.notes().into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the [`Mode`]'s notes as a string.
    #[wasm_bindgen(js_name = notesString)]
    pub fn notes_string(&self) -> String {
        self.inner.notes().iter().map(|n| n.name()).collect::<Vec<_>>().join(" ")
    }

    /// Returns the clone of the [`Mode`].
    #[wasm_bindgen]
    pub fn copy(&self) -> KordMode {
        self.clone()
    }
}

// [`Scale`] ABI.

/// The [`Scale`] wrapper.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct KordScale {
    inner: Scale,
}

impl From<Scale> for KordScale {
    fn from(scale: Scale) -> Self {
        KordScale { inner: scale }
    }
}

impl From<KordScale> for Scale {
    fn from(kord_scale: KordScale) -> Self {
        kord_scale.inner
    }
}

/// The [`Scale`] impl.
#[wasm_bindgen]
impl KordScale {
    /// Creates a new [`Scale`] by parsing a string (e.g., "C major", "A harmonic minor", "E blues").
    #[wasm_bindgen]
    pub fn parse(name: String) -> JsRes<KordScale> {
        Ok(Self {
            inner: Scale::parse(&name).to_js_error()?,
        })
    }

    /// Returns the [`Scale`]'s friendly name.
    #[wasm_bindgen]
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the [`Scale`]'s precise name.
    #[wasm_bindgen(js_name = preciseName)]
    pub fn precise_name(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Scale`] as a string (same as `precise_name`).
    #[allow(clippy::inherent_to_string)]
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Scale`]'s description.
    #[wasm_bindgen]
    pub fn description(&self) -> String {
        self.inner.description().to_string()
    }

    /// Returns the [`Scale`]'s root note.
    #[wasm_bindgen]
    pub fn root(&self) -> String {
        self.inner.root().name()
    }

    /// Returns the [`Scale`]'s notes.
    #[wasm_bindgen]
    pub fn notes(&self) -> Array {
        self.inner.notes().into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the [`Scale`]'s notes as a string.
    #[wasm_bindgen(js_name = notesString)]
    pub fn notes_string(&self) -> String {
        self.inner.notes().iter().map(|n| n.name()).collect::<Vec<_>>().join(" ")
    }

    /// Returns the clone of the [`Scale`].
    #[wasm_bindgen]
    pub fn copy(&self) -> KordScale {
        self.clone()
    }
}

// [`ScaleCandidate`] ABI.

/// The [`ScaleCandidate`] wrapper for WASM.
/// Represents a recommended scale or mode for a chord.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct KordScaleCandidate {
    inner: ScaleCandidate,
    root: Note,
}

impl KordScaleCandidate {
    /// Creates a new [`KordScaleCandidate`] from a [`ScaleCandidate`] and root note.
    fn new(candidate: ScaleCandidate, root: Note) -> Self {
        KordScaleCandidate { inner: candidate, root }
    }
}

/// The [`ScaleCandidate`] impl.
#[wasm_bindgen]
impl KordScaleCandidate {
    /// Returns the candidate's rank (1 = most relevant).
    #[wasm_bindgen]
    pub fn rank(&self) -> u8 {
        self.inner.rank()
    }

    /// Returns the reason why this scale/mode fits the chord.
    #[wasm_bindgen]
    pub fn reason(&self) -> String {
        self.inner.reason().to_string()
    }

    /// Returns the name of the scale or mode.
    #[wasm_bindgen]
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the description of the scale or mode.
    #[wasm_bindgen]
    pub fn description(&self) -> String {
        self.inner.description().to_string()
    }

    /// Returns the notes of this scale/mode rooted at the chord's root.
    #[wasm_bindgen]
    pub fn notes(&self) -> Array {
        self.inner.notes(self.root).into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the notes as a space-separated string.
    #[wasm_bindgen(js_name = notesString)]
    pub fn notes_string(&self) -> String {
        self.inner.notes(self.root).iter().map(|n| n.name()).collect::<Vec<_>>().join(" ")
    }

    /// Returns whether this is a mode candidate (vs. scale candidate).
    #[wasm_bindgen(js_name = isMode)]
    pub fn is_mode(&self) -> bool {
        matches!(self.inner, ScaleCandidate::Mode { .. })
    }

    /// Returns whether this is a scale candidate (vs. mode candidate).
    #[wasm_bindgen(js_name = isScale)]
    pub fn is_scale(&self) -> bool {
        matches!(self.inner, ScaleCandidate::Scale { .. })
    }

    /// Returns the clone of the [`KordScaleCandidate`].
    #[wasm_bindgen]
    pub fn copy(&self) -> KordScaleCandidate {
        self.clone()
    }
}

// [`Chord`] ABI.

/// The [`Chord`] wrapper.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct KordChord {
    inner: Chord,
}

impl From<Chord> for KordChord {
    fn from(chord: Chord) -> Self {
        KordChord { inner: chord }
    }
}

impl From<KordChord> for Chord {
    fn from(kord_chord: KordChord) -> Self {
        kord_chord.inner
    }
}

/// The [`Chord`] impl.
#[wasm_bindgen]
impl KordChord {
    /// Creates a new [`Chord`] from a frequency.
    #[wasm_bindgen]
    pub fn parse(name: String) -> JsRes<KordChord> {
        Ok(Self {
            inner: Chord::parse(&name).to_js_error()?,
        })
    }

    /// Creates a new [`Chord`] from a set of [`Note`]s.
    ///
    /// The [`Note`]s should be represented as a space-separated string.
    /// E.g., `C E G`.
    #[wasm_bindgen(js_name = fromNotesString)]
    pub fn from_notes_string(notes: String) -> JsRes<Array> {
        let notes = notes.split_whitespace().map(|note| Note::parse(note).to_js_error()).collect::<JsRes<Vec<Note>>>()?;

        let candidates = Chord::try_from_notes(&notes).to_js_error()?.into_iter().map(KordChord::from);

        Ok(candidates.into_js_array())
    }

    /// Creates a new [`Chord`] from a set of [`Note`]s.
    #[wasm_bindgen(js_name = fromNotes)]
    pub fn from_notes(notes: Array) -> JsRes<Array> {
        let notes: Vec<Note> = notes.cloned_into_vec_inner::<KordNote, Note>()?;

        let candidates = Chord::try_from_notes(&notes).to_js_error()?.into_iter().map(KordChord::from);

        Ok(candidates.into_js_array())
    }

    /// Returns the [`Chord`]'s friendly name.
    #[wasm_bindgen]
    pub fn name(&self) -> String {
        self.inner.name()
    }

    /// Returns the [`Chord`]'s precise name.
    #[wasm_bindgen(js_name = preciseName)]
    pub fn precise_name(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Chord`] as a string (same as `precise_name`).
    #[allow(clippy::inherent_to_string)]
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.inner.precise_name()
    }

    /// Returns the [`Chord`]'s description.
    #[wasm_bindgen]
    pub fn description(&self) -> String {
        self.inner.description().to_string()
    }

    /// Returns the [`Chord`]'s display text.
    #[wasm_bindgen]
    pub fn display(&self) -> String {
        self.inner.to_string()
    }

    /// Returns the [`Chord`]'s root note.
    #[wasm_bindgen]
    pub fn root(&self) -> String {
        self.inner.root().name()
    }

    /// Returns the [`Chord`]'s slash note.
    #[wasm_bindgen]
    pub fn slash(&self) -> String {
        self.inner.slash().name()
    }

    /// Returns the [`Chord`]'s inversion.
    #[wasm_bindgen]
    pub fn inversion(&self) -> u8 {
        self.inner.inversion()
    }

    /// Returns whether or not the [`Chord`] is "crunchy".
    #[wasm_bindgen(js_name = isCrunchy)]
    pub fn is_crunchy(&self) -> bool {
        self.inner.is_crunchy()
    }

    /// Returns the [`Chord`]'s chord tones.
    #[wasm_bindgen]
    pub fn chord(&self) -> Array {
        self.inner.chord().into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the [`Chord`]'s chord tones as a string.
    #[wasm_bindgen(js_name = chordString)]
    pub fn chord_string(&self) -> String {
        self.inner.chord().iter().map(|n| n.name()).collect::<Vec<_>>().join(" ")
    }

    /// Returns the [`Chord`]'s scale tones.
    #[wasm_bindgen]
    pub fn scale(&self) -> Array {
        self.inner.scale().into_iter().map(KordNote::from).into_js_array()
    }

    /// Returns the [`Chord`]'s scale tones as a string.
    #[wasm_bindgen(js_name = scaleString)]
    pub fn scale_string(&self) -> String {
        self.inner.scale().iter().map(|n| n.name()).collect::<Vec<_>>().join(" ")
    }

    /// Returns the recommended scale/mode candidates for this chord.
    /// The candidates are ranked by relevance (rank 1 = most relevant).
    #[wasm_bindgen(js_name = scaleCandidates)]
    pub fn scale_candidates(&self) -> Array {
        let root = self.inner.root();
        self.inner.scale_candidates().into_iter().map(|candidate| KordScaleCandidate::new(candidate, root)).into_js_array()
    }

    /// Returns the [`Chord`]'s modifiers.
    #[wasm_bindgen]
    pub fn modifiers(&self) -> Array {
        self.inner.modifiers().iter().map(|m| m.static_name()).into_js_array()
    }

    /// Returns the [`Chord`]'s extensions.
    #[wasm_bindgen]
    pub fn extensions(&self) -> Array {
        self.inner.extensions().iter().map(|e| e.static_name()).into_js_array()
    }

    /// Returns a new [`Chord`] with the inversion set to the provided value.
    #[wasm_bindgen(js_name = withInversion)]
    pub fn with_inversion(&self, inversion: u8) -> Self {
        KordChord {
            inner: self.inner.clone().with_inversion(inversion),
        }
    }

    /// Returns a new [`Chord`] with the slash set to the provided value.
    #[wasm_bindgen(js_name = withSlash)]
    pub fn with_slash(&self, slash: &KordNote) -> Self {
        KordChord {
            inner: self.inner.clone().with_slash(slash.inner),
        }
    }

    /// Returns a new [`Chord`] with the octave of the root set to the provided value.
    #[wasm_bindgen(js_name = withOctave)]
    pub fn with_octave(&self, octave: u8) -> JsRes<KordChord> {
        Ok(KordChord {
            inner: self.inner.clone().with_octave(Octave::try_from(octave)?),
        })
    }

    /// Returns a new [`Chord`] with the "crunchiness" set to the provided value.
    #[wasm_bindgen(js_name = withCrunchy)]
    pub fn with_crunchy(&self, is_crunchy: bool) -> Self {
        KordChord {
            inner: self.inner.clone().with_crunchy(is_crunchy),
        }
    }

    /// Plays the [`Chord`].
    #[wasm_bindgen]
    #[cfg(feature = "audio")]
    pub async fn play(&self, delay: f32, length: f32, fade_in: f32) -> JsRes<()> {
        use crate::core::base::Playable;
        use anyhow::Context;
        use gloo_timers::future::TimeoutFuture;
        use std::time::Duration;

        let delay = Duration::from_secs_f32(delay);
        let length = Duration::from_secs_f32(length);
        let fade_in = Duration::from_secs_f32(fade_in);

        let _handle = self.inner.play(delay, length, fade_in).context("Could not start the playback.").to_js_error()?;

        TimeoutFuture::new(length.as_millis() as u32).await;

        Ok(())
    }

    /// Returns the clone of the [`Chord`].
    #[wasm_bindgen]
    pub fn copy(&self) -> KordChord {
        self.clone()
    }
}

// Playback handle.

/// A handle to a [`Chord`] playback.
///
/// Should be dropped to stop the playback, or after playback is finished.
#[cfg(feature = "audio")]
#[wasm_bindgen]
pub struct KordPlaybackHandle {
    _inner: PlaybackHandle,
}

// The modifiers.

/// The chord modifiers.
#[derive(Clone, Debug)]
#[wasm_bindgen]
pub enum KordModifier {
    /// Minor modifier.
    Minor,

    /// Flat 5 modifier.
    Flat5,
    /// Sharp 5 modifier.
    Augmented5,

    /// Major 7 modifier.
    Major7,
    /// Dominant 7 modifier.
    Dominant7,
    /// Dominant 9 modifier.
    Dominant9,
    /// Dominant 11 modifier.
    Dominant11,
    /// Dominant 13 modifier.
    Dominant13,

    /// Flat 9 modifier.
    Flat9,
    /// Sharp 9 modifier.
    Sharp9,

    /// Sharp 11 modifier.
    Sharp11,

    /// Diminished modifier.
    Diminished,
}

// Helpers.

/// Helper trait for converting errors to [`JsValue`]s.
trait ToJsError<T> {
    /// Converts the error to a [`JsValue`].
    fn to_js_error(self) -> JsRes<T>;
}

impl<T> ToJsError<T> for Res<T> {
    fn to_js_error(self) -> JsRes<T> {
        self.map_err(|e| JsValue::from_str(&e.to_string()))
    }
}

/// Helper trait for converting a [`IntoIterator<Item = T>`] (where `T: Into<JsValue>`) to an [`Array`].
trait IntoJsArray {
    /// Converts the [`Vec`] to an [`Array`].
    fn into_js_array(self) -> Array;
}

impl<I, T> IntoJsArray for I
where
    I: IntoIterator<Item = T>,
    T: Into<JsValue>,
{
    fn into_js_array(self) -> Array {
        Array::from_iter(self.into_iter().map(Into::into))
    }
}

/// Helpers trait for converting an [`Array`] to a [`Vec`].
#[allow(dead_code)]
trait ClonedIntoVec {
    /// Converts the [`Array`] to a [`Vec<T>`].
    fn cloned_into_vec<T>(self) -> JsRes<Vec<T>>
    where
        T: RefFromJsValue + RefFromWasmAbi + Clone;
}

impl ClonedIntoVec for Array {
    fn cloned_into_vec<T>(self) -> JsRes<Vec<T>>
    where
        T: RefFromJsValue + RefFromWasmAbi + Clone,
    {
        let mut result = Vec::with_capacity(self.length() as usize);

        for k in 0..self.length() {
            let value = self.get(k);
            let value = T::ref_from_js_value(&value)?.clone();

            result.push(value);
        }

        Ok(result)
    }
}

/// Helper trait for converting a [`Array`] (where `T: JsCast`) to a [`Vec`].
trait ClonedIntoVecInner {
    /// Converts the [`Array`] to a [`Vec<I>`] (where `I` is the wrapped type, first casting the [`JsValue`] into `T`).
    fn cloned_into_vec_inner<T, I>(self) -> JsRes<Vec<I>>
    where
        T: RefFromJsValue + RefFromWasmAbi + Clone,
        I: From<T>;
}

impl ClonedIntoVecInner for Array {
    fn cloned_into_vec_inner<T, I>(self) -> JsRes<Vec<I>>
    where
        T: RefFromJsValue + RefFromWasmAbi + Clone,
        I: From<T>,
    {
        let mut result = Vec::with_capacity(self.length() as usize);

        for k in 0..self.length() {
            let value = self.get(k);
            let value = T::ref_from_js_value(&value)?.clone();
            let value = I::from(value);

            result.push(value);
        }

        Ok(result)
    }
}

/// Helper trait for converting a [`JsValue`] representing a shared pointer (e.g., `{ ptr: XXX }`)
/// into a type.
trait RefFromJsValue {
    /// Converts the [`JsValue`] into a type.
    fn ref_from_js_value(abi: &JsValue) -> JsRes<Self::Anchor>
    where
        Self: Sized + RefFromWasmAbi;
}

/// Extracts a u32 pointer value from a JsValue by trying multiple conversion strategies.
///
/// This handles different pointer representations that can occur across JavaScript environments:
/// - JavaScript Number (most common, works with as_f64())
/// - JavaScript BigInt (converted to string, then parsed)
/// - Edge cases like boolean values for 0/1 pointers
fn extract_ptr_from_js_value(ptr_value: &JsValue) -> JsRes<u32> {
    if let Some(f) = ptr_value.as_f64() {
        Ok(f as u32)
    } else if let Some(b) = ptr_value.as_bool() {
        // Sometimes happens with 0/1 pointers
        Ok(b as u32)
    } else {
        // Try parsing as string (handles BigInt case)
        ptr_value
            .as_string()
            .and_then(|s| s.parse::<u32>().ok())
            .ok_or_else(|| JsValue::from_str("Could not cast pointer to u32 from any supported type."))
    }
}

impl RefFromJsValue for KordNote {
    fn ref_from_js_value(abi: &JsValue) -> JsRes<<KordNote as RefFromWasmAbi>::Anchor>
    where
        Self: Sized + RefFromWasmAbi,
    {
        let ptr_value = Reflect::get(abi, &JsValue::from_str("ptr"))?;
        let ptr = extract_ptr_from_js_value(&ptr_value)?;

        let object = abi.dyn_ref::<Object>().ok_or("Value is not an object.")?;
        if object.constructor().name() != "KordNote" {
            return Err("Invalid object type.".into());
        }

        // SAFETY: We have done as much as we can to ensure that this is as safe as it can
        // be, considering the inherent unsafety of working with an ABI.
        //
        // We have confirmed that the JsValue is, indeed, an Object, and that
        // it is of the proper type.
        let value = unsafe { KordNote::ref_from_abi(ptr) };

        Ok(value)
    }
}

impl RefFromJsValue for KordChord {
    fn ref_from_js_value(abi: &JsValue) -> JsRes<<KordChord as RefFromWasmAbi>::Anchor>
    where
        Self: Sized + RefFromWasmAbi,
    {
        let ptr_value = Reflect::get(abi, &JsValue::from_str("ptr"))?;
        let ptr = extract_ptr_from_js_value(&ptr_value)?;

        let object = abi.dyn_ref::<Object>().ok_or("Value is not an object.")?;
        if object.constructor().name() != "KordChord" {
            return Err("Invalid object type.".into());
        }

        // SAFETY: We have done as much as we can to ensure that this is as safe as it can
        // be, considering the inherent unsafety of working with an ABI.
        //
        // We have confirmed that the JsValue is, indeed, an Object, and that
        // it is of the proper type.
        let value = unsafe { KordChord::ref_from_abi(ptr) };

        Ok(value)
    }
}

// JS helpers.

// #[wasm_bindgen(inline_js = "export function sleep(millis) { return new Promise(resolve => setTimeout(() => resolve(), millis)); }")]
// extern "C" {
//     fn sleep(millis: u32) -> Promise;
// }

// The [`Chord`] modifier / extension impl.
#[wasm_bindgen]
impl KordChord {
    /// Returns a new [`Chord`] with the `minor` modifier.
    #[wasm_bindgen]
    pub fn minor(&self) -> Self {
        KordChord { inner: self.inner.clone().minor() }
    }

    /// Returns a new [`Chord`] with the `flat5` modifier.
    #[wasm_bindgen]
    pub fn flat5(&self) -> Self {
        KordChord { inner: self.inner.clone().flat5() }
    }

    /// Returns a new [`Chord`] with the `augmented` modifier.
    #[wasm_bindgen]
    pub fn aug(&self) -> Self {
        KordChord { inner: self.inner.clone().aug() }
    }

    /// Returns a new [`Chord`] with the `maj7` modifier.
    #[wasm_bindgen]
    pub fn maj7(&self) -> Self {
        KordChord { inner: self.inner.clone().maj7() }
    }

    /// Returns a new [`Chord`] with the `dom7` modifier.
    #[wasm_bindgen]
    pub fn seven(&self) -> Self {
        KordChord { inner: self.inner.clone().dominant7() }
    }

    /// Returns a new [`Chord`] with the `dom9` modifier.
    #[wasm_bindgen]
    pub fn nine(&self) -> Self {
        KordChord { inner: self.inner.clone().dominant9() }
    }

    /// Returns a new [`Chord`] with the `dom11` modifier.
    #[wasm_bindgen]
    pub fn eleven(&self) -> Self {
        KordChord { inner: self.inner.clone().dominant11() }
    }

    /// Returns a new [`Chord`] with the `dom13` modifier.
    #[wasm_bindgen]
    pub fn thirteen(&self) -> Self {
        KordChord { inner: self.inner.clone().dominant13() }
    }

    /// Returns a new [`Chord`] with the `flat9` modifier.
    #[wasm_bindgen]
    pub fn flat9(&self) -> Self {
        KordChord { inner: self.inner.clone().flat9() }
    }

    /// Returns a new [`Chord`] with the `sharp9` modifier.
    #[wasm_bindgen]
    pub fn sharp9(&self) -> Self {
        KordChord { inner: self.inner.clone().sharp9() }
    }

    /// Returns a new [`Chord`] with the `sharp11` modifier.
    #[wasm_bindgen]
    pub fn sharp11(&self) -> Self {
        KordChord { inner: self.inner.clone().sharp11() }
    }

    /// Returns a new [`Chord`] with the `dim` modifier.
    #[wasm_bindgen]
    pub fn dim(&self) -> Self {
        KordChord { inner: self.inner.clone().dim() }
    }

    /// Returns a new [`Chord`] with the `halfDim` modifier.
    #[wasm_bindgen(js_name = halfDim)]
    pub fn half_dim(&self) -> Self {
        KordChord { inner: self.inner.clone().half_dim() }
    }

    /// Returns a new [`Chord`] with the `sus2` extension.
    #[wasm_bindgen]
    pub fn sus2(&self) -> Self {
        KordChord { inner: self.inner.clone().sus2() }
    }

    /// Returns a new [`Chord`] with the `sus4` extension.
    #[wasm_bindgen]
    pub fn sus4(&self) -> Self {
        KordChord { inner: self.inner.clone().sus4() }
    }

    /// Returns a new [`Chord`] with the `flat11` extension.
    #[wasm_bindgen]
    pub fn flat11(&self) -> Self {
        KordChord { inner: self.inner.clone().flat11() }
    }

    /// Returns a new [`Chord`] with the `flat13` extension.
    #[wasm_bindgen]
    pub fn flat13(&self) -> Self {
        KordChord { inner: self.inner.clone().flat13() }
    }

    /// Returns a new [`Chord`] with the `sharp13` extension.
    #[wasm_bindgen]
    pub fn sharp13(&self) -> Self {
        KordChord { inner: self.inner.clone().sharp13() }
    }

    /// Returns a new [`Chord`] with the `add2` extension.
    #[wasm_bindgen]
    pub fn add2(&self) -> Self {
        KordChord { inner: self.inner.clone().add2() }
    }

    /// Returns a new [`Chord`] with the `add4` extension.
    #[wasm_bindgen]
    pub fn add4(&self) -> Self {
        KordChord { inner: self.inner.clone().add4() }
    }

    /// Returns a new [`Chord`] with the `add6` extension.
    #[wasm_bindgen]
    pub fn add6(&self) -> Self {
        KordChord { inner: self.inner.clone().add6() }
    }

    /// Returns a new [`Chord`] with the `add9` extension.
    #[wasm_bindgen]
    pub fn add9(&self) -> Self {
        KordChord { inner: self.inner.clone().add9() }
    }

    /// Returns a new [`Chord`] with the `add11` extension.
    #[wasm_bindgen]
    pub fn add11(&self) -> Self {
        KordChord { inner: self.inner.clone().add11() }
    }

    /// Returns a new [`Chord`] with the `add13` extension.
    #[wasm_bindgen]
    pub fn add13(&self) -> Self {
        KordChord { inner: self.inner.clone().add13() }
    }
}

#[cfg(all(test, target_arch = "wasm32"))]
mod tests {
    use super::*;
    use wasm_bindgen::JsValue;
    use wasm_bindgen_test::*;

    wasm_bindgen_test_configure!(run_in_browser);

    /// Test the pointer extraction function directly with different types
    /// This validates the fix for Issue #21 - handling different pointer representations
    #[wasm_bindgen_test]
    fn test_ptr_extraction_strategies() {
        // Test with f64 (Number - most common case in most browsers)
        let f64_val = JsValue::from_f64(12345.0);
        let result = extract_ptr_from_js_value(&f64_val);
        assert!(result.is_ok(), "Should extract f64 as u32");
        assert_eq!(result.unwrap(), 12345u32);

        // Test with string (BigInt case - the root cause of Issue #21)
        // Some JavaScript engines represent WASM pointers as BigInt, which converts to string
        let string_val = JsValue::from_str("67890");
        let result = extract_ptr_from_js_value(&string_val);
        assert!(result.is_ok(), "Should extract string (BigInt) as u32");
        assert_eq!(result.unwrap(), 67890u32);

        // Test with bool (0/1 edge case - seen in some edge conditions)
        let bool_val = JsValue::from_bool(true);
        let result = extract_ptr_from_js_value(&bool_val);
        assert!(result.is_ok(), "Should extract bool as u32");
        assert_eq!(result.unwrap(), 1u32);

        let bool_val_false = JsValue::from_bool(false);
        let result = extract_ptr_from_js_value(&bool_val_false);
        assert!(result.is_ok(), "Should extract false as u32");
        assert_eq!(result.unwrap(), 0u32);
    }

    // KordNote tests

    #[wasm_bindgen_test]
    fn test_note_parse_valid() {
        let note = KordNote::parse("C4".to_string());
        assert!(note.is_ok(), "Should parse valid note");

        let note = note.unwrap();
        assert_eq!(note.name(), "C4");
        assert_eq!(note.pitch(), "C");
        assert_eq!(note.octave(), 4);
    }

    #[wasm_bindgen_test]
    fn test_note_parse_invalid() {
        let note = KordNote::parse("Invalid".to_string());
        assert!(note.is_err(), "Should fail to parse invalid note");
    }

    #[wasm_bindgen_test]
    fn test_note_parse_with_sharp() {
        let note = KordNote::parse("F#5".to_string()).unwrap();
        assert_eq!(note.name(), "F♯5");
        assert_eq!(note.pitch(), "F♯");
        assert_eq!(note.octave(), 5);
    }

    #[wasm_bindgen_test]
    fn test_note_parse_with_flat() {
        let note = KordNote::parse("Bb3".to_string()).unwrap();
        assert_eq!(note.name(), "Bâ™­3");
        assert_eq!(note.pitch(), "Bâ™­");
        assert_eq!(note.octave(), 3);
    }

    #[wasm_bindgen_test]
    fn test_note_frequency() {
        let note = KordNote::parse("A4".to_string()).unwrap();
        let freq = note.frequency();
        // A4 is 440 Hz
        assert!((freq - 440.0).abs() < 0.1, "A4 should be approximately 440 Hz");
    }

    #[wasm_bindgen_test]
    fn test_note_to_string() {
        let note = KordNote::parse("D5".to_string()).unwrap();
        assert_eq!(note.to_string(), "D5");
    }

    #[wasm_bindgen_test]
    fn test_note_add_interval() {
        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let e4 = c4.add_interval(Interval::MajorThird);
        assert_eq!(e4.name(), "E4");
    }

    #[wasm_bindgen_test]
    fn test_note_subtract_interval() {
        let e4 = KordNote::parse("E4".to_string()).unwrap();
        let c4 = e4.subtract_interval(Interval::MajorThird);
        assert_eq!(c4.name(), "C4");
    }

    #[wasm_bindgen_test]
    fn test_note_distance_to() {
        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let e4 = KordNote::parse("E4".to_string()).unwrap();
        let interval = c4.distance_to(e4);
        assert_eq!(interval, Interval::MajorThird);
    }

    #[wasm_bindgen_test]
    fn test_note_harmonic_series() {
        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let harmonics = c4.harmonic_series();
        assert_eq!(harmonics.length(), 13, "Should return 13 harmonics");

        // Just verify we can get elements from the array
        assert!(!harmonics.get(0).is_undefined(), "First harmonic should exist");
    }

    #[wasm_bindgen_test]
    fn test_note_copy() {
        let note = KordNote::parse("G4".to_string()).unwrap();
        let copy = note.copy();
        assert_eq!(copy.name(), note.name());
        assert_eq!(copy.octave(), note.octave());
    }

    // KordChord tests

    #[wasm_bindgen_test]
    fn test_chord_parse_simple() {
        let chord = KordChord::parse("C".to_string());
        assert!(chord.is_ok(), "Should parse simple chord");

        let chord = chord.unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        assert_eq!(chord.name(), "C");
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_major_seventh() {
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        assert!(chord.name().contains("maj7") || chord.display().contains("maj7"));
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_minor() {
        let chord = KordChord::parse("Cm".to_string()).unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        assert!(chord.name().contains("m") || chord.name().contains("minor"));
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_dominant_seventh() {
        let chord = KordChord::parse("C7".to_string()).unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        assert!(chord.display().contains("7"));
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_with_slash() {
        let chord = KordChord::parse("C/E".to_string()).unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        assert!(chord.slash().starts_with("E"), "Slash should start with E");
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_complex() {
        let chord = KordChord::parse("Cm7b5".to_string()).unwrap();
        assert!(chord.root().starts_with("C"), "Root should start with C");
        // Half-diminished chord
        assert!(chord.display().contains("m7") || chord.display().contains("ø"));
    }

    #[wasm_bindgen_test]
    fn test_chord_parse_invalid() {
        let chord = KordChord::parse("NotAChord".to_string());
        assert!(chord.is_err(), "Should fail to parse invalid chord");
    }

    #[wasm_bindgen_test]
    fn test_chord_from_notes_string() {
        let result = KordChord::from_notes_string("C E G".to_string());
        assert!(result.is_ok(), "Should create chords from note string");

        let chords = result.unwrap();
        assert!(chords.length() > 0, "Should return at least one chord candidate");

        // Just verify we can access the first element
        let first = chords.get(0);
        assert!(!first.is_undefined(), "First chord should exist");
    }

    #[wasm_bindgen_test]
    fn test_chord_from_notes_string_invalid() {
        let result = KordChord::from_notes_string("X Y Z".to_string());
        assert!(result.is_err(), "Should fail with invalid note names");
    }

    #[wasm_bindgen_test]
    fn test_chord_chord_and_scale() {
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();

        let chord_notes = chord.chord();
        assert!(chord_notes.length() >= 4, "Major 7th should have at least 4 notes");

        let scale_notes = chord.scale();
        assert!(scale_notes.length() >= 7, "Scale should have at least 7 notes");
    }

    #[wasm_bindgen_test]
    fn test_chord_string_representations() {
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();

        let chord_string = chord.chord_string();
        assert!(chord_string.contains("C"), "Chord string should contain root");

        let scale_string = chord.scale_string();
        assert!(scale_string.contains("C"), "Scale string should contain root");
    }

    #[wasm_bindgen_test]
    fn test_chord_modifiers_and_extensions() {
        let chord = KordChord::parse("Cm7b5".to_string()).unwrap();

        let modifiers = chord.modifiers();
        // Cm7b5 (half-diminished) has: minor, dominant 7, flat 5
        assert!(modifiers.length() >= 3, "Cm7b5 should have at least 3 modifiers");

        let extensions = chord.extensions();
        // Half-diminished chord has no extensions (only modifiers)
        assert_eq!(extensions.length(), 0, "Cm7b5 should have no extensions");
    }

    #[wasm_bindgen_test]
    fn test_chord_precise_name_and_description() {
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();

        let precise_name = chord.precise_name();
        assert!(!precise_name.is_empty(), "Should have precise name");

        let description = chord.description();
        assert!(!description.is_empty(), "Should have description");

        let display = chord.display();
        assert!(!display.is_empty(), "Should have display text");
    }

    #[wasm_bindgen_test]
    fn test_chord_inversion() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        assert_eq!(chord.inversion(), 0, "Root position should be 0");

        let inverted = chord.with_inversion(1);
        assert_eq!(inverted.inversion(), 1, "First inversion should be 1");
    }

    #[wasm_bindgen_test]
    fn test_chord_with_slash() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let e_note = KordNote::parse("E4".to_string()).unwrap();

        let with_slash = chord.with_slash(&e_note);
        assert_eq!(with_slash.slash(), "E4");
    }

    #[wasm_bindgen_test]
    fn test_chord_with_octave() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let result = chord.with_octave(5);
        assert!(result.is_ok(), "Should set octave successfully");
    }

    #[wasm_bindgen_test]
    fn test_chord_with_octave_invalid() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let result = chord.with_octave(20); // Invalid octave
        assert!(result.is_err(), "Should fail with invalid octave");
    }

    #[wasm_bindgen_test]
    fn test_chord_is_crunchy() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let _is_crunchy = chord.is_crunchy(); // Just verify it doesn't panic

        let with_crunchy = chord.with_crunchy(true);
        assert!(with_crunchy.is_crunchy(), "Should be crunchy after setting");
    }

    #[wasm_bindgen_test]
    fn test_chord_copy() {
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();
        let copy = chord.copy();
        assert_eq!(copy.root(), chord.root());
        assert_eq!(copy.name(), chord.name());
    }

    // Chord modifier/extension builder tests

    #[wasm_bindgen_test]
    fn test_chord_builder_minor() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let minor = chord.minor();
        assert!(minor.name().contains("m") || minor.name().contains("minor"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_flat5() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let flat5 = chord.flat5();
        // Verify display shows the flat 5
        let display = flat5.display();
        assert!(display.contains("â™­5") || display.contains("b5"), "Flat5 chord should show â™­5 in display: {}", display);
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_aug() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let aug = chord.aug();
        // Augmented chord has sharp 5
        let display = aug.display();
        assert!(display.contains("aug") || display.contains("+") || display.contains("#5"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_maj7() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let maj7 = chord.maj7();
        assert!(maj7.display().contains("maj7") || maj7.display().contains("M7"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_seven() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let seven = chord.seven();
        assert!(seven.display().contains("7"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_nine() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let nine = chord.nine();
        assert!(nine.display().contains("9"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_eleven() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let eleven = chord.eleven();
        assert!(eleven.display().contains("11"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_thirteen() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let thirteen = chord.thirteen();
        assert!(thirteen.display().contains("13"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_altered_notes() {
        let chord = KordChord::parse("C7".to_string()).unwrap();

        let flat9 = chord.flat9();
        assert!(flat9.display().contains("â™­9") || flat9.display().contains("b9"));

        let sharp9 = chord.sharp9();
        assert!(sharp9.display().contains("♯9") || sharp9.display().contains("#9"));

        let sharp11 = chord.sharp11();
        assert!(sharp11.display().contains("♯11") || sharp11.display().contains("#11"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_dim() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let dim = chord.dim();
        assert!(dim.display().contains("dim") || dim.display().contains("°"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_half_dim() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let half_dim = chord.half_dim();
        // Half-diminished creates Cm7b5, verify it contains both minor and flat 5
        let display = half_dim.display();
        assert!(
            display.contains("m") && (display.contains("b5") || display.contains("♭5") || display.contains("ø")),
            "Half-dim should show minor with flat 5: {}",
            display
        );
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_sus() {
        let chord = KordChord::parse("C".to_string()).unwrap();

        let sus2 = chord.sus2();
        assert!(sus2.display().contains("sus2"));

        let sus4 = chord.sus4();
        assert!(sus4.display().contains("sus4"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_add_extensions() {
        let chord = KordChord::parse("C".to_string()).unwrap();

        let add2 = chord.add2();
        assert!(add2.display().contains("add2") || add2.display().contains("add9"));

        let add4 = chord.add4();
        assert!(add4.display().contains("add4") || add4.display().contains("add11"));

        let add6 = chord.add6();
        assert!(add6.display().contains("add6") || add6.display().contains("6"));

        let add9 = chord.add9();
        assert!(add9.display().contains("add9"));

        let add11 = chord.add11();
        assert!(add11.display().contains("add11"));

        let add13 = chord.add13();
        assert!(add13.display().contains("add13"));
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_flat_extensions() {
        let chord = KordChord::parse("C".to_string()).unwrap();

        let flat11 = chord.flat11();
        let display = flat11.display();
        assert!(display.contains("â™­11") || display.contains("b11") || display.contains("11"), "Should show 11: {}", display);

        let flat13 = chord.flat13();
        let display = flat13.display();
        assert!(display.contains("â™­13") || display.contains("b13") || display.contains("13"), "Should show 13: {}", display);
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_sharp13() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let sharp13 = chord.sharp13();
        let extensions = sharp13.extensions();
        assert_eq!(extensions.length(), 1, "Should have exactly 1 extension (sharp13)");
    }

    #[wasm_bindgen_test]
    fn test_chord_builder_chaining() {
        // Test that we can chain multiple modifiers
        let chord = KordChord::parse("C".to_string()).unwrap();
        let complex = chord.minor().seven().flat9();

        assert!(complex.display().contains("m"), "Should be minor");
        assert!(complex.display().contains("7"), "Should have dominant 7");
    }

    // Helper conversion tests

    #[wasm_bindgen_test]
    fn test_js_array_conversion() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let notes = chord.chord();

        // C major triad has exactly 3 notes
        assert_eq!(notes.length(), 3, "C major should have 3 notes");

        // Test that we can get individual elements
        let first = notes.get(0);
        assert!(!first.is_undefined(), "First element should exist");
    }

    // ABI Compatibility Tests

    #[wasm_bindgen_test]
    fn test_abi_string_encoding_basic() {
        // Test basic ASCII strings pass through correctly
        let note = KordNote::parse("C4".to_string()).unwrap();
        assert_eq!(note.name(), "C4");

        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();
        assert!(chord.display().contains("maj7"), "Display should contain 'maj7'");
    }

    #[wasm_bindgen_test]
    fn test_abi_string_encoding_unicode() {
        // Test that Unicode characters (sharps/flats) parse correctly
        let sharp_note = KordNote::parse("C♯4".to_string()).unwrap();
        assert_eq!(sharp_note.name(), "C♯4", "Should parse and display sharp unicode correctly");
        assert_eq!(sharp_note.pitch(), "C♯", "Pitch should preserve sharp unicode");

        let flat_note = KordNote::parse("Dâ™­4".to_string()).unwrap();
        assert_eq!(flat_note.name(), "Dâ™­4", "Should parse and display flat unicode correctly");
        assert_eq!(flat_note.pitch(), "Dâ™­", "Pitch should preserve flat unicode");

        // Test that display strings contain unicode correctly
        let chord = KordChord::parse("C#m7b5".to_string()).unwrap();
        let display = chord.display();
        assert!(display.contains("♯") || display.contains("#"), "Display should contain sharp symbol");
        assert!(display.len() > 0, "Display should return non-empty string");
    }

    #[wasm_bindgen_test]
    fn test_abi_empty_strings() {
        // Test that empty strings are handled correctly
        let result = KordNote::parse("".to_string());
        assert!(result.is_err(), "Empty string should fail to parse");

        let result = KordChord::parse("".to_string());
        assert!(result.is_err(), "Empty string should fail to parse");
    }

    #[wasm_bindgen_test]
    fn test_abi_error_handling() {
        // Test that Rust Result<T, E> properly converts to JS exceptions
        let invalid_note = KordNote::parse("Invalid".to_string());
        assert!(invalid_note.is_err(), "Should return error for invalid note");

        let invalid_chord = KordChord::parse("NotAChord123".to_string());
        assert!(invalid_chord.is_err(), "Should return error for invalid chord");

        // Test that errors from array operations are handled
        let result = KordChord::from_notes_string("".to_string());
        assert!(result.is_err(), "Empty notes string should error");
    }

    #[wasm_bindgen_test]
    fn test_abi_object_construction() {
        // Test that we can construct multiple independent objects
        let note1 = KordNote::parse("C4".to_string()).unwrap();
        let note2 = KordNote::parse("D4".to_string()).unwrap();
        let note3 = KordNote::parse("E4".to_string()).unwrap();

        // Verify they're distinct
        assert_ne!(note1.name(), note2.name());
        assert_ne!(note2.name(), note3.name());

        // Test chord construction
        let chord1 = KordChord::parse("C".to_string()).unwrap();
        let chord2 = KordChord::parse("G".to_string()).unwrap();

        assert!(chord1.root().starts_with("C"));
        assert!(chord2.root().starts_with("G"));
    }

    #[wasm_bindgen_test]
    fn test_abi_object_copy_independence() {
        // Test that copy() creates independent objects
        let original = KordNote::parse("C4".to_string()).unwrap();
        let copy = original.copy();

        assert_eq!(original.name(), copy.name());
        assert_eq!(original.frequency(), copy.frequency());

        // Test chord copy
        let chord_original = KordChord::parse("Cmaj7".to_string()).unwrap();
        let chord_copy = chord_original.copy();

        assert_eq!(chord_original.name(), chord_copy.name());
    }

    #[wasm_bindgen_test]
    fn test_abi_array_empty() {
        // Test that arrays handle empty cases correctly
        let chord = KordChord::parse("C".to_string()).unwrap();
        let extensions = chord.extensions();

        // Simple C major has no extensions
        assert_eq!(extensions.length(), 0, "Simple C major should have no extensions");

        // Test with a chord that has extensions
        let complex = KordChord::parse("Csus2".to_string()).unwrap();
        let complex_extensions = complex.extensions();
        assert_eq!(complex_extensions.length(), 1, "Sus2 chord should have exactly 1 extension");
    }

    #[wasm_bindgen_test]
    fn test_abi_array_multiple_elements() {
        // Test arrays with multiple elements
        let chord = KordChord::parse("Cmaj7#9b13".to_string()).unwrap();
        let notes = chord.chord();

        // Cmaj7#9b13 has: root, 3rd, 5th, 7th, #9, b13 = 6 notes
        assert!(notes.length() >= 5, "Complex chord should have at least 5 notes, got {}", notes.length());

        // Verify we can access multiple elements
        for i in 0..notes.length() {
            let element = notes.get(i);
            assert!(!element.is_undefined(), "Element {} should exist", i);
        }
    }

    #[wasm_bindgen_test]
    fn test_abi_number_precision_frequency() {
        // Test that floating-point frequencies maintain precision
        let a4 = KordNote::parse("A4".to_string()).unwrap();
        let freq = a4.frequency();

        // A4 should be 440 Hz
        assert!((freq - 440.0).abs() < 0.1, "A4 frequency should be ~440 Hz, got {}", freq);

        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let c_freq = c4.frequency();

        // C4 should be ~261.63 Hz
        assert!(c_freq > 260.0 && c_freq < 263.0, "C4 frequency should be ~261.63 Hz, got {}", c_freq);
    }

    #[wasm_bindgen_test]
    fn test_abi_number_octave_range() {
        // Test octave numbers across valid range
        let c0 = KordNote::parse("C0".to_string()).unwrap();
        assert_eq!(c0.octave(), 0);

        let c4 = KordNote::parse("C4".to_string()).unwrap();
        assert_eq!(c4.octave(), 4);

        let c8 = KordNote::parse("C8".to_string()).unwrap();
        assert_eq!(c8.octave(), 8);
    }

    #[wasm_bindgen_test]
    fn test_abi_number_interval_operations() {
        // Test that interval arithmetic works correctly across ABI
        let c4 = KordNote::parse("C4".to_string()).unwrap();

        let up_octave = c4.add_interval(Interval::PerfectOctave);
        assert_eq!(up_octave.octave(), 5, "Should be an octave higher");

        let down_fifth = c4.subtract_interval(Interval::PerfectFifth);
        assert_eq!(down_fifth.octave(), 3, "Should be in octave 3");

        // Test distance calculation
        let g4 = KordNote::parse("G4".to_string()).unwrap();
        let distance = c4.distance_to(g4);
        assert_eq!(distance, Interval::PerfectFifth, "C to G should be a perfect fifth");
    }

    #[wasm_bindgen_test]
    fn test_abi_method_chaining() {
        // Test that method chaining works correctly (verifies proper return types)
        let chord = KordChord::parse("C".to_string()).unwrap().minor().seven().flat9().sharp11();

        let display = chord.display();
        assert!(display.contains("m"), "Should show minor");
        assert!(display.contains("7"), "Should show 7");
    }

    #[wasm_bindgen_test]
    fn test_abi_boolean_returns() {
        // Test that boolean values cross the ABI correctly
        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let c4_copy = KordNote::parse("C4".to_string()).unwrap();
        let d4 = KordNote::parse("D4".to_string()).unwrap();

        // Test that same notes have equal names
        assert_eq!(c4.name(), c4_copy.name(), "Same notes should have equal names");

        // Test that different notes have different names
        assert_ne!(c4.name(), d4.name(), "Different notes should have different names");

        // Test chord crunchy check with known values
        let simple = KordChord::parse("C".to_string()).unwrap();
        assert!(!simple.is_crunchy(), "Simple major chord should not be crunchy");

        let crunchy = KordChord::parse("Cmaj7#9#11b13!".to_string()).unwrap();
        assert!(crunchy.is_crunchy(), "Complex altered chord with ! should be crunchy");
    }

    #[wasm_bindgen_test]
    fn test_abi_null_handling_slash() {
        // Test that optional slash bass note is handled correctly
        let no_slash = KordChord::parse("C".to_string()).unwrap();
        let slash_str = no_slash.slash();

        // When no slash is present, slash() returns the root note
        assert!(slash_str.starts_with("C"), "No slash should return root note");

        let with_slash = KordChord::parse("C/E".to_string()).unwrap();
        let slash_str = with_slash.slash();
        assert!(slash_str.starts_with("E"), "Should return slash note");
    }

    #[wasm_bindgen_test]
    fn test_abi_long_strings() {
        // Test that long chord names work correctly
        let complex = KordChord::parse("Cmaj7#9#11b13".to_string()).unwrap();
        let name = complex.name();
        let display = complex.display();
        let description = complex.description();

        assert!(name.len() > 0, "Name should be non-empty");
        assert!(display.len() > 0, "Display should be non-empty");
        assert!(description.len() > 0, "Description should be non-empty");
    }

    #[wasm_bindgen_test]
    fn test_abi_large_array() {
        // Test harmonic series returns correct-sized array
        let c4 = KordNote::parse("C4".to_string()).unwrap();
        let harmonics = c4.harmonic_series();

        assert_eq!(harmonics.length(), 13, "Should return 13 harmonics");

        // Verify all elements are accessible
        for i in 0..harmonics.length() {
            let harmonic = harmonics.get(i);
            assert!(!harmonic.is_undefined(), "Harmonic {} should exist", i);
        }
    }

    #[wasm_bindgen_test]
    fn test_abi_object_reuse() {
        // Test that we can reuse objects multiple times
        let chord = KordChord::parse("C".to_string()).unwrap();

        // Call same method multiple times
        let notes1 = chord.chord();
        let notes2 = chord.chord();

        assert_eq!(notes1.length(), notes2.length(), "Should return consistent results");

        // Call different methods on same object
        let _name = chord.name();
        let _display = chord.display();
        let _root = chord.root();
        let _description = chord.description();

        // Object should still be valid with expected note count
        assert_eq!(chord.chord().length(), 3, "C major should still have 3 notes after reuse");
    }

    #[wasm_bindgen_test]
    fn test_abi_transformation_returns_new_object() {
        // Test that transformations return new independent objects
        let original = KordChord::parse("C".to_string()).unwrap();
        let minor = original.minor();

        // Original should be unchanged
        assert!(original.name().contains("C"));

        // New object should be different
        assert!(minor.display().contains("m"), "Should be minor");

        // Both should be independently usable
        let _original_notes = original.chord();
        let _minor_notes = minor.chord();
    }

    // KordMode tests

    #[wasm_bindgen_test]
    fn test_mode_parse_simple() {
        let mode = KordMode::parse("C dorian".to_string());
        assert!(mode.is_ok(), "Should parse simple mode");

        let mode = mode.unwrap();
        assert_eq!(mode.root(), "C4", "Root should be C4");
        assert!(mode.name().contains("dorian"), "Name should contain dorian");
    }

    #[wasm_bindgen_test]
    fn test_mode_parse_invalid() {
        let mode = KordMode::parse("Invalid mode".to_string());
        assert!(mode.is_err(), "Should fail to parse invalid mode");
    }

    #[wasm_bindgen_test]
    fn test_mode_parse_lydian_dominant() {
        let mode = KordMode::parse("D lydian dominant".to_string()).unwrap();
        assert_eq!(mode.root(), "D4");
        assert!(mode.name().contains("lydian"), "Should contain lydian");
    }

    #[wasm_bindgen_test]
    fn test_mode_parse_with_sharps_flats() {
        let mode = KordMode::parse("F# phrygian".to_string()).unwrap();
        assert_eq!(mode.root(), "F♯4");

        let mode = KordMode::parse("Bb locrian".to_string()).unwrap();
        assert_eq!(mode.root(), "Bâ™­4");
    }

    #[wasm_bindgen_test]
    fn test_mode_parse_altered() {
        let mode = KordMode::parse("G altered".to_string()).unwrap();
        assert_eq!(mode.root(), "G4");
        assert!(mode.description().len() > 0, "Should have description");
    }

    #[wasm_bindgen_test]
    fn test_mode_notes() {
        let mode = KordMode::parse("C ionian".to_string()).unwrap();
        let notes = mode.notes();
        assert_eq!(notes.length(), 7, "Ionian mode should have 7 notes");
    }

    #[wasm_bindgen_test]
    fn test_mode_notes_string() {
        let mode = KordMode::parse("C dorian".to_string()).unwrap();
        let notes_str = mode.notes_string();
        assert!(notes_str.contains("C"), "Notes string should contain root");
        assert!(notes_str.len() > 0, "Notes string should not be empty");
    }

    #[wasm_bindgen_test]
    fn test_mode_precise_name() {
        let mode = KordMode::parse("C mixolydian".to_string()).unwrap();
        let precise = mode.precise_name();
        assert!(precise.len() > 0, "Should have precise name");
    }

    #[wasm_bindgen_test]
    fn test_mode_to_string() {
        let mode = KordMode::parse("D phrygian".to_string()).unwrap();
        let string = mode.to_string();
        assert!(string.len() > 0, "toString should return non-empty string");
    }

    #[wasm_bindgen_test]
    fn test_mode_copy() {
        let mode = KordMode::parse("E locrian".to_string()).unwrap();
        let copy = mode.copy();
        assert_eq!(copy.root(), mode.root());
        assert_eq!(copy.name(), mode.name());
    }

    #[wasm_bindgen_test]
    fn test_mode_harmonic_minor_modes() {
        // Test some harmonic minor modes
        let locrian_nat6 = KordMode::parse("B locrian nat6".to_string());
        assert!(locrian_nat6.is_ok(), "Should parse locrian nat6");

        let phrygian_dominant = KordMode::parse("E phrygian dominant".to_string());
        assert!(phrygian_dominant.is_ok(), "Should parse phrygian dominant");

        let ionian_aug = KordMode::parse("C ionian augmented".to_string());
        assert!(ionian_aug.is_ok(), "Should parse ionian augmented");
    }

    #[wasm_bindgen_test]
    fn test_mode_melodic_minor_modes() {
        // Test some melodic minor modes
        let dorian_flat2 = KordMode::parse("B dorian b2".to_string());
        assert!(dorian_flat2.is_ok(), "Should parse dorian flat 2");

        let lydian_aug = KordMode::parse("C lydian augmented".to_string());
        assert!(lydian_aug.is_ok(), "Should parse lydian augmented");

        let mixolydian_flat6 = KordMode::parse("G mixolydian b6".to_string());
        assert!(mixolydian_flat6.is_ok(), "Should parse mixolydian flat 6");
    }

    // KordScale tests

    #[wasm_bindgen_test]
    fn test_scale_parse_major() {
        let scale = KordScale::parse("C major".to_string());
        assert!(scale.is_ok(), "Should parse major scale");

        let scale = scale.unwrap();
        assert_eq!(scale.root(), "C4");
        assert!(scale.name().contains("major"), "Name should contain major");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_minor() {
        let scale = KordScale::parse("A natural minor".to_string()).unwrap();
        assert_eq!(scale.root(), "A4");
        assert!(scale.name().contains("minor"));
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_harmonic_minor() {
        let scale = KordScale::parse("D harmonic minor".to_string()).unwrap();
        assert_eq!(scale.root(), "D4");
        assert!(scale.description().contains("harmonic"), "Should mention harmonic");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_melodic_minor() {
        let scale = KordScale::parse("G melodic minor".to_string()).unwrap();
        assert_eq!(scale.root(), "G4");
        assert!(scale.description().contains("melodic"), "Should mention melodic");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_pentatonic() {
        let major_pent = KordScale::parse("C major pentatonic".to_string()).unwrap();
        let notes = major_pent.notes();
        assert_eq!(notes.length(), 5, "Major pentatonic should have 5 notes");

        let minor_pent = KordScale::parse("A minor pentatonic".to_string()).unwrap();
        let notes = minor_pent.notes();
        assert_eq!(notes.length(), 5, "Minor pentatonic should have 5 notes");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_blues() {
        let blues = KordScale::parse("E blues".to_string()).unwrap();
        assert_eq!(blues.root(), "E4");
        let notes = blues.notes();
        assert_eq!(notes.length(), 6, "Blues scale should have 6 notes");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_whole_tone() {
        let whole_tone = KordScale::parse("C whole tone".to_string()).unwrap();
        assert_eq!(whole_tone.root(), "C4");
        let notes = whole_tone.notes();
        assert_eq!(notes.length(), 6, "Whole tone should have 6 notes");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_diminished() {
        let dim_hw = KordScale::parse("C diminished half-whole".to_string()).unwrap();
        let notes = dim_hw.notes();
        assert_eq!(notes.length(), 8, "Diminished scale should have 8 notes");

        let dim_wh = KordScale::parse("C diminished whole-half".to_string()).unwrap();
        let notes = dim_wh.notes();
        assert_eq!(notes.length(), 8, "Diminished scale should have 8 notes");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_chromatic() {
        let chromatic = KordScale::parse("C chromatic".to_string()).unwrap();
        assert_eq!(chromatic.root(), "C4");
        let notes = chromatic.notes();
        assert_eq!(notes.length(), 12, "Chromatic scale should have 12 notes");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_invalid() {
        let scale = KordScale::parse("Invalid scale".to_string());
        assert!(scale.is_err(), "Should fail to parse invalid scale");
    }

    #[wasm_bindgen_test]
    fn test_scale_parse_with_sharps_flats() {
        let scale = KordScale::parse("F# major".to_string()).unwrap();
        assert_eq!(scale.root(), "F♯4");

        let scale = KordScale::parse("Bb major".to_string()).unwrap();
        assert_eq!(scale.root(), "Bâ™­4");
    }

    #[wasm_bindgen_test]
    fn test_scale_notes() {
        let scale = KordScale::parse("C major".to_string()).unwrap();
        let notes = scale.notes();
        assert_eq!(notes.length(), 7, "Major scale should have 7 notes");

        // Verify all elements are accessible
        for i in 0..notes.length() {
            let note = notes.get(i);
            assert!(!note.is_undefined(), "Note {} should exist", i);
        }
    }

    #[wasm_bindgen_test]
    fn test_scale_notes_string() {
        let scale = KordScale::parse("G major".to_string()).unwrap();
        let notes_str = scale.notes_string();
        assert!(notes_str.contains("G"), "Notes string should contain root");
        assert!(notes_str.len() > 0, "Notes string should not be empty");
    }

    #[wasm_bindgen_test]
    fn test_scale_precise_name() {
        let scale = KordScale::parse("D major".to_string()).unwrap();
        let precise = scale.precise_name();
        assert!(precise.len() > 0, "Should have precise name");
    }

    #[wasm_bindgen_test]
    fn test_scale_description() {
        let scale = KordScale::parse("A harmonic minor".to_string()).unwrap();
        let description = scale.description();
        assert!(description.len() > 0, "Should have description");
    }

    #[wasm_bindgen_test]
    fn test_scale_to_string() {
        let scale = KordScale::parse("E melodic minor".to_string()).unwrap();
        let string = scale.to_string();
        assert!(string.len() > 0, "toString should return non-empty string");
    }

    #[wasm_bindgen_test]
    fn test_scale_copy() {
        let scale = KordScale::parse("B major".to_string()).unwrap();
        let copy = scale.copy();
        assert_eq!(copy.root(), scale.root());
        assert_eq!(copy.name(), scale.name());
    }

    // Integration tests for Mode and Scale with Chord

    #[wasm_bindgen_test]
    fn test_chord_scale_candidates_with_modes() {
        // Test that chord scale candidates work
        let chord = KordChord::parse("Cmaj7".to_string()).unwrap();
        let scale_notes = chord.scale();
        assert!(scale_notes.length() >= 7, "Should have scale notes");
    }

    #[wasm_bindgen_test]
    fn test_mode_scale_enharmonic_spelling() {
        // Test that enharmonic spelling is correct for modes and scales
        let mode = KordMode::parse("C# ionian".to_string()).unwrap();
        let notes_str = mode.notes_string();
        // C# major should have all sharps (C# D# E# F# G# A# B#)
        assert!(notes_str.contains("C♯"), "Should contain C sharp");
        assert!(notes_str.contains("E♯"), "Should contain E sharp (not F)");

        let scale = KordScale::parse("Db major".to_string()).unwrap();
        let notes_str = scale.notes_string();
        // Db major should have flats (Db Eb F Gb Ab Bb C)
        assert!(notes_str.contains("Dâ™­"), "Should contain D flat");
        assert!(notes_str.contains("Eâ™­"), "Should contain E flat");
    }

    #[wasm_bindgen_test]
    fn test_mode_scale_unicode_symbols() {
        // Test that unicode symbols are handled correctly
        let mode = KordMode::parse("F♯ lydian".to_string());
        assert!(mode.is_ok(), "Should parse mode with unicode sharp");

        let mode = KordMode::parse("Bâ™­ dorian".to_string());
        assert!(mode.is_ok(), "Should parse mode with unicode flat");

        let scale = KordScale::parse("G♯ harmonic minor".to_string());
        assert!(scale.is_ok(), "Should parse scale with unicode sharp");
    }

    #[wasm_bindgen_test]
    fn test_mode_scale_abi_object_independence() {
        // Test that mode and scale objects are independent
        let mode1 = KordMode::parse("C dorian".to_string()).unwrap();
        let mode2 = KordMode::parse("D dorian".to_string()).unwrap();
        assert_ne!(mode1.root(), mode2.root(), "Different modes should have different roots");

        let scale1 = KordScale::parse("C major".to_string()).unwrap();
        let scale2 = KordScale::parse("G major".to_string()).unwrap();
        assert_ne!(scale1.root(), scale2.root(), "Different scales should have different roots");
    }

    // ScaleCandidate WASM tests

    #[wasm_bindgen_test]
    fn test_scale_candidates_basic() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // C major should have exactly 4 candidates: Ionian, Major Pentatonic, Lydian, Mixolydian
        assert_eq!(candidates.length(), 4, "C major should have 4 scale candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_notes() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // First candidate for C major should be Ionian with 7 notes
        assert_eq!(candidates.length(), 4, "Should have 4 candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_major_chord() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        assert!(candidates.length() >= 3, "C major should have at least 3 candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_dominant_chord() {
        let chord = KordChord::parse("G7".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        assert!(candidates.length() >= 5, "G7 should have multiple candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_minor_chord() {
        let chord = KordChord::parse("Cm".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // Minor chord has: Aeolian, Minor Pentatonic, Blues, Dorian, Phrygian, Harmonic Minor, Melodic Minor
        assert_eq!(candidates.length(), 7, "Cm should have 7 candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_altered_dominant() {
        let chord = KordChord::parse("C7#9".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        assert!(candidates.length() >= 1, "C7#9 should have candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_half_diminished() {
        let chord = KordChord::parse("Cm7b5".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        assert!(candidates.length() >= 1, "Cm7b5 should have candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_ranking_order() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // Ionian (rank 1), Major Pentatonic (rank 2), Lydian (rank 3), Mixolydian (rank 4)
        assert_eq!(candidates.length(), 4, "Should have 4 candidates in ranked order");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_copy() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates1 = chord.scale_candidates();
        let candidates2 = chord.scale_candidates();
        // Multiple calls should return same candidates
        assert_eq!(candidates1.length(), candidates2.length(), "Repeated calls should return same count");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_rooting() {
        let c_chord = KordChord::parse("C".to_string()).unwrap();
        let g_chord = KordChord::parse("G".to_string()).unwrap();

        let c_candidates = c_chord.scale_candidates();
        let g_candidates = g_chord.scale_candidates();

        // Same chord type (major) should have same number of candidates regardless of root
        assert_eq!(c_candidates.length(), g_candidates.length(), "C and G major should have same candidate count");
        assert_eq!(c_candidates.length(), 4, "Major chords should have 4 candidates");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_pentatonic_in_recommendations() {
        let chord = KordChord::parse("C".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // Major Pentatonic is rank 2 for major chords
        assert_eq!(candidates.length(), 4, "C major should have 4 candidates including Major Pentatonic");
    }

    #[wasm_bindgen_test]
    fn test_scale_candidates_blues_in_recommendations() {
        let chord = KordChord::parse("G7".to_string()).unwrap();
        let candidates = chord.scale_candidates();
        // Dominant 7 has: Mixolydian, Blues, Lydian Dominant, Mixolydian b6, Whole Tone, Dim HW, Altered
        assert_eq!(candidates.length(), 7, "G7 should have 7 candidates including Blues");
    }
}