feat: v0.9.2 — power-of-two time scale: slider steps through the 1/2/4/8/16/32/64 ladder, POST /api/sim clamp widened to [1,64]

This commit is contained in:
2026-09-17 19:15:15 +00:00
parent 62f9929b51
commit 03653fc50e
12 changed files with 80 additions and 51 deletions
+30 -15
View File
@@ -1,30 +1,34 @@
import { describe, expect, it } from 'vitest';
import { MAX_SCALE, MIN_SCALE, scaleToSlider, sliderToScale } from './timeScale';
import { MAX_SCALE, MIN_SCALE, TIME_SCALES, scaleToSlider, sliderToScale } from './timeScale';
const STEPS = TIME_SCALES.length - 1; // 6 intervals across the 7 ladder positions
describe('sliderToScale', () => {
it('is exact at the endpoints (0 → 1×, 1 → 60×)', () => {
it('is exact at the endpoints (0 → 1×, 1 → 64×)', () => {
expect(sliderToScale(0)).toBe(MIN_SCALE);
expect(sliderToScale(1)).toBe(MAX_SCALE);
});
it('maps mid-slider to ~8× (√60 rounds to 8)', () => {
expect(sliderToScale(0.5)).toBe(8);
it('maps each of the 7 positions to the power-of-two ladder', () => {
const expected = [1, 2, 4, 8, 16, 32, 64];
for (let i = 0; i < TIME_SCALES.length; i++) {
expect(sliderToScale(i / STEPS)).toBe(expected[i]);
}
});
it('is monotonic non-decreasing across the whole range', () => {
let prev = sliderToScale(0);
for (let i = 1; i <= 2000; i++) {
const t = i / 2000;
const s = sliderToScale(t);
const s = sliderToScale(i / 2000);
expect(s).toBeGreaterThanOrEqual(prev);
prev = s;
}
});
it('covers every integer scale 1..60 somewhere on the slider', () => {
const seen = new Set<number>();
for (let i = 0; i <= 2000; i++) seen.add(sliderToScale(i / 2000));
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) expect(seen.has(s)).toBe(true);
it('only ever returns a ladder value', () => {
for (let i = 0; i <= 2000; i++) {
expect(TIME_SCALES).toContain(sliderToScale(i / 2000));
}
});
it('clamps out-of-range and non-finite input', () => {
@@ -35,9 +39,15 @@ describe('sliderToScale', () => {
});
describe('scaleToSlider', () => {
it('is exact at the endpoints (1× → 0, 60× → 1)', () => {
it('is exact at the endpoints (1× → 0, 64× → 1)', () => {
expect(scaleToSlider(1)).toBe(0);
expect(scaleToSlider(60)).toBe(1);
expect(scaleToSlider(64)).toBe(1);
});
it('places each ladder value on its own position', () => {
for (let i = 0; i < TIME_SCALES.length; i++) {
expect(scaleToSlider(TIME_SCALES[i])).toBeCloseTo(i / STEPS, 12);
}
});
it('stays within [0, 1] for in-range scales', () => {
@@ -51,15 +61,20 @@ describe('scaleToSlider', () => {
it('clamps out-of-range and non-finite input', () => {
expect(scaleToSlider(0)).toBe(0);
expect(scaleToSlider(-3)).toBe(0);
expect(scaleToSlider(61)).toBe(1);
expect(scaleToSlider(65)).toBe(1);
expect(scaleToSlider(NaN)).toBe(0);
});
});
describe('round-trip', () => {
it('sliderToScale(scaleToSlider(s)) === s for every integer scale 1..60', () => {
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) {
it('sliderToScale(scaleToSlider(s)) === s for every ladder value', () => {
for (const s of TIME_SCALES) {
expect(sliderToScale(scaleToSlider(s))).toBe(s);
}
});
it('maps off-ladder scales to the nearest power of two', () => {
expect(sliderToScale(scaleToSlider(3))).toBe(4); // log2(3) ≈ 1.58 → step 2 (4×)
expect(sliderToScale(scaleToSlider(60))).toBe(64); // legacy max from the v0.9.x era
});
});