Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions dashboard/e2e/knowledge-app-choice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { expect, test, type Page, type Route } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";

// The app choice in the Knowledge add flow on a self-hosted install with
// hybrid (Notion and Confluence):
// - "Use Coderhelm's app" shows one "Sign in with <app>" button and the
// choice's one line: no token steps and no form fields;
// - a key stays behind "Use a key instead";
// - "Use my own app" without the install's own app shows the token steps;
// - when Coderhelm's app isn't ready yet, it says so, still with no form.
// Every call is answered here; nothing reaches a real backend.

const SHOTS_DIR = process.env.E2E_SHOTS_DIR || path.join("test-results", "shots");
const SELF = { apiUrl: "", streamUrl: "", selfHosted: true, version: "v1.2.0" };
const LABEL: Record<string, string> = { notion: "Notion", confluence: "Confluence", google_drive: "Google Drive", sanity: "Sanity", github: "GitHub", web: "Website" };

interface State {
/** Which tools use Coderhelm's app. */
coderhelm: Set<string>;
/** Whether Coderhelm's app answers for a tool that uses it (the broker is ready). */
brokerReady: boolean;
puts: Record<string, unknown>[];
}

function hybridStatus(s: State) {
const row = (id: string, name: string) => ({ id, name, mode: "hosted", through_coderhelm: "The sign-in only.", stays_with_you: "Every page Coderhelm reads.", uses_coderhelm: s.coderhelm.has(id) });
return {
available: true,
hosted_url: "https://app.coderhelm.com",
registered: true,
install_id: "ins_abcdefghijklmnopqrstuvwx",
cloudfront: "d111test.cloudfront.net",
verified: true,
name: null,
github: { mode: "hosted", links: [], app_slug: "coderhelm" },
integrations: [row("notion", "Notion"), row("confluence", "Confluence (Atlassian)"), row("google_drive", "Google Drive")],
sharing: { enabled: false, payloads: false },
checks: [],
};
}

async function mockApi(page: Page, s: State) {
const json = (route: Route, body: unknown, code = 200) => route.fulfill({ status: code, contentType: "application/json", body: JSON.stringify(body) });
await page.route((url) => url.pathname === "/runtime-config.js", (route) =>
route.fulfill({ contentType: "text/javascript", body: `window.__CODERHELM_CONFIG__ = ${JSON.stringify(SELF)};` }),
);
await page.route((url) => !["localhost", "127.0.0.1"].includes(url.hostname), (route) => route.abort());
await page.route((url) => ["localhost", "127.0.0.1"].includes(url.hostname) && /^\/(api|auth|hybrid)\//.test(url.pathname), async (route) => {
const req = route.request();
const p = new URL(req.url()).pathname;
const method = req.method();
const body = req.postData() ? JSON.parse(req.postData()!) : null;
if (p === "/api/me") return json(route, { user_id: "u_1", team_id: "t_1", github_login: null, email: "owner@example.org", avatar_url: "", role: "owner", platform_admin: true });
if (p === "/api/usage") return json(route, { month: "2026-10", total_tokens: 0, tokens_in: 0, tokens_out: 0, total_runs: 0, max_tokens: 0 });
if (p === "/api/banners") return json(route, { banners: [] });
if (p === "/api/teams") return json(route, { teams: [{ team_id: "t_1", org: "example-org", status: "active", current: true }] });
if (p === "/api/system/hybrid" && method === "GET") return json(route, hybridStatus(s));
if (p === "/api/system/hybrid/settings" && method === "PUT") {
s.puts.push(body);
for (const [id, v] of Object.entries((body?.apps ?? {}) as Record<string, string>)) {
if (v === "coderhelm") s.coderhelm.add(id);
else s.coderhelm.delete(id);
}
return json(route, hybridStatus(s));
}
if (p === "/api/knowledge" && method === "GET")
return json(route, { bases: [], can_manage: true, limits: { max_file_bytes: 1 << 24, max_files: 500, extensions: ["pdf", "md"], agents: [], read_roles: ["viewer", "member", "admin"] } });
if (p === "/api/knowledge/connectors")
return json(route, {
connectors: Object.keys(LABEL).map((k) => ({
kind: k,
label: LABEL[k],
blurb: `Pick what to bring in from ${LABEL[k]}`,
connected: k === "github" || k === "web",
via: "",
// The install has no app of its own: sign-in only through Coderhelm's.
can_sign_in: s.coderhelm.has(k) && s.brokerReady,
reconnect_reason: "",
connected_by: "",
hint: "",
live_updates: true,
picker: k === "web" ? "address" : "tree",
member_sign_in: k === "sanity",
signed_in: false,
signed_in_as: "owner@example.org",
has_saved_key: false,
token_path: k === "notion" || k === "confluence",
})),
visibility_note: "Everyone who can read this knowledge base can read what you add.",
});
return json(route, {});
});
}

