| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { MAX_RECENT, movedPin, pinFromForm, recentWith } from "./pins.ts"; |
| 5 | |
| 6 | const p = (name: string, namespace = "acme") => ({ namespace, name, isPrivate: false }); |
| 7 | const names = (list: { name: string }[]) => list.map((project) => project.name); |
| 8 | |
| 9 | test("the project being looked at leads recent, once", () => { |
| 10 | assert.deepEqual(names(recentWith([p("a"), p("b")], [], p("c"), "acme")), ["c", "a", "b"]); |
| 11 | assert.deepEqual(names(recentWith([p("a"), p("b")], [], p("b"), "acme")), ["b", "a"]); |
| 12 | }); |
| 13 | |
| 14 | test("pinned projects are never also recent", () => { |
| 15 | assert.deepEqual(names(recentWith([p("a"), p("b")], [p("a")], p("b"), "acme")), ["b"]); |
| 16 | assert.deepEqual(names(recentWith([p("a")], [p("c")], p("c"), "acme")), ["a"]); |
| 17 | }); |
| 18 | |
| 19 | test("another workspace's project is not this one's recent", () => { |
| 20 | assert.deepEqual(names(recentWith([p("a")], [], p("x", "other"), "acme")), ["a"]); |
| 21 | assert.deepEqual(names(recentWith([p("a")], [], p("x"), null)), ["a"]); |
| 22 | }); |
| 23 | |
| 24 | test("recent holds a few", () => { |
| 25 | const many = Array.from({ length: 9 }, (_, i) => p(`r${i}`)); |
| 26 | assert.equal(recentWith(many, [], p("now"), "acme").length, MAX_RECENT); |
| 27 | }); |
| 28 | |
| 29 | test("a pin form says what to change", () => { |
| 30 | const form = (entries: [string, string][]) => { |
| 31 | const data = new FormData(); |
| 32 | for (const [key, value] of entries) data.append(key, value); |
| 33 | return data; |
| 34 | }; |
| 35 | assert.deepEqual(pinFromForm(form([["intent", "pin"], ["slug", " Web "]])), { intent: "pin", slug: "web", position: null }); |
| 36 | assert.deepEqual(pinFromForm(form([["intent", "pin"], ["slug", "web"], ["position", "2"]])), { intent: "pin", slug: "web", position: 2 }); |
| 37 | assert.deepEqual(pinFromForm(form([["intent", "unpin"], ["slug", "web"]])), { intent: "unpin", slug: "web" }); |
| 38 | assert.deepEqual(pinFromForm(form([["intent", "reorder"], ["slug", "b"], ["slug", "a"]])), { intent: "reorder", slugs: ["b", "a"] }); |
| 39 | assert.equal(pinFromForm(form([["intent", "reorder"], ["slug", "a"], ["slug", "a"]])), null); |
| 40 | assert.equal(pinFromForm(form([["intent", "pin"]])), null); |
| 41 | assert.equal(pinFromForm(form([["intent", "star"], ["slug", "web"]])), null); |
| 42 | }); |
| 43 | |
| 44 | test("a pin moves up or down within the list", () => { |
| 45 | assert.deepEqual(movedPin(["a", "b", "c"], 2, -1), ["a", "c", "b"]); |
| 46 | assert.deepEqual(movedPin(["a", "b", "c"], 0, 1), ["b", "a", "c"]); |
| 47 | assert.equal(movedPin(["a", "b", "c"], 0, -1), null); |
| 48 | assert.equal(movedPin(["a", "b", "c"], 2, 1), null); |
| 49 | }); |