| 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( |
| 36 | pinFromForm( |
| 37 | form([ |
| 38 | ["intent", "pin"], |
| 39 | ["slug", " Web "], |
| 40 | ]), |
| 41 | ), |
| 42 | { intent: "pin", slug: "web", position: null }, |
| 43 | ); |
| 44 | assert.deepEqual( |
| 45 | pinFromForm( |
| 46 | form([ |
| 47 | ["intent", "pin"], |
| 48 | ["slug", "web"], |
| 49 | ["position", "2"], |
| 50 | ]), |
| 51 | ), |
| 52 | { intent: "pin", slug: "web", position: 2 }, |
| 53 | ); |
| 54 | assert.deepEqual( |
| 55 | pinFromForm( |
| 56 | form([ |
| 57 | ["intent", "unpin"], |
| 58 | ["slug", "web"], |
| 59 | ]), |
| 60 | ), |
| 61 | { intent: "unpin", slug: "web" }, |
| 62 | ); |
| 63 | assert.deepEqual( |
| 64 | pinFromForm( |
| 65 | form([ |
| 66 | ["intent", "reorder"], |
| 67 | ["slug", "b"], |
| 68 | ["slug", "a"], |
| 69 | ]), |
| 70 | ), |
| 71 | { intent: "reorder", slugs: ["b", "a"] }, |
| 72 | ); |
| 73 | assert.equal( |
| 74 | pinFromForm( |
| 75 | form([ |
| 76 | ["intent", "reorder"], |
| 77 | ["slug", "a"], |
| 78 | ["slug", "a"], |
| 79 | ]), |
| 80 | ), |
| 81 | null, |
| 82 | ); |
| 83 | assert.equal(pinFromForm(form([["intent", "pin"]])), null); |
| 84 | assert.equal( |
| 85 | pinFromForm( |
| 86 | form([ |
| 87 | ["intent", "star"], |
| 88 | ["slug", "web"], |
| 89 | ]), |
| 90 | ), |
| 91 | null, |
| 92 | ); |
| 93 | }); |
| 94 | |
| 95 | test("a pin moves up or down within the list", () => { |
| 96 | assert.deepEqual(movedPin(["a", "b", "c"], 2, -1), ["a", "c", "b"]); |
| 97 | assert.deepEqual(movedPin(["a", "b", "c"], 0, 1), ["b", "a", "c"]); |
| 98 | assert.equal(movedPin(["a", "b", "c"], 0, -1), null); |
| 99 | assert.equal(movedPin(["a", "b", "c"], 2, 1), null); |
| 100 | }); |