refactor(h3d-carve-1b): extract render-order, note-key, camera-bootstrap, fretMid to src/geometry.js

Move 9 symbols verbatim from screen.js factory scope to geometry.js:
- RENDER_ORDER_LAYER_STACK, RENDER_ORDER_LAYER_INDEX, RENDER_ORDER_AT_Z_ZERO,
  RENDER_ORDER_FAR_CLAMP — compile-time constants, now exported from geometry.js
- renderOrderForLayerAtZ — pure fn; reads K + RENDER_ORDER_* from geometry scope
- _noteKey, lowerBoundT — hot-path helpers; no external deps
- hwyFirstRelevantFrettedTime — camera bootstrap scan; no external deps
- fretMid → geoFretMid(f, uniform) — same fretX delegator pattern as Cut 1;
  screen.js keeps: const fretMid = f => geoFretMid(f, _h3dFretUniform); (1 beyond-subst)

screen.js import line updated to import all 9 new exports; tombstones replace
each original definition.

Source-scan retargets:
- highway_3d_render_order.test.js: layers()/zZeroRenderOrder() now read
  GEOMETRY_JS; the 5 renderOrderForLayerAtZ-internals asserts in
  chordFrameRenderOrder test retargeted to geo() (call-site assert stays on src()).
- highway_3d_camera_bootstrap.test.js: extractFn now reads geoSrc (GEOMETRY_JS);
  sourceBetween wiring tests remain on SCREEN_JS.
- highway_3d_panel_controls.test.js: sandbox stubs extended with 9 new names.

Class-killer tests added to highway_3d_geometry.test.js (8 new tests, 180 total):
- RENDER_ORDER_LAYER_STACK length + first/last entries
- RENDER_ORDER_LAYER_INDEX spot-checks (CHORD_FILL=0, NOTE_CORE=10)
- renderOrderForLayerAtZ far-clamp (worldZ=-5 gives 50, not 33 without max)
- renderOrderForLayerAtZ unknown-layer throws
- _noteKey |0 truncation (1.5,3)=150003 not float-derived 150008
- lowerBoundT strict lower-bound (3 in [{t:1},{t:3},{t:5}] gives 1, not 2)
- hwyFirstRelevantFrettedTime smoke (empty → null)
- geoFretMid sentinel (f=0 gives -2K≈-0.015, not 0) + ratio invariant

Mutation analysis confirmed all 8 tests fail under their named mutation before
committing (per Toby r1 lesson).

