mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-12 09:12:40 +02:00
test(web): add native Google Gmail live-tool journey
This commit is contained in:
parent
a5c04cb38d
commit
9dafd38e19
5 changed files with 183 additions and 1 deletions
74
surfsense_web/tests/connectors/google/gmail/journey.spec.ts
Normal file
74
surfsense_web/tests/connectors/google/gmail/journey.spec.ts
Normal file
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
@ -20,7 +20,7 @@ export const nativeDriveFixtures = searchSpaceFixtures.extend<NativeDriveFixture
|
|||
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."
|
||||
"Check the backend log — the native Google fake likely rejected an unmodelled call."
|
||||
);
|
||||
}
|
||||
try {
|
||||
|
|
|
|||
32
surfsense_web/tests/fixtures/connectors/native-gmail.fixture.ts
vendored
Normal file
32
surfsense_web/tests/fixtures/connectors/native-gmail.fixture.ts
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import {
|
||||
type ConnectorRow,
|
||||
deleteConnector,
|
||||
runNativeGoogleGmailOAuth,
|
||||
} from "../../helpers/api/connectors";
|
||||
import { searchSpaceFixtures } from "../search-space.fixture";
|
||||
|
||||
export type NativeGmailFixtures = {
|
||||
/**
|
||||
* A pre-connected native Google Gmail connector inside the fixture's
|
||||
* `searchSpace`. OAuth uses E2E native Google fakes and is cleaned up
|
||||
* automatically after the test.
|
||||
*/
|
||||
nativeGmailConnector: ConnectorRow;
|
||||
};
|
||||
|
||||
export const nativeGmailFixtures = searchSpaceFixtures.extend<NativeGmailFixtures>({
|
||||
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);
|
||||
}
|
||||
},
|
||||
});
|
||||
9
surfsense_web/tests/fixtures/index.ts
vendored
9
surfsense_web/tests/fixtures/index.ts
vendored
|
|
@ -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/<name>.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>(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>(chatThreadFixtures);
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue