Skip to content

Commit

Permalink
test fixes and package.json fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchitharajaghatta committed Nov 15, 2024
1 parent 102306b commit 514816c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 50 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,5 @@
"@types/react-dom": "^17.0.25",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"dependencies": {
"query-string": "^9.1.1"
}
}
1 change: 1 addition & 0 deletions packages/compass-generative-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"hadron-app-registry": "^9.2.7",
"mongodb": "^6.9.0",
"mongodb-schema": "^12.2.0",
"query-string": "^9.1.1",
"react": "^17.0.2",
"react-redux": "^8.1.3",
"redux": "^4.2.1",
Expand Down
20 changes: 2 additions & 18 deletions packages/compass-generative-ai/src/atlas-ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { EJSON } from 'bson';
import { signIntoAtlasWithModalPrompt } from './store/atlas-signin-reducer';
import { getStore } from './store/atlas-ai-store';
import { optIntoGenAIWithModalPrompt } from './store/atlas-optin-reducer';
import type { StringifyOptions } from 'query-string';
import queryString from 'query-string';

type GenerativeAiInput = {
userInput: string;
Expand All @@ -30,18 +28,6 @@ type GenerativeAiInput = {
const AI_MAX_REQUEST_SIZE = 5120000;
const AI_MIN_SAMPLE_DOCUMENTS = 1;

// Usage: when sending a request of content type 'application/x-www-form-urlencoded'
export const formParams = function formParams(
params: Record<string, any>,
{
skipNull = true,
arrayFormat = 'bracket',
...options
}: StringifyOptions = {}
) {
return queryString.stringify(params, { skipNull, arrayFormat, ...options });
};

type AIAggregation = {
content: {
aggregation?: {
Expand Down Expand Up @@ -458,7 +444,7 @@ export class AtlasAiService {

// optIn function w post request to set the user preference to true
async optIntoGenAIFeaturesAtlas() {
const res = await this.atlasService.authenticatedFetch(
await this.atlasService.authenticatedFetch(
this.atlasService.cloudEndpoint(
'/settings/optInDataExplorerGenAIFeatures'
),
Expand All @@ -468,9 +454,7 @@ export class AtlasAiService {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body: formParams({
value: true,
}),
body: new URLSearchParams([['value', 'true']]),
}
);
await this.preferences.savePreferences({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ describe('atlasOptInReducer', function () {
preferences: mockPreferences,
});

expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().optIn).to.have.nested.property(
'state',
'initial'
);
void store.dispatch(atlasAiServiceOptedIn());
await store.dispatch(optIn());
expect(mockAtlasService.optIn).not.to.have.been.called;
expect(store.getState()).to.have.nested.property('state', 'success');
expect(store.getState().optIn).to.have.nested.property(
'state',
'success'
);
});

it('should start opt in, and set state to success', async function () {
Expand All @@ -53,11 +59,17 @@ describe('atlasOptInReducer', function () {
preferences: mockPreferences,
});

expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().optIn).to.have.nested.property(
'state',
'initial'
);
void store.dispatch(optIntoGenAIWithModalPrompt()).catch(() => {});
await store.dispatch(optIn());
expect(mockAtlasService.optIn).to.have.been.calledOnce;
expect(store.getState()).to.have.nested.property('state', 'success');
expect(store.getState().optIn).to.have.nested.property(
'state',
'success'
);
});

it('should fail opt in if opt in failed', async function () {
Expand All @@ -76,7 +88,7 @@ describe('atlasOptInReducer', function () {
AttemptStateMap.get(attemptId)?.promise.catch(() => {});
await optInPromise;
expect(mockAtlasService.optIn).to.have.been.calledOnce;
expect(store.getState()).to.have.nested.property('state', 'error');
expect(store.getState().optIn).to.have.nested.property('state', 'error');
});
});

Expand All @@ -87,9 +99,15 @@ describe('atlasOptInReducer', function () {
atlasAiService: {} as any,
preferences: mockPreferences,
});
expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().optIn).to.have.nested.property(
'state',
'initial'
);
store.dispatch(cancelOptIn());
expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().optIn).to.have.nested.property(
'state',
'initial'
);
});

it('should cancel opt in if opt in is in progress', async function () {
Expand All @@ -116,7 +134,10 @@ describe('atlasOptInReducer', function () {
store.dispatch(optIn()),
store.dispatch(cancelOptIn()),
]);
expect(store.getState()).to.have.nested.property('state', 'canceled');
expect(store.getState().optIn).to.have.nested.property(
'state',
'canceled'
);
});
});

Expand All @@ -135,7 +156,7 @@ describe('atlasOptInReducer', function () {
await store.dispatch(optIn());
await optInPromise;

expect(store.getState()).to.have.property('state', 'success');
expect(store.getState().optIn).to.have.property('state', 'success');
});

