Skip to content
140 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { SETTINGS_PAGES, WORKSPACE_PAGES, pagePath, sidebarCurrent, underWorkspace, workspacePage, workspaceRedirect } from "./workspace-nav.ts";
5
6test("a path is one of the workspace's pages, or none", () => {
7 assert.equal(workspacePage("/acme", "acme"), "overview");
8 assert.equal(workspacePage("/Acme/", "acme"), "overview");
9 assert.equal(workspacePage("/acme.data", "acme"), "overview");
10 assert.equal(workspacePage("/acme/-/projects", "acme"), "projects");
11 assert.equal(workspacePage("/acme/-/people", "acme"), "people");
12 assert.equal(workspacePage("/acme/-/packages", "acme"), "packages");
13 assert.equal(workspacePage("/acme/-/teams", "acme"), "teams");
14 assert.equal(workspacePage("/acme/-/insights", "acme"), "insights");
15 // One package or team has a page of its own; settings and the rest have headings.
16 assert.equal(workspacePage("/acme/-/packages/npm/web", "acme"), null);
17 assert.equal(workspacePage("/acme/-/teams/web", "acme"), null);
18 assert.equal(workspacePage("/acme/-/settings", "acme"), null);
19 assert.equal(workspacePage("/acme/-/agents", "acme"), null);
20 assert.equal(workspacePage("/acme/web", "acme"), null);
21 assert.equal(workspacePage("/other", "acme"), null);
22});
23
24test("every page that once had a tab has a sidebar row", () => {
25 for (const page of ["overview", ...WORKSPACE_PAGES] as const) {
26 const path = page === "overview" ? "/acme" : `/acme/-/${page}`;
27 assert.equal(sidebarCurrent(path, "acme"), page, path);
28 }
29});
30
31test("one sidebar row is current wherever you are in the workspace", () => {
32 assert.equal(sidebarCurrent("/", "acme"), "mission");
33 assert.equal(sidebarCurrent("/inbox", "acme"), "inbox");
34 assert.equal(sidebarCurrent("/support", "acme"), "support");
35 assert.equal(sidebarCurrent("/acme/-/projects.data", "acme"), "projects");
36 // Within a team or a package, its row stays lit.
37 assert.equal(sidebarCurrent("/acme/-/teams/web/settings", "acme"), "teams");
38 assert.equal(sidebarCurrent("/acme/-/packages/npm/web", "acme"), "packages");
39 assert.equal(sidebarCurrent("/acme/-/agents", "acme"), "agents");
40 assert.equal(sidebarCurrent("/acme/-/usage", "acme"), "usage");
41 // Every page the Settings row drills into.
42 for (const page of SETTINGS_PAGES) assert.equal(sidebarCurrent(`/acme/-/${page}`, "acme"), "settings", page);
43 // Another workspace's pages, a project and a person light nothing here.
44 assert.equal(sidebarCurrent("/other/-/projects", "acme"), null);
45 assert.equal(sidebarCurrent("/acme/web", "acme"), null);
46 assert.equal(sidebarCurrent("/u/ada", "acme"), null);
47 assert.equal(sidebarCurrent("/acme", null), null);
48});
49
50test("old addresses go to where their pages are now", () => {
51 assert.equal(workspaceRedirect("/acme/-/members"), "/acme/-/people");
52 // Code's Overview lives at -/overview now: not an old address.
53 assert.equal(workspaceRedirect("/acme/-/overview"), null);
54 assert.equal(workspaceRedirect("/acme/-/soon/teams"), "/acme/-/teams");
55 assert.equal(workspaceRedirect("/acme/-/soon/insights", "?x=1"), "/acme/-/insights?x=1");
56 assert.equal(workspaceRedirect("/acme/-/soon/board"), null);
57 assert.equal(workspaceRedirect("/acme/-/people"), null);
58 assert.equal(workspaceRedirect("/acme/web"), null);
59});
60
61test("?tab= opens that page, keeping the rest of the query", () => {
62 assert.equal(workspaceRedirect("/acme", "?tab=repositories"), "/acme/-/projects");
63 assert.equal(workspaceRedirect("/acme", "?tab=projects&q=api"), "/acme/-/projects?q=api");
64 assert.equal(workspaceRedirect("/acme", "?tab=members"), "/acme/-/people");
65 assert.equal(workspaceRedirect("/acme", "?tab=overview"), "/acme");
66 assert.equal(workspaceRedirect("/acme", "?tab=settings"), "/acme/-/settings");
67 assert.equal(workspaceRedirect("/acme", "?tab=nonsense"), null);
68 assert.equal(workspaceRedirect("/acme", ""), null);
69});
70
71test("a click's data request is for the same page as a full load", () => {
72 assert.equal(pagePath("/acme/-/insights.data"), "/acme/-/insights");
73 assert.equal(pagePath("/acme/-/insights/"), "/acme/-/insights");
74 assert.equal(workspaceRedirect("/acme/-/members.data", "?_routes=routes%2Fworkspace%2Fmoved-members"), "/acme/-/people");
75 assert.equal(workspaceRedirect("/acme/-/members.data", "?_routes=x&q=ada"), "/acme/-/people?q=ada");
76});
77
78test("an alias or old name leads to the same page under the workspace", () => {
79 assert.equal(underWorkspace("/g1t", "", "flagon-io"), "/flagon-io");
80 assert.equal(underWorkspace("/g1t/g1t/issues", "?q=is%3Aopen", "flagon-io"), "/flagon-io/g1t/issues?q=is%3Aopen");
81 assert.equal(underWorkspace("/g1t/g1t/blob/main/README.md", "", "flagon-io"), "/flagon-io/g1t/blob/main/README.md");
82 assert.equal(underWorkspace("/g1t/-/people/", "", "flagon-io"), "/flagon-io/-/people");
83});
84
85test("a click's data request for an alias leads to the page, not its data", () => {
86 assert.equal(underWorkspace("/g1t.data", "?_routes=routes%2Fworkspace%2Flayout", "flagon-io"), "/flagon-io");
87 assert.equal(
88 underWorkspace("/g1t/g1t/pulls.data", "?_routes=routes%2Frepo%2Flayout&state=closed", "flagon-io"),
89 "/flagon-io/g1t/pulls?state=closed",
90 );
91});
92
93test("the rail's mode follows the address", async () => {
94 const { modeOf, modeHome, homePath } = await import("./workspace-nav.ts");
95 assert.equal(modeOf("/", "acme"), "home");
96 assert.equal(modeOf("/acme", "acme"), "home");
97 assert.equal(modeOf("/acme/-/home", "acme"), "home");
98 assert.equal(modeOf("/explore", "acme"), "home");
99 assert.equal(modeOf("/inbox", "acme"), "inbox");
100 assert.equal(modeOf("/settings/emails", "acme"), "account");
101 assert.equal(modeOf("/acme/-/chat/general", "acme"), "chat");
102 assert.equal(modeOf("/acme/-/chat/dm/c1.data", "acme"), "chat");
103 assert.equal(modeOf("/acme/-/agents", "acme"), "agents");
104 assert.equal(modeOf("/acme/-/agents/ship/profile", "acme"), "agents");
105 assert.equal(modeOf("/acme/-/context", "acme"), "agents");
106 assert.equal(modeOf("/acme/-/memory", "acme"), "agents");
107 assert.equal(modeOf("/acme/-/docs", "acme"), "docs");
108 assert.equal(modeOf("/acme/-/overview", "acme"), "code");
109 assert.equal(modeOf("/acme/-/projects", "acme"), "code");
110 assert.equal(modeOf("/acme/-/security", "acme"), "code");
111 assert.equal(modeOf("/acme/-/security/settings", "acme"), "workspace");
112 assert.equal(modeOf("/acme/-/people", "acme"), "workspace");
113 assert.equal(modeOf("/acme/-/billing", "acme"), "workspace");
114 assert.equal(modeOf("/acme/-/tokens", "acme"), "workspace");
115 assert.equal(modeOf("/acme/-/workspace", "acme"), "workspace");
116 assert.equal(modeOf("/acme/web/pulls", "acme"), "code");
117 assert.equal(modeOf("/new", "acme"), "code");
118 // Another workspace's chat is not this one's mode; its repositories are Code's.
119 assert.equal(modeOf("/other/-/chat", "acme"), "code");
120 assert.equal(modeHome("home", "acme"), "/acme/-/home");
121 assert.equal(modeHome("code", "acme"), "/acme/-/overview");
122 assert.equal(modeHome("workspace", "acme"), "/acme/-/workspace");
123 assert.equal(homePath("acme"), "/acme/-/home");
124 assert.equal(homePath("acme", "?agent=new"), "/acme/-/overview?agent=new");
125 assert.equal(homePath("acme", "?tab=landed&_routes=x"), "/acme/-/overview?tab=landed");
126});
127
128test("a member without Code access is sent around Code's pages", async () => {
129 const { codeGate } = await import("./workspace-nav.ts");
130 const none = ["acme"];
131 assert.equal(codeGate("/", "", none, "acme"), "/acme/-/home");
132 assert.equal(codeGate("/", "", none, "other"), null);
133 assert.equal(codeGate("/acme", "", none, "acme"), "/acme/-/home");
134 assert.equal(codeGate("/acme/web/pull/3", "?x=1", none, "acme"), "/acme/-/code-access?from=%2Facme%2Fweb%2Fpull%2F3%3Fx%3D1");
135 assert.equal(codeGate("/acme/-/projects", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2F-%2Fprojects");
136 assert.equal(codeGate("/acme/-/chat/general", "", none, "acme"), null);
137 assert.equal(codeGate("/acme/-/code-access", "", none, "acme"), null);
138 assert.equal(codeGate("/other/web", "", none, "acme"), null);
139 assert.equal(codeGate("/acme/web", "", [], "acme"), null);
140});