jevstrudel.git / test / tunes.test.mjs
1import { queryCode, testCycles } from './runtime.mjs';
2import * as tunes from '../website/src/repl/tunes.mjs';
3import { describe, it } from 'vitest';
4
5const tuneKeys = Object.keys(tunes);
6
7// Node 24 tightened Number→string rounding; clamp decimals so snapshots stay stable across engines.
8const roundFloatStrings = (input, precision = 12) => {
9  // if matches a decimal number ex: 12.34, -0.5, 0.123, 99.0, 1.932093850293
10  const regex = /-?\d+\.\d+/g;
11  return input.replace(regex, (match) => {
12    // converts the literal to a number, performs round to nearest (ties to even)
13    // at the requested precision, and returns the rounded decimal string
14    const rounded = Number(match).toFixed(precision);
15    // trims trailing zeros (and a dangling dot) after rounding, so the displayed string looks tidy
16    return rounded.replace(/\.?0+$/, '').replace(/\.$/, '');
17  });
18};
19
20describe('renders tunes', () => {
21  tuneKeys.forEach((key) => {
22    it(`tune: ${key}`, async ({ expect }) => {
23      const haps = await queryCode(tunes[key], testCycles[key] || 1);
24      const normalized = haps.map((hap) => roundFloatStrings(hap));
25      expect(normalized).toMatchSnapshot();
26    });
27  });
28});