95
})
96
);
97
98
+const orgList = (ids: ReadonlyArray<string>, nextCursor: string | null = null) =>
99
+ jsonResponse({
100
+ organizations: ids.map(id => ({
101
+ id,
102
+ name: id,
103
+ created_at: '2024-01-01T00:00:00.000Z',
104
+ updated_at: '2024-01-01T00:00:00.000Z',
105
+ })),
106
+ next_cursor: nextCursor,
107
+ total_pages: 1,
108
+ current_page: 1,
109
+ total_items: ids.length,
110
+ });
111
+
112
describe('resolveCommandProject', () => {
113
afterEach(() => {
114
vi.restoreAllMocks();
141
}).pipe(Effect.provide(testLayer))
142
);
143
144
+ it.effect('refuses a directory project outside the logged-in org without a request', () =>
145
+ Effect.gen(function* () {
146
+ const fetchSpy = vi.spyOn(globalThis, 'fetch');
147
+
148
+ const error = yield* resolveCommandProject({ mode: 'developer' }).pipe(Effect.flip);
149
+
150
+ expect(error).toMatchObject({ reason: 'org_mismatch' });
151
+ expect(error.message).toContain('"org_other"');
152
+ expect(error.message).toContain('composio orgs switch');
153
+ expect(fetchSpy).not.toHaveBeenCalled();
154
+ }).pipe(
155
+ Effect.provide(
156
+ Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other'))
157
+ )
158
+ )
159
+ );
160
+
161
+ it.effect('resolves a directory project in the logged-in org without a request', () =>
162
+ Effect.gen(function* () {
163
+ const fetchSpy = vi.spyOn(globalThis, 'fetch');
164
+ const project = yield* resolveCommandProject({ mode: 'developer' });
165
+ expect(fetchSpy).not.toHaveBeenCalled();
166
+
167
+ expect(project).toMatchObject({
168
+ orgId: 'org_1',
169
+ projectId: 'pr_local',
170
+ source: 'developer-local-config',
171
+ });
172
+ }).pipe(
173
+ Effect.provide(
174
+ Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_1'))
175
+ )
176
+ )
177
+ );
178
+
179
+ it.effect(
180
+ 'checks a directory project against every page of the key orgs when none is stored',
181
+ () =>
182
+ Effect.gen(function* () {
183
+ const fetchSpy = vi
184
+ .spyOn(globalThis, 'fetch')
185
+ .mockResolvedValueOnce(orgList(['org_a'], 'cursor_2'))
186
+ .mockResolvedValueOnce(orgList(['org_2']));
187
+
188
+ const project = yield* resolveCommandProject({ mode: 'developer' });
189
+
190
+ expect(project).toMatchObject({ orgId: 'org_2', projectId: 'pr_local' });
191
+ expect(fetchSpy).toHaveBeenCalledTimes(2);
192
+ }).pipe(
193
+ Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2')))
194
+ )
195
+ );
196
+
197
+ it.effect('refuses a directory project from another org when no org is stored', () =>
198
+ Effect.gen(function* () {
199
+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(orgList(['org_mine']));
200
+
201
+ const error = yield* resolveCommandProject({ mode: 'developer' }).pipe(Effect.flip);
202
+
203
+ expect(error).toMatchObject({ reason: 'org_mismatch' });
204
+ expect(error.message).toContain('cannot access');
205
+ }).pipe(
206
+ Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2')))
207
+ )
208
+ );
209
+
210
it.effect('turns an unknown project name into project_not_found with the remedy', () =>
211
Effect.gen(function* () {
212
vi.spyOn(globalThis, 'fetch').mockResolvedValue(projectsPage(['Other']));
376
}).pipe(Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), noLocalProject)))
377
);
378
379
+ it.effect(
380
+ 'ignores a directory project outside the logged-in org and names it in the notice',
381
+ () =>
382
+ Effect.gen(function* () {
383
+ const fetchSpy = vi
384
+ .spyOn(globalThis, 'fetch')
385
+ .mockResolvedValue(defaultProjectInfo('org_1'));
386
+
387
+ const { project, notice } = yield* resolveDeveloperProjectOrDefault({});
388
+
389
+ expect(project).toMatchObject({
390
+ orgId: 'org_1',
391
+ projectId: 'pr_newest',
392
+ source: 'developer-default',
393
+ });
394
+ expect(notice).toEqual(
395
+ Option.some(
396
+ 'This directory\'s developer project is in org "org_other", not the org you are logged in to ("org_1"); using "Newest App" (pr_newest), the newest developer project in the org you are logged in to, instead. Run `composio orgs switch` to use the directory\'s org, or `composio dev init` to set this directory up in yours.'
397
+ )
398
+ );
399
+ // No request ever names the directory's project.
400
+ expect(fetchSpy).toHaveBeenCalledTimes(1);
401
+ expect(requestOf(fetchSpy.mock.calls[0]).headers.get('x-org-id')).toBe('org_1');
402
+ }).pipe(
403
+ Effect.provide(
404
+ Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other'))
405
+ )
406
+ )
407
+ );
408
+
409
+ it.effect(
410
+ 'ignores a directory project in an org the key cannot access when no org is stored',
411
+ () =>
412
+ Effect.gen(function* () {
413
+ const fetchSpy = vi
414
+ .spyOn(globalThis, 'fetch')
415
+ .mockResolvedValueOnce(orgList(['org_mine']))
416
+ .mockResolvedValueOnce(defaultProjectInfo('org_mine'));
417
+
418
+ const { project, notice } = yield* resolveDeveloperProjectOrDefault({});
419
+
420
+ expect(project).toMatchObject({
421
+ orgId: 'org_mine',
422
+ projectId: 'pr_newest',
423
+ source: 'developer-default',
424
+ });
425
+ expect(notice).toEqual(
426
+ Option.some(
427
+ 'This directory\'s developer project is in org "org_2", which your API key cannot access; using "Newest App" (pr_newest), the newest developer project in the org you joined most recently, instead. Run `composio dev init` to set this directory up in one of your orgs.'
428
+ )
429
+ );
430
+ expect(fetchSpy).toHaveBeenCalledTimes(2);
431
+ const sessionRequest = requestOf(fetchSpy.mock.calls[1]);
432
+ expect(sessionRequest.path).toBe(SESSION_INFO_PATH);
433
+ expect(sessionRequest.headers.has('x-org-id')).toBe(false);
434
+ }).pipe(
435
+ Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2')))
436
+ )
437
+ );
438
+
439
it.effect.each([
440
{
441
given: 'session info without a project',
466
}).pipe(Effect.provide(testLayer))
467
);
468
469
+ it.effect('names the ignored directory project when no default can be found either', () =>
470
+ Effect.gen(function* () {
471
+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(
472
+ jsonResponse({ ...makeSessionInfo(), project: null })
473
+ );
474
+
475
+ const error = yield* resolveDeveloperProjectOrDefault({}).pipe(Effect.flip);
476
+
477
+ expect(error).toMatchObject({
478
+ reason: 'missing_developer_project',
479
+ message:
480
+ 'This directory\'s developer project is in org "org_other", not the org you are logged in to ("org_1"), and no developer project was found in your org to use instead. Run `composio dev init` to set one up.',
481
+ });
482
+ }).pipe(
483
+ Effect.provide(
484
+ Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other'))
485
+ )
486
+ )
487
+ );
488
+
489
it.effect.each([
490
{ status: 401, message: 'Invalid API key' },
491
{ status: 403, message: 'Access denied' },