import { test } from "node:test"; import assert from "node:assert/strict"; import { SPACE_ITEMS_PER_KIND, alternateDisposition, capPerKind, compressSaving, diskNeedOrder, diskSuggestedBytes, fullestDiskId, groupSuggestions, leadKindsFor, moveFit, refusalGroup, spaceSummaryLine, suggestionsForDisk, type SpaceDisk, type SpaceScanResult, type SpaceSuggestion, } from "./spaceSuggestions"; const GB = 1024 ** 3; function disk(id: string, size: number, free: number, extra: Partial = {}): SpaceDisk { return { id, label: id, mountpoint: `/${id}`, status: "ok", roles: [], locationIds: [], sizeBytes: size * GB, freeBytes: free * GB, archive: null, archiveBytes: null, ...extra, }; } function sug(kind: SpaceSuggestion["kind"], diskId: string, bytes: number | null, extra: Partial = {}): SpaceSuggestion { return { key: `${kind}:${diskId}:${bytes}:${Math.random()}`, kind, title: kind, frees: bytes === null ? null : { disk: diskId, bytes: bytes * GB }, basis: "measured", basisText: "measured", loss: "nothing", ...extra, }; } test("disk need: below the floor first, then the smallest free share; unmounted and not answering last", () => { const disks = [ disk("platter", 1800, 202), disk("gone", 0, 0, { status: "not-mounted", sizeBytes: null, freeBytes: null }), disk("corpus", 1500, 24), disk("roomy", 100, 5), disk("tiny", 4000, 40), ]; // floor 30 GB: corpus (24) and roomy (5) are under it, and of those the smaller share free first. const order = diskNeedOrder(disks, 30 * GB).map((d) => d.id); assert.deepEqual(order, ["corpus", "roomy", "tiny", "platter", "gone"]); assert.equal(fullestDiskId(disks, 30 * GB), "corpus"); // No floor: by share alone. assert.deepEqual(diskNeedOrder(disks, 0).map((d) => d.id), ["tiny", "corpus", "roomy", "platter", "gone"]); }); test("suggestions for a disk: only what frees bytes there, biggest first; a compaction (frees nothing now) after every saving", () => { const result = { suggestions: [ sug("evict-clips", "corpus", 9), sug("compress-captions", "corpus", 62), sug("compact", "platter", null, { later: { disk: "platter", low: 40 * GB, high: 60 * GB, when: "after its original is dropped" } }), sug("drop-original", "platter", 30), sug("move-media", "corpus", 21, { fills: { disk: "platter", bytes: 21 * GB, fits: true, freeAfter: 181 * GB } }), ], } as Pick; assert.deepEqual( suggestionsForDisk(result, "corpus").map((s) => s.kind), ["compress-captions", "move-media", "evict-clips"], ); assert.deepEqual(suggestionsForDisk(result, "platter").map((s) => s.kind), ["drop-original", "compact"]); }); test("groups: by kind, subtotal of what each frees, the biggest group first; caption compression leads the corpus disk", () => { const list = [ sug("clean-audio", "corpus", 10), sug("clean-audio", "corpus", 11), sug("compress-captions", "corpus", 30), sug("compress-captions", "corpus", 32), sug("unstored-container", "corpus", 23), ]; const groups = groupSuggestions(list); assert.deepEqual(groups.map((g) => g.kind), ["compress-captions", "unstored-container", "clean-audio"]); assert.equal(groups[0].bytes, 62 * GB); assert.equal(groups[0].rows[0].frees!.bytes, 32 * GB); }); test("move fit: the target keeps its floor plus a margin, or the move is shown as not fitting", () => { assert.deepEqual(moveFit(200 * GB, 21 * GB, 30 * GB), { fits: true, freeAfter: 179 * GB }); assert.equal(moveFit(40 * GB, 21 * GB, 30 * GB).fits, false); assert.equal(moveFit(null, 21 * GB, 30 * GB).fits, false); assert.equal(moveFit(null, 21 * GB, 30 * GB).freeAfter, null); }); test("compress saving: what the plain files lose at the controller's ratio", () => { assert.equal(compressSaving(1000, 0.2), 800); assert.equal(compressSaving(0, 0.2), 0); }); test("cap per kind and disk: at most N rows each, the rest counted exactly", () => { const many = Array.from({ length: SPACE_ITEMS_PER_KIND + 7 }, (_, i) => sug("evict-clips", "corpus", i + 1)); const other = [sug("compress-captions", "corpus", 1)]; const { kept, overflow } = capPerKind([...many, ...other]); assert.equal(kept.filter((s) => s.kind === "evict-clips").length, SPACE_ITEMS_PER_KIND); assert.equal(kept.filter((s) => s.kind === "compress-captions").length, 1); assert.equal(overflow.length, 1); assert.equal(overflow[0].kind, "evict-clips"); assert.equal(overflow[0].count, 7); // The seven smallest are the ones left out: 1..7 GB. assert.equal(overflow[0].bytes, (1 + 2 + 3 + 4 + 5 + 6 + 7) * GB); }); test("refusal groups: a selector refusal reads as its rule, numbers left out", () => { assert.equal(refusalGroup("1.20 Mbit/s video is below minVideoMbps 3"), "below the bitrate floor"); assert.equal(refusalGroup("480p is below minHeight 720"), "below the height floor"); assert.equal(refusalGroup("platform youtube is not selected"), "platform not selected"); assert.equal( refusalGroup("an automatic keep-latest container (compaction is for deliberately stored videos only)"), "keep-latest (not deliberately stored)", ); assert.equal(refusalGroup('kept as "override" with no record that a person asked for it (persist-kept…)'), "override (not known to be deliberate)"); assert.equal(refusalGroup("no keep reason recorded (a legacy pointer): not known to be kept on purpose"), "legacy pointer"); assert.equal(refusalGroup("already AV1"), "already AV1"); assert.equal(refusalGroup("the channel opts out (excludeFromCompaction)"), "channel opts out"); assert.equal(refusalGroup("its last compaction failed its checks on 2026-11-01: x"), "last compaction failed"); assert.equal(refusalGroup("something new"), "other"); assert.equal(refusalGroup("already compacted"), null); }); test("alternates: a pin keeps every copy; a proxy is kept on purpose; an original is the compaction's; a former original needs its own word", () => { const base = { storedAt: "", dir: "/s", file: "a.mp4", bytes: 1 }; const alt = { file: "b.mp4", bytes: 1, storedAt: "", setAsideAt: "" }; assert.deepEqual(alternateDisposition({ ...base, keepReason: "override" }, alt), { offer: true }); assert.equal(alternateDisposition({ ...base, keepReason: "pin" }, alt).offer, false); assert.equal(alternateDisposition(base, { ...alt, keptAs: "proxy" }).offer, false); assert.equal(alternateDisposition(base, { ...alt, keptAs: "original" }).offer, false); const former = alternateDisposition(base, { ...alt, keptAs: "former-original" }); assert.equal(former.offer, true); assert.ok(former.offer && former.formerOriginal); }); test("summary line: the fullest disk, its free space and what the scan suggests there", () => { const result = { computedAt: "2026-11-24T10:00:00.000Z", floorBytes: 0, disks: [disk("corpus", 1500, 24, { label: "Corpus disk" }), disk("platter", 1800, 202)], suggestions: [sug("compress-captions", "corpus", 62), sug("evict-clips", "corpus", 9), sug("drop-original", "platter", 30)], } as unknown as SpaceScanResult; const line = spaceSummaryLine(result, { corpus: 25 * GB }); assert.match(line!, /^Corpus disk: 25\.00 GB free \(98 % used\) · ~71\.00 GB suggested there/); assert.equal(spaceSummaryLine(null, {}), null); }); test("groups: a lead kind goes first whatever its subtotal (caption compression on the corpus disk)", () => { const list = [sug("unstored-container", "corpus", 30), sug("compress-captions", "corpus", 20), sug("evict-clips", "corpus", 25)]; assert.deepEqual(groupSuggestions(list).map((g) => g.kind), ["unstored-container", "evict-clips", "compress-captions"]); assert.deepEqual(groupSuggestions(list, { lead: ["compress-captions"] }).map((g) => g.kind), ["compress-captions", "unstored-container", "evict-clips"]); assert.deepEqual(groupSuggestions([sug("evict-clips", "corpus", 1)], { lead: ["compress-captions"] }).map((g) => g.kind), ["evict-clips"]); assert.deepEqual(leadKindsFor("corpus"), ["compress-captions"]); assert.deepEqual(leadKindsFor("platter"), []); }); test("a disk's suggested total counts each file once: one channel's overlapping kinds are the larger, a move that does not fit is left out", () => { const list = [ sug("clean-audio", "corpus", 10, { slug: "a" }), sug("clean-extra-formats", "corpus", 4, { slug: "a" }), sug("move-media", "corpus", 12, { slug: "a" }), sug("compress-live-chat", "corpus", 3, { slug: "a" }), sug("clean-audio", "corpus", 5, { slug: "b" }), sug("move-media", "corpus", 50, { slug: "c", disabled: "does not fit" }), sug("compress-captions", "corpus", 20, { slug: "a" }), sug("evict-clips", "corpus", 1, { slug: "a" }), ]; // a: max(10, 4, 12, 3) = 12; b: 5; c's move left out; captions 20 and clips 1 add. assert.equal(diskSuggestedBytes(list), (12 + 5 + 20 + 1) * GB); });