Skip to content

Commit d2c0290

Browse files
test and reducer bug fixes
1 parent 59bdf04 commit d2c0290

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

packages/compass-generative-ai/src/store/atlas-optin-reducer.spec.ts

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ describe('atlasOptInReducer', function () {
1919

2020
beforeEach(async function () {
2121
mockPreferences = await createSandboxFromDefaultPreferences();
22+
await mockPreferences.savePreferences({
23+
optInDataExplorerGenAIFeatures: false,
24+
});
2225
});
2326

2427
afterEach(function () {
2528
sandbox.reset();
2629
});
2730

2831
describe('optIn', function () {
29-
it('should check authenticated state and set state to success if already authenticated', async function () {
30-
const mockAtlasService = {
31-
optIn: sandbox.stub().resolves({ sub: '1234' }),
32+
it('should check state and set state to success if already opted in', async function () {
33+
const mockAtlasAiService = {
34+
optIntoGenAIFeaturesAtlas: sandbox.stub().resolves({ sub: '1234' }),
3235
};
3336
const store = configureStore({
34-
atlasAuthService: mockAtlasService as any,
35-
atlasAiService: mockAtlasService as any,
37+
atlasAuthService: {} as any,
38+
atlasAiService: mockAtlasAiService as any,
3639
preferences: mockPreferences,
3740
});
3841

@@ -41,22 +44,22 @@ describe('atlasOptInReducer', function () {
4144
'initial'
4245
);
4346
void store.dispatch(atlasAiServiceOptedIn());
44-
console.log(store.getState());
4547
await store.dispatch(optIn());
46-
expect(mockAtlasService.optIn).not.to.have.been.called;
48+
expect(mockAtlasAiService.optIntoGenAIFeaturesAtlas).not.to.have.been
49+
.called;
4750
expect(store.getState().optIn).to.have.nested.property(
4851
'state',
4952
'optin-success'
5053
);
5154
});
5255

5356
it('should start opt in, and set state to success', async function () {
54-
const mockAtlasService = {
55-
optIn: sandbox.stub().resolves({ sub: '1234' }),
57+
const mockAtlasAiService = {
58+
optIntoGenAIFeaturesAtlas: sandbox.stub().resolves({ sub: '1234' }),
5659
};
5760
const store = configureStore({
58-
atlasAuthService: mockAtlasService as any,
59-
atlasAiService: mockAtlasService as any,
61+
atlasAuthService: {} as any,
62+
atlasAiService: mockAtlasAiService as any,
6063
preferences: mockPreferences,
6164
});
6265

@@ -66,20 +69,23 @@ describe('atlasOptInReducer', function () {
6669
);
6770
void store.dispatch(optIntoGenAIWithModalPrompt()).catch(() => {});
6871
await store.dispatch(optIn());
69-
expect(mockAtlasService.optIn).to.have.been.calledOnce;
72+
expect(mockAtlasAiService.optIntoGenAIFeaturesAtlas).to.have.been
73+
.calledOnce;
7074
expect(store.getState().optIn).to.have.nested.property(
7175
'state',
7276
'optin-success'
7377
);
7478
});
7579

7680
it('should fail opt in if opt in failed', async function () {
77-
const mockAtlasService = {
78-
optIn: sandbox.stub().rejects(new Error('Pineapples!')),
81+
const mockAtlasAiService = {
82+
optIntoGenAIFeaturesAtlas: sandbox
83+
.stub()
84+
.rejects(new Error('Whooops!')),
7985
};
8086
const store = configureStore({
81-
atlasAuthService: mockAtlasService as any,
82-
atlasAiService: mockAtlasService as any,
87+
atlasAuthService: {} as any,
88+
atlasAiService: mockAtlasAiService as any,
8389
preferences: mockPreferences,
8490
});
8591

@@ -88,7 +94,8 @@ describe('atlasOptInReducer', function () {
8894
// Avoid unhandled rejections.
8995
AttemptStateMap.get(attemptId)?.promise.catch(() => {});
9096
await optInPromise;
91-
expect(mockAtlasService.optIn).to.have.been.calledOnce;
97+
expect(mockAtlasAiService.optIntoGenAIFeaturesAtlas).to.have.been
98+
.calledOnce;
9299
expect(store.getState().optIn).to.have.nested.property('state', 'error');
93100
});
94101
});
@@ -112,8 +119,8 @@ describe('atlasOptInReducer', function () {
112119
});
113120

114121
it('should cancel opt in if opt in is in progress', async function () {
115-
const mockAtlasService = {
116-
optIn: sandbox
122+
const mockAtlasAiService = {
123+
optIntoGenAIFeaturesAtlas: sandbox
117124
.stub()
118125
.callsFake(({ signal }: { signal: AbortSignal }) => {
119126
return new Promise((resolve, reject) => {
@@ -123,9 +130,10 @@ describe('atlasOptInReducer', function () {
123130
});
124131
}),
125132
};
133+
126134
const store = configureStore({
127-
atlasAuthService: mockAtlasService as any,
128-
atlasAiService: mockAtlasService as any,
135+
atlasAuthService: {} as any,
136+
atlasAiService: mockAtlasAiService as any,
129137
preferences: mockPreferences,
130138
});
131139

@@ -144,12 +152,12 @@ describe('atlasOptInReducer', function () {
144152

145153
describe('optIntoAtlasWithModalPrompt', function () {
146154
it('should resolve when user finishes opt in with prompt flow', async function () {
147-
const mockAtlasService = {
148-
optIn: sandbox.stub().resolves({ sub: '1234' }),
155+
const mockAtlasAiService = {
156+
optIntoGenAIFeaturesAtlas: sandbox.stub().resolves({ sub: '1234' }),
149157
};
150158
const store = configureStore({
151-
atlasAuthService: mockAtlasService as any,
152-
atlasAiService: mockAtlasService as any,
159+
atlasAuthService: {} as any,
160+
atlasAiService: mockAtlasAiService as any,
153161
preferences: mockPreferences,
154162
});
155163

@@ -161,12 +169,12 @@ describe('atlasOptInReducer', function () {
161169
});
162170

163171
it('should reject if opt in flow fails', async function () {
164-
const mockAtlasService = {
165-
optIn: sandbox.stub().rejects(new Error('Whoops!')),
172+
const mockAtlasAiService = {
173+
optIntoGenAIFeaturesAtlas: sandbox.stub().rejects(new Error('Whoops!')),
166174
};
167175
const store = configureStore({
168-
atlasAuthService: mockAtlasService as any,
169-
atlasAiService: mockAtlasService as any,
176+
atlasAuthService: {} as any,
177+
atlasAiService: mockAtlasAiService as any,
170178
preferences: mockPreferences,
171179
});
172180

@@ -184,12 +192,12 @@ describe('atlasOptInReducer', function () {
184192
});
185193

186194
it('should reject if user dismissed the modal', async function () {
187-
const mockAtlasService = {
188-
optIn: sandbox.stub().resolves({ sub: '1234' }),
195+
const mockAtlasAiService = {
196+
optIntoGenAIFeaturesAtlas: sandbox.stub().resolves({ sub: '1234' }),
189197
};
190198
const store = configureStore({
191-
atlasAuthService: mockAtlasService as any,
192-
atlasAiService: mockAtlasService as any,
199+
atlasAuthService: {} as any,
200+
atlasAiService: mockAtlasAiService as any,
193201
preferences: mockPreferences,
194202
});
195203

@@ -207,12 +215,12 @@ describe('atlasOptInReducer', function () {
207215
});
208216

209217
it('should reject if provided signal was aborted', async function () {
210-
const mockAtlasService = {
211-
optIn: sandbox.stub().resolves({ sub: '1234' }),
218+
const mockAtlasAiService = {
219+
optIntoGenAIFeaturesAtlas: sandbox.stub().resolves({ sub: '1234' }),
212220
};
213221
const store = configureStore({
214-
atlasAuthService: mockAtlasService as any,
215-
atlasAiService: mockAtlasService as any,
222+
atlasAuthService: {} as any,
223+
atlasAiService: mockAtlasAiService as any,
216224
preferences: mockPreferences,
217225
});
218226

packages/compass-generative-ai/src/store/atlas-optin-reducer.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const startAttempt = (
199199
fn: () => void
200200
): GenAIAtlasOptInThunkAction<AttemptState> => {
201201
return (dispatch, getState) => {
202-
if (getState().attemptId) {
202+
if (getState().optIn.attemptId) {
203203
throw new Error(
204204
"Can't start opt in with prompt while another opt in attempt is in progress"
205205
);
@@ -230,7 +230,7 @@ export const optIntoGenAIWithModalPrompt = ({
230230
> => {
231231
return (dispatch, getState, { preferences }) => {
232232
// Nothing to do if we already opted in.
233-
const { state } = getState();
233+
const { state } = getState().optIn;
234234
if (
235235
state === 'optin-success' ||
236236
preferences.getPreferences().optInDataExplorerGenAIFeatures
@@ -251,23 +251,25 @@ export const optIntoGenAIWithModalPrompt = ({
251251

252252
export const optIn = (): GenAIAtlasOptInThunkAction<Promise<void>> => {
253253
return async (dispatch, getState, { atlasAiService }) => {
254-
const { attemptId } = getState();
254+
if (['in-progress', 'optin-success'].includes(getState().optIn.state)) {
255+
return;
256+
}
257+
const { attemptId } = getState().optIn;
255258
if (attemptId === null) {
256259
return;
257260
}
258261
const {
259262
controller: { signal },
260263
resolve,
261264
reject,
262-
} = getAttempt(getState().attemptId);
265+
} = getAttempt(getState().optIn.attemptId);
263266
dispatch({
264267
type: AtlasOptInActions.Start,
265268
});
266269

267270
try {
268271
throwIfAborted(signal);
269-
console.log(atlasAiService);
270-
await atlasAiService.optIn.optIntoGenAIFeaturesAtlas();
272+
await atlasAiService.optIntoGenAIFeaturesAtlas();
271273
dispatch(atlasAiServiceOptedIn());
272274
resolve();
273275
} catch (err) {
@@ -300,10 +302,10 @@ export const cancelOptIn = (reason?: any): GenAIAtlasOptInThunkAction<void> => {
300302
return (dispatch, getState) => {
301303
// Can't cancel opt in after the flow was finished indicated by current
302304
// attempt id being set to null.
303-
if (getState().attemptId === null) {
305+
if (getState().optIn.attemptId === null) {
304306
return;
305307
}
306-
const attempt = getAttempt(getState().attemptId);
308+
const attempt = getAttempt(getState().optIn.attemptId);
307309
attempt.controller.abort();
308310
attempt.reject(reason ?? attempt.controller.signal.reason);
309311
dispatch({ type: AtlasOptInActions.Cancel });

packages/compass-generative-ai/src/store/atlas-signin-reducer.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ describe('atlasSignInReducer', function () {
227227
} catch (err) {
228228
expect(err).to.have.property('message', 'Aborted from outside');
229229
}
230-
expect(store.getState()).to.include('signIn');
231230
expect(store.getState().signIn).to.have.property('state', 'canceled');
232231
});
233232
});

packages/compass-generative-ai/src/store/atlas-signin-reducer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ const startAttempt = (
227227
fn: () => void
228228
): GenAIAtlasSignInThunkAction<AttemptState> => {
229229
return (dispatch, getState) => {
230-
if (getState().attemptId) {
230+
if (getState().signIn.attemptId) {
231231
throw new Error(
232232
"Can't start sign in with prompt while another sign in attempt is in progress"
233233
);
@@ -257,7 +257,7 @@ export const signIntoAtlasWithModalPrompt = ({
257257
> => {
258258
return (dispatch, getState) => {
259259
// Nothing to do if we already signed in.
260-
const { state } = getState();
260+
const { state } = getState().signIn;
261261
if (state === 'success') {
262262
return Promise.resolve();
263263
}
@@ -275,18 +275,18 @@ export const signIntoAtlasWithModalPrompt = ({
275275

276276
export const signIn = (): GenAIAtlasSignInThunkAction<Promise<void>> => {
277277
return async (dispatch, getState, { atlasAuthService }) => {
278-
if (['in-progress', 'authenticated'].includes(getState().state)) {
278+
if (['in-progress', 'authenticated'].includes(getState().signIn.state)) {
279279
return;
280280
}
281-
const { attemptId } = getState();
281+
const { attemptId } = getState().signIn;
282282
if (attemptId === null) {
283283
return;
284284
}
285285
const {
286286
controller: { signal },
287287
resolve,
288288
reject,
289-
} = getAttempt(getState().attemptId);
289+
} = getAttempt(getState().signIn.attemptId);
290290
dispatch({
291291
type: AtlasSignInActions.Start,
292292
});
@@ -332,10 +332,10 @@ export const cancelSignIn = (
332332
return (dispatch, getState) => {
333333
// Can't cancel sign in after the flow was finished indicated by current
334334
// attempt id being set to null.
335-
if (getState().attemptId === null) {
335+
if (getState().signIn.attemptId === null) {
336336
return;
337337
}
338-
const attempt = getAttempt(getState().attemptId);
338+
const attempt = getAttempt(getState().signIn.attemptId);
339339
attempt.controller.abort();
340340
attempt.reject(reason ?? attempt.controller.signal.reason);
341341
dispatch({ type: AtlasSignInActions.Cancel });

0 commit comments

Comments
 (0)