| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { movedCodePath, pagePath, repositoriesPath, underWorkspace, workspacePage, workspaceRedirect } from "./workspace-nav.ts"; |
| 5 | |
| 6 | test("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/-/code/repositories", "acme"), "code/repositories"); |
| 11 | assert.equal(workspacePage("/acme/-/projects", "acme"), null); |
| 12 | assert.equal(workspacePage("/acme/-/people", "acme"), "people"); |
| 13 | assert.equal(workspacePage("/acme/-/packages", "acme"), "packages"); |
| 14 | assert.equal(workspacePage("/acme/-/teams", "acme"), "teams"); |
| 15 | assert.equal(workspacePage("/acme/-/insights", "acme"), "insights"); |
| 16 | // One package or team has a page of its own; settings and the rest have headings. |
| 17 | assert.equal(workspacePage("/acme/-/packages/npm/web", "acme"), null); |
| 18 | assert.equal(workspacePage("/acme/-/teams/web", "acme"), null); |
| 19 | assert.equal(workspacePage("/acme/-/settings", "acme"), null); |
| 20 | assert.equal(workspacePage("/acme/-/agents", "acme"), null); |
| 21 | assert.equal(workspacePage("/acme/web", "acme"), null); |
| 22 | assert.equal(workspacePage("/other", "acme"), null); |
| 23 | }); |
| 24 | |
| 25 | test("old addresses go to where their pages are now", () => { |
| 26 | // Members and invites is a page of People's again. |
| 27 | assert.equal(workspaceRedirect("/acme/-/members"), null); |
| 28 | assert.equal(workspaceRedirect("/acme/-/today", "?x=1"), "/acme/-/home?x=1"); |
| 29 | // Home is the front page itself: not an old address. |
| 30 | assert.equal(workspaceRedirect("/acme/-/home"), null); |
| 31 | // Code's Overview lives at -/overview now: not an old address. |
| 32 | assert.equal(workspaceRedirect("/acme/-/overview"), null); |
| 33 | assert.equal(workspaceRedirect("/acme/-/soon/teams"), "/acme/-/teams"); |
| 34 | // The Marketplace, Customize and Agents are gone: their old addresses lead nowhere. |
| 35 | assert.equal(workspaceRedirect("/acme/-/marketplace/agents"), null); |
| 36 | assert.equal(workspaceRedirect("/acme/-/marketplace/extensions"), null); |
| 37 | assert.equal(workspaceRedirect("/acme/-/agents/skills"), null); |
| 38 | assert.equal(workspaceRedirect("/acme/-/soon/insights", "?x=1"), "/acme/-/insights?x=1"); |
| 39 | assert.equal(workspaceRedirect("/acme/-/soon/uptime"), null); |
| 40 | assert.equal(workspaceRedirect("/acme/-/people"), null); |
| 41 | assert.equal(workspaceRedirect("/acme/web"), null); |
| 42 | }); |
| 43 | |
| 44 | test("Code's Repositories are at -/code/repositories, and its old Projects address goes there, query and all", () => { |
| 45 | assert.equal(repositoriesPath("acme"), "/acme/-/code/repositories"); |
| 46 | assert.equal(repositoriesPath("acme", "q=api&sort=name"), "/acme/-/code/repositories?q=api&sort=name"); |
| 47 | assert.equal(movedCodePath("/acme/-/projects"), "/acme/-/code/repositories"); |
| 48 | assert.equal(movedCodePath("/acme/-/projects/", "?q=api&kind=app"), "/acme/-/code/repositories?q=api&kind=app"); |
| 49 | assert.equal(movedCodePath("/acme/-/projects.data", "?q=api&_routes=x"), "/acme/-/code/repositories?q=api"); |
| 50 | assert.equal(movedCodePath("/acme/-/work/projects"), null); |
| 51 | assert.equal(movedCodePath("/acme/projects"), null); |
| 52 | }); |
| 53 | |
| 54 | test("?tab= opens that page, keeping the rest of the query", () => { |
| 55 | assert.equal(workspaceRedirect("/acme", "?tab=repositories"), "/acme/-/code/repositories"); |
| 56 | assert.equal(workspaceRedirect("/acme", "?tab=projects&q=api"), "/acme/-/code/repositories?q=api"); |
| 57 | assert.equal(workspaceRedirect("/acme", "?tab=members"), "/acme/-/members"); |
| 58 | assert.equal(workspaceRedirect("/acme", "?tab=people"), "/acme/-/people"); |
| 59 | assert.equal(workspaceRedirect("/acme", "?tab=overview"), "/acme"); |
| 60 | assert.equal(workspaceRedirect("/acme", "?tab=settings"), "/acme/-/settings/profile"); |
| 61 | assert.equal(workspaceRedirect("/acme", "?tab=nonsense"), null); |
| 62 | assert.equal(workspaceRedirect("/acme", ""), null); |
| 63 | }); |
| 64 | |
| 65 | test("a click's data request is for the same page as a full load", () => { |
| 66 | assert.equal(pagePath("/acme/-/insights.data"), "/acme/-/insights"); |
| 67 | assert.equal(pagePath("/acme/-/insights/"), "/acme/-/insights"); |
| 68 | assert.equal(workspaceRedirect("/acme/-/today.data", "?_routes=routes%2Fworkspace%2Fmoved-today"), "/acme/-/home"); |
| 69 | assert.equal(workspaceRedirect("/acme/-/today.data", "?_routes=x&q=ada"), "/acme/-/home?q=ada"); |
| 70 | }); |
| 71 | |
| 72 | test("an alias or old name leads to the same page under the workspace", () => { |
| 73 | assert.equal(underWorkspace("/g1t", "", "flagon-io"), "/flagon-io"); |
| 74 | assert.equal(underWorkspace("/g1t/g1t/issues", "?q=is%3Aopen", "flagon-io"), "/flagon-io/g1t/issues?q=is%3Aopen"); |
| 75 | assert.equal(underWorkspace("/g1t/g1t/blob/main/README.md", "", "flagon-io"), "/flagon-io/g1t/blob/main/README.md"); |
| 76 | assert.equal(underWorkspace("/g1t/-/people/", "", "flagon-io"), "/flagon-io/-/people"); |
| 77 | }); |
| 78 | |
| 79 | test("a click's data request for an alias leads to the page, not its data", () => { |
| 80 | assert.equal(underWorkspace("/g1t.data", "?_routes=routes%2Fworkspace%2Flayout", "flagon-io"), "/flagon-io"); |
| 81 | assert.equal(underWorkspace("/g1t/g1t/pulls.data", "?_routes=routes%2Frepo%2Flayout&state=closed", "flagon-io"), "/flagon-io/g1t/pulls?state=closed"); |
| 82 | }); |
| 83 | |
| 84 | test("the rail's mode follows the address", async () => { |
| 85 | const { modeOf, modeHome, homePath } = await import("./workspace-nav.ts"); |
| 86 | assert.equal(modeOf("/", "acme"), "home"); |
| 87 | // The workspace's own page is Code's front page. |
| 88 | assert.equal(modeOf("/acme", "acme"), "code"); |
| 89 | assert.equal(modeOf("/acme/-/home", "acme"), "home"); |
| 90 | // g1t's own public pages light no mode. |
| 91 | assert.equal(modeOf("/explore", "acme"), "site"); |
| 92 | assert.equal(modeOf("/u/ada", "acme"), "site"); |
| 93 | assert.equal(modeOf("/search.data", "acme"), "site"); |
| 94 | assert.equal(modeOf("/notifications", "acme"), "notifications"); |
| 95 | assert.equal(modeOf("/settings/emails", "acme"), "account"); |
| 96 | assert.equal(modeOf("/acme/-/chat/general", "acme"), "chat"); |
| 97 | assert.equal(modeOf("/acme/-/chat/dm/c1.data", "acme"), "chat"); |
| 98 | // Agents mode and Memory are gone: their old addresses fall to Code's not-found page. |
| 99 | assert.equal(modeOf("/acme/-/agents", "acme"), "code"); |
| 100 | assert.equal(modeOf("/acme/-/memory", "acme"), "code"); |
| 101 | assert.equal(modeOf("/acme/-/library/artifacts", "acme"), "artifacts"); |
| 102 | assert.equal(modeOf("/acme/-/library/artifacts/spaces/general", "acme"), "artifacts"); |
| 103 | assert.equal(modeOf("/acme/-/routines", "acme"), "routines"); |
| 104 | // Docs mode is gone: its old addresses are nobody's mode, so they fall to Code's not-found page. |
| 105 | assert.equal(modeOf("/acme/-/docs", "acme"), "code"); |
| 106 | assert.equal(modeOf("/acme/-/overview", "acme"), "code"); |
| 107 | assert.equal(modeOf("/acme/-/projects", "acme"), "code"); |
| 108 | assert.equal(modeOf("/acme/-/code/repositories", "acme"), "code"); |
| 109 | assert.equal(modeOf("/acme/-/security", "acme"), "code"); |
| 110 | assert.equal(modeOf("/acme/-/settings/security", "acme"), "workspace"); |
| 111 | assert.equal(modeOf("/acme/-/people", "acme"), "people"); |
| 112 | assert.equal(modeOf("/acme/-/teams/web", "acme"), "people"); |
| 113 | assert.equal(modeOf("/acme/-/people/ana", "acme"), "people"); |
| 114 | // Members, invitations and permissions are the workspace's settings. |
| 115 | assert.equal(modeOf("/acme/-/settings/members", "acme"), "workspace"); |
| 116 | assert.equal(modeOf("/acme/-/settings/members/invitations", "acme"), "workspace"); |
| 117 | assert.equal(modeOf("/acme/-/settings/permissions", "acme"), "workspace"); |
| 118 | assert.equal(modeOf("/acme/-/apps", "acme"), "apps"); |
| 119 | assert.equal(modeOf("/acme/-/settings/billing", "acme"), "workspace"); |
| 120 | assert.equal(modeOf("/acme/-/settings/tokens", "acme"), "workspace"); |
| 121 | assert.equal(modeOf("/acme/-/settings/profile", "acme"), "workspace"); |
| 122 | assert.equal(modeOf("/acme/web/pulls", "acme"), "code"); |
| 123 | assert.equal(modeOf("/new", "acme"), "code"); |
| 124 | // Another workspace's chat is Chat (a guest in its channels); its repositories are Code's. |
| 125 | assert.equal(modeOf("/other/-/chat", "acme"), "chat"); |
| 126 | assert.equal(modeOf("/other/web", "acme"), "code"); |
| 127 | assert.equal(modeHome("home", "acme"), "/acme/-/home"); |
| 128 | assert.equal(modeHome("notifications", "acme"), "/notifications"); |
| 129 | assert.equal(modeHome("people", "acme"), "/acme/-/people"); |
| 130 | assert.equal(modeHome("apps", "acme"), "/acme/-/apps"); |
| 131 | assert.equal(modeHome("code", "acme"), "/acme"); |
| 132 | assert.equal(modeHome("workspace", "acme"), "/acme/-/settings/profile"); |
| 133 | assert.equal(homePath("acme"), "/acme/-/home"); |
| 134 | assert.equal(homePath("acme", "?tab=landed&_routes=x"), "/acme/-/home"); |
| 135 | }); |
| 136 | |
| 137 | test("Workspace mode's sidebar knows which group and page an address is", async () => { |
| 138 | const { WORKSPACE_SETTINGS_GROUPS, modeOf, workspaceSettingsPage } = await import("./workspace-nav.ts"); |
| 139 | const at = (group: string, page: string, parent = page) => ({ group, page, parent }); |
| 140 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/profile", "acme"), at("profile", "profile")); |
| 141 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/chat", "acme"), at("automation", "chat-settings")); |
| 142 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/emoji", "acme"), at("automation", "emoji", "chat-settings")); |
| 143 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/work", "acme"), at("automation", "work-settings")); |
| 144 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/members", "acme"), at("access", "members")); |
| 145 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/members/invitations.data", "acme"), at("access", "invitations", "members")); |
| 146 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/permissions", "acme"), at("access", "permissions")); |
| 147 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/rules/rs_1", "acme"), at("automation", "rules", "repositories")); |
| 148 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/security", "acme"), at("security", "security-settings")); |
| 149 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/sso", "acme"), at("security", "sso", "authentication")); |
| 150 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/usage", "acme"), at("access", "usage", "billing")); |
| 151 | assert.deepEqual(workspaceSettingsPage("/acme/-/settings/billing/entries", "acme"), at("access", "billing")); |
| 152 | // The overview, Code's security page, an old address and another workspace are no settings page. |
| 153 | assert.equal(workspaceSettingsPage("/acme/-/overview", "acme"), null); |
| 154 | assert.equal(workspaceSettingsPage("/acme/-/security", "acme"), null); |
| 155 | assert.equal(workspaceSettingsPage("/acme/-/members", "acme"), null); |
| 156 | assert.equal(workspaceSettingsPage("/other/-/settings/profile", "acme"), null); |
| 157 | // Every real page is in Workspace mode, under `-/settings/`, and every key is unique. |
| 158 | const keys = new Set<string>(); |
| 159 | for (const group of WORKSPACE_SETTINGS_GROUPS) { |
| 160 | for (const page of group.pages.flatMap((top) => [top, ...(top.pages ?? [])])) { |
| 161 | assert.ok(!keys.has(page.key), `${page.key} twice`); |
| 162 | keys.add(page.key); |
| 163 | assert.ok(page.path.startsWith("settings/"), page.path); |
| 164 | // Integrations are Apps's, beside the settings' sidebar. |
| 165 | assert.equal(modeOf(`/acme/-/${page.path}`, "acme"), page.key === "integrations" ? "apps" : "workspace", page.path); |
| 166 | } |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | test("a member without Code access is sent around Code's pages", async () => { |
| 171 | const { codeGate } = await import("./workspace-nav.ts"); |
| 172 | const none = ["acme"]; |
| 173 | assert.equal(codeGate("/", "", none, "acme"), "/acme/-/home"); |
| 174 | assert.equal(codeGate("/", "", none, "other"), null); |
| 175 | assert.equal(codeGate("/acme", "", none, "acme"), "/acme/-/home"); |
| 176 | assert.equal(codeGate("/acme/web/pull/3", "?x=1", none, "acme"), "/acme/-/code-access?from=%2Facme%2Fweb%2Fpull%2F3%3Fx%3D1"); |
| 177 | assert.equal(codeGate("/acme/-/projects", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2F-%2Fprojects"); |
| 178 | assert.equal(codeGate("/acme/-/code/repositories", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2F-%2Fcode%2Frepositories"); |
| 179 | assert.equal(codeGate("/acme/-/chat/general", "", none, "acme"), null); |
| 180 | assert.equal(codeGate("/acme/-/code-access", "", none, "acme"), null); |
| 181 | // The settings of Code's parts are closed with them; the rest of the settings are not. |
| 182 | assert.equal(codeGate("/acme/-/settings/rules/rs_1", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2F-%2Fsettings%2Frules%2Frs_1"); |
| 183 | assert.equal(codeGate("/acme/-/settings/actions", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2F-%2Fsettings%2Factions"); |
| 184 | assert.equal(codeGate("/acme/-/settings/members", "", none, "acme"), null); |
| 185 | assert.equal(codeGate("/acme/-/settings/profile", "", none, "acme"), null); |
| 186 | assert.equal(codeGate("/other/web", "", none, "acme"), null); |
| 187 | assert.equal(codeGate("/acme/web", "", [], "acme"), null); |
| 188 | // Work is every member's: its pages, and a repository's old issue, label |
| 189 | // and milestone addresses and `#12` links, which lead there. |
| 190 | assert.equal(codeGate("/acme/-/work", "", none, "acme"), null); |
| 191 | assert.equal(codeGate("/acme/-/work/12", "", none, "acme"), null); |
| 192 | assert.equal(codeGate("/acme/-/work/labels", "", none, "acme"), null); |
| 193 | assert.equal(codeGate("/acme/web/issues/12", "", none, "acme"), null); |
| 194 | assert.equal(codeGate("/acme/web/issues", "", none, "acme"), null); |
| 195 | assert.equal(codeGate("/acme/web/milestones/2", "", none, "acme"), null); |
| 196 | assert.equal(codeGate("/acme/web/ref/12", "", none, "acme"), null); |
| 197 | // A repository's Sessions page is Code's. |
| 198 | assert.equal(codeGate("/acme/web/sessions", "", none, "acme"), "/acme/-/code-access?from=%2Facme%2Fweb%2Fsessions"); |
| 199 | }); |
| 200 | |
| 201 | test("Work is a mode of its own, after Home", async () => { |
| 202 | const { modeOf, modeHome } = await import("./workspace-nav.ts"); |
| 203 | assert.equal(modeOf("/acme/-/work", "acme"), "work"); |
| 204 | assert.equal(modeOf("/acme/-/work.data", "acme"), "work"); |
| 205 | assert.equal(modeOf("/acme/-/work/12", "acme"), "work"); |
| 206 | assert.equal(modeOf("/acme/-/work/new", "acme"), "work"); |
| 207 | assert.equal(modeOf("/acme/-/work/labels", "acme"), "work"); |
| 208 | assert.equal(modeOf("/acme/-/work/milestones/3", "acme"), "work"); |
| 209 | // Sessions are Work's: its list and each session light Work. |
| 210 | assert.equal(modeOf("/acme/-/work/sessions", "acme"), "work"); |
| 211 | assert.equal(modeOf("/acme/-/work/sessions/ses_1.data", "acme"), "work"); |
| 212 | // Sessions are Chat's now, beside the people you talk to. |
| 213 | assert.equal(modeOf("/acme/-/chat/sessions", "acme"), "chat"); |
| 214 | assert.equal(modeOf("/acme/-/chat/sessions/ses_1", "acme"), "chat"); |
| 215 | assert.equal(modeOf("/acme/-/library/artifacts", "acme"), "artifacts"); |
| 216 | assert.equal(modeOf("/acme/-/library", "acme"), "artifacts"); |
| 217 | // Their old address is no page of its own any more, so it falls to Code's not-found page. |
| 218 | assert.equal(modeOf("/acme/-/sessions", "acme"), "code"); |
| 219 | assert.equal(modeHome("work", "acme"), "/acme/-/work"); |
| 220 | // A repository's pages, its Sessions among them, are Code's. |
| 221 | assert.equal(modeOf("/acme/web/sessions", "acme"), "code"); |
| 222 | }); |
| 223 | |
| 224 | test("g1t beyond your workspaces is browsed, and whose it is", async () => { |
| 225 | const { browsingOf, shellMode } = await import("./workspace-nav.ts"); |
| 226 | const viewer = { slug: "acme", memberships: ["acme", "Side-Co"], shared: [{ namespace: "friend", name: "notes" }], username: "ada" }; |
| 227 | // Explore and search are nobody's. |
| 228 | assert.deepEqual(browsingOf("/explore", viewer), { owner: null }); |
| 229 | assert.deepEqual(browsingOf("/explore.data", viewer), { owner: null }); |
| 230 | assert.deepEqual(browsingOf("/search", viewer), { owner: null }); |
| 231 | // Someone's profile is theirs; your own is no one's being browsed. |
| 232 | assert.deepEqual(browsingOf("/u/grace", viewer), { owner: { kind: "person", name: "grace" } }); |
| 233 | assert.deepEqual(browsingOf("/u/Ada", viewer), { owner: null }); |
| 234 | // Another workspace, its pages and its projects. |
| 235 | assert.deepEqual(browsingOf("/other", viewer), { owner: { kind: "workspace", name: "other", repo: null } }); |
| 236 | assert.deepEqual(browsingOf("/other/-/projects", viewer), { owner: { kind: "workspace", name: "other", repo: null } }); |
| 237 | assert.deepEqual(browsingOf("/other/site/pulls/3", viewer), { owner: { kind: "workspace", name: "other", repo: "site" } }); |
| 238 | // Your workspaces are never browsed, the one you are in or another (the shell switches to it). |
| 239 | assert.equal(browsingOf("/acme/web", viewer), null); |
| 240 | assert.equal(browsingOf("/acme/-/projects", viewer), null); |
| 241 | assert.equal(browsingOf("/side-co/api", viewer), null); |
| 242 | // A project shared with you is Code's; the rest of its workspace is browsed. |
| 243 | assert.equal(browsingOf("/friend/notes/issues", viewer), null); |
| 244 | assert.deepEqual(browsingOf("/friend/other", viewer), { owner: { kind: "workspace", name: "friend", repo: "other" } }); |
| 245 | // g1t's own pages and files are no one's. |
| 246 | for (const path of [ |
| 247 | "/", |
| 248 | "/settings/emails", |
| 249 | "/notifications", |
| 250 | "/new", |
| 251 | "/policies/terms", |
| 252 | "/workspaces/new", |
| 253 | "/invitations", |
| 254 | "/robots.txt", |
| 255 | "/.well-known/g1t.json", |
| 256 | "/u", |
| 257 | ]) { |
| 258 | assert.equal(browsingOf(path, viewer), null, path); |
| 259 | } |
| 260 | // Without a workspace there is no rail, and nothing is browsed. |
| 261 | assert.equal(browsingOf("/explore", { slug: null, memberships: [] }), null); |
| 262 | // The shell's mode: browse beyond your workspaces, the address's mode otherwise. |
| 263 | assert.equal(shellMode("/explore", viewer), "browse"); |
| 264 | assert.equal(shellMode("/u/grace", viewer), "browse"); |
| 265 | assert.equal(shellMode("/other/site", viewer), "browse"); |
| 266 | assert.equal(shellMode("/acme/web", viewer), "code"); |
| 267 | assert.equal(shellMode("/friend/notes", viewer), "code"); |
| 268 | assert.equal(shellMode("/acme/-/chat", viewer), "chat"); |
| 269 | // Another workspace's channels, where you are a guest, are Chat's, not browsed. |
| 270 | assert.equal(browsingOf("/flagon-io/-/chat/community-support", viewer), null); |
| 271 | assert.equal(shellMode("/flagon-io/-/chat/community-support", viewer), "chat"); |
| 272 | assert.equal(shellMode("/flagon-io/-/chat", viewer), "chat"); |
| 273 | assert.equal(shellMode("/policies", viewer), "site"); |
| 274 | }); |