Skip to content

g1t/services/projects/src/pins.test.ts

44 lines1,989 bytesCodeBlame
1import { test } from "node:test";
2import assert from "node:assert/strict";
3
4import { MAX_PINS, placePin, recentAfterPins, reorderPins } from "./pins.ts";
5
6test("a pin goes at the end, or where it is asked for", () => {
7 assert.deepEqual(placePin([], "a"), ["a"]);
8 assert.deepEqual(placePin(["a", "b"], "c"), ["a", "b", "c"]);
9 assert.deepEqual(placePin(["a", "b"], "c", 0), ["c", "a", "b"]);
10 assert.deepEqual(placePin(["a", "b"], "c", 1), ["a", "c", "b"]);
11 // Past either end lands at that end.
12 assert.deepEqual(placePin(["a", "b"], "c", 99), ["a", "b", "c"]);
13 assert.deepEqual(placePin(["a", "b"], "c", -3), ["c", "a", "b"]);
14});
15
16test("pinning one already pinned moves it, never twice", () => {
17 assert.deepEqual(placePin(["a", "b", "c"], "c", 0), ["c", "a", "b"]);
18 assert.deepEqual(placePin(["a", "b", "c"], "a"), ["b", "c", "a"]);
19});
20
21test("a workspace holds a limited number of pins", () => {
22 const full = Array.from({ length: MAX_PINS }, (_, i) => `p${i}`);
23 assert.equal(placePin(full, "new"), null);
24 // Moving one that is already there is still fine.
25 assert.deepEqual(placePin(full, "p3", 0)?.[0], "p3");
26 assert.equal(placePin(full.slice(1), "new")?.length, MAX_PINS);
27});
28
29test("reordering names every pin once and nothing else", () => {
30 assert.deepEqual(reorderPins(["a", "b", "c"], ["c", "a", "b"]), { ok: true, order: ["c", "a", "b"] });
31 assert.equal(reorderPins(["a", "b"], ["a", "a"]).ok, false);
32 assert.equal(reorderPins(["a", "b"], ["a"]).ok, false);
33 assert.equal(reorderPins(["a", "b"], ["a", "b", "z"]).ok, false);
34 assert.deepEqual(reorderPins([], []), { ok: true, order: [] });
35});
36
37test("recent leaves out what is pinned and keeps the latest few", () => {
38 const visited = ["a", "b", "c", "d", "e", "f", "g"].map((id) => ({ id }));
39 assert.deepEqual(
40 recentAfterPins(visited, ["b", "d"]).map((p) => p.id),
41 ["a", "c", "e", "f", "g"],
42 );
43 assert.deepEqual(recentAfterPins(visited, [], 2).map((p) => p.id), ["a", "b"]);
44});