// The Play view's sound bank trim (bankTrim.mjs): past the budget the decodes // nobody wants go first, oldest use first; a wanted one never goes. // // Run with: node --test lib/report/bankTrim.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { coveringSlot, keysToTrim } from "./bankTrim.mjs"; const MB = 1024 * 1024; const slot = (key, mb, used, state = "ready") => ({ key, state, bytes: mb * MB, used }); test("under the budget nothing goes", () => { assert.deepEqual(keysToTrim([slot("a", 10, 1), slot("b", 10, 2)], null, 72 * MB), []); }); test("with nothing said wanted, the least recently used go until under", () => { assert.deepEqual(keysToTrim([slot("a", 40, 3), slot("b", 40, 1), slot("c", 40, 2)], null, 72 * MB), ["b", "c"]); }); test("a wanted decode never goes, even over the budget (the thrash at nt1/lw1/lj1)", () => { // nt1 81 s, lw1 88 s, og1 44 s: ~83 MB of PCM wanted together, past 72 MB. const slots = [slot("og1", 17, 1), slot("nt1", 31, 2), slot("lw1", 34, 3)]; assert.deepEqual(keysToTrim(slots, new Set(["og1", "nt1", "lw1"]), 72 * MB), []); }); test("stale-edge and out-of-window decodes go first, wanted ones stay", () => { const slots = [ slot("lw1|manifest-edges", 34, 9), // used recently, but its edges were replaced slot("tj2", 4, 1), // left the window slot("nt1", 31, 2), slot("lw1|render-edges", 34, 3), ]; assert.deepEqual(keysToTrim(slots, new Set(["nt1", "lw1|render-edges"]), 72 * MB), ["tj2", "lw1|manifest-edges"]); }); test("a decode still loading is not trimmed (it has no bytes to give back)", () => { assert.deepEqual(keysToTrim([slot("a", 0, 1, "loading"), slot("b", 80, 2)], new Set(["b"]), 72 * MB), []); }); test("coveringSlot: a decode of the clip's window that holds the seconds; ready before loading before failed", () => { const s = (key, state, from, to, used = 1) => ({ key, state, used, span: { from, to } }); const slots = [s("a|w|1|9|20", "loading", 9, 20, 5), s("a|w|1|9.5|15", "ready", 9.5, 15, 1), s("b|w|1|0|100", "ready", 0, 100, 9)]; assert.equal(coveringSlot(slots, "a|w|1|", { from: 10, to: 14 }), "a|w|1|9.5|15", "the ready one"); assert.equal(coveringSlot(slots, "a|w|1|", { from: 10, to: 19 }), "a|w|1|9|20", "only the one under way covers it"); assert.equal(coveringSlot(slots, "a|w|1|", { from: 8, to: 14 }), null, "none: another clip's never counts"); assert.equal(coveringSlot([s("a|w|1|0|9", "failed", 0, 9)], "a|w|1|", { from: 1, to: 2 }), "a|w|1|0|9", "a failure plays silent, not held"); assert.equal(coveringSlot([s("a|w|2|0|9", "ready", 0, 9)], "a|w|1|", { from: 1, to: 2 }), null, "the window's bytes replaced: another version"); });