async function shot(page: Page, name: string) {
fs.mkdirSync(SHOTS_DIR, { recursive: true });
await page.screenshot({ path: path.join(SHOTS_DIR, `knowledge-app-choice-${name}.png`) });
}

for (const kind of ["notion", "confluence"] as const) {
const label = LABEL[kind];

test.describe(`${label}: the app choice decides what the dialog asks for`, () => {
test(`Coderhelm's app: one sign-in button, no token steps or fields`, async ({ page }) => {
await mockApi(page, { coderhelm: new Set([kind]), brokerReady: true, puts: [] });
await page.goto("/knowledge/");
await page.getByTestId(`start-${kind}`).click();
const step = page.getByTestId("connect-step");
await expect(step).toHaveAttribute("data-state", "sign-in");
await expect(step.getByTestId("hybrid-app-choice")).toHaveAttribute("data-state", "coderhelm");
await expect(step.getByTestId("connect-button")).toHaveText(`Sign in with ${label}`);
await expect(step.getByTestId("token-path")).toHaveCount(0);
await expect(step.locator("input")).toHaveCount(0);
await shot(page, `${kind}-coderhelm`);
// A key stays one click away, behind its own link.
await expect(step.getByTestId("use-token")).toHaveText("Use a key instead");
await step.getByTestId("use-token").click();
await expect(step.getByTestId("token-path")).toBeVisible();
});

test(`my own app (none set up): the token steps, and no sign-in button`, async ({ page }) => {
await mockApi(page, { coderhelm: new Set(), brokerReady: true, puts: [] });
await page.goto("/knowledge/");
await page.getByTestId(`start-${kind}`).click();
const step = page.getByTestId("connect-step");
await expect(step.getByTestId("hybrid-app-choice")).toHaveAttribute("data-state", "own");
await expect(step).toHaveAttribute("data-state", "token");
await expect(step.getByTestId("connect-button")).toHaveCount(0);
await expect(step.getByTestId("token-path")).toBeVisible();
await shot(page, `${kind}-own`);
});

test(`switching to Coderhelm's app swaps the form for the sign-in button`, async ({ page }) => {
const s: State = { coderhelm: new Set(), brokerReady: true, puts: [] };
await mockApi(page, s);
await page.goto("/knowledge/");
await page.getByTestId(`start-${kind}`).click();
const step = page.getByTestId("connect-step");
await expect(step.getByTestId("token-path")).toBeVisible();
await step.getByRole("button", { name: "Use Coderhelm's app (easiest)" }).click();
await expect(step).toHaveAttribute("data-state", "sign-in");
await expect(step.getByTestId("connect-button")).toHaveText(`Sign in with ${label}`);
await expect(step.getByTestId("token-path")).toHaveCount(0);
expect(s.puts).toContainEqual({ apps: { [kind]: "coderhelm" } });
});

test(`Coderhelm's app not ready yet: says so, still no form`, async ({ page }) => {
await mockApi(page, { coderhelm: new Set([kind]), brokerReady: false, puts: [] });
await page.goto("/knowledge/");
await page.getByTestId(`start-${kind}`).click();
const step = page.getByTestId("connect-step");
await expect(step).toHaveAttribute("data-state", "app-not-ready");
await expect(step.getByTestId("app-not-ready")).toContainText(`Coderhelm's ${label} app isn't ready on this install yet`);
await expect(step.getByTestId("connect-button")).toHaveCount(0);
await expect(step.getByTestId("token-path")).toHaveCount(0);
});
});
}

test.describe("at phone width", () => {
test.use({ viewport: { width: 375, height: 812 } });
test("Coderhelm's app fits with one button and no sideways scroll", async ({ page }) => {
await mockApi(page, { coderhelm: new Set(["notion", "confluence"]), brokerReady: true, puts: [] });
await page.goto("/knowledge/");
await page.getByTestId("start-confluence").click();
const step = page.getByTestId("connect-step");
await expect(step.getByTestId("connect-button")).toHaveText("Sign in with Confluence");
await expect(step.locator("input")).toHaveCount(0);
const wide = await page.evaluate(() => document.documentElement.scrollWidth > document.documentElement.clientWidth);
expect(wide).toBe(false);
await shot(page, "confluence-coderhelm-375");
});
});
2 changes: 1 addition & 1 deletion dashboard/e2e/knowledge-connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ test.describe("connecting without the app's sign-in", () => {
await page.getByTestId("start-notion").click();
const step = page.getByTestId("connect-step");
await expect(step).toHaveAttribute("data-state", "sign-in");
await expect(step.getByTestId("connect-button")).toHaveText("Connect Notion");
await expect(step.getByTestId("connect-button")).toHaveText("Sign in with Notion");
await expect(step.getByTestId("token-path")).toHaveCount(0);
await shot(page, "notion-oauth");
await step.getByTestId("use-token").click();
Expand Down
61 changes: 59 additions & 2 deletions dashboard/e2e/mcp-servers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ function linearPlugin() {
credential_fields: [{ key: "api_key", label: "API key", placeholder: "lin_api_...", secret: true }],
docs_url: "https://example.com/docs", repo_url: "https://example.com/repo", default_prompt: "Read only.", recommended_permissions: "Read-only access",
remote_url: "https://mcp.linear.example.com/mcp",
remote_key: true,
key_unavailable: "",
};
}

Expand Down Expand Up @@ -138,6 +140,12 @@ async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend
}
if (p === "/api/mcp-servers" && method === "POST") {
if (!admin) return json(r, { error: "Only admins can change MCP servers." }, 403);
if (body?.catalog_id === "linear" && body?.key) {
// A key for a server that takes one connects at once.
const s = server({ id: "linear", name: "Linear", catalog_id: "linear", url: linearPlugin().remote_url, auth: "header", header_name: "Authorization", has_key: true, issuer: null, my_connection: null });
db.servers.push(s);
return json(r, { server: s, detected: { auth: "header" } });
}
if (body?.catalog_id === "linear") {
const s = server({ id: "linear", name: "Linear", catalog_id: "linear", url: linearPlugin().remote_url, status: "not_connected", my_connection: null, tools: [], tools_listed_at: "" });
db.servers.push(s);
Expand Down Expand Up @@ -363,11 +371,11 @@ test("a catalog server offers its vendor's sign-in first and keeps the key as a
await page.goto("/settings/integrations");
await row(page, "linear").getByRole("button", { name: /^Linear, Not connected/ }).click();
const panel = row(page, "linear").getByTestId("integration-panel");
await expect(panel.getByTestId("oauth-offer")).toContainText("nobody pastes a key");
await expect(panel.getByTestId("oauth-offer")).toContainText("No key to paste");
await expect(panel.getByText("Use a key instead")).toBeVisible();
await expect(panel.getByLabel("API key")).toBeHidden();
await shot(page, "mcp-catalog-oauth");
await panel.getByRole("button", { name: "Connect with Linear" }).click();
await panel.getByRole("button", { name: "Sign in with Linear" }).click();
await expect(page).toHaveURL(/#integration-linear$/);
expect(db.calls.find((c) => c.path === "/api/mcp-servers" && c.method === "POST")?.body).toEqual({ catalog_id: "linear" });
await expect(row(page, "linear")).toHaveAttribute("data-status", "connected");
Expand Down Expand Up @@ -405,3 +413,52 @@ test("a sign-in that can't be finished says so and keeps no code in the address"
await expect(page.getByText("That sign-in was already used or has expired.")).toBeVisible();
expect(page.url()).not.toContain("code=");
});

test("a key for a server whose vendor takes one connects to that server", async ({ page }) => {
const db = await mockApi(page);
await page.goto("/settings/integrations");
await row(page, "linear").getByRole("button", { name: /^Linear, Not connected/ }).click();
const panel = row(page, "linear").getByTestId("integration-panel");
await panel.getByText("Use a key instead").click();
const form = panel.getByTestId("remote-key-form");
await form.getByLabel("API key").fill("lin_api_example");
await form.getByRole("button", { name: "Connect with this key" }).click();
expect(db.calls.find((c) => c.path === "/api/mcp-servers" && c.method === "POST")?.body).toEqual({ catalog_id: "linear", key: "lin_api_example" });
await expect(row(page, "linear")).toHaveAttribute("data-status", "connected");
});

test("a server no key works for says why, with no key form", async ({ page }) => {
await mockApi(page);
const card = (id: string, name: string, remote: string, why: string) => ({
id, section: "other", name, does: `${name} tools.`, status: "not_connected", reason: "", action: "connect", last_check: "", manage_href: "",
detail: {
plugin: { ...linearPlugin(), id, name, remote_url: remote, remote_key: false, key_unavailable: why },
enabled: false, has_credentials: false, custom_prompt: "", last_test_ok: null, last_test_message: "", remote: null,
},
});
await page.route((url) => url.pathname === "/api/integrations", (r) =>
r.fulfill({
contentType: "application/json",
body: JSON.stringify({
can_edit: true,
migrated: 0,
items: [
card("hosting", "Hosting", "https://mcp.hosting.example.com", "Hosting's MCP server takes no key: use Sign in with Hosting."),
card("cache", "Cache", "", "Cache's MCP server runs with Python, which Coderhelm can't run yet."),
],
}),
}),
);
await page.goto("/settings/integrations");
await row(page, "hosting").getByRole("button", { name: /^Hosting, Not connected/ }).click();
const hosting = row(page, "hosting").getByTestId("integration-panel");
await expect(hosting.getByRole("button", { name: "Sign in with Hosting" })).toBeVisible();
await expect(hosting.getByTestId("key-unavailable")).toHaveText("Hosting's MCP server takes no key: use Sign in with Hosting.");
await expect(hosting.getByText("Use a key instead")).toHaveCount(0);
await expect(hosting.locator("input")).toHaveCount(0);
await row(page, "cache").getByRole("button", { name: /^Cache, Not connected/ }).click();
const cache = row(page, "cache").getByTestId("integration-panel");
await expect(cache.getByTestId("key-unavailable")).toContainText("runs with Python");
await expect(cache.locator("input")).toHaveCount(0);
await shot(page, "mcp-no-key");
});
30 changes: 28 additions & 2 deletions dashboard/src/components/hybrid-app-choice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,40 @@ import { Segmented } from "./segmented";
import { useToast } from "./toast";
import { linkButton } from "./ui";

type Choice = "coderhelm" | "own";
export type Choice = "coderhelm" | "own";

/**
* Which app this install uses for `id` right now, when there is a choice:
* "coderhelm" or "own". `null` on the hosted service, without hybrid, before
* it's registered, or when Coderhelm's app isn't available for `id`.
*/
export function useHybridChoice(id: string): Choice | null {
const s = useHybrid().status;
if (!s || !s.available || !s.registered) return null;
if (id === "github") return s.github.mode === "hosted" ? "coderhelm" : "own";
const integration = s.integrations.find((i) => i.id === id);
if (!canUseCoderhelm(integration)) return null;
return integration?.uses_coderhelm ? "coderhelm" : "own";
}

/**
* On a self-hosted install: whether this integration uses Coderhelm's app
* (easiest) or the install's own (fully private). Reads and saves the same
* settings as Settings, then System, Hybrid. Shows nothing on the hosted
* service, on an install without hybrid, or where there is no choice.
*/
export function HybridAppChoice({ id, name, className = "" }: { id: string; name: string; className?: string }) {
export function HybridAppChoice({
id,
name,
className = "",
onChanged,
}: {
id: string;
name: string;
className?: string;
/** After the choice is saved (what the dialog offers depends on it). */
onChanged?: (next: Choice) => void;
}) {
const hybrid = useHybrid();
const { toast } = useToast();
const [busy, setBusy] = useState(false);
Expand Down Expand Up @@ -46,6 +71,7 @@ export function HybridAppChoice({ id, name, className = "" }: { id: string; name
try {
await hybrid.save(id === "github" ? { github_mode: next === "coderhelm" ? "hosted" : "own" } : appChoice(id, next === "coderhelm"));
toast(next === "coderhelm" ? `${name} now uses Coderhelm's app` : `${name} now uses your own app`);
onChanged?.(next);
} catch (e) {
toast(e instanceof Error ? e.message : "Could not save", "error");
} finally {
Expand Down
Loading
Loading