Skip to content

Commit 60b7ef4

Browse files
committed
fixup: use shared params endpoint, add preferences listener for compass web e2e tests
1 parent 5139504 commit 60b7ef4

File tree

13 files changed

+120
-298
lines changed

13 files changed

+120
-298
lines changed

packages/compass-e2e-tests/helpers/atlas-ai-preferences-override.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/compass-e2e-tests/helpers/commands/set-feature.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { CompassBrowser } from '../compass-browser';
2-
import type { UserPreferences } from 'compass-preferences-model';
2+
import type {
3+
AllPreferences,
4+
UserPreferences,
5+
} from 'compass-preferences-model';
36

47
export async function setFeature<K extends keyof UserPreferences>(
58
browser: CompassBrowser,
@@ -17,3 +20,23 @@ export async function setFeature<K extends keyof UserPreferences>(
1720
value
1821
);
1922
}
23+
24+
// When running in Compass web we cannot use the IPC to update the
25+
// preferences so we use a global function.
26+
export async function setFeatureCompassWeb<K extends keyof UserPreferences>(
27+
browser: CompassBrowser,
28+
name: K,
29+
value: UserPreferences[K]
30+
): Promise<void> {
31+
await browser.execute(
32+
async (_name, _value) => {
33+
const attributes: Partial<AllPreferences> = {
34+
[_name]: _value === null ? undefined : _value,
35+
};
36+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37+
await (globalThis as any).__compassWebE2ETestSavePreferences(attributes);
38+
},
39+
name,
40+
value
41+
);
42+
}

packages/compass-e2e-tests/helpers/test-runner-context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { hideBin } from 'yargs/helpers';
99
import Debug from 'debug';
1010
import fs from 'fs';
1111
import { getAtlasCloudSandboxDefaultConnections } from './compass-web-sandbox';
12-
import { E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT } from './atlas-ai-preferences-override';
1312

1413
const debug = Debug('compass-e2e-tests:context');
1514

@@ -346,8 +345,7 @@ process.env.COMPASS_WEB_HTTP_PROXY_CLOUD_CONFIG ??=
346345
context.atlasCloudSandboxCloudConfig ?? 'dev';
347346

348347
if (isTestingAtlasCloudSandbox(context)) {
349-
process.env.E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT =
350-
E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT;
348+
process.env.E2E_TEST_CLOUD_WEB_ENABLE_PREFERENCE_SAVING ??= 'true';
351349
}
352350

353351
const testServerVersion =

packages/compass-e2e-tests/tests/atlas-cloud/collection-ai-query.test.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,24 @@ import type { Compass } from '../../helpers/compass';
1111
import * as Selectors from '../../helpers/selectors';
1212
import { createNumbersCollection } from '../../helpers/insert-data';
1313
import { isTestingAtlasCloudSandbox } from '../../helpers/test-runner-context';
14-
import {
15-
enabledPreferencesResponse,
16-
startPreferencesOverrideServer,
17-
} from '../../helpers/atlas-ai-preferences-override';
18-
import type { PreferencesServerResponse } from '../../helpers/atlas-ai-preferences-override';
1914

