import { test } from "node:test"; import assert from "node:assert/strict"; import { placeChannelRank, rankedOrder, RankPlacementError, sanitizeChannelPriority, type ChannelPriority, } from "./channelPriority"; // `pnpm ops channel-rank`: one channel moved in the ONE order; every other // channel's order kept exactly; nothing but the rank changes. const doc = (channels: ChannelPriority["channels"]): ChannelPriority => ({ focus: { kind: "none" }, channels }); const order = (m: ChannelPriority) => rankedOrder(m).map((r) => r.slug); const ABC = doc({ a: { tier: "normal", rank: 0 }, b: { tier: "low", rank: 1, overrides: { sync: "normal" } }, c: { tier: "normal", rank: 2 }, u: { tier: "paused", overrides: { sync: "normal" } }, }); test("top and bottom: first and last of the ranked channels; tier and overrides untouched", () => { const top = placeChannelRank(ABC, "c", { kind: "top" }); assert.deepEqual(order(top), ["c", "a", "b"]); assert.deepEqual(top.channels.b, { tier: "low", rank: 2, overrides: { sync: "normal" } }); const bottom = placeChannelRank(ABC, "a", { kind: "bottom" }); assert.deepEqual(order(bottom), ["b", "c", "a"]); assert.equal(bottom.channels.a.rank, 3, "after the last rank; nothing else shifts"); assert.equal(bottom.channels.b.rank, 1); // An unranked channel taken to the top keeps its tier and overrides. const u = placeChannelRank(ABC, "u", { kind: "top" }); assert.deepEqual(order(u), ["u", "a", "b", "c"]); assert.deepEqual(u.channels.u, { tier: "paused", overrides: { sync: "normal" }, rank: 0 }); // A channel with no entry at all gets one, at the default tier. assert.deepEqual(placeChannelRank(ABC, "new", { kind: "bottom" }).channels.new, { tier: "normal", rank: 3 }); // Nothing ranked: top and bottom are both rank 0. assert.equal(placeChannelRank(doc({}), "x", { kind: "top" }).channels.x.rank, 0); assert.equal(placeChannelRank(doc({}), "x", { kind: "bottom" }).channels.x.rank, 0); }); test("before and after another ranked channel", () => { assert.deepEqual(order(placeChannelRank(ABC, "a", { kind: "before", slug: "c" })), ["b", "a", "c"]); assert.deepEqual(order(placeChannelRank(ABC, "c", { kind: "after", slug: "a" })), ["a", "c", "b"]); assert.deepEqual(order(placeChannelRank(ABC, "a", { kind: "after", slug: "c" })), ["b", "c", "a"]); assert.deepEqual(order(placeChannelRank(ABC, "u", { kind: "before", slug: "a" })), ["u", "a", "b", "c"]); assert.throws( () => placeChannelRank(ABC, "a", { kind: "before", slug: "u" }), (e: Error) => e instanceof RankPlacementError && /"u" has no rank, so there is no place before it/.test(e.message), ); assert.throws(() => placeChannelRank(ABC, "a", { kind: "after", slug: "a" }), /cannot be placed after itself/); }); test("rank n: taken pushes it and everything below down one; free takes the gap; unrank removes it", () => { const taken = placeChannelRank(ABC, "c", { kind: "rank", rank: 1 }); assert.deepEqual(order(taken), ["a", "c", "b"]); assert.deepEqual(rankedOrder(taken), [ { slug: "a", rank: 0 }, { slug: "c", rank: 1 }, { slug: "b", rank: 2 }, ]); const gap = placeChannelRank(doc({ a: { tier: "normal", rank: 0 }, c: { tier: "normal", rank: 10 } }), "b", { kind: "rank", rank: 5, }); assert.deepEqual(rankedOrder(gap), [ { slug: "a", rank: 0 }, { slug: "b", rank: 5 }, { slug: "c", rank: 10 }, ]); assert.throws(() => placeChannelRank(ABC, "a", { kind: "rank", rank: -1 }), /a whole number, 0 or more/); assert.throws(() => placeChannelRank(ABC, "a", { kind: "rank", rank: 1.5 }), /a whole number, 0 or more/); const un = placeChannelRank(ABC, "b", { kind: "unrank" }); assert.deepEqual(order(un), ["a", "c"]); assert.deepEqual(un.channels.b, { tier: "low", overrides: { sync: "normal" } }); // A channel whose entry said only its rank is dropped by the sanitizer. assert.equal(sanitizeChannelPriority(placeChannelRank(ABC, "a", { kind: "unrank" })).channels.a, undefined); assert.equal(placeChannelRank(ABC, "u", { kind: "unrank" }), ABC, "nothing to remove: the input"); }); test("ties: after X lands immediately after X, before its tie partners, and ties below stay tied", () => { const tied = doc({ a: { tier: "normal", rank: 0 }, x: { tier: "normal", rank: 0 }, y: { tier: "normal", rank: 0 }, p: { tier: "normal", rank: 3 }, q: { tier: "normal", rank: 3 }, }); assert.deepEqual(order(tied), ["a", "x", "y", "p", "q"]); assert.deepEqual(order(placeChannelRank(tied, "z", { kind: "after", slug: "a" })), ["a", "z", "x", "y", "p", "q"]); assert.deepEqual(order(placeChannelRank(tied, "z", { kind: "after", slug: "x" })), ["a", "x", "z", "y", "p", "q"]); assert.deepEqual(order(placeChannelRank(tied, "z", { kind: "before", slug: "y" })), ["a", "x", "z", "y", "p", "q"]); assert.deepEqual(order(placeChannelRank(tied, "z", { kind: "before", slug: "a" })), ["z", "a", "x", "y", "p", "q"]); const moved = placeChannelRank(tied, "z", { kind: "after", slug: "a" }); assert.equal(moved.channels.x.rank, moved.channels.y.rank, "x and y were tied and stay tied"); assert.equal(moved.channels.p.rank, 3, "a channel already far enough down does not move"); assert.equal(moved.channels.p.rank, moved.channels.q.rank); // Moving a channel out of a tie. assert.deepEqual(order(placeChannelRank(tied, "a", { kind: "after", slug: "y" })), ["x", "y", "a", "p", "q"]); });