-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.property.test.js
More file actions
345 lines (303 loc) · 9.42 KB
/
draw.property.test.js
File metadata and controls
345 lines (303 loc) · 9.42 KB
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
import { test } from "node:test";
import assert from "node:assert";
import * as fc from "fast-check";
import {
drawFretboard,
drawFrets,
drawNut,
drawStrings,
drawStringNames,
} from "./src/draw.js";
// Mock canvas context that tracks calls
function createMockContext() {
const calls = [];
return {
calls, // Expose for assertions
strokeStyle: null,
fillStyle: null,
lineWidth: null,
textAlign: null,
font: null,
beginPath() {
calls.push("beginPath");
},
moveTo(x, y) {
calls.push(["moveTo", x, y]);
},
lineTo(x, y) {
calls.push(["lineTo", x, y]);
},
stroke() {
calls.push("stroke");
},
arc(x, y, r, start, end) {
calls.push(["arc", x, y, r, start, end]);
},
fill() {
calls.push("fill");
},
fillText(text, x, y) {
calls.push(["fillText", text, x, y]);
},
clearRect(x, y, w, h) {
calls.push(["clearRect", x, y, w, h]);
},
};
}
function createMockCanvas(width = 300, height = 500) {
return { width, height };
}
// All valid notes in the requinto (C-D-G-C tuning)
const allValidNotes = [
{ string: 4, fret: 0, note: "C" },
{ string: 4, fret: 2, note: "D" },
{ string: 3, fret: 0, note: "D" },
{ string: 3, fret: 2, note: "E" },
{ string: 3, fret: 3, note: "F" },
{ string: 2, fret: 0, note: "G" },
{ string: 2, fret: 2, note: "A" },
{ string: 2, fret: 4, note: "B" },
{ string: 1, fret: 0, note: "C" },
{ string: 1, fret: 2, note: "D" },
{ string: 1, fret: 4, note: "E" },
{ string: 1, fret: 5, note: "F" },
];
test("property: drawFretboard never throws for any valid note and showingAnswer combination", () => {
// Test EVERY possible case: 12 notes × 2 showingAnswer states = 24 cases
fc.assert(
fc.property(
fc.constantFrom(...allValidNotes),
fc.boolean(),
(note, showingAnswer) => {
const ctx = createMockContext();
const canvas = createMockCanvas();
// Should never throw
assert.doesNotThrow(() => {
drawFretboard(ctx, canvas, note, showingAnswer);
});
},
),
{ numRuns: 24 }, // We have exactly 24 combinations to test
);
});
test("property: drawFretboard handles null note gracefully", () => {
fc.assert(
fc.property(fc.boolean(), (showingAnswer) => {
const ctx = createMockContext();
const canvas = createMockCanvas();
assert.doesNotThrow(() => {
drawFretboard(ctx, canvas, null, showingAnswer);
});
}),
);
});
test("property: drawFretboard always clears canvas first", () => {
fc.assert(
fc.property(
fc.option(fc.constantFrom(...allValidNotes), { nil: null }),
fc.boolean(),
(note, showingAnswer) => {
const ctx = createMockContext();
const canvas = createMockCanvas();
drawFretboard(ctx, canvas, note, showingAnswer);
// First call should be clearRect
assert.strictEqual(ctx.calls[0][0], "clearRect");
},
),
);
});
test("property: drawFretboard calls arc when note is provided", () => {
fc.assert(
fc.property(
fc.constantFrom(...allValidNotes),
fc.boolean(),
(note, showingAnswer) => {
const ctx = createMockContext();
const canvas = createMockCanvas();
drawFretboard(ctx, canvas, note, showingAnswer);
// Should have at least one arc call for highlighting the note
const arcCalls = ctx.calls.filter(
(call) => Array.isArray(call) && call[0] === "arc",
);
assert.ok(
arcCalls.length > 0,
"Expected arc calls for note highlighting",
);
},
),
);
});
test("property: drawFretboard shows note text only when showingAnswer is true", () => {
fc.assert(
fc.property(fc.constantFrom(...allValidNotes), (note) => {
// Test with showingAnswer = false
const ctx1 = createMockContext();
const canvas1 = createMockCanvas();
drawFretboard(ctx1, canvas1, note, false);
const textCallsHidden = ctx1.calls.filter(
(call) =>
Array.isArray(call) &&
call[0] === "fillText" &&
call[1] === note.note,
);
assert.strictEqual(
textCallsHidden.length,
0,
"Should not show note text when hidden",
);
// Test with showingAnswer = true
const ctx2 = createMockContext();
const canvas2 = createMockCanvas();
drawFretboard(ctx2, canvas2, note, true);
const textCallsShown = ctx2.calls.filter(
(call) =>
Array.isArray(call) &&
call[0] === "fillText" &&
call[1] === note.note,
);
assert.strictEqual(
textCallsShown.length,
1,
"Should show note text when revealed",
);
}),
);
});
test("property: drawFrets creates correct number of horizontal lines", () => {
fc.assert(
fc.property(
fc.integer({ min: 100, max: 1000 }),
fc.integer({ min: 100, max: 1000 }),
(width, height) => {
const ctx = createMockContext();
const canvas = { width, height };
const config = {
padding: 40,
numFrets: 5,
fretSpacing: (height - 80) / 5,
};
drawFrets(ctx, canvas, config);
// Should have 6 lines (0 through 5 = 6 frets)
const strokes = ctx.calls.filter((call) => call === "stroke");
assert.strictEqual(strokes.length, 6);
},
),
);
});
test("property: drawStrings creates correct number of vertical lines", () => {
fc.assert(
fc.property(
fc.integer({ min: 100, max: 1000 }),
fc.integer({ min: 100, max: 1000 }),
(width, height) => {
const ctx = createMockContext();
const canvas = { width, height };
const config = {
padding: 40,
numStrings: 4,
stringSpacing: (width - 80) / 3,
};
drawStrings(ctx, canvas, config);
// Should have 4 lines (4 strings)
const strokes = ctx.calls.filter((call) => call === "stroke");
assert.strictEqual(strokes.length, 4);
},
),
);
});
test("property: drawStringNames labels all strings", () => {
fc.assert(
fc.property(fc.integer({ min: 100, max: 1000 }), (width) => {
const ctx = createMockContext();
const canvas = { width, height: 500 };
const config = {
padding: 40,
numStrings: 4,
stringNames: ["C", "D", "G", "C"],
stringSpacing: (width - 80) / 3,
};
drawStringNames(ctx, canvas, config);
// Should have 4 fillText calls (one per string)
const textCalls = ctx.calls.filter(
(call) => Array.isArray(call) && call[0] === "fillText",
);
assert.strictEqual(textCalls.length, 4);
// Verify all string names appear
const texts = textCalls.map((call) => call[1]);
assert.deepStrictEqual(texts, ["C", "D", "G", "C"]);
}),
);
});
test("property: drawNut creates exactly one stroke", () => {
fc.assert(
fc.property(fc.integer({ min: 100, max: 1000 }), (width) => {
const ctx = createMockContext();
const canvas = { width, height: 500 };
const config = { padding: 40 };
drawNut(ctx, canvas, config);
// Should have exactly 1 stroke for the nut
const strokes = ctx.calls.filter((call) => call === "stroke");
assert.strictEqual(strokes.length, 1);
}),
);
});
// Exhaustive test: every single note with both showingAnswer states
test("exhaustive: all 12 notes × 2 states = 24 cases", () => {
let testCount = 0;
for (const note of allValidNotes) {
for (const showingAnswer of [false, true]) {
const ctx = createMockContext();
const canvas = createMockCanvas();
assert.doesNotThrow(() => {
drawFretboard(ctx, canvas, note, showingAnswer);
}, `Failed on note ${note.note} (string ${note.string}, fret ${note.fret}), showing=${showingAnswer}`);
testCount++;
}
}
assert.strictEqual(testCount, 24, "Should have tested exactly 24 cases");
});
test("property: explore mode shows all notes with labels", () => {
const ctx = createMockContext();
const canvas = createMockCanvas();
drawFretboard(ctx, canvas, null, false, allValidNotes);
// Should have 12 arc calls (one per note)
const arcCalls = ctx.calls.filter(
(call) => Array.isArray(call) && call[0] === "arc",
);
// Plus natural note markers (12 natural notes in the array)
assert.ok(
arcCalls.length >= 12,
`Expected at least 12 arc calls, got ${arcCalls.length}`,
);
// Should have 12 fillText calls with note names
const noteLabelCalls = ctx.calls.filter(
(call) =>
Array.isArray(call) &&
call[0] === "fillText" &&
allValidNotes.some((n) => n.note === call[1]),
);
assert.strictEqual(
noteLabelCalls.length,
12,
"Should show all 12 note labels in explore mode",
);
});
test("property: explore mode and training mode are mutually exclusive", () => {
const ctx = createMockContext();
const canvas = createMockCanvas();
const singleNote = allValidNotes[0];
// Call with both currentNote and allNotes - allNotes should take precedence
drawFretboard(ctx, canvas, singleNote, false, allValidNotes);
// Should have note labels for all notes, not just one
const noteLabelCalls = ctx.calls.filter(
(call) =>
Array.isArray(call) &&
call[0] === "fillText" &&
allValidNotes.some((n) => n.note === call[1]),
);
assert.strictEqual(
noteLabelCalls.length,
12,
"Should show all notes when in explore mode",
);
});