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