diff --git a/surfsense_web/tests/connectors/onedrive/journey.spec.ts b/surfsense_web/tests/connectors/onedrive/journey.spec.ts new file mode 100644 index 000000000..81a9a52ff --- /dev/null +++ b/surfsense_web/tests/connectors/onedrive/journey.spec.ts @@ -0,0 +1,129 @@ +import { expect, nativeOneDriveWithChatTest 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_ONEDRIVE_FILES } from "../../helpers/canary"; +import { openConnectorPopup } from "../../helpers/ui/connector-popup"; +import { + waitForDocumentByTitle, + waitForIndexingComplete, +} from "../../helpers/waits/indexing"; + +/** + * Proves the native OneDrive wiring from Microsoft OAuth fixture -> selection + * persistence -> OneDrive Graph API indexing -> stored source_markdown -> chat. + * + * The folder picker is intentionally bypassed; this journey exercises the + * persisted config and indexing contract the picker ultimately feeds. + */ +test.describe("Native OneDrive journey", () => { + test( + "user connects OneDrive, selects a file, indexes it, and chats with the canary token", + async ({ + page, + request, + apiToken, + searchSpace, + nativeOneDriveConnector, + chatThread, + }) => { + test.setTimeout(180_000); // worker cold-start + summarize + embed + chunk + + expect(nativeOneDriveConnector.connector_type).toBe("ONEDRIVE_CONNECTOR"); + expect(nativeOneDriveConnector.is_indexable).toBe(true); + expect(nativeOneDriveConnector.config._token_encrypted).toBe(true); + expect(nativeOneDriveConnector.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(); + + const selectedFiles = [ + { + id: FAKE_ONEDRIVE_FILES.canary.id, + name: FAKE_ONEDRIVE_FILES.canary.name, + mimeType: FAKE_ONEDRIVE_FILES.canary.mimeType, + }, + ]; + // Keep the shared Drive-style body shape. OneDrive currently defaults + // these internally because the indexer expects max_files/use_delta_sync. + const indexingOptions = { + max_files_per_folder: 10, + incremental_sync: false, + include_subfolders: false, + }; + + await updateConnectorConfig(request, apiToken, nativeOneDriveConnector.id, { + ...nativeOneDriveConnector.config, + selected_folders: [], + selected_files: selectedFiles, + indexing_options: indexingOptions, + }); + + await triggerIndex(request, apiToken, nativeOneDriveConnector.id, searchSpace.id, { + files: selectedFiles, + indexing_options: indexingOptions, + }); + + await waitForIndexingComplete( + request, + apiToken, + nativeOneDriveConnector.id, + searchSpace.id, + { + timeoutMs: 150_000, + intervalMs: 1_500, + minDocuments: 1, + } + ); + + await waitForDocumentByTitle( + request, + apiToken, + searchSpace.id, + FAKE_ONEDRIVE_FILES.canary.name, + { timeoutMs: 30_000 } + ); + + const docs = await listDocuments(request, apiToken, searchSpace.id); + const canaryDoc = docs.find((d) => d.title === FAKE_ONEDRIVE_FILES.canary.name); + + expect(canaryDoc, "OneDrive canary document must exist after indexing").toBeDefined(); + if (!canaryDoc) throw new Error("unreachable: canaryDoc asserted defined above"); + expect(canaryDoc.document_type).toBe("ONEDRIVE_FILE"); + + const editor = await getEditorContent(request, apiToken, searchSpace.id, canaryDoc.id); + expect( + editor.source_markdown, + `canary token ${CANARY_TOKENS.onedriveCanary} should appear in editor source_markdown; ` + + `got first 200 chars: ${editor.source_markdown.slice(0, 200)}` + ).toContain(CANARY_TOKENS.onedriveCanary); + expect(editor.document_type).toBe("ONEDRIVE_FILE"); + expect(editor.chunk_count).toBeGreaterThan(0); + + const refreshedConnectors = await listConnectors(request, apiToken, searchSpace.id); + const refreshed = refreshedConnectors.find((c) => c.id === nativeOneDriveConnector.id); + expect(refreshed?.connector_type).toBe("ONEDRIVE_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-onedrive-canary.txt OneDrive file?", + }); + expect( + chat.assistantText, + "chat agent should surface OneDrive canary token after indexing; " + + `got: ${chat.assistantText.slice(0, 200)}` + ).toContain(CANARY_TOKENS.onedriveCanary); + } + ); +}); diff --git a/surfsense_web/tests/fixtures/connectors/native-onedrive.fixture.ts b/surfsense_web/tests/fixtures/connectors/native-onedrive.fixture.ts new file mode 100644 index 000000000..bdcd9ec86 --- /dev/null +++ b/surfsense_web/tests/fixtures/connectors/native-onedrive.fixture.ts @@ -0,0 +1,32 @@ +import { + type ConnectorRow, + deleteConnector, + runNativeOneDriveOAuth, +} from "../../helpers/api/connectors"; +import { searchSpaceFixtures } from "../search-space.fixture"; + +export type NativeOneDriveFixtures = { + /** + * A pre-connected native Microsoft OneDrive connector inside the + * fixture's `searchSpace`. OAuth uses E2E Microsoft Graph fakes and is + * cleaned up automatically after the test. + */ + nativeOneDriveConnector: ConnectorRow; +}; + +export const nativeOneDriveFixtures = searchSpaceFixtures.extend({ + nativeOneDriveConnector: async ({ request, apiToken, searchSpace }, use) => { + const { connector } = await runNativeOneDriveOAuth(request, apiToken, searchSpace.id); + if (!connector) { + throw new Error( + "nativeOneDriveConnector fixture: OAuth completed but no ONEDRIVE_CONNECTOR was created. " + + "Check the backend log — the OneDrive 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 4020bdd12..e1099762c 100644 --- a/surfsense_web/tests/fixtures/index.ts +++ b/surfsense_web/tests/fixtures/index.ts @@ -20,6 +20,8 @@ * └─ nativeGmailWithChatTest — chatThread * └─ nativeCalendarFixtures — nativeCalendarConnector * └─ nativeCalendarWithChatTest — chatThread + * └─ nativeOneDriveFixtures — nativeOneDriveConnector + * └─ nativeOneDriveWithChatTest — chatThread * └─ notionFixtures — notionConnector * └─ notionWithChatTest — chatThread * └─ confluenceFixtures — confluenceConnector @@ -47,6 +49,7 @@ export { linearFixtures } from "./connectors/linear.fixture"; export { nativeCalendarFixtures } from "./connectors/native-calendar.fixture"; export { nativeDriveFixtures } from "./connectors/native-drive.fixture"; export { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; +export { nativeOneDriveFixtures } from "./connectors/native-onedrive.fixture"; export { notionFixtures } from "./connectors/notion.fixture"; export { searchSpaceFixtures } from "./search-space.fixture"; export { slackFixtures } from "./connectors/slack.fixture"; @@ -61,6 +64,7 @@ import { linearFixtures } from "./connectors/linear.fixture"; import { nativeCalendarFixtures } from "./connectors/native-calendar.fixture"; import { nativeDriveFixtures } from "./connectors/native-drive.fixture"; import { nativeGmailFixtures } from "./connectors/native-gmail.fixture"; +import { nativeOneDriveFixtures } from "./connectors/native-onedrive.fixture"; import { notionFixtures } from "./connectors/notion.fixture"; import { searchSpaceFixtures } from "./search-space.fixture"; import { slackFixtures } from "./connectors/slack.fixture"; @@ -97,6 +101,11 @@ export const nativeCalendarTest = nativeCalendarFixtures; /** `test` for native Calendar specs that also need a chat thread. */ export const nativeCalendarWithChatTest = nativeCalendarFixtures.extend(chatThreadFixtures); +/** `test` for specs that also need a pre-connected native OneDrive connector. */ +export const nativeOneDriveTest = nativeOneDriveFixtures; +/** `test` for native OneDrive specs that also need a chat thread. */ +export const nativeOneDriveWithChatTest = + nativeOneDriveFixtures.extend(chatThreadFixtures); /** `test` for specs that also need a pre-connected Notion connector. */ export const notionTest = notionFixtures; /** `test` for Notion specs that also need a chat thread. */ diff --git a/surfsense_web/tests/helpers/api/connectors.ts b/surfsense_web/tests/helpers/api/connectors.ts index ec3f6ac45..a638d18b6 100644 --- a/surfsense_web/tests/helpers/api/connectors.ts +++ b/surfsense_web/tests/helpers/api/connectors.ts @@ -256,6 +256,62 @@ export async function runNativeGoogleDriveOAuth( return { authUrl: auth_url, finalUrl: location, connector }; } +/** + * Drives the native Microsoft OneDrive OAuth flow programmatically. + * + * The OneDrive authorization URL is off-origin, so the helper extracts the + * signed state and calls the backend callback directly. The E2E backend fakes + * Microsoft's token and profile HTTP responses inside that callback. + */ +export async function runNativeOneDriveOAuth( + request: APIRequestContext, + token: string, + searchSpaceId: number +): Promise<{ + authUrl: string; + finalUrl: string; + connector: ConnectorRow | null; +}> { + const initiateResp = await request.get( + `${BACKEND_URL}/api/v1/auth/onedrive/connector/add?space_id=${searchSpaceId}`, + { headers: authHeaders(token) } + ); + if (!initiateResp.ok()) { + throw new Error( + `native OneDrive initiate failed (${initiateResp.status()}): ${await initiateResp.text()}` + ); + } + const { auth_url } = (await initiateResp.json()) as { auth_url: string }; + if (!auth_url) { + throw new Error("native OneDrive initiate response missing auth_url"); + } + + const state = new URL(auth_url).searchParams.get("state"); + if (!state) { + throw new Error(`native OneDrive auth_url missing state: ${auth_url}`); + } + + const callbackResp = await request.get( + `${BACKEND_URL}/api/v1/auth/onedrive/connector/callback?code=fake-onedrive-oauth-code&state=${encodeURIComponent(state)}`, + { + headers: authHeaders(token), + maxRedirects: 0, + failOnStatusCode: false, + } + ); + if (callbackResp.status() >= 400) { + throw new Error( + `native OneDrive 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 === "ONEDRIVE_CONNECTOR") ?? null; + + return { authUrl: auth_url, finalUrl: location, connector }; +} + /** * Drives the native Google Gmail OAuth flow programmatically. * diff --git a/surfsense_web/tests/helpers/canary.ts b/surfsense_web/tests/helpers/canary.ts index 480842c10..82c41f339 100644 --- a/surfsense_web/tests/helpers/canary.ts +++ b/surfsense_web/tests/helpers/canary.ts @@ -20,6 +20,7 @@ export const CANARY_TOKENS = { driveArchive: "SURFSENSE_E2E_ARCHIVE_MARKER", gmailCanary: "SURFSENSE_E2E_CANARY_TOKEN_GMAIL_001", calendarCanary: "SURFSENSE_E2E_CANARY_TOKEN_CALENDAR_001", + onedriveCanary: "SURFSENSE_E2E_CANARY_TOKEN_ONEDRIVE_001", notionCanary: "SURFSENSE_E2E_CANARY_TOKEN_NOTION_001", confluenceCanary: "SURFSENSE_E2E_CANARY_TOKEN_CONFLUENCE_001", linearCanary: "SURFSENSE_E2E_CANARY_TOKEN_LINEAR_001", @@ -50,6 +51,18 @@ export const FAKE_DRIVE_FOLDERS = { }, } as const; +/** + * Fake OneDrive file IDs that match the Microsoft Graph-shaped backend + * fake in onedrive_files.json. + */ +export const FAKE_ONEDRIVE_FILES = { + canary: { + id: "fake-onedrive-canary", + name: "e2e-onedrive-canary.txt", + mimeType: "text/plain", + }, +} as const; + /** * Fake Gmail message IDs that match what the backend fake returns from * GMAIL_FETCH_EMAILS / GMAIL_FETCH_MESSAGE_BY_MESSAGE_ID.