Files
Flight-Ops/lib/timeScale.test.ts
T

66 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { MAX_SCALE, MIN_SCALE, scaleToSlider, sliderToScale } from './timeScale';
describe('sliderToScale', () => {
it('is exact at the endpoints (0 → 1×, 1 → 60×)', () => {
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('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);
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('clamps out-of-range and non-finite input', () => {
expect(sliderToScale(-0.5)).toBe(MIN_SCALE);
expect(sliderToScale(1.5)).toBe(MAX_SCALE);
expect(sliderToScale(NaN)).toBe(MIN_SCALE);
});
});
describe('scaleToSlider', () => {
it('is exact at the endpoints (1× → 0, 60× → 1)', () => {
expect(scaleToSlider(1)).toBe(0);
expect(scaleToSlider(60)).toBe(1);
});
it('stays within [0, 1] for in-range scales', () => {
for (let s = MIN_SCALE; s <= MAX_SCALE; s++) {
const t = scaleToSlider(s);
expect(t).toBeGreaterThanOrEqual(0);
expect(t).toBeLessThanOrEqual(1);
}
});
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(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++) {
expect(sliderToScale(scaleToSlider(s))).toBe(s);
}
});
});