g1t/apps/web/app/lib/not-found.test.ts

66 lines2,668 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { type MissingKind, missingKind, notFoundCopy } from "./not-found.ts";
5
6test("the kind a loader names wins over the address", () => {
7 assert.equal(missingKind({ kind: "project" }, "/acme/web/blob/main/x"), "project");
8 assert.equal(missingKind({ kind: "pull" }, "/acme/web/pull/7"), "pull");
9 // Anything else a loader put there is ignored.
10 assert.equal(missingKind({ kind: "secret" }, "/acme/web"), "project");
11 assert.equal(missingKind("Issue not found.", "/acme/web/issues/3"), "issue");
12});
13
14test("without one, the shape of the address decides", () => {
15 const cases: [string, MissingKind][] = [
16 ["/acme", "workspace"],
17 ["/acme/web", "project"],
18 ["/acme/web/issues/12", "issue"],
19 ["/acme/web/pull/12", "pull"],
20 ["/acme/web/issues", "page"],
21 ["/acme/web/settings", "page"],
22 ["/acme/-/billing", "page"],
23 ["/u/ada", "person"],
24 ["/explore/nothing", "page"],
25 ["/", "page"],
26 ];
27 for (const [path, kind] of cases) assert.equal(missingKind(null, path), kind, path);
28});
29
30const KINDS: MissingKind[] = ["project", "issue", "pull", "workspace", "page"];
31
32test("signed out: nothing here, which may be private, and sign in if it is yours", () => {
33 for (const kind of KINDS) {
34 const copy = notFoundCopy(kind, null);
35 assert.equal(copy.title, "Nothing here");
36 assert.equal(copy.body, "This page doesn't exist, or it's private. Sign in if it's yours.");
37 assert.equal(copy.signIn, true);
38 assert.equal(copy.signedInAs, null);
39 }
40});
41
42test("signed in: it does not exist or you cannot see it, and who you are", () => {
43 for (const kind of KINDS) {
44 const copy = notFoundCopy(kind, "ada");
45 assert.equal(copy.title, "Nothing here");
46 assert.equal(copy.body, "This page doesn't exist, or you don't have access to it.");
47 assert.equal(copy.signIn, false);
48 assert.equal(copy.signedInAs, "ada");
49 }
50});
51
52test("a missing person is simply no one, signed in or not", () => {
53 for (const viewer of [null, "ada"]) {
54 const copy = notFoundCopy("person", viewer);
55 assert.equal(copy.body, "No one on g1t goes by that name.");
56 assert.equal(copy.signIn, false);
57 }
58});
59
60test("the page depends only on the kind and the viewer", () => {
61 // The same inputs for a private project and a missing one: the loader
62 // throws the same kind, at the same address, so the copy is identical.
63 const privateOne = notFoundCopy(missingKind({ kind: "project" }, "/acme/secret"), null);
64 const missingOne = notFoundCopy(missingKind({ kind: "project" }, "/acme/secret"), null);
65 assert.deepEqual(privateOne, missingOne);
66});