Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import { describe, expect, it } from "vitest";
import { randomIndex, randomStart, RANDOM_START } from "./random";
/** An rng that walks an even ladder, so "uniform" is checkable exactly rather * than within some tolerance you have to argue about. */function ladder(steps: number): () => number { let i = 0; return () => { const v = i / steps; i = (i + 1) % steps; return v; };}
const tally = (count: number, avoid: number, rolls: number) => { const next = ladder(rolls); const hits = new Map<number, number>(); for (let n = 0; n < rolls; n++) { const at = randomIndex(count, avoid, next); hits.set(at, (hits.get(at) ?? 0) + 1); } return hits;};
describe("randomStart", () => { it("plays a short track from the top — there is no middle to drop into", () => { for (const d of [undefined, NaN, 0, 12, 44]) { expect(randomStart(d, () => 0.5)).toBe(0); } });
it("drops you into the part of a long track where that is the point", () => { const d = 300; for (const roll of [0, 0.2, 0.5, 0.999999]) { const at = randomStart(d, () => roll); expect(at).toBeGreaterThanOrEqual(RANDOM_START.earliest); // and never so late that the track announces itself by ending expect(at).toBeLessThanOrEqual(d - RANDOM_START.tail); } });
it("walks the window with the roll", () => { const d = 300; const first = randomStart(d, () => 0); const middle = randomStart(d, () => 0.5); const last = randomStart(d, () => 0.999999); expect(first).toBe(RANDOM_START.earliest); expect(middle).toBeGreaterThan(first); expect(last).toBeGreaterThan(middle); expect(last).toBeCloseTo(d - RANDOM_START.tail, 0); });
it("plays normally instead of inventing a position when the rng misbehaves", () => { for (const broken of [NaN, 1, -1, Infinity]) { expect(randomStart(300, () => broken)).toBe(0); } });
it("refuses a position that would leave less than the tail to hear", () => { // 45s is the shortest track it will place at all, and the window there is // 10 … 25 — pressing the key must not be able to skip the record expect(randomStart(45, () => 0.999999)).toBeLessThanOrEqual(45 - RANDOM_START.tail); expect(randomStart(45, () => 0)).toBeGreaterThanOrEqual(RANDOM_START.earliest); });});
describe("randomIndex", () => { it("says when there is nothing to choose", () => { expect(randomIndex(0)).toBe(-1); expect(randomIndex(-3)).toBe(-1); expect(randomIndex(NaN)).toBe(-1); expect(randomIndex(2.5)).toBe(-1); // a count is a count });
it("never hands back the track already playing", () => { const next = ladder(97); for (let n = 0; n < 500; n++) { const at = randomIndex(6, 2, next); expect(at).toBeGreaterThanOrEqual(0); expect(at).toBeLessThan(6); expect(at).not.toBe(2); } });
it("plays the only track there is, even if it is the one already playing", () => { // "play something else" loses to "play the only thing there is" expect(randomIndex(1, 0, () => 0.5)).toBe(0); });
it("is uniform over the eligible ones — including the neighbour of the excluded", () => { // the bias the skip-mapping exists to avoid: `if (at === avoid) at++` would // give index 3 twice the share of 0 and 1 const hits = tally(5, 2, 400); expect(hits.get(2)).toBeUndefined(); expect([...hits.keys()].sort()).toEqual([0, 1, 3, 4]); expect(new Set(hits.values()).size).toBe(1); });
it("without an exclusion covers every index", () => { const hits = tally(4, -1, 400); expect([...hits.keys()].sort()).toEqual([0, 1, 2, 3]); expect(new Set(hits.values()).size).toBe(1); // an avoid outside the range is not an exclusion at all expect(randomIndex(4, 9, () => 0.99)).toBe(3); });
it("survives an rng that has lost its mind", () => { for (const broken of [NaN, 1, 1.5, -0.5, Infinity]) { const at = randomIndex(5, 2, () => broken); expect(at).toBeGreaterThanOrEqual(0); expect(at).toBeLessThan(5); expect(at).not.toBe(2); // a broken roll is no licence to replay the current track } });});