20-
describe('Collection ai query', function () {
15+
describe.only('Collection ai query', function () {
2116
let compass: Compass;
2217
let browser: CompassBrowser;
23-
let setPreferencesResponse: (response: PreferencesServerResponse) => void;
24-
let stopPreferencesServer: () => Promise<void>;
2518

26-
before(async function () {
19+
before(function () {
2720
if (!isTestingAtlasCloudSandbox()) {
2821
this.skip();
2922
}
30-
31-
// Start a mock server to pass an ai response.
32-
const { setPreferencesResponse: _setPreferencesResponse, stop } =
33-
await startPreferencesOverrideServer();
34-
35-
stopPreferencesServer = stop;
36-
setPreferencesResponse = _setPreferencesResponse;
3723
});
3824

3925
afterEach(async function () {
4026
await screenshotIfFailed(compass, this.currentTest);
4127
await cleanup(compass);
4228
});
4329

44-
after(async function () {
45-
if (!isTestingAtlasCloudSandbox()) {
46-
return;
47-
}
48-
49-
await stopPreferencesServer();
50-
});
51-
5230
describe('when the feature is enabled', function () {
5331
beforeEach(async function () {
54-
setPreferencesResponse(enabledPreferencesResponse);
55-
5632
compass = await init(this.test?.fullTitle());
5733
browser = compass.browser;
5834
await browser.setupDefaultConnections();
@@ -65,6 +41,20 @@ describe('Collection ai query', function () {
6541
'numbers',
6642
'Documents'
6743
);
44+
45+
await browser.setFeatureCompassWeb(
46+
'enableGenAIFeaturesAtlasProject',
47+
true
48+
);
49+
await browser.setFeatureCompassWeb(
50+
'enableGenAISampleDocumentPassingOnAtlasProject',
51+
true
52+
);
53+
await browser.setFeatureCompassWeb('enableGenAIFeaturesAtlasOrg', true);
54+
await browser.setFeatureCompassWeb(
55+
'optInDataExplorerGenAIFeatures',
56+
true
57+
);
6858
});
6959

7060
it('should update the query bar with a generated query', async function () {
@@ -104,13 +94,6 @@ describe('Collection ai query', function () {
10494

10595
describe('when the org feature is disabled', function () {
10696
beforeEach(async function () {
107-
setPreferencesResponse({
108-
enableGenAIFeaturesAtlasProject: true,
109-
enableGenAISampleDocumentPassingOnAtlasProject: true,
110-
enableGenAIFeaturesAtlasOrg: false,
111-
optInDataExplorerGenAIFeatures: true,
112-
});
113-
11497
compass = await init(this.test?.fullTitle());
11598
browser = compass.browser;
11699
await browser.setupDefaultConnections();
@@ -123,6 +106,20 @@ describe('Collection ai query', function () {
123106
'numbers',
124107
'Documents'
125108
);
109+
110+
await browser.setFeatureCompassWeb(
111+
'enableGenAIFeaturesAtlasProject',
112+
true
113+
);
114+
await browser.setFeatureCompassWeb(
115+
'enableGenAISampleDocumentPassingOnAtlasProject',
116+
true
117+
);
118+
await browser.setFeatureCompassWeb('enableGenAIFeaturesAtlasOrg', true);
119+
await browser.setFeatureCompassWeb(
120+
'optInDataExplorerGenAIFeatures',
121+
false
122+
);
126123
});
127124

128125
it('should not show the gen ai intro button', async function () {

packages/compass-generative-ai/src/atlas-ai-service.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -298,39 +298,7 @@ export class AtlasAiService {
298298
return body;
299299
}
300300

301-
async _e2eTestOnlyOverrideAIFeaturePreferences() {
302-
try {
303-
// When we're running e2e tests and want to customize these preferences on the fly
304-
// we make a request to the server to override the preferences.
305-
const {
306-
enableGenAIFeaturesAtlasProject,
307-
enableGenAISampleDocumentPassingOnAtlasProject,
308-
enableGenAIFeaturesAtlasOrg,
309-
optInDataExplorerGenAIFeatures,
310-
} = await fetch('e2e-test-atlas-preferences-override').then((res) =>
311-
res.json()
312-
);
313-
314-
await this.preferences.savePreferences({
315-
enableGenAIFeaturesAtlasProject,
316-
enableGenAISampleDocumentPassingOnAtlasProject,
317-
enableGenAIFeaturesAtlasOrg,
318-
optInDataExplorerGenAIFeatures,
319-
});
320-
return;
321-
} catch (e) {
322-
/** no-op when the server isn't up. */
323-
}
324-
}
325-
326301
async setupAIAccess(): Promise<void> {
327-
if (
328-
process.env.E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT !== undefined &&
329-
process.env.E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT !== 'false'
330-
) {
331-
await this._e2eTestOnlyOverrideAIFeaturePreferences();
332-
}
333-
334302
try {
335303
const featureResponse = await this.getAIFeatureEnablement();
336304

packages/compass-preferences-model/src/compass-web-preferences-access.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ export class CompassWebPreferencesAccess implements PreferencesAccess {
1212
logger: createNoopLogger(),
1313
preferencesStorage: new InMemoryStorage(preferencesOverrides),
1414
});
15-
}
1615

17-
savePreferences(_attributes: Partial<UserPreferences>) {
18-
if (
19-
process.env.E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT !== undefined &&
20-
process.env.E2E_TEST_ATLAS_PREFERENCES_OVERRIDE_PORT !== 'false'
21-
) {
22-
return Promise.resolve(this._preferences.savePreferences(_attributes));
16+
if (process.env.E2E_TEST_CLOUD_WEB_ENABLE_PREFERENCE_SAVING) {
17+
// Useful for e2e test to override preferences.
18+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19+
(globalThis as any).__compassWebE2ETestSavePreferences = async (
20+
attributes: Partial<AllPreferences>
21+
) => {
22+
await this._preferences.savePreferences(attributes);
23+
};
2324
}
25+
}
2426

27+
savePreferences(_attributes: Partial<UserPreferences>) {
2528
// Only allow saving the optInDataExplorerGenAIFeatures preference.
2629
if (
2730
Object.keys(_attributes).length === 1 &&
@@ -61,17 +64,12 @@ export class CompassWebPreferencesAccess implements PreferencesAccess {
6164
preferenceName: K,
6265
callback: (value: AllPreferences[K]) => void
6366
) {
64-
return (
65-
this._preferences?.onPreferencesChanged?.(
66-
(preferences: Partial<AllPreferences>) => {
67-
if (Object.keys(preferences).includes(preferenceName)) {
68-
return callback((preferences as AllPreferences)[preferenceName]);
69-
}
67+
return this._preferences.onPreferencesChanged(
68+
(preferences: Partial<AllPreferences>) => {
69+
if (Object.keys(preferences).includes(preferenceName)) {
70+
return callback((preferences as AllPreferences)[preferenceName]);
7071
}
71-
) ??
72-
(() => {
73-
/* no fallback */
74-
})
72+
}
7573
);
7674
}
7775

packages/compass-web/sandbox/index.tsx

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import React, { useLayoutEffect } from 'react';
22
import ReactDOM from 'react-dom';
3-
import {
4-
resetGlobalCSS,
5-
css,
6-
Body,
7-
SpinLoaderWithLabel,
8-
} from '@mongodb-js/compass-components';
3+
import { resetGlobalCSS, css, Body } from '@mongodb-js/compass-components';
94
import { CompassWeb } from '../src/index';
105
import { SandboxConnectionStorageProvider } from '../src/connection-storage';
116
import { sandboxLogger } from './sandbox-logger';
127
import { sandboxTelemetry } from './sandbox-telemetry';
138
import { useAtlasProxySignIn } from './sandbox-atlas-sign-in';
149
import { sandboxConnectionStorage } from './sandbox-connection-storage';
1510
import { useWorkspaceTabRouter } from './sandbox-workspace-tab-router';
16-
import { useAtlasPreferences } from './sandbox-atlas-preferences';
1711

1812
const sandboxContainerStyles = css({
1913
width: '100%',
@@ -37,11 +31,15 @@ function getMetaEl(name: string) {
3731
const App = () => {
3832
const [currentTab, updateCurrentTab] = useWorkspaceTabRouter();
3933
const { status, projectParams } = useAtlasProxySignIn();
40-
const { projectId, csrfToken, csrfTime } = projectParams ?? {};
41-
const { status: preferencesStatus, preferences: atlasPreferences } =
42-
useAtlasPreferences({
43-
projectId,
44-
});
34+
const {
35+
projectId,
36+
csrfToken,
37+
csrfTime,
38+
enableGenAIFeaturesAtlasProject,
39+
enableGenAISampleDocumentPassingOnAtlasProject,
40+
enableGenAIFeaturesAtlasOrg,
41+
optInDataExplorerGenAIFeatures,
42+
} = projectParams ?? {};
4543

4644
const atlasServiceSandboxBackendVariant =
4745
process.env.COMPASS_WEB_HTTP_PROXY_CLOUD_CONFIG === 'local'
@@ -62,10 +60,6 @@ const App = () => {
6260

6361
const isAtlas = status === 'signed-in';
6462

65-
if (isAtlas && preferencesStatus === 'loading') {
66-
return <SpinLoaderWithLabel progressText="Loading Atlas preferences" />;
67-
}
68-
6963
return (
7064
<SandboxConnectionStorageProvider
7165
value={isAtlas ? null : sandboxConnectionStorage}
@@ -92,14 +86,13 @@ const App = () => {
9286
enableGlobalWrites: isAtlas,
9387
enableRollingIndexes: isAtlas,
9488
enableGenAIFeaturesAtlasProject:
95-
isAtlas && !!atlasPreferences?.enableGenAIFeaturesAtlasProject,
89+
isAtlas && !!enableGenAIFeaturesAtlasProject,
9690
enableGenAISampleDocumentPassingOnAtlasProject:
97-
isAtlas &&
98-
!!atlasPreferences?.enableGenAISampleDocumentPassingOnAtlasProject,
91+
isAtlas && !!enableGenAISampleDocumentPassingOnAtlasProject,
9992
enableGenAIFeaturesAtlasOrg:
100-
isAtlas && !!atlasPreferences?.enableGenAIFeaturesAtlasOrg,
93+
isAtlas && !!enableGenAIFeaturesAtlasOrg,
10194
optInDataExplorerGenAIFeatures:
102-
isAtlas && !!atlasPreferences?.optInDataExplorerGenAIFeatures,
95+
isAtlas && !!optInDataExplorerGenAIFeatures,
10396
}}
10497
onTrack={sandboxTelemetry.track}
10598
onDebug={sandboxLogger.debug}

0 commit comments

Comments
 (0)