import assert from "node:assert/strict"; import test from "node:test"; import { FRAME_BUDGET_MS, FREEZE_BUDGET_MS, QUALITY_TIERS, chooseTier, initialTier } from "../src/lib/blackhole/quality.ts"; const samples = (ms, count = 30) => Array.from({ length: count }, () => ms); test("the governor steps down one tier when the median render time exceeds the budget", () => { assert.equal(chooseTier({ tier: 0, samples: samples(FRAME_BUDGET_MS + 4), sinceChangeMs: 5000 }), 1); assert.equal(chooseTier({ tier: 0, samples: samples(FRAME_BUDGET_MS - 4), sinceChangeMs: 5000 }), 0); }); test("the governor ignores a lone spike, too few samples and a tier that has not settled", () => { const spiky = [...samples(4, 29), 90]; assert.equal(chooseTier({ tier: 1, samples: spiky, sinceChangeMs: 5000 }), 1); assert.equal(chooseTier({ tier: 1, samples: samples(40, 5), sinceChangeMs: 5000 }), 1); assert.equal(chooseTier({ tier: 1, samples: samples(40), sinceChangeMs: 200 }), 1); }); test("the governor stops at the lowest tier and never steps back up", () => { const last = QUALITY_TIERS.length - 1; assert.equal(chooseTier({ tier: last, samples: samples(80), sinceChangeMs: 5000 }), last); assert.equal(chooseTier({ tier: 2, samples: samples(0.5), sinceChangeMs: 5000 }), 2); }); test("motion only freezes when the lowest animated tier is clearly failing", () => { const minimal = QUALITY_TIERS.findIndex(({ name }) => name === "minimal"); assert.equal(chooseTier({ tier: minimal, samples: samples(FREEZE_BUDGET_MS - 4), sinceChangeMs: 5000 }), minimal); assert.equal(chooseTier({ tier: minimal, samples: samples(FREEZE_BUDGET_MS + 4), sinceChangeMs: 5000 }), minimal + 1); assert.equal(QUALITY_TIERS[minimal + 1].maxFps, 0); }); test("tiers get strictly cheaper and software rendering starts at the bottom", () => { for (let i = 1; i < QUALITY_TIERS.length; i++) { assert.ok(QUALITY_TIERS[i].scale <= QUALITY_TIERS[i - 1].scale); assert.ok(QUALITY_TIERS[i].steps <= QUALITY_TIERS[i - 1].steps); assert.ok(QUALITY_TIERS[i].maxFps <= QUALITY_TIERS[i - 1].maxFps); } assert.equal(initialTier({ software: true, pixels: 1e6 }), QUALITY_TIERS.length - 1); assert.equal(initialTier({ software: false, cores: 8, memoryGb: 16, pixels: 2e6 }), 0); assert.equal(initialTier({ software: false, cores: 4, pixels: 2e6 }), 1); });