it('should reject if opt in flow fails', async function () {
Expand All @@ -158,7 +179,7 @@ describe('atlasOptInReducer', function () {
expect(err).to.have.property('message', 'Whoops!');
}

expect(store.getState()).to.have.property('state', 'error');
expect(store.getState().optIn).to.have.property('state', 'error');
});

it('should reject if user dismissed the modal', async function () {
Expand All @@ -181,7 +202,7 @@ describe('atlasOptInReducer', function () {
expect(err).to.have.property('message', 'This operation was aborted');
}

expect(store.getState()).to.have.property('state', 'canceled');
expect(store.getState().optIn).to.have.property('state', 'canceled');
});

it('should reject if provided signal was aborted', async function () {
Expand All @@ -207,7 +228,7 @@ describe('atlasOptInReducer', function () {
expect(err).to.have.property('message', 'Aborted from outside');
}

expect(store.getState()).to.have.property('state', 'canceled');
expect(store.getState().optIn).to.have.property('state', 'canceled');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ describe('atlasSignInReducer', function () {
preferences: mockPreferences,
});

expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().signIn).to.have.nested.property(
'state',
'initial'
);
void store.dispatch(atlasServiceSignedIn());
await store.dispatch(signIn());
expect(mockAtlasService.signIn).not.to.have.been.called;
expect(store.getState()).to.have.nested.property('state', 'success');
expect(store.getState().signIn).to.have.nested.property(
'state',
'success'
);
});

it('should start sign in, and set state to success', async function () {
Expand All @@ -54,11 +60,17 @@ describe('atlasSignInReducer', function () {
preferences: mockPreferences,
});

expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().signIn).to.have.nested.property(
'state',
'initial'
);
void store.dispatch(signIntoAtlasWithModalPrompt()).catch(() => {});
await store.dispatch(signIn());
expect(mockAtlasService.signIn).to.have.been.calledOnce;
expect(store.getState()).to.have.nested.property('state', 'success');
expect(store.getState().signIn).to.have.nested.property(
'state',
'success'
);
});

it('should fail sign in if sign in failed', async function () {
Expand All @@ -70,14 +82,13 @@ describe('atlasSignInReducer', function () {
atlasAiService: mockAtlasService as any,
preferences: mockPreferences,
});

void store.dispatch(signIntoAtlasWithModalPrompt()).catch(() => {});
const signInPromise = store.dispatch(signIn());
// Avoid unhandled rejections.
AttemptStateMap.get(attemptId)?.promise.catch(() => {});
await signInPromise;
expect(mockAtlasService.signIn).to.have.been.calledOnce;
expect(store.getState()).to.have.nested.property('state', 'error');
expect(store.getState().signIn).to.have.nested.property('state', 'error');
});
});

Expand All @@ -88,9 +99,15 @@ describe('atlasSignInReducer', function () {
atlasAiService: {} as any,
preferences: mockPreferences,
});
expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().signIn).to.have.nested.property(
'state',
'initial'
);
store.dispatch(cancelSignIn());
expect(store.getState()).to.have.nested.property('state', 'initial');
expect(store.getState().signIn).to.have.nested.property(
'state',
'initial'
);
});

it('should cancel sign in if sign in is in progress', async function () {
Expand All @@ -117,7 +134,10 @@ describe('atlasSignInReducer', function () {
store.dispatch(signIn()),
store.dispatch(cancelSignIn()),
]);
expect(store.getState()).to.have.nested.property('state', 'canceled');
expect(store.getState().signIn).to.have.nested.property(
'state',
'canceled'
);
});
});

Expand All @@ -136,7 +156,7 @@ describe('atlasSignInReducer', function () {
await store.dispatch(signIn());
await signInPromise;

expect(store.getState()).to.have.property('state', 'success');
expect(store.getState().signIn).to.have.property('state', 'success');
});

it('should reject if sign in flow fails', async function () {
Expand All @@ -159,7 +179,7 @@ describe('atlasSignInReducer', function () {
expect(err).to.have.property('message', 'Whoops!');
}

expect(store.getState()).to.have.property('state', 'error');
expect(store.getState().signIn).to.have.property('state', 'error');
});

it('should reject if user dismissed the modal', async function () {
Expand All @@ -182,7 +202,7 @@ describe('atlasSignInReducer', function () {
expect(err).to.have.property('message', 'This operation was aborted');
}

expect(store.getState()).to.have.property('state', 'canceled');
expect(store.getState().signIn).to.have.property('state', 'canceled');
});

it('should reject if provided signal was aborted', async function () {
Expand All @@ -207,8 +227,8 @@ describe('atlasSignInReducer', function () {
} catch (err) {
expect(err).to.have.property('message', 'Aborted from outside');
}

expect(store.getState()).to.have.property('state', 'canceled');
expect(store.getState()).to.include('signIn');
expect(store.getState().signIn).to.have.property('state', 'canceled');
});
});
});

0 comments on commit 514816c

Please sign in to comment.