| 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 | } from "./apps.ts"; |
| 23 | |
| 24 | test("only apps installed from the Marketplace are pinned; built-in apps and their pages are not", () => { |
| 25 | for (const app of BUILTINS) assert.equal(isPinnable(app.key), false, app.key); |
| 26 | for (const page of ["projects", "packages", "security", "context", "memory", "teams", "usage", "gateway", "integrations", "audit"]) { |
| 27 | assert.equal(isPinnable(page), false, page); |
| 28 | } |
| 29 | assert.equal(isPinnable("int-sentry"), true); |
| 30 | assert.equal(isPinnable("int-slack"), false, "Slack can't be connected yet"); |
| 31 | assert.equal(isPinnable("int-mcp"), false, "MCP clients are each person's, not the workspace's"); |
| 32 | assert.equal(isPinnable("ext-support"), false, "no extensions yet"); |
| 33 | assert.equal(isPinnable("int-Sentry"), false); |
| 34 | }); |
| 35 | |
| 36 | test("an integration's app opens its page", () => { |
| 37 | assert.equal(installedAppOf("int-sentry")?.path("acme"), "/acme/-/integrations/alerts"); |
| 38 | assert.equal(installedAppOf("int-linear")?.path("acme"), "/acme/-/integrations/trackers"); |
| 39 | assert.equal(installedAppOf("int-anthropic")?.path("acme"), "/acme/-/integrations/models"); |
| 40 | assert.equal(installedAppOf("int-webhooks")?.path("acme"), "/acme/-/webhooks"); |
| 41 | assert.equal(installedAppOf(integrationAppKey("github"))?.listing, "integration:github"); |
| 42 | // Every key fits what identity keeps (MAX_DOCK_APP_KEY, 32). |
| 43 | assert.ok(integrationAppKey("anthropic-endpoint").length <= 32); |
| 44 | }); |
| 45 | |
| 46 | test("installed apps are what the workspace connected, opening where each is managed", () => { |
| 47 | const apps = installedApps({ sentry: { manage: "/acme/-/integrations/alerts" }, github: { manage: null }, nonsense: { manage: null } }); |
| 48 | assert.deepEqual(apps.map((app) => app.key), ["int-github", "int-sentry"]); |
| 49 | assert.equal(apps[0]!.path("acme"), "/new/github?workspace=acme"); |
| 50 | assert.deepEqual(appData(apps[1]!, "acme").href, "/acme/-/integrations/alerts"); |
| 51 | assert.deepEqual(installedApps({}), []); |
| 52 | }); |
| 53 | |
| 54 | test("pins are kept per workspace, in the order they were pinned", () => { |
| 55 | const value = writeDock(writeDock(null, "acme", ["int-sentry", "int-linear"]), "Beta", ["int-jira"]); |
| 56 | assert.equal(value, "beta:int-jira,acme:int-sentry.int-linear"); |
| 57 | assert.deepEqual(pinsIn(value, "acme"), ["int-sentry", "int-linear"]); |
| 58 | assert.deepEqual(pinsIn(value, "BETA"), ["int-jira"]); |
| 59 | assert.deepEqual(pinsIn(value, "other"), []); |
| 60 | // Unpinning everything keeps the workspace, with no pins. |
| 61 | assert.deepEqual(pinsIn(writeDock(value, "acme", []), "acme"), []); |
| 62 | }); |
| 63 | |
| 64 | test("a cookie someone wrote by hand reads as far as it makes sense", () => { |
| 65 | assert.deepEqual(readDock("acme:int-sentry.today.nonsense.int-sentry,BAD SLUG:int-jira,beta"), { acme: ["int-sentry"] }); |
| 66 | assert.deepEqual(readDock(""), {}); |
| 67 | const many = Array.from({ length: 30 }, () => "int-sentry").join("."); |
| 68 | assert.ok(pinsIn(`acme:${many}`, "acme").length <= MAX_APP_PINS); |
| 69 | }); |
| 70 | |
| 71 | test("pins of built-in pages from before Apps were the Marketplace's are dropped", () => { |
| 72 | assert.deepEqual(pinsFrom(["usage", "int-sentry", "projects", "today", "int-sentry", "teams", "int-linear"]), ["int-sentry", "int-linear"]); |
| 73 | assert.deepEqual(pinsIn("acme:projects.usage.teams", "acme"), []); |
| 74 | assert.deepEqual(pinsFrom([]), []); |
| 75 | }); |
| 76 | |
| 77 | test("the account's pins win over the cookie, which is the fallback", () => { |
| 78 | const cookie = writeDock(null, "acme", ["int-sentry", "int-linear"]); |
| 79 | // Saved with the account, even with nothing pinned: the account's. |
| 80 | assert.deepEqual(pinsToShow(["int-jira"], cookie, "acme"), ["int-jira"]); |
| 81 | assert.deepEqual(pinsToShow([], cookie, "acme"), []); |
| 82 | // Never saved, or identity could not be asked: this device's cookie. |
| 83 | assert.deepEqual(pinsToShow(null, cookie, "acme"), ["int-sentry", "int-linear"]); |
| 84 | assert.deepEqual(pinsToShow(undefined, null, "acme"), []); |
| 85 | }); |
| 86 | |
| 87 | test("a pin form pins or unpins one app", () => { |
| 88 | const form = (fields: Record<string, string>) => { |
| 89 | const data = new FormData(); |
| 90 | for (const [name, value] of Object.entries(fields)) data.set(name, value); |
| 91 | return data; |
| 92 | }; |
| 93 | assert.deepEqual(appPinFromForm(form({ intent: "pin", app: "int-sentry" })), { app: "int-sentry", pinned: true }); |
| 94 | assert.deepEqual(appPinFromForm(form({ intent: "unpin", app: "int-sentry" })), { app: "int-sentry", pinned: false }); |
| 95 | assert.equal(appPinFromForm(form({ intent: "pin", app: "chat" })), null); |
| 96 | assert.equal(appPinFromForm(form({ intent: "pin", app: "teams" })), null); |
| 97 | assert.equal(appPinFromForm(form({ intent: "drop", app: "int-sentry" })), null); |
| 98 | assert.deepEqual(withPin(["int-sentry", "int-jira"], "int-sentry", true), ["int-jira", "int-sentry"]); |
| 99 | assert.deepEqual(withPin(["int-sentry", "int-jira"], "int-sentry", false), ["int-jira"]); |
| 100 | }); |
| 101 | |
| 102 | test("the cookies last a year and are secure when the site is", () => { |
| 103 | assert.match(dockCookie("acme:int-sentry", true), /^g1t_dock=acme%3Aint-sentry; Path=\/; Max-Age=31536000; SameSite=Lax; Secure$/); |
| 104 | assert.equal(sidebarCookie(true, false), "g1t_sidebar=closed; Path=/; Max-Age=31536000; SameSite=Lax"); |
| 105 | assert.equal(sidebarClosed("closed"), true); |
| 106 | assert.equal(sidebarClosed("open"), false); |
| 107 | assert.equal(sidebarClosed(null), false); |
| 108 | }); |