Base run (a1f7ad5): 172/172 pass.
Post-cut run: 180/180 pass.
Command: node --test tests/js/highway_3d*.test.js plugins/highway_3d/tests/*.test.js

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uZ169yfoFYArXz962g7KW
This commit is contained in:
byrongamatos
2026-09-05 07:19:58 +02:00
co-authored by Claude Sonnet 4.6
parent a1f7ad5e68
commit 5e401afe87
7 changed files with 244 additions and 139 deletions
+4 -1
View File
@@ -13,6 +13,9 @@ const path = require('node:path');
const SCREEN_JS = path.join(__dirname, '..', '..', 'plugins', 'highway_3d', 'screen.js');
const src = fs.readFileSync(SCREEN_JS, 'utf8');
// Since h3d-carve-1b, hwyFirstRelevantFrettedTime lives in geometry.js.
const GEOMETRY_JS = path.join(__dirname, '..', '..', 'plugins', 'highway_3d', 'src', 'geometry.js');
const geoSrc = fs.readFileSync(GEOMETRY_JS, 'utf8');
function extractFn(source, name) {
const start = source.indexOf('function ' + name);
@@ -36,7 +39,7 @@ function sourceBetween(startText, endText) {
const hwyFirstRelevantFrettedTime = new Function(
'"use strict";'
+ extractFn(src, 'hwyFirstRelevantFrettedTime')
+ extractFn(geoSrc, 'hwyFirstRelevantFrettedTime')
+ '\nreturn hwyFirstRelevantFrettedTime;',
)();
+61
View File
@@ -99,6 +99,67 @@ test('camLowFretPullbackU is clamped to zero — high fret gives 0 not negative'
assert.strictEqual(camLowFretPullbackU(10), 0, 'fret 10: clamped to 0, not -20');
});
// ── Cut 1b class-killers ───────────────────────────────────────────────────────
test('RENDER_ORDER_LAYER_STACK has 17 layers with CHORD_FILL first and CHORD_FRET_LABEL last', async () => {
const { RENDER_ORDER_LAYER_STACK } = await import(GEOMETRY_JS);
assert.strictEqual(RENDER_ORDER_LAYER_STACK.length, 17, 'stack must have exactly 17 layers');
assert.strictEqual(RENDER_ORDER_LAYER_STACK[0], 'CHORD_FILL', 'first layer must be CHORD_FILL');
assert.strictEqual(RENDER_ORDER_LAYER_STACK[RENDER_ORDER_LAYER_STACK.length - 1], 'CHORD_FRET_LABEL', 'last layer must be CHORD_FRET_LABEL');
});
test('RENDER_ORDER_LAYER_INDEX maps CHORD_FILL to 0 and NOTE_CORE to 10', async () => {
// Mutation: wrong layer order → NOTE_CORE would not map to 10.
const { RENDER_ORDER_LAYER_INDEX } = await import(GEOMETRY_JS);
assert.strictEqual(RENDER_ORDER_LAYER_INDEX['CHORD_FILL'], 0, 'CHORD_FILL must be index 0 (bottom of stack)');
assert.strictEqual(RENDER_ORDER_LAYER_INDEX['NOTE_CORE'], 10, 'NOTE_CORE must be index 10');
});
test('renderOrderForLayerAtZ applies the far clamp — worldZ=-5 gives 50 not 33', async () => {
// Mutation: remove Math.max(RENDER_ORDER_FAR_CLAMP, ...) clamp.
// K=2.25/300=0.0075; Math.round(700+(-5)/0.0075)=Math.round(33.33)=33; max(50,33)=50.
// Without clamp: 33 + 0/17 ≈ 33. Test pins the clamped value.
const { renderOrderForLayerAtZ } = await import(GEOMETRY_JS);
assert.strictEqual(renderOrderForLayerAtZ(-5, 'CHORD_FILL'), 50, 'far objects must be clamped to RENDER_ORDER_FAR_CLAMP=50');
});
test('renderOrderForLayerAtZ throws for unknown layer names', async () => {
const { renderOrderForLayerAtZ } = await import(GEOMETRY_JS);
assert.throws(() => renderOrderForLayerAtZ(0, 'NONEXISTENT'), /Unknown 3D highway depth layer/);
});
test('_noteKey integer-truncates float times — _noteKey(1.5, 3) is 150003 not 150008', async () => {
// Mutation: drop |0 → (15000.5)*10+3 = 150008.
const { _noteKey } = await import(GEOMETRY_JS);
assert.strictEqual(_noteKey(1.5, 3), 150003, '|0 truncation must give 150003, not float-derived 150008');
assert.strictEqual(_noteKey(0, 0), 0);
});
test('lowerBoundT returns first index where arr[i].t >= t (strict lower bound)', async () => {
// Mutation: < → <= causes lowerBoundT([{t:1},{t:3},{t:5}], 3) → 2 instead of 1.
const { lowerBoundT } = await import(GEOMETRY_JS);
const arr = [{ t: 1 }, { t: 3 }, { t: 5 }];
assert.strictEqual(lowerBoundT(arr, 3), 1, 'strict lower-bound: first index where .t >= 3 is 1 (not 2)');
assert.strictEqual(lowerBoundT(arr, 0), 0, 'value before all: must return 0');
assert.strictEqual(lowerBoundT(arr, 6), 3, 'value after all: must return length');
});
test('hwyFirstRelevantFrettedTime returns null for empty/all-open input', async () => {
const { hwyFirstRelevantFrettedTime } = await import(GEOMETRY_JS);
assert.strictEqual(hwyFirstRelevantFrettedTime([], [], 0, 0.2, 6), null);
});
test('geoFretMid returns -2K sentinel for f<=0, positive for f=1', async () => {
// Mutation: drop f<=0 guard → geoFretMid(0, true) returns (0+0)/2=0, not -0.015.
const { geoFretMid } = await import(GEOMETRY_JS);
const K = 2.25 / 300;
assert.ok(Math.abs(geoFretMid(0, true) - (-2 * K)) < 1e-10, 'f=0 must return -2K sentinel (≈-0.015)');
assert.ok(geoFretMid(1, true) > 0, 'f=1 must return positive X');
// In uniform mode: geoFretX(0,true)=0, geoFretX(1,true)=step, so mid=step/2.
// geoFretMid(2,true) = (step+2step)/2 = 1.5step. Ratio 3 catches wrong f offset.
assert.ok(Math.abs(geoFretMid(2, true) / geoFretMid(1, true) - 3) < 1e-9, 'uniform mid(2)/mid(1) must equal 3');
});
test('_makeGaussTex peak alpha is 255 at the centre pixel', async () => {
// Mutation: default sigma changed to 0 → (u-0.5)/0 = NaN chain → all Uint8Array writes
// become 0 (TypedArray coerces NaN to 0). Test calls without explicit sigma so the
+10 -1
View File
@@ -55,7 +55,7 @@ function loadHighway3dStatics() {
},
},
// Geometry stubs — panel-controls test only reads factory statics;
// it never invokes the render path where these are called (h3d-carve-1).
// it never invokes the render path where these are called (h3d-carve-1b).
geoFretX: (f, _uniform) => f * 0.1,
dZ: dt => -dt,
slideTrailEnd: () => null,
@@ -63,6 +63,15 @@ function loadHighway3dStatics() {
camLowFretPullbackU: () => 0,
computeBPM: () => 120,
_makeGaussTex: () => ({}),
RENDER_ORDER_LAYER_STACK: Object.freeze([]),
RENDER_ORDER_LAYER_INDEX: Object.freeze(Object.create(null)),
RENDER_ORDER_AT_Z_ZERO: 700,
RENDER_ORDER_FAR_CLAMP: 50,
renderOrderForLayerAtZ: () => 0,
_noteKey: () => 0,
lowerBoundT: () => 0,
hwyFirstRelevantFrettedTime: () => null,
geoFretMid: (f, _uniform) => f * 0.1,
};
vm.createContext(sandbox);
vm.runInContext(instrumented, sandbox, { filename: SCREEN_JS });
+19 -8
View File
@@ -41,6 +41,9 @@ const fs = require('node:fs');
const path = require('node:path');
const SCREEN_JS = path.join(__dirname, '..', '..', 'plugins', 'highway_3d', 'screen.js');
// Since h3d-carve-1b, RENDER_ORDER_* constants and renderOrderForLayerAtZ
// live in geometry.js; screen.js imports them.
const GEOMETRY_JS = path.join(__dirname, '..', '..', 'plugins', 'highway_3d', 'src', 'geometry.js');
// ---------------------------------------------------------------------------
// Helpers
@@ -53,9 +56,16 @@ function src() {
return _src;
}
/** Parses the declared render-order layer stack from screen.js. */
let _geo;
/** Returns the cached geometry source (render-order constants + renderOrderForLayerAtZ). */
function geo() {
if (!_geo) _geo = fs.readFileSync(GEOMETRY_JS, 'utf8');
return _geo;
}
/** Parses the declared render-order layer stack from geometry.js. */
function layers() {
const match = src().match(/const\s+RENDER_ORDER_LAYER_STACK\s*=\s*Object\.freeze\(\s*\[([\s\S]*?)\]\s*\)/);
const match = geo().match(/const\s+RENDER_ORDER_LAYER_STACK\s*=\s*Object\.freeze\(\s*\[([\s\S]*?)\]\s*\)/);
assert.ok(match, 'RENDER_ORDER_LAYER_STACK must be declared');
return Array.from(match[1].matchAll(/'([^']+)'/g), m => m[1]);
}
@@ -70,7 +80,7 @@ function layerIndex(name) {
/** Reads the render-order base used for objects at z = 0. */
function zZeroRenderOrder() {
const match = src().match(/const\s+RENDER_ORDER_AT_Z_ZERO\s*=\s*(-?\d+(?:\.\d+)?)\s*;/);
const match = geo().match(/const\s+RENDER_ORDER_AT_Z_ZERO\s*=\s*(-?\d+(?:\.\d+)?)\s*;/);
assert.ok(match, 'RENDER_ORDER_AT_Z_ZERO must be declared');
return Number(match[1]);
}
@@ -301,14 +311,15 @@ test('chordFrameRenderOrder uses renderOrderForLayerAtZ(z, CHORD_FRAME)', () =>
/const\s+chordFrameRenderOrder\s*=\s*renderOrderForLayerAtZ\(\s*z\s*,\s*'CHORD_FRAME'\s*\)\s*;/,
'chordFrameRenderOrder must use renderOrderForLayerAtZ(z, CHORD_FRAME)',
);
assert.match(src(), /const\s+RENDER_ORDER_LAYER_INDEX\s*=\s*Object\.freeze\(\s*RENDER_ORDER_LAYER_STACK\.reduce\(/);
assert.match(src(), /const\s+layerIndex\s*=\s*RENDER_ORDER_LAYER_INDEX\[layerName\]\s*;/);
assert.match(src(), /if\s*\(\s*layerIndex\s*===\s*undefined\s*\)\s*throw\s+new\s+Error\(`Unknown 3D highway depth layer: \$\{layerName\}`\)\s*;/);
assert.match(src(), /const\s+depthRenderOrder\s*=\s*Math\.max\(\s*RENDER_ORDER_FAR_CLAMP\s*,\s*Math\.round\(\s*RENDER_ORDER_AT_Z_ZERO\s*\+\s*worldZ\s*\/\s*K\s*\)\s*\)\s*;/);
// renderOrderForLayerAtZ implementation lives in geometry.js since h3d-carve-1b.
assert.match(geo(), /const\s+RENDER_ORDER_LAYER_INDEX\s*=\s*Object\.freeze\(\s*RENDER_ORDER_LAYER_STACK\.reduce\(/);
assert.match(geo(), /const\s+layerIndex\s*=\s*RENDER_ORDER_LAYER_INDEX\[layerName\]\s*;/);
assert.match(geo(), /if\s*\(\s*layerIndex\s*===\s*undefined\s*\)\s*throw\s+new\s+Error\(`Unknown 3D highway depth layer: \$\{layerName\}`\)\s*;/);
assert.match(geo(), /const\s+depthRenderOrder\s*=\s*Math\.max\(\s*RENDER_ORDER_FAR_CLAMP\s*,\s*Math\.round\(\s*RENDER_ORDER_AT_Z_ZERO\s*\+\s*worldZ\s*\/\s*K\s*\)\s*\)\s*;/);
// Layer is a sub-unit fraction so the integer depth bucket strictly
// dominates (a farther object can't outrank a nearer one via a higher
// layer); the layer only breaks ties within the same depth bucket.
assert.match(src(), /return\s+depthRenderOrder\s*\+\s*layerIndex\s*\/\s*RENDER_ORDER_LAYER_STACK\.length\s*;/);
assert.match(geo(), /return\s+depthRenderOrder\s*\+\s*layerIndex\s*\/\s*RENDER_ORDER_LAYER_STACK\.length\s*;/);
assert.ok(layerIndex('CHORD_FRAME') < layerIndex('NOTE_OUTLINE'));
});