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

74 lines2,821 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, string][] = [
31 ["project", "project"],
32 ["issue", "issue"],
33 ["pull", "pull request"],
34 ["workspace", "workspace"],
35 ["page", "page"],
36];
37
38test("signed out: sign in to see it, which may be private or missing", () => {
39 for (const [kind, noun] of KINDS) {
40 const copy = notFoundCopy(kind, null);
41 assert.equal(copy.title, `Sign in to see this ${noun}`);
42 assert.equal(copy.body, "It may be private, or it may not exist.");
43 assert.equal(copy.signIn, true);
44 assert.equal(copy.signedInAs, null);
45 assert.equal(copy.askOwner, false);
46 }
47});
48
49test("signed in: it does not exist or you cannot see it, and who you are", () => {
50 for (const [kind, noun] of KINDS) {
51 const copy = notFoundCopy(kind, "ada");
52 assert.equal(copy.title, `This ${noun} doesn't exist, or you don't have access to it`);
53 assert.equal(copy.signIn, false);
54 assert.equal(copy.signedInAs, "ada");
55 assert.equal(copy.askOwner, true);
56 }
57});
58
59test("a missing person is simply no one, signed in or not", () => {
60 for (const viewer of [null, "ada"]) {
61 const copy = notFoundCopy("person", viewer);
62 assert.equal(copy.title, "No one goes by that name");
63 assert.equal(copy.signIn, false);
64 assert.equal(copy.askOwner, false);
65 }
66});
67
68test("the page depends only on the kind and the viewer", () => {
69 // The same inputs for a private project and a missing one: the loader
70 // throws the same kind, at the same address, so the copy is identical.
71 const privateOne = notFoundCopy(missingKind({ kind: "project" }, "/acme/secret"), null);
72 const missingOne = notFoundCopy(missingKind({ kind: "project" }, "/acme/secret"), null);
73 assert.deepEqual(privateOne, missingOne);
74});