From 029f2168b32837eea5a0b5bfbdc07ce29e07cb4a Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Fri, 8 May 2026 03:09:11 +0530 Subject: [PATCH] test(e2e): add Slack connector Playwright journey --- .../tests/connectors/slack/journey.spec.ts | 86 +++++++++++++++++++ .../fixtures/connectors/slack.fixture.ts | 28 ++++++ surfsense_web/tests/fixtures/index.ts | 8 ++ surfsense_web/tests/helpers/api/connectors.ts | 55 ++++++++++++ surfsense_web/tests/helpers/canary.ts | 14 +++ 5 files changed, 191 insertions(+) create mode 100644 surfsense_web/tests/connectors/slack/journey.spec.ts create mode 100644 surfsense_web/tests/fixtures/connectors/slack.fixture.ts diff --git a/surfsense_web/tests/connectors/slack/journey.spec.ts b/surfsense_web/tests/connectors/slack/journey.spec.ts new file mode 100644 index 000000000..ae773befc --- /dev/null +++ b/surfsense_web/tests/connectors/slack/journey.spec.ts @@ -0,0 +1,86 @@ +import { expect, slackWithChatTest as test } from "../../fixtures"; +import { streamChatToCompletion } from "../../helpers/api/chat"; +import { listConnectors, triggerIndexExpectDisabled } from "../../helpers/api/connectors"; +import { listDocuments } from "../../helpers/api/documents"; +import { CANARY_TOKENS, FAKE_SLACK_CHANNELS } from "../../helpers/canary"; +import { openConnectorPopup } from "../../helpers/ui/connector-popup"; + +/** + * Proves Slack MCP OAuth -> live MCP tool discovery/call -> chat. + * + * Slack is live-tool only: the public indexing route returns + * indexing_started=false and chat should call Slack MCP tools. + */ +test.describe("Slack connector journey", () => { + test("user connects Slack and chats through live MCP tools with indexing disabled", async ({ + page, + request, + apiToken, + searchSpace, + slackConnector, + chatThread, + }) => { + test.setTimeout(90_000); // worker cold-start + live tool chat + + expect(slackConnector.connector_type).toBe("SLACK_CONNECTOR"); + expect(slackConnector.is_indexable).toBe(false); + expect(slackConnector.config._token_encrypted).toBe(true); + expect(slackConnector.config.mcp_service).toBe("slack"); + expect(slackConnector.config.server_config).toMatchObject({ + transport: "streamable-http", + url: "https://mcp.slack.com/mcp", + }); + expect(slackConnector.config.mcp_oauth).toMatchObject({ + client_id: "fake-slack-mcp-client-id", + token_endpoint: "https://slack.com/api/oauth.v2.user.access", + }); + expect((slackConnector.config.mcp_oauth as Record).access_token).toBeTruthy(); + expect(slackConnector.config.access_token).toBeUndefined(); + expect(slackConnector.config.refresh_token).toBeUndefined(); + expect(slackConnector.config.team_id).toBe(FAKE_SLACK_CHANNELS.canary.teamId); + expect(slackConnector.config.team_name).toBe(FAKE_SLACK_CHANNELS.canary.teamName); + + await page.goto(`/dashboard/${searchSpace.id}/new-chat`, { + waitUntil: "domcontentloaded", + }); + await openConnectorPopup(page); + const connectorDialog = page.getByRole("dialog", { name: "Manage Connectors" }); + await expect(connectorDialog).toBeVisible(); + await connectorDialog.getByPlaceholder("Search").fill("Slack"); + await expect(connectorDialog.getByText("Slack", { exact: true })).toBeVisible(); + + const beforeDocs = await listDocuments(request, apiToken, searchSpace.id); + expect(beforeDocs).toHaveLength(0); + + const disabledIndex = await triggerIndexExpectDisabled( + request, + apiToken, + slackConnector.id, + searchSpace.id + ); + expect(disabledIndex.message ?? "").toContain("real-time agent tools"); + expect(disabledIndex.message ?? "").toContain("background indexing is disabled"); + + const chat = await streamChatToCompletion(request, apiToken, { + searchSpaceId: searchSpace.id, + threadId: chatThread.id, + query: `What is in my Slack channel "${FAKE_SLACK_CHANNELS.canary.name}"?`, + }); + expect( + chat.assistantText, + `chat agent should surface Slack canary token from live MCP tools; got: ${chat.assistantText.slice(0, 200)}` + ).toContain(CANARY_TOKENS.slackCanary); + + const eventText = JSON.stringify(chat.events); + expect(eventText).toContain("slack_search_channels"); + + const refreshedConnectors = await listConnectors(request, apiToken, searchSpace.id); + const refreshed = refreshedConnectors.find((c) => c.id === slackConnector.id); + expect(refreshed?.connector_type).toBe("SLACK_CONNECTOR"); + expect(refreshed?.is_indexable).toBe(false); + expect(refreshed?.last_indexed_at).toBeNull(); + + const afterDocs = await listDocuments(request, apiToken, searchSpace.id); + expect(afterDocs).toHaveLength(0); + }); +}); diff --git a/surfsense_web/tests/fixtures/connectors/slack.fixture.ts b/surfsense_web/tests/fixtures/connectors/slack.fixture.ts new file mode 100644 index 000000000..898c2013b --- /dev/null +++ b/surfsense_web/tests/fixtures/connectors/slack.fixture.ts @@ -0,0 +1,28 @@ +import { type ConnectorRow, deleteConnector, runSlackOAuth } from "../../helpers/api/connectors"; +import { searchSpaceFixtures } from "../search-space.fixture"; + +export type SlackFixtures = { + /** + * A pre-connected Slack connector inside the fixture's `searchSpace`. + * OAuth and MCP tool calls use E2E Slack fakes and are cleaned up + * automatically after the test. + */ + slackConnector: ConnectorRow; +}; + +export const slackFixtures = searchSpaceFixtures.extend({ + slackConnector: async ({ request, apiToken, searchSpace }, use) => { + const { connector } = await runSlackOAuth(request, apiToken, searchSpace.id); + if (!connector) { + throw new Error( + "slackConnector fixture: OAuth completed but no SLACK_CONNECTOR was created. " + + "Check the backend log — the Slack MCP fake likely rejected an unmodelled call." + ); + } + try { + await use(connector); + } finally { + await deleteConnector(request, apiToken, connector.id); + } + }, +}); diff --git a/surfsense_web/tests/fixtures/index.ts b/surfsense_web/tests/fixtures/index.ts index f63ee058a..4020bdd12 100644 --- a/surfsense_web/tests/fixtures/index.ts +++ b/surfsense_web/tests/fixtures/index.ts @@ -28,6 +28,8 @@ * └─ linearWithChatTest — chatThread * └─ jiraFixtures — jiraConnector * └─ jiraWithChatTest — chatThread + * └─ slackFixtures — slackConnector + * └─ slackWithChatTest — chatThread * * To add a new connector (Gmail, Slack, manual upload, etc.): * 1. Add a fixture file under `fixtures/connectors/.fixture.ts`. @@ -47,6 +49,7 @@ export { nativeDriveFixtures } from "./connectors/native-drive.fixture"; export { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; export { notionFixtures } from "./connectors/notion.fixture"; export { searchSpaceFixtures } from "./search-space.fixture"; +export { slackFixtures } from "./connectors/slack.fixture"; import { type ChatThreadFixtures, chatThreadFixtures } from "./chat-thread.fixture"; import { composioCalendarFixtures } from "./connectors/composio-calendar.fixture"; @@ -60,6 +63,7 @@ import { nativeDriveFixtures } from "./connectors/native-drive.fixture"; import { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; import { notionFixtures } from "./connectors/notion.fixture"; import { searchSpaceFixtures } from "./search-space.fixture"; +import { slackFixtures } from "./connectors/slack.fixture"; /** Default `test` for specs that just need auth + a clean search space. */ export const test = searchSpaceFixtures; @@ -110,3 +114,7 @@ export const linearWithChatTest = linearFixtures.extend(chat export const jiraTest = jiraFixtures; /** `test` for Jira specs that also need a chat thread. */ export const jiraWithChatTest = jiraFixtures.extend(chatThreadFixtures); +/** `test` for specs that also need a pre-connected Slack connector. */ +export const slackTest = slackFixtures; +/** `test` for Slack specs that also need a chat thread. */ +export const slackWithChatTest = slackFixtures.extend(chatThreadFixtures); diff --git a/surfsense_web/tests/helpers/api/connectors.ts b/surfsense_web/tests/helpers/api/connectors.ts index 20e4414a9..ec3f6ac45 100644 --- a/surfsense_web/tests/helpers/api/connectors.ts +++ b/surfsense_web/tests/helpers/api/connectors.ts @@ -542,3 +542,58 @@ export async function runJiraOAuth( return { authUrl: auth_url, finalUrl: location, connector }; } + +/** + * Drives the Slack MCP OAuth flow programmatically. + * + * The E2E backend keeps SurfSense's generic MCP OAuth routes real and + * patches Slack's external discovery/token/MCP tool boundaries. + */ +export async function runSlackOAuth( + request: APIRequestContext, + token: string, + searchSpaceId: number +): Promise<{ + authUrl: string; + finalUrl: string; + connector: ConnectorRow | null; +}> { + const initiateResp = await request.get( + `${BACKEND_URL}/api/v1/auth/mcp/slack/connector/add?space_id=${searchSpaceId}`, + { headers: authHeaders(token) } + ); + if (!initiateResp.ok()) { + throw new Error( + `Slack MCP initiate failed (${initiateResp.status()}): ${await initiateResp.text()}` + ); + } + const { auth_url } = (await initiateResp.json()) as { auth_url: string }; + if (!auth_url) { + throw new Error("Slack MCP initiate response missing auth_url"); + } + + const state = new URL(auth_url).searchParams.get("state"); + if (!state) { + throw new Error(`Slack MCP auth_url missing state: ${auth_url}`); + } + + const callbackResp = await request.get( + `${BACKEND_URL}/api/v1/auth/mcp/slack/connector/callback?code=fake-slack-oauth-code&state=${encodeURIComponent(state)}`, + { + headers: authHeaders(token), + maxRedirects: 0, + failOnStatusCode: false, + } + ); + if (callbackResp.status() >= 400) { + throw new Error( + `Slack MCP callback failed (${callbackResp.status()}): ${await callbackResp.text()}` + ); + } + const location = callbackResp.headers().location ?? auth_url; + + const connectors = await listConnectors(request, token, searchSpaceId); + const connector = connectors.find((c) => c.connector_type === "SLACK_CONNECTOR") ?? null; + + return { authUrl: auth_url, finalUrl: location, connector }; +} diff --git a/surfsense_web/tests/helpers/canary.ts b/surfsense_web/tests/helpers/canary.ts index 63afa6c8b..480842c10 100644 --- a/surfsense_web/tests/helpers/canary.ts +++ b/surfsense_web/tests/helpers/canary.ts @@ -24,6 +24,7 @@ export const CANARY_TOKENS = { confluenceCanary: "SURFSENSE_E2E_CANARY_TOKEN_CONFLUENCE_001", linearCanary: "SURFSENSE_E2E_CANARY_TOKEN_LINEAR_001", jiraCanary: "SURFSENSE_E2E_CANARY_TOKEN_JIRA_001", + slackCanary: "SURFSENSE_E2E_CANARY_TOKEN_SLACK_001", } as const; /** @@ -143,6 +144,19 @@ export const FAKE_JIRA_ISSUES = { }, } as const; +/** + * Fake Slack channel IDs that match what the backend MCP fake returns from + * slack_search_channels. + */ +export const FAKE_SLACK_CHANNELS = { + canary: { + id: "C_FAKE_SLACK_CANARY", + name: "slack-e2e-canary", + teamId: "T_FAKE_SLACK_TEAM", + teamName: "SurfSense E2E Slack Workspace", + }, +} as const; + /** Generate a unique-per-run search space name. Keeps parallel tests isolated. */ export function uniqueSearchSpaceName(prefix = "e2e"): string { return `${prefix}-${randomUUID().slice(0, 8)}`;