|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { Scale } from "tonal"; |
3 | 3 | import frets from "../index.js"; |
| 4 | +import caged from "./caged.js"; |
4 | 5 |
|
5 | 6 | describe("CAGED system", () => { |
6 | | - it("assigns G shape (position 5) correctly for C major", () => { |
7 | | - const scale = Scale.get("C major"); |
8 | | - const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
9 | | - const lowE = fb.strings[5]; // Low E string (reversed order) |
| 7 | + describe("C major scale", () => { |
| 8 | + it("assigns G shape (position 5) correctly - starts at fret 5 with A, B, C", () => { |
| 9 | + const scale = Scale.get("C major"); |
| 10 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 11 | + const lowE = fb.strings[5]; // Low E string (reversed order) |
10 | 12 |
|
11 | | - // G shape on low E string should have A (fret 5), B (fret 7), C (fret 8) |
12 | | - const aNote = lowE[5]; |
13 | | - const bNote = lowE[7]; |
14 | | - const cNote = lowE[8]; |
| 13 | + // G shape pattern on low E has degrees [6, 7, 1] = A, B, C |
| 14 | + expect(lowE[5].note.pc).toBe("A"); |
| 15 | + expect(lowE[5].positions.CAGED).toContain(5); |
15 | 16 |
|
16 | | - expect(aNote.note.pc).toBe("A"); |
17 | | - expect(aNote.positions.CAGED).toContain(5); |
| 17 | + expect(lowE[7].note.pc).toBe("B"); |
| 18 | + expect(lowE[7].positions.CAGED).toContain(5); |
18 | 19 |
|
19 | | - expect(bNote.note.pc).toBe("B"); |
20 | | - expect(bNote.positions.CAGED).toContain(5); |
| 20 | + expect(lowE[8].note.pc).toBe("C"); |
| 21 | + expect(lowE[8].positions.CAGED).toContain(5); |
| 22 | + }); |
21 | 23 |
|
22 | | - expect(cNote.note.pc).toBe("C"); |
23 | | - expect(cNote.positions.CAGED).toContain(5); |
| 24 | + it("assigns E shape (position 1) correctly - starts at open position", () => { |
| 25 | + const scale = Scale.get("C major"); |
| 26 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 27 | + const lowE = fb.strings[5]; |
| 28 | + |
| 29 | + // E shape pattern on low E has degrees [7, 1, 2] = B, C, D |
| 30 | + expect(lowE[7].note.pc).toBe("B"); |
| 31 | + expect(lowE[7].positions.CAGED).toContain(1); |
| 32 | + |
| 33 | + expect(lowE[8].note.pc).toBe("C"); |
| 34 | + expect(lowE[8].positions.CAGED).toContain(1); |
| 35 | + |
| 36 | + expect(lowE[10].note.pc).toBe("D"); |
| 37 | + expect(lowE[10].positions.CAGED).toContain(1); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("C minor scale", () => { |
| 42 | + it("assigns G shape (position 5) correctly - starts at fret 8 with C, D, Eb", () => { |
| 43 | + const scale = Scale.get("C minor"); |
| 44 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 45 | + const lowE = fb.strings[5]; |
| 46 | + |
| 47 | + // For minor, G shape pattern rotates +2, so low E has degrees [1, 2, 3] = C, D, Eb |
| 48 | + expect(lowE[8].note.pc).toBe("C"); |
| 49 | + expect(lowE[8].positions.CAGED).toContain(5); |
| 50 | + |
| 51 | + expect(lowE[10].note.pc).toBe("D"); |
| 52 | + expect(lowE[10].positions.CAGED).toContain(5); |
| 53 | + |
| 54 | + // Use label which reflects the scale's spelling (E♭ not D#) |
| 55 | + expect(lowE[11].label).toBe("E♭"); |
| 56 | + expect(lowE[11].positions.CAGED).toContain(5); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("error handling", () => { |
| 61 | + it("throws error for chromatic scale (12 notes)", () => { |
| 62 | + const chromatic = Scale.get("C chromatic"); |
| 63 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16); |
| 64 | + expect(() => caged(fb.strings, chromatic)).toThrow( |
| 65 | + "CAGED system only works with 5-note pentatonic or 7-note scales" |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("throws error for 6-note scales", () => { |
| 70 | + const blues = Scale.get("C blues"); |
| 71 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16); |
| 72 | + expect(() => caged(fb.strings, blues)).toThrow( |
| 73 | + "CAGED system only works with 5-note pentatonic or 7-note scales" |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("position assignment", () => { |
| 79 | + it("assigns positions to all scale notes", () => { |
| 80 | + const scale = Scale.get("G major"); |
| 81 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 82 | + |
| 83 | + for (const string of fb.strings) { |
| 84 | + for (const note of string) { |
| 85 | + if (note.interval) { |
| 86 | + expect(note.positions.CAGED).toBeDefined(); |
| 87 | + expect(Array.isArray(note.positions.CAGED)).toBe(true); |
| 88 | + expect(note.positions.CAGED.length).toBeGreaterThan(0); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it("assigns positions between 1 and 5", () => { |
| 95 | + const scale = Scale.get("A minor"); |
| 96 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 97 | + |
| 98 | + for (const string of fb.strings) { |
| 99 | + for (const note of string) { |
| 100 | + if (note.positions.CAGED?.length > 0) { |
| 101 | + for (const pos of note.positions.CAGED) { |
| 102 | + expect(pos).toBeGreaterThanOrEqual(1); |
| 103 | + expect(pos).toBeLessThanOrEqual(5); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + it("does not assign positions to non-scale notes", () => { |
| 111 | + const scale = Scale.get("C major"); |
| 112 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 113 | + |
| 114 | + for (const string of fb.strings) { |
| 115 | + for (const note of string) { |
| 116 | + if (!note.interval) { |
| 117 | + expect( |
| 118 | + note.positions.CAGED === undefined || |
| 119 | + note.positions.CAGED.length === 0 |
| 120 | + ).toBe(true); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("works with different keys", () => { |
| 128 | + it("works with G major", () => { |
| 129 | + const scale = Scale.get("G major"); |
| 130 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 131 | + const lowE = fb.strings[5]; |
| 132 | + |
| 133 | + // G is at fret 3, should be in position 5 (G shape starts on root) |
| 134 | + expect(lowE[3].note.pc).toBe("G"); |
| 135 | + expect(lowE[3].positions.CAGED).toContain(5); |
| 136 | + }); |
| 137 | + |
| 138 | + it("works with E minor", () => { |
| 139 | + const scale = Scale.get("E minor"); |
| 140 | + const fb = frets(["E2", "A2", "D3", "G3", "B3", "E4"], 16, scale); |
| 141 | + const lowE = fb.strings[5]; |
| 142 | + |
| 143 | + // E is at fret 0 (open), should be in G shape (position 5) for minor |
| 144 | + expect(lowE[0].note.pc).toBe("E"); |
| 145 | + expect(lowE[0].positions.CAGED).toContain(5); |
| 146 | + }); |
24 | 147 | }); |
25 | 148 | }); |
0 commit comments