From 9dafd38e1987bae39acbaf8c820de54a87b0d8ad Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Thu, 7 May 2026 04:23:20 +0530 Subject: [PATCH] test(web): add native Google Gmail live-tool journey --- .../connectors/google/gmail/journey.spec.ts | 74 +++++++++++++++++++ .../connectors/native-drive.fixture.ts | 2 +- .../connectors/native-gmail.fixture.ts | 32 ++++++++ surfsense_web/tests/fixtures/index.ts | 9 +++ surfsense_web/tests/helpers/api/connectors.ts | 67 +++++++++++++++++ 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 surfsense_web/tests/connectors/google/gmail/journey.spec.ts create mode 100644 surfsense_web/tests/fixtures/connectors/native-gmail.fixture.ts diff --git a/surfsense_web/tests/connectors/google/gmail/journey.spec.ts b/surfsense_web/tests/connectors/google/gmail/journey.spec.ts new file mode 100644 index 000000000..4c3fd464d --- /dev/null +++ b/surfsense_web/tests/connectors/google/gmail/journey.spec.ts @@ -0,0 +1,74 @@ +import { expect, nativeGmailWithChatTest 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_GMAIL_MESSAGES } from "../../../helpers/canary"; +import { openConnectorPopup } from "../../../helpers/ui/connector-popup"; + +/** + * Proves the native Gmail wiring from Google OAuth fixture -> live Gmail + * tools -> chat. + * + * Native Gmail is currently live-tool only: the public indexing route + * returns indexing_started=false and chat should call Gmail tools. + */ +test.describe("Native Google Gmail journey", () => { + test("user connects native Gmail and chats through live Gmail tools with indexing disabled", async ({ + page, + request, + apiToken, + searchSpace, + nativeGmailConnector, + chatThread, + }) => { + test.setTimeout(90_000); // worker cold-start + live tool chat + + expect(nativeGmailConnector.connector_type).toBe("GOOGLE_GMAIL_CONNECTOR"); + expect(nativeGmailConnector.is_indexable).toBe(false); + expect(nativeGmailConnector.config._token_encrypted).toBe(true); + expect(nativeGmailConnector.config.composio_connected_account_id).toBeUndefined(); + + 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 expect(connectorDialog.getByRole("button", { name: "Manage" })).toBeVisible(); + + const beforeDocs = await listDocuments(request, apiToken, searchSpace.id); + expect(beforeDocs).toHaveLength(0); + + const disabledIndex = await triggerIndexExpectDisabled( + request, + apiToken, + nativeGmailConnector.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 Gmail message titled "${FAKE_GMAIL_MESSAGES.canary.subject}"?`, + }); + expect( + chat.assistantText, + `chat agent should surface native Gmail canary token from live tools; got: ${chat.assistantText.slice(0, 200)}` + ).toContain(CANARY_TOKENS.gmailCanary); + + const eventText = JSON.stringify(chat.events); + expect(eventText).toContain("search_gmail"); + expect(eventText).toContain("read_gmail_email"); + + const refreshedConnectors = await listConnectors(request, apiToken, searchSpace.id); + const refreshed = refreshedConnectors.find((c) => c.id === nativeGmailConnector.id); + expect(refreshed?.connector_type).toBe("GOOGLE_GMAIL_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/native-drive.fixture.ts b/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts index 4beff0ac5..35b4b5ddc 100644 --- a/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts +++ b/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts @@ -20,7 +20,7 @@ export const nativeDriveFixtures = searchSpaceFixtures.extend({ + nativeGmailConnector: async ({ request, apiToken, searchSpace }, use) => { + const { connector } = await runNativeGoogleGmailOAuth(request, apiToken, searchSpace.id); + if (!connector) { + throw new Error( + "nativeGmailConnector fixture: OAuth completed but no GOOGLE_GMAIL_CONNECTOR was created. " + + "Check the backend log — the native Google 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 90ab5510d..39e0d2e73 100644 --- a/surfsense_web/tests/fixtures/index.ts +++ b/surfsense_web/tests/fixtures/index.ts @@ -16,6 +16,8 @@ * └─ composioCalendarWithChatTest — chatThread * └─ nativeDriveFixtures — nativeDriveConnector * └─ nativeDriveWithChatTest — chatThread + * └─ nativeGmailFixtures — nativeGmailConnector + * └─ nativeGmailWithChatTest — chatThread * * To add a new connector (Gmail, Slack, manual upload, etc.): * 1. Add a fixture file under `fixtures/connectors/.fixture.ts`. @@ -28,6 +30,7 @@ export { composioCalendarFixtures } from "./connectors/composio-calendar.fixture export { composioDriveFixtures } from "./connectors/composio-drive.fixture"; export { composioGmailFixtures } from "./connectors/composio-gmail.fixture"; export { nativeDriveFixtures } from "./connectors/native-drive.fixture"; +export { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; export { searchSpaceFixtures } from "./search-space.fixture"; import { type ChatThreadFixtures, chatThreadFixtures } from "./chat-thread.fixture"; @@ -35,6 +38,7 @@ import { composioCalendarFixtures } from "./connectors/composio-calendar.fixture import { composioDriveFixtures } from "./connectors/composio-drive.fixture"; import { composioGmailFixtures } from "./connectors/composio-gmail.fixture"; import { nativeDriveFixtures } from "./connectors/native-drive.fixture"; +import { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; import { searchSpaceFixtures } from "./search-space.fixture"; /** Default `test` for specs that just need auth + a clean search space. */ @@ -59,3 +63,8 @@ export const nativeDriveTest = nativeDriveFixtures; /** `test` for native Drive specs that also need a chat thread. */ export const nativeDriveWithChatTest = nativeDriveFixtures.extend(chatThreadFixtures); +/** `test` for specs that also need a pre-connected native Gmail connector. */ +export const nativeGmailTest = nativeGmailFixtures; +/** `test` for native Gmail specs that also need a chat thread. */ +export const nativeGmailWithChatTest = + nativeGmailFixtures.extend(chatThreadFixtures); diff --git a/surfsense_web/tests/helpers/api/connectors.ts b/surfsense_web/tests/helpers/api/connectors.ts index 087bff582..39c1d340e 100644 --- a/surfsense_web/tests/helpers/api/connectors.ts +++ b/surfsense_web/tests/helpers/api/connectors.ts @@ -126,6 +126,31 @@ export async function triggerIndex( return { ok: true }; } +export async function triggerIndexExpectDisabled( + request: APIRequestContext, + token: string, + connectorId: number, + searchSpaceId: number, + body: IndexBody = {} +): Promise<{ indexing_started: false; message?: string }> { + const response = await request.post( + `${BACKEND_URL}/api/v1/search-source-connectors/${connectorId}/index?search_space_id=${searchSpaceId}`, + { headers: authHeaders(token), data: body } + ); + if (!response.ok()) { + throw new Error( + `triggerIndexExpectDisabled(${connectorId}) failed (${response.status()}): ${await response.text()}` + ); + } + const payload = (await response.json()) as { indexing_started?: boolean; message?: string }; + if (payload.indexing_started !== false) { + throw new Error( + `triggerIndexExpectDisabled(${connectorId}) expected indexing_started=false, got ${JSON.stringify(payload)}` + ); + } + return { indexing_started: false, message: payload.message }; +} + /** * Drives the OAuth flow for a Composio toolkit programmatically. * @@ -230,3 +255,45 @@ export async function runNativeGoogleDriveOAuth( return { authUrl: auth_url, finalUrl: location, connector }; } + +/** + * Drives the native Google Gmail OAuth flow programmatically. + * + * The E2E backend patches Google OAuth so the returned auth_url points + * straight back to the backend callback with a deterministic code/state. + */ +export async function runNativeGoogleGmailOAuth( + request: APIRequestContext, + token: string, + searchSpaceId: number +): Promise<{ + authUrl: string; + finalUrl: string; + connector: ConnectorRow | null; +}> { + const initiateResp = await request.get( + `${BACKEND_URL}/api/v1/auth/google/gmail/connector/add?space_id=${searchSpaceId}`, + { headers: authHeaders(token) } + ); + if (!initiateResp.ok()) { + throw new Error( + `native Google Gmail initiate failed (${initiateResp.status()}): ${await initiateResp.text()}` + ); + } + const { auth_url } = (await initiateResp.json()) as { auth_url: string }; + if (!auth_url) { + throw new Error("native Google Gmail initiate response missing auth_url"); + } + + const callbackResp = await request.get(auth_url, { + headers: authHeaders(token), + maxRedirects: 0, + failOnStatusCode: false, + }); + const location = callbackResp.headers().location ?? auth_url; + + const connectors = await listConnectors(request, token, searchSpaceId); + const connector = connectors.find((c) => c.connector_type === "GOOGLE_GMAIL_CONNECTOR") ?? null; + + return { authUrl: auth_url, finalUrl: location, connector }; +}