| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | DEFAULT_QUERY, |
| 6 | type Listed, |
| 7 | PAGE_SIZE, |
| 8 | facetsOf, |
| 9 | filterProjects, |
| 10 | isFiltered, |
| 11 | nextRow, |
| 12 | pageOf, |
| 13 | projectQueryString, |
| 14 | readProjectQuery, |
| 15 | } from "./project-list.ts"; |
| 16 | |
| 17 | function project(slug: string, more: Partial<Listed> = {}): Listed { |
| 18 | return { |
| 19 | id: `prj_${slug}`, |
| 20 | slug, |
| 21 | name: slug, |
| 22 | description: null, |
| 23 | private: false, |
| 24 | archived: false, |
| 25 | kind: "app", |
| 26 | ecosystem: null, |
| 27 | updatedAt: "2026-10-01T00:00:00.000Z", |
| 28 | pushedAt: null, |
| 29 | activity: 0, |
| 30 | deploying: false, |
| 31 | mirror: null, |
| 32 | ...more, |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | const q = (search: string) => readProjectQuery(new URLSearchParams(search)); |
| 37 | |
| 38 | test("an empty address is the default list", () => { |
| 39 | assert.deepEqual(q(""), DEFAULT_QUERY); |
| 40 | assert.equal(projectQueryString(DEFAULT_QUERY), ""); |
| 41 | assert.equal(isFiltered(DEFAULT_QUERY), false); |
| 42 | }); |
| 43 | |
| 44 | test("what the address says is read, and nonsense is the default", () => { |
| 45 | const query = q("q=%20api%20&visibility=private&kind=library&language=Rust&deployments=on&archived=only&sort=active&view=grid&page=3"); |
| 46 | assert.deepEqual(query, { |
| 47 | q: "api", |
| 48 | visibility: "private", |
| 49 | kind: "library", |
| 50 | language: "Rust", |
| 51 | deployments: true, |
| 52 | archived: "only", |
| 53 | sort: "active", |
| 54 | view: "grid", |
| 55 | page: 3, |
| 56 | }); |
| 57 | assert.deepEqual(q("visibility=secret&sort=random&page=-2&view=table"), DEFAULT_QUERY); |
| 58 | }); |
| 59 | |
| 60 | test("the address round-trips, and a change goes back to page one", () => { |
| 61 | const query = q("q=web&sort=name&page=4"); |
| 62 | assert.equal(projectQueryString(query, { page: 4 }), "?q=web&sort=name&page=4"); |
| 63 | assert.equal(projectQueryString(query, { kind: "library" }), "?q=web&kind=library&sort=name"); |
| 64 | assert.deepEqual(q(projectQueryString(query, { page: 4 }).slice(1)), query); |
| 65 | }); |
| 66 | |
| 67 | test("archived projects are left out unless asked for", () => { |
| 68 | const list = [project("live"), project("old", { archived: true })]; |
| 69 | assert.deepEqual(filterProjects(list, DEFAULT_QUERY).map((p) => p.slug), ["live"]); |
| 70 | assert.deepEqual(filterProjects(list, { ...DEFAULT_QUERY, archived: "only" }).map((p) => p.slug), ["old"]); |
| 71 | assert.equal(filterProjects(list, { ...DEFAULT_QUERY, archived: "include" }).length, 2); |
| 72 | }); |
| 73 | |
| 74 | test("filters narrow by visibility, kind, language and deployments", () => { |
| 75 | const list = [ |
| 76 | project("site", { deploying: true, ecosystem: "npm" }), |
| 77 | project("lib", { kind: "library", ecosystem: "cargo", private: true }), |
| 78 | project("tool", { ecosystem: "go" }), |
| 79 | ]; |
| 80 | const slugs = (change: object) => filterProjects(list, { ...DEFAULT_QUERY, ...change }).map((p) => p.slug).sort(); |
| 81 | assert.deepEqual(slugs({ visibility: "private" }), ["lib"]); |
| 82 | assert.deepEqual(slugs({ visibility: "public" }), ["site", "tool"]); |
| 83 | assert.deepEqual(slugs({ kind: "library" }), ["lib"]); |
| 84 | assert.deepEqual(slugs({ language: "Rust" }), ["lib"]); |
| 85 | assert.deepEqual(slugs({ language: "JavaScript" }), ["site"]); |
| 86 | assert.deepEqual(slugs({ deployments: true }), ["site"]); |
| 87 | }); |
| 88 | |
| 89 | test("the kind filter also tells mirrors from projects managed on g1t", () => { |
| 90 | const list = [ |
| 91 | project("own"), |
| 92 | project("copy", { mirror: { state: "standby", remote: "github.com/acme/copy" } }), |
| 93 | project("taken", { kind: "library", mirror: { state: "takeover", remote: "github.com/acme/taken" } }), |
| 94 | ]; |
| 95 | const slugs = (change: object) => filterProjects(list, { ...DEFAULT_QUERY, ...change }).map((p) => p.slug).sort(); |
| 96 | assert.deepEqual(slugs({ kind: "mirror" }), ["copy", "taken"], "a taken-over mirror is still a mirror"); |
| 97 | assert.deepEqual(slugs({ kind: "g1t" }), ["own"]); |
| 98 | assert.deepEqual(slugs({ kind: "library" }), ["taken"]); |
| 99 | assert.equal(q("?kind=mirror").kind, "mirror"); |
| 100 | assert.equal(q("?kind=g1t").kind, "g1t"); |
| 101 | assert.equal(projectQueryString({ ...DEFAULT_QUERY, kind: "mirror" }), "?kind=mirror"); |
| 102 | assert.equal(facetsOf(list).mirrors, 2); |
| 103 | }); |
| 104 | |
| 105 | test("search matches every word, in the name, slug or description, names that start with it first", () => { |
| 106 | const list = [ |
| 107 | project("docs-site", { name: "Docs site", description: "The API reference" }), |
| 108 | project("api", { name: "API" }), |
| 109 | project("billing", { description: "charges cards" }), |
| 110 | ]; |
| 111 | assert.deepEqual(filterProjects(list, { ...DEFAULT_QUERY, q: "api" }).map((p) => p.slug), ["api", "docs-site"]); |
| 112 | assert.deepEqual(filterProjects(list, { ...DEFAULT_QUERY, q: "api reference" }).map((p) => p.slug), ["docs-site"]); |
| 113 | assert.deepEqual(filterProjects(list, { ...DEFAULT_QUERY, q: "CARDS" }).map((p) => p.slug), ["billing"]); |
| 114 | }); |
| 115 | |
| 116 | test("each sort orders as it says, ties by name", () => { |
| 117 | const list = [ |
| 118 | project("b", { updatedAt: "2026-10-01T00:00:00Z", pushedAt: "2026-10-05T00:00:00Z", activity: 1 }), |
| 119 | project("a", { updatedAt: "2026-10-03T00:00:00Z", activity: 9 }), |
| 120 | project("c", { updatedAt: "2026-10-02T00:00:00Z", pushedAt: "2026-10-04T00:00:00Z", activity: 1 }), |
| 121 | ]; |
| 122 | const order = (sort: string) => filterProjects(list, { ...DEFAULT_QUERY, sort: sort as never }).map((p) => p.slug); |
| 123 | assert.deepEqual(order("name"), ["a", "b", "c"]); |
| 124 | // Updated is the later of its own changes and its last push. |
| 125 | assert.deepEqual(order("updated"), ["b", "c", "a"]); |
| 126 | assert.deepEqual(order("pushed"), ["b", "c", "a"]); |
| 127 | assert.deepEqual(order("active"), ["a", "b", "c"]); |
| 128 | }); |
| 129 | |
| 130 | test("pages hold PAGE_SIZE, and a page past the end is the last", () => { |
| 131 | const list = Array.from({ length: 412 }, (_, i) => i); |
| 132 | const first = pageOf(list, 1); |
| 133 | assert.equal(first.items.length, PAGE_SIZE); |
| 134 | assert.equal(first.pages, 14); |
| 135 | assert.deepEqual([first.from, first.to], [1, 30]); |
| 136 | const last = pageOf(list, 99); |
| 137 | assert.equal(last.page, 14); |
| 138 | assert.deepEqual([last.from, last.to], [391, 412]); |
| 139 | assert.deepEqual(pageOf([], 1), { items: [], page: 1, pages: 1, from: 0, to: 0 }); |
| 140 | }); |
| 141 | |
| 142 | test("facets count the whole workspace", () => { |
| 143 | const facets = facetsOf([ |
| 144 | project("a", { ecosystem: "npm", deploying: true }), |
| 145 | project("b", { ecosystem: "npm", private: true, kind: "library" }), |
| 146 | project("c", { ecosystem: "go", archived: true }), |
| 147 | project("d", { kind: "tool" }), |
| 148 | ]); |
| 149 | assert.deepEqual(facets.visibility, { public: 3, private: 1 }); |
| 150 | assert.deepEqual(facets.kind, { app: 2, library: 1, tool: 1, docs: 0, other: 0 }); |
| 151 | assert.deepEqual(facets.languages, [{ name: "JavaScript", count: 2 }, { name: "Go", count: 1 }]); |
| 152 | assert.equal(facets.deploying, 1); |
| 153 | assert.equal(facets.archived, 1); |
| 154 | }); |
| 155 | |
| 156 | test("the keyboard moves through rows and stops at the ends", () => { |
| 157 | assert.equal(nextRow("ArrowDown", -1, 3), 0); |
| 158 | assert.equal(nextRow("j", 0, 3), 1); |
| 159 | assert.equal(nextRow("ArrowDown", 2, 3), 2); |
| 160 | assert.equal(nextRow("k", 0, 3), 0); |
| 161 | assert.equal(nextRow("End", 0, 3), 2); |
| 162 | assert.equal(nextRow("Home", 2, 3), 0); |
| 163 | assert.equal(nextRow("x", 0, 3), null); |
| 164 | assert.equal(nextRow("ArrowDown", 0, 0), null); |
| 165 | }); |