metronome/tests/adapters/js_adapter.mjs
Me Here 754ed1c22d Formalize track format: spec + golden-vector conformance suite
Single source of truth for the track ("program"/"patch") grammar, which was
implemented by hand in src/engine.js and pico-cp/app.py with no cross-check and
had quietly drifted.

- docs/track-format.md: formal grammar, container (programs.json) schema with a
  version field, the new per-track playback-flow model (rep/end + relative goto;
  default = loop forever), normalization rules, and a list of known divergences.
- tests/: golden vectors + a runner that loads the REAL engine.js and app.py
  grammar (no copies; app.py via ast extraction) and compares both against the
  spec. Exit non-zero on unexpected mismatch or round-trip break -> usable as CI.

Surfaces real divergences for follow-up: default accent pattern (no =pattern)
differs web vs device and affects shipped presets; euclid not parsed on device;
vol/cd dropped on device; unknown-sound fallback; tempo clamp; empty patch.
The rep/end playback-flow vectors are the acceptance test for building that.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-30 23:54:20 -05:00

51 lines
2.1 KiB
JavaScript

// JS adapter: loads the REAL grammar out of src/engine.js (no copy) and emits the
// neutral normalized structure defined in docs/track-format.md §5.
//
// engine.js has no top-level side effects (only const/function declarations) and references
// `window` only inside ensureAudio(), which we never call — so it loads cleanly in Node.
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const here = dirname(fileURLToPath(import.meta.url));
const src = readFileSync(join(here, "..", "..", "src", "engine.js"), "utf8");
// Expose the pure codec functions from engine.js's top-level scope.
globalThis.window = globalThis.window || {};
const api = new Function(src + "\n;return { patchToSetup, setupToPatch };")();
const groupsArr = (s) => String(s).split(/[^0-9]+/).filter(Boolean).map(Number);
export function normalize(patch) {
const s = api.patchToSetup(patch);
return {
bpm: s.bpm,
bars: s.bars || 0,
volume: s.volume == null ? null : s.volume,
countMs: s.countMs || 0,
ramp: s.ramp && s.ramp.on ? { start: s.ramp.startBpm, amt: s.ramp.amount, every: s.ramp.everyBars } : null,
trainer: s.trainer && s.trainer.on ? { play: s.trainer.playBars, mute: s.trainer.muteBars } : null,
rep: s.rep == null ? null : s.rep, // not parsed yet → undefined → null
end: s.end == null ? null : s.end, // not parsed yet → undefined → null
lanes: (s.lanes || []).map((c) => ({
sound: c.sound,
groups: groupsArr(c.groupsStr),
sub: c.stepsPerBeat || 1,
swing: !!c.swing,
poly: !!c.poly,
mute: c.enabled === false,
gainDb: c.gainDb || 0,
levels: (c.beatsOn || []).map((v) => v | 0),
})),
};
}
// serialize(parse(x)) — used for the idempotency check.
export function canonical(patch) {
return api.setupToPatch(api.patchToSetup(patch));
}
// CLI: `node js_adapter.mjs '<patch>'` prints normalized JSON.
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
process.stdout.write(JSON.stringify(normalize(process.argv[2] ?? "")));
}