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