diff --git a/surfsense_web/tests/connectors/google/drive/journey.spec.ts b/surfsense_web/tests/connectors/google/drive/journey.spec.ts new file mode 100644 index 000000000..1a31bbd0b --- /dev/null +++ b/surfsense_web/tests/connectors/google/drive/journey.spec.ts @@ -0,0 +1,111 @@ +import { expect, nativeDriveWithChatTest as test } from "../../../fixtures"; +import { streamChatToCompletion } from "../../../helpers/api/chat"; +import { + listConnectors, + triggerIndex, + updateConnectorConfig, +} from "../../../helpers/api/connectors"; +import { getEditorContent, listDocuments } from "../../../helpers/api/documents"; +import { CANARY_TOKENS, FAKE_DRIVE_FILES } from "../../../helpers/canary"; +import { openConnectorPopup } from "../../../helpers/ui/connector-popup"; +import { waitForDocumentByTitle, waitForIndexingComplete } from "../../../helpers/waits/indexing"; + +/** + * Proves the native Drive wiring from Google OAuth fixture -> selection + * persistence -> native Drive API indexing -> stored source_markdown -> chat. + * + * Google Picker is intentionally bypassed; this journey exercises the + * persisted config and indexing contract the Picker ultimately feeds. + */ +test.describe("Native Google Drive journey", () => { + test("user connects native Drive, selects a file, indexes it, and chats with the canary token", async ({ + page, + request, + apiToken, + searchSpace, + nativeDriveConnector, + chatThread, + }) => { + test.setTimeout(180_000); // worker cold-start + summarize + embed + chunk + + expect(nativeDriveConnector.connector_type).toBe("GOOGLE_DRIVE_CONNECTOR"); + expect(nativeDriveConnector.is_indexable).toBe(true); + expect(nativeDriveConnector.config._token_encrypted).toBe(true); + expect(nativeDriveConnector.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 selectedFiles = [ + { + id: FAKE_DRIVE_FILES.canary.id, + name: FAKE_DRIVE_FILES.canary.name, + mimeType: FAKE_DRIVE_FILES.canary.mimeType, + }, + ]; + const indexingOptions = { + max_files_per_folder: 10, + incremental_sync: false, + include_subfolders: false, + }; + + await updateConnectorConfig(request, apiToken, nativeDriveConnector.id, { + ...nativeDriveConnector.config, + selected_folders: [], + selected_files: selectedFiles, + indexing_options: indexingOptions, + }); + + await triggerIndex(request, apiToken, nativeDriveConnector.id, searchSpace.id, { + files: selectedFiles, + indexing_options: indexingOptions, + }); + + await waitForIndexingComplete(request, apiToken, nativeDriveConnector.id, searchSpace.id, { + timeoutMs: 150_000, + intervalMs: 1_500, + minDocuments: 1, + }); + + await waitForDocumentByTitle(request, apiToken, searchSpace.id, FAKE_DRIVE_FILES.canary.name, { + timeoutMs: 30_000, + }); + + const docs = await listDocuments(request, apiToken, searchSpace.id); + const canaryDoc = docs.find((d) => d.title === FAKE_DRIVE_FILES.canary.name); + + expect(canaryDoc, "native Drive canary document must exist after indexing").toBeDefined(); + if (!canaryDoc) throw new Error("unreachable: canaryDoc asserted defined above"); + expect(canaryDoc.document_type).toBe("GOOGLE_DRIVE_FILE"); + + const editor = await getEditorContent(request, apiToken, searchSpace.id, canaryDoc.id); + expect( + editor.source_markdown, + `canary token ${CANARY_TOKENS.driveCanaryFile} should appear in editor source_markdown; ` + + `got first 200 chars: ${editor.source_markdown.slice(0, 200)}` + ).toContain(CANARY_TOKENS.driveCanaryFile); + expect(editor.document_type).toBe("GOOGLE_DRIVE_FILE"); + expect(editor.chunk_count).toBeGreaterThan(0); + + const refreshedConnectors = await listConnectors(request, apiToken, searchSpace.id); + const refreshed = refreshedConnectors.find((c) => c.id === nativeDriveConnector.id); + expect(refreshed?.connector_type).toBe("GOOGLE_DRIVE_CONNECTOR"); + expect(refreshed?.is_indexable).toBe(true); + expect(refreshed?.last_indexed_at).not.toBeNull(); + + const chat = await streamChatToCompletion(request, apiToken, { + searchSpaceId: searchSpace.id, + threadId: chatThread.id, + query: "What is in my e2e-canary.txt native Drive file?", + }); + expect( + chat.assistantText, + `chat agent should surface native Drive canary token after indexing; got: ${chat.assistantText.slice(0, 200)}` + ).toContain(CANARY_TOKENS.driveCanaryFile); + }); +}); diff --git a/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts b/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts new file mode 100644 index 000000000..4beff0ac5 --- /dev/null +++ b/surfsense_web/tests/fixtures/connectors/native-drive.fixture.ts @@ -0,0 +1,32 @@ +import { + type ConnectorRow, + deleteConnector, + runNativeGoogleDriveOAuth, +} from "../../helpers/api/connectors"; +import { searchSpaceFixtures } from "../search-space.fixture"; + +export type NativeDriveFixtures = { + /** + * A pre-connected native Google Drive connector inside the fixture's + * `searchSpace`. OAuth uses E2E native Google fakes and is cleaned up + * automatically after the test. + */ + nativeDriveConnector: ConnectorRow; +}; + +export const nativeDriveFixtures = searchSpaceFixtures.extend({ + nativeDriveConnector: async ({ request, apiToken, searchSpace }, use) => { + const { connector } = await runNativeGoogleDriveOAuth(request, apiToken, searchSpace.id); + if (!connector) { + throw new Error( + "nativeDriveConnector fixture: OAuth completed but no GOOGLE_DRIVE_CONNECTOR was created. " + + "Check the backend log — the native Google Drive 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 97174f12d..90ab5510d 100644 --- a/surfsense_web/tests/fixtures/index.ts +++ b/surfsense_web/tests/fixtures/index.ts @@ -14,6 +14,8 @@ * └─ composioGmailWithChatTest — chatThread * └─ composioCalendarFixtures — composioCalendarConnector * └─ composioCalendarWithChatTest — chatThread + * └─ nativeDriveFixtures — nativeDriveConnector + * └─ nativeDriveWithChatTest — chatThread * * To add a new connector (Gmail, Slack, manual upload, etc.): * 1. Add a fixture file under `fixtures/connectors/.fixture.ts`. @@ -25,12 +27,14 @@ export { chatThreadFixtures } from "./chat-thread.fixture"; 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 { searchSpaceFixtures } from "./search-space.fixture"; import { type ChatThreadFixtures, chatThreadFixtures } from "./chat-thread.fixture"; 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 { searchSpaceFixtures } from "./search-space.fixture"; /** Default `test` for specs that just need auth + a clean search space. */ @@ -50,3 +54,8 @@ export const composioCalendarTest = composioCalendarFixtures; /** `test` for Calendar specs that also need a chat thread. */ export const composioCalendarWithChatTest = composioCalendarFixtures.extend(chatThreadFixtures); +/** `test` for specs that also need a pre-connected native Google Drive connector. */ +export const nativeDriveTest = nativeDriveFixtures; +/** `test` for native Drive specs that also need a chat thread. */ +export const nativeDriveWithChatTest = + nativeDriveFixtures.extend(chatThreadFixtures); diff --git a/surfsense_web/tests/helpers/api/connectors.ts b/surfsense_web/tests/helpers/api/connectors.ts index 98ac74659..087bff582 100644 --- a/surfsense_web/tests/helpers/api/connectors.ts +++ b/surfsense_web/tests/helpers/api/connectors.ts @@ -188,3 +188,45 @@ export async function runComposioOAuth( return { authUrl: auth_url, finalUrl: location, connector }; } + +/** + * Drives the native Google Drive 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 runNativeGoogleDriveOAuth( + 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/drive/connector/add?space_id=${searchSpaceId}`, + { headers: authHeaders(token) } + ); + if (!initiateResp.ok()) { + throw new Error( + `native Google Drive 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 Drive 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_DRIVE_CONNECTOR") ?? null; + + return { authUrl: auth_url, finalUrl: location, connector }; +}