mirror of
https://github.com/got-feedBack/feedBack.git
synced 2026-08-10 18:59:56 +00:00
6 functions, 53 lines. highway.js 4,158 -> 4,105. NOT ONE CALL SITE CHANGES. project, roundRect, bnvNormalizedPoints, teachingFingerLabel, teachingDegreeLabel, chordHarmonyLabels — the shared primitives every drawing function leans on. ━━━ PURITY IS THE WHOLE POINT OF THIS SLICE ━━━ Every one of these is a pure function of its arguments. None touches hwState. None closes over the canvas context — roundRect() already took `ctx` explicitly, and the rest need nothing but numbers. project() reads only the module-level constants from #914. That matters because createHighway() is a FACTORY: a plugin can build a second highway for its own panel, so anything holding per-instance state must be PASSED hwState rather than importing it, or two panels silently share one clock and palette. These six hold no state at all, so they move VERBATIM — the module boundary is invisible to every caller. The asserts are mechanical and in the extractor: it REFUSES to move a function whose body mentions hwState, or that references `ctx` without taking it as a parameter. Purity is checked, not assumed. ━━━ WHAT IS DELIBERATELY LEFT BEHIND ━━━ The four primitives that DO need hwState — fretX, fillTextReadable, _noteState, _paintGemGlow — stay in the factory for now. They need an explicit hwState parameter threaded through 53 call sites, which is a real behavioural change and belongs in its own commit rather than smuggled in beside a provably-identical move. Separating the provable from the risky is the whole discipline of this epic. TESTS. Three harnesses brace-match these functions out of the source and run them in a sandbox; they now read static/js/highway-geometry.js. `export function x` still contains `function x`, so the extractor needed no change — only the path. VERIFIED. A/B against origin/main: 15 probes IDENTICAL, zero page errors. PERF GATE PASSES AT 1.91ms against its 12ms budget — and this is the one that could plausibly have cost something: project() runs for every visible note on every frame and is now a CROSS-MODULE call. It costs nothing measurable. That is the answer #910 was built to give. node 1045, pytest 2416, ESLint 0, Codex 0. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
73 lines
3.5 KiB
JavaScript
73 lines
3.5 KiB
JavaScript
// Behavioural tests for the chord harmony-annotation render helper
|
|
// chordHarmonyLabels (§6.3.1 / §6.6), shared by the 2D and 3D highways.
|
|
// Pure, so we extract the function source by brace-matching and eval it in
|
|
// isolation — same pattern as highway_teaching_marks.test.js.
|
|
|
|
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
function extractFn(src, name) {
|
|
const start = src.indexOf('function ' + name);
|
|
assert.ok(start >= 0, `function ${name} must exist`);
|
|
const open = src.indexOf('{', start);
|
|
let depth = 0;
|
|
for (let i = open; i < src.length; i++) {
|
|
if (src[i] === '{') depth++;
|
|
else if (src[i] === '}' && --depth === 0) return src.slice(start, i + 1);
|
|
}
|
|
throw new Error(`unbalanced braces extracting ${name}`);
|
|
}
|
|
|
|
function loadFn(file, name) {
|
|
const src = fs.readFileSync(path.join(__dirname, '..', '..', file), 'utf8');
|
|
return new Function('"use strict";' + extractFn(src, name) + `\nreturn ${name};`)();
|
|
}
|
|
|
|
// R3c: the PURE geometry/label primitives were carved out of highway.js into
|
|
// static/js/highway-geometry.js. Same bodies, byte-for-byte — only the file moved.
|
|
const labels2D = loadFn('static/js/highway-geometry.js', 'chordHarmonyLabels');
|
|
const labels3D = loadFn('plugins/highway_3d/screen.js', 'chordHarmonyLabels');
|
|
|
|
for (const [name, fn] of [['2D', labels2D], ['3D', labels3D]]) {
|
|
test(`chordHarmonyLabels (${name}) surfaces rn + voicing + caged + guideTones`, () => {
|
|
assert.deepEqual(fn({ rn: 'ii7', q: 'm7', deg: 2 }, 'open', 'E', [4, 10]),
|
|
{ rn: 'ii7', voicing: 'open', caged: 'CAGED: E', guideTones: 'gt 4,10' });
|
|
});
|
|
|
|
test(`chordHarmonyLabels (${name}) trims whitespace`, () => {
|
|
assert.deepEqual(fn({ rn: ' V7 ' }, ' drop2 ', ' G ', []),
|
|
{ rn: 'V7', voicing: 'drop2', caged: 'CAGED: G', guideTones: '' });
|
|
});
|
|
|
|
test(`chordHarmonyLabels (${name}) empties absent / malformed inputs`, () => {
|
|
assert.deepEqual(fn(null, undefined),
|
|
{ rn: '', voicing: '', caged: '', guideTones: '' });
|
|
assert.deepEqual(fn({}, ''),
|
|
{ rn: '', voicing: '', caged: '', guideTones: '' });
|
|
assert.deepEqual(fn({ rn: 7 }, 7), // non-string
|
|
{ rn: '', voicing: '', caged: '', guideTones: '' });
|
|
assert.deepEqual(fn(undefined, 'shell'),
|
|
{ rn: '', voicing: 'shell', caged: '', guideTones: '' });
|
|
assert.deepEqual(fn({ rn: 'vi' }, null),
|
|
{ rn: 'vi', voicing: '', caged: '', guideTones: '' });
|
|
});
|
|
|
|
test(`chordHarmonyLabels (${name}) rejects invalid caged enum`, () => {
|
|
assert.equal(fn(null, null, 'X').caged, ''); // not a CAGED letter
|
|
assert.equal(fn(null, null, 'e').caged, ''); // lower-case rejected
|
|
assert.equal(fn(null, null, 7).caged, ''); // non-string
|
|
assert.equal(fn(null, null, ['E']).caged, ''); // non-string
|
|
assert.equal(fn(null, null, 'C').caged, 'CAGED: C');
|
|
});
|
|
|
|
test(`chordHarmonyLabels (${name}) filters out-of-range / non-int guide tones`, () => {
|
|
assert.equal(fn(null, null, '', [12, -1, 3, 'x', 10]).guideTones, 'gt 3,10');
|
|
assert.equal(fn(null, null, '', [0, 11]).guideTones, 'gt 0,11'); // boundaries kept
|
|
assert.equal(fn(null, null, '', []).guideTones, '');
|
|
assert.equal(fn(null, null, '', '4,10').guideTones, ''); // non-array
|
|
assert.equal(fn(null, null, '', [12, -1]).guideTones, ''); // all dropped
|
|
});
|
|
}
|