| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | BUILTINS, |
| 6 | MAX_APP_PINS, |
| 7 | appData, |
| 8 | appPinFromForm, |
| 9 | dockCookie, |
| 10 | installedAppOf, |
| 11 | installedApps, |
| 12 | integrationAppKey, |
| 13 | isPinnable, |
| 14 | pinsFrom, |
| 15 | pinsIn, |
| 16 | pinsToShow, |
| 17 | readDock, |
| 18 | sidebarClosed, |
| 19 | sidebarCookie, |
| 20 | withPin, |
| 21 | writeDock, |
| 22 | PHONE_TABS, |
| 23 | appOf, |
| 24 | phoneMoreApps, |
| 25 | railApps, |
| 26 | } from "./apps.ts"; |
| 27 | |
| 28 | test("Work is a built-in app right after Home, for every member, and Sessions are in it", () => { |
| 29 | assert.deepEqual(BUILTINS.slice(0, 3).map((app) => app.key), ["home", "work", "chat"]); |
| 30 | assert.equal(appOf("work").path("acme"), "/acme/-/work"); |
| 31 | assert.equal(appOf("work").code, undefined, "not part of Code"); |
| 32 | assert.equal(BUILTINS.some((app) => (app.key as string) === "sessions"), false, "no Sessions app of its own"); |
| 33 | }); |
| 34 | |
| 35 | test("the rail is Home, Explore, Work, Chat, Code, Artifacts and Routines", () => { |
| 36 | assert.deepEqual(railApps(true), ["home", "explore", "work", "chat", "code", "artifacts", "routines"]); |
| 37 | // A member without Code access has no Code, and the rest in the same order. |
| 38 | assert.deepEqual(railApps(false), ["home", "explore", "work", "chat", "artifacts", "routines"]); |
| 39 | }); |
| 40 | |
| 41 | test("a phone's bar is Home, Work, Chat and Notifications, with Code first of More's apps", () => { |
| 42 | assert.deepEqual([...PHONE_TABS], ["home", "work", "chat", "notifications"]); |
| 43 | // Four tabs and More: five slots. |
| 44 | assert.equal(PHONE_TABS.length + 1, 5); |
| 45 | // Explore heads the sheet before these; Sessions are Work's, not More's. |
| 46 | assert.deepEqual(phoneMoreApps(true), ["code", "artifacts", "routines", "people", "workspace"]); |
| 47 | // A member without Code access has the rest, Work still on the bar. |
| 48 | assert.deepEqual(phoneMoreApps(false), ["artifacts", "routines", "people", "workspace"]); |
| 49 | }); |
| 50 | |
| 51 | test("only integrations are pinned; built-in apps, their pages, old plugins and connectors are not", () => { |
| 52 | for (const app of BUILTINS) assert.equal(isPinnable(app.key), false, app.key); |
| 53 | for (const page of ["projects", "packages", "security", "context", "memory", "teams", "usage", "gateway", "integrations", "audit"]) { |
| 54 | assert.equal(isPinnable(page), false, page); |
| 55 | } |
| 56 | assert.equal(isPinnable("int-sentry"), true); |
| 57 | assert.equal(isPinnable("int-slack"), false, "Slack can't be connected yet"); |
| 58 | assert.equal(isPinnable("int-mcp"), false, "MCP clients are each person's, not the workspace's"); |
| 59 | assert.equal(isPinnable("ext-support"), false, "extensions became plugins, which aren't pinned"); |
| 60 | assert.equal(isPinnable("plg-pli_01"), false); |
| 61 | assert.equal(isPinnable("con-con_01"), false); |
| 62 | assert.equal(isPinnable("int-Sentry"), false); |
| 63 | }); |
| 64 | |
| 65 | test("an integration's app opens its page", () => { |
| 66 | assert.equal(installedAppOf("int-sentry")?.path("acme"), "/acme/-/integrations/alerts"); |
| 67 | assert.equal(installedAppOf("int-linear")?.path("acme"), "/acme/-/integrations/trackers"); |
| 68 | // AI providers are the workspace's AI settings, not apps. |
| 69 | assert.equal(installedAppOf("int-anthropic"), null); |
| 70 | assert.equal(installedAppOf("int-webhooks")?.path("acme"), "/acme/-/webhooks"); |
| 71 | assert.equal(installedAppOf(integrationAppKey("github"))?.listing, "integration:github"); |
| 72 | // Every key fits what identity keeps (MAX_DOCK_APP_KEY, 32). |
| 73 | assert.ok(integrationAppKey("anthropic-endpoint").length <= 32); |
| 74 | }); |
| 75 | |
| 76 | test("installed apps are what the workspace connected, opening where each is managed", () => { |
| 77 | const apps = installedApps({ sentry: { manage: "/acme/-/integrations/alerts" }, github: { manage: null }, nonsense: { manage: null } }); |
| 78 | assert.deepEqual(apps.map((app) => app.key), ["int-github", "int-sentry"]); |
| 79 | assert.equal(apps[0]!.path("acme"), "/new/github?workspace=acme"); |
| 80 | assert.deepEqual(appData(apps[1]!, "acme").href, "/acme/-/integrations/alerts"); |
| 81 | assert.deepEqual(installedApps({}), []); |
| 82 | assert.deepEqual( |
| 83 | installedApps({ sentry: { manage: null }, anthropic: { manage: null } }).map((app) => [app.key, app.from, app.pinnable]), |
| 84 | // AI providers are the workspace's AI settings, not apps. |
| 85 | [["int-sentry", "g1t", true]], |
| 86 | ); |
| 87 | }); |
| 88 | |
| 89 | test("pins are kept per workspace, in the order they were pinned", () => { |
| 90 | const value = writeDock(writeDock(null, "acme", ["int-sentry", "int-linear"]), "Beta", ["int-jira"]); |
| 91 | assert.equal(value, "beta:int-jira,acme:int-sentry.int-linear"); |
| 92 | assert.deepEqual(pinsIn(value, "acme"), ["int-sentry", "int-linear"]); |
| 93 | assert.deepEqual(pinsIn(value, "BETA"), ["int-jira"]); |
| 94 | assert.deepEqual(pinsIn(value, "other"), []); |
| 95 | // Unpinning everything keeps the workspace, with no pins. |
| 96 | assert.deepEqual(pinsIn(writeDock(value, "acme", []), "acme"), []); |
| 97 | }); |
| 98 | |
| 99 | test("a cookie someone wrote by hand reads as far as it makes sense", () => { |
| 100 | assert.deepEqual(readDock("acme:int-sentry.today.nonsense.int-sentry,BAD SLUG:int-jira,beta"), { acme: ["int-sentry"] }); |
| 101 | assert.deepEqual(readDock(""), {}); |
| 102 | const many = Array.from({ length: 30 }, () => "int-sentry").join("."); |
| 103 | assert.ok(pinsIn(`acme:${many}`, "acme").length <= MAX_APP_PINS); |
| 104 | }); |
| 105 | |
| 106 | test("pins of built-in pages from before are dropped", () => { |
| 107 | assert.deepEqual(pinsFrom(["usage", "int-sentry", "projects", "today", "int-sentry", "teams", "int-linear"]), ["int-sentry", "int-linear"]); |
| 108 | assert.deepEqual(pinsIn("acme:projects.usage.teams", "acme"), []); |
| 109 | assert.deepEqual(pinsFrom([]), []); |
| 110 | }); |
| 111 | |
| 112 | test("the account's pins win over the cookie, which is the fallback", () => { |
| 113 | const cookie = writeDock(null, "acme", ["int-sentry", "int-linear"]); |
| 114 | // Saved with the account, even with nothing pinned: the account's. |
| 115 | assert.deepEqual(pinsToShow(["int-jira"], cookie, "acme"), ["int-jira"]); |
| 116 | assert.deepEqual(pinsToShow([], cookie, "acme"), []); |
| 117 | // Never saved, or identity could not be asked: this device's cookie. |
| 118 | assert.deepEqual(pinsToShow(null, cookie, "acme"), ["int-sentry", "int-linear"]); |
| 119 | assert.deepEqual(pinsToShow(undefined, null, "acme"), []); |
| 120 | }); |
| 121 | |
| 122 | test("a pin form pins or unpins one app", () => { |
| 123 | const form = (fields: Record<string, string>) => { |
| 124 | const data = new FormData(); |
| 125 | for (const [name, value] of Object.entries(fields)) data.set(name, value); |
| 126 | return data; |
| 127 | }; |
| 128 | assert.deepEqual(appPinFromForm(form({ intent: "pin", app: "int-sentry" })), { app: "int-sentry", pinned: true }); |
| 129 | assert.deepEqual(appPinFromForm(form({ intent: "unpin", app: "int-sentry" })), { app: "int-sentry", pinned: false }); |
| 130 | assert.equal(appPinFromForm(form({ intent: "pin", app: "chat" })), null); |
| 131 | assert.equal(appPinFromForm(form({ intent: "pin", app: "teams" })), null); |
| 132 | assert.equal(appPinFromForm(form({ intent: "drop", app: "int-sentry" })), null); |
| 133 | assert.deepEqual(withPin(["int-sentry", "int-jira"], "int-sentry", true), ["int-jira", "int-sentry"]); |
| 134 | assert.deepEqual(withPin(["int-sentry", "int-jira"], "int-sentry", false), ["int-jira"]); |
| 135 | }); |
| 136 | |
| 137 | test("the cookies last a year and are secure when the site is", () => { |
| 138 | assert.match(dockCookie("acme:int-sentry", true), /^g1t_dock=acme%3Aint-sentry; Path=\/; Max-Age=31536000; SameSite=Lax; Secure$/); |
| 139 | assert.equal(sidebarCookie(true, false), "g1t_sidebar=closed; Path=/; Max-Age=31536000; SameSite=Lax"); |
| 140 | assert.equal(sidebarClosed("closed"), true); |
| 141 | assert.equal(sidebarClosed("open"), false); |
| 142 | assert.equal(sidebarClosed(null), false); |
| 143 | }); |