3
import { Effect, Layer, Option } from 'effect';
4
import {
5
actingUser,
6
- actingUserOption,
6
CommandSurface,
7
+ devActingUserOption,
8
fallbackNotice,
9
MissingActingUserError,
10
+ resolveSurfaceProject,
11
+ resolveDeveloperResourceProject,
12
+ resolveSurfaceProjectAndUserOption,
13
type ActingUserSource,
14
type CommandTree,
15
} from 'src/commands/surface';
16
import type { ProjectKeys } from 'src/models/project-keys';
17
+import { ProjectResolutionError } from 'src/services/command-project';
18
import { ComposioClientSingleton } from 'src/services/composio-clients';
19
import { ProjectContext } from 'src/services/project-context';
20
import { TerminalUI } from 'src/services/terminal-ui';
21
import { ComposioUserContext } from 'src/services/user-context';
22
import { mockAccountResources } from 'test/__utils__/layers/accounts';
19
-import { makeOrgProject } from 'test/__utils__/models/account';
23
+import { makeOrgProject, makeSessionInfo } from 'test/__utils__/models/account';
24
import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
25
26
const ORG_ID = 'org_test';
28
const CONSUMER_PROJECT_ID = 'consumer_project_test';
29
const DIRECTORY_PROJECT_ID = 'proj_directory';
30
const OTHER_PROJECT_ID = 'proj_other';
31
+/** The developer project the backend acts on for a request that names none. */
32
+const DEFAULT_PROJECT_ID = 'proj_newest';
33
34
const projects = [
35
makeOrgProject({ id: DIRECTORY_PROJECT_ID, name: 'directory', orgId: ORG_ID }),
69
email: Option.none(),
70
testUserId: has('directory') ? Option.some('directory-user') : Option.none(),
71
};
66
- const resources = mockAccountResources({ projects }, {});
72
+ const resources = mockAccountResources(
73
+ {
74
+ projects,
75
+ sessionInfo: makeSessionInfo({
76
+ orgId: ORG_ID,
77
+ projectNanoId: DEFAULT_PROJECT_ID,
78
+ projectName: 'newest',
79
+ }),
80
+ },
81
+ {}
82
+ );
83
// The account resources answer only the fields `resolveCommandProject` reads.
84
const client = {
85
...resources,
112
...terminalUITestImpl.log,
113
warn: message => Effect.sync(() => warnings.push(message)),
114
},
115
+ // Off a terminal a warning that must still show is a plain stderr line.
116
+ error: message => Effect.sync(() => warnings.push(message)),
117
})
118
),
119
Layer.succeed(
283
expect(error).toBeInstanceOf(MissingActingUserError);
284
})
285
);
286
+});
287
+
288
+describe('devActingUserOption', () => {
289
+ it.effect.each(cases.filter(({ tree }) => tree === 'dev'))(
290
+ '[Given] $name [Then] finds the rule’s user for the resolved project, or none',
291
+ fixture =>
292
+ Effect.gen(function* () {
293
+ const warnings: Array<string> = [];
294
+ const user = yield* run(
295
+ Effect.flatMap(resolveSurfaceProject('developer'), devActingUserOption),
296
+ fixture,
297
+ warnings
298
+ );
299
+ const expected = expectedUser(fixture);
300
+
301
+ expect(warnings).toEqual(expectedNotices(fixture));
302
+ expect(Option.map(user, ({ userId, source }) => ({ userId, source }))).toEqual(expected);
303
+ expect(Option.map(user, found => found.project.projectId)).toEqual(
304
+ Option.map(expected, () => expectedProjectId(fixture))
305
+ );
306
+ })
307
+ );
308
+});
309
+
310
+const withoutDirectoryProject = Layer.succeed(
311
+ ProjectContext,
312
+ ProjectContext.of({ resolve: Effect.succeedNone })
313
+);
314
+
315
+/** A directory set up, by someone else, for a project in an org the user is not logged in to. */
316
+const directoryProjectElsewhere = Layer.succeed(
317
+ ProjectContext,
318
+ ProjectContext.of({
319
+ resolve: Effect.succeedSome({
320
+ orgId: 'org_elsewhere',
321
+ projectId: 'proj_elsewhere',
322
+ projectName: Option.some('elsewhere'),
323
+ orgName: Option.none(),
324
+ email: Option.none(),
325
+ testUserId: Option.some('elsewhere-user'),
326
+ }),
327
+ })
328
+);
329
+
330
+const NO_DIRECTORY_PROJECT_NOTICE =
331
+ 'Warning: No developer project is set up for this directory; using "newest" (proj_newest), the newest developer project in the org you are logged in to, instead. Run `composio dev init` to choose one, or pass --project-name.';
332
+
333
+const DIRECTORY_PROJECT_ELSEWHERE_NOTICE =
334
+ 'Warning: This directory\'s developer project is in org "org_elsewhere", not the org you are logged in to ("org_test"); using "newest" (proj_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.';
335
+
336
+describe('resolveDeveloperResourceProject', () => {
337
+ it.effect.each([
338
+ { projectName: 'absent', expected: DIRECTORY_PROJECT_ID },
339
+ { projectName: 'other', expected: OTHER_PROJECT_ID },
340
+ ] as const)(
341
+ '[Given] --project-name $projectName [Then] resolves that project with no notice',
342
+ ({ projectName, expected }) =>
343
+ Effect.gen(function* () {
344
+ const warnings: Array<string> = [];
345
+ const project = yield* run(
346
+ resolveDeveloperResourceProject,
347
+ { tree: 'dev', candidates: 'none', projectName },
348
+ warnings
349
+ );
350
+
351
+ expect(project.projectId).toBe(expected);
352
+ expect(warnings).toEqual([]);
353
+ })
354
+ );
355
269
- it.effect('[Given] an empty consumer user id [Then] actingUserOption finds none', () =>
356
+ it.effect.each([
357
+ {
358
+ given: 'no directory project',
359
+ layer: withoutDirectoryProject,
360
+ notice: NO_DIRECTORY_PROJECT_NOTICE,
361
+ },
362
+ {
363
+ given: 'a directory project in another org',
364
+ layer: directoryProjectElsewhere,
365
+ notice: DIRECTORY_PROJECT_ELSEWHERE_NOTICE,
366
+ },
367
+ ])(
368
+ '[Given] $given [Then] uses the default developer project and says so on stderr',
369
+ ({ layer, notice }) =>
370
+ Effect.gen(function* () {
371
+ const warnings: Array<string> = [];
372
+ const project = yield* run(
373
+ resolveDeveloperResourceProject.pipe(Effect.provide(layer)),
374
+ { tree: 'dev', candidates: 'none', projectName: 'absent' },
375
+ warnings
376
+ );
377
+
378
+ expect(project).toMatchObject({
379
+ orgId: ORG_ID,
380
+ projectId: DEFAULT_PROJECT_ID,
381
+ source: 'developer-default',
382
+ });
383
+ expect(warnings).toEqual([notice]);
384
+ })
385
+ );
386
+
387
+ it.effect('[Given] a --project-name the org does not hold [Then] fails', () =>
388
Effect.gen(function* () {
271
- const user = yield* run(actingUserOption(), fixture);
389
+ const error = yield* run(
390
+ Effect.flip(
391
+ resolveDeveloperResourceProject.pipe(
392
+ Effect.provide(
393
+ CommandSurface.dev({ userId: Option.none(), projectName: Option.some('missing') })
394
+ )
395
+ )
396
+ ),
397
+ { tree: 'dev', candidates: 'none', projectName: 'absent' }
398
+ );
399
273
- expect(user).toEqual(Option.none());
400
+ expect(error).toBeInstanceOf(ProjectResolutionError);
401
+ expect(error).toMatchObject({ reason: 'project_not_found' });
402
})
403
);
404
+
405
+ it.effect(
406
+ '[Given] the default project stands in for another org’s directory project [Then] its test user does not',
407
+ () =>
408
+ Effect.gen(function* () {
409
+ const warnings: Array<string> = [];
410
+ const user = yield* run(
411
+ Effect.flatMap(resolveDeveloperResourceProject, devActingUserOption).pipe(
412
+ Effect.provide(directoryProjectElsewhere)
413
+ ),
414
+ { tree: 'dev', candidates: 'global', projectName: 'absent' },
415
+ warnings
416
+ );
417
+
418
+ expect(Option.map(user, ({ userId, source }) => ({ userId, source }))).toEqual(
419
+ Option.some({ userId: 'global-user', source: 'global-test-user' })
420
+ );
421
+ })
422
+ );
423
});
424
278
-describe('actingUserOption', () => {
279
- it.effect.each(cases)('[Given] $name [Then] finds the rule’s user, or none', fixture =>
425
+describe('resolveSurfaceProjectAndUserOption', () => {
426
+ it.effect('[Given] a directory project and its test user [Then] finds both', () =>
427
+ Effect.gen(function* () {
428
+ const { project, user } = yield* run(resolveSurfaceProjectAndUserOption, {
429
+ tree: 'dev',
430
+ candidates: 'directory',
431
+ projectName: 'absent',
432
+ });
433
+
434
+ expect(Option.map(project, found => found.projectId)).toEqual(
435
+ Option.some(DIRECTORY_PROJECT_ID)
436
+ );
437
+ expect(Option.map(user, ({ userId, source }) => ({ userId, source }))).toEqual(
438
+ Option.some({ userId: 'directory-user', source: 'directory-test-user' })
439
+ );
440
+ })
441
+ );
442
+
443
+ it.effect.each([
444
+ { candidates: 'flag', userId: 'flag-user' },
445
+ { candidates: 'global', userId: 'global-user' },
446
+ ] as const)(
447
+ '[Given] no directory project and a $candidates user [Then] uses the default project, saying so',
448
+ ({ candidates, userId }) =>
449
+ Effect.gen(function* () {
450
+ const warnings: Array<string> = [];
451
+ const { project, user } = yield* run(
452
+ resolveSurfaceProjectAndUserOption.pipe(Effect.provide(withoutDirectoryProject)),
453
+ { tree: 'dev', candidates, projectName: 'absent' },
454
+ warnings
455
+ );
456
+
457
+ expect(Option.map(project, found => found.projectId)).toEqual(
458
+ Option.some(DEFAULT_PROJECT_ID)
459
+ );
460
+ expect(Option.map(user, found => found.userId)).toEqual(Option.some(userId));
461
+ expect(warnings[0]).toBe(NO_DIRECTORY_PROJECT_NOTICE);
462
+ })
463
+ );
464
+
465
+ it.effect.each([
466
+ { given: 'no directory project', layer: withoutDirectoryProject },
467
+ { given: 'a directory project in another org', layer: directoryProjectElsewhere },
468
+ ])('[Given] $given and no user but the directory’s [Then] finds neither, quietly', ({ layer }) =>
469
Effect.gen(function* () {
470
const warnings: Array<string> = [];
282
- const user = yield* run(actingUserOption(), fixture, warnings);
283
- const expected = expectedUser(fixture);
471
+ const result = yield* run(
472
+ resolveSurfaceProjectAndUserOption.pipe(Effect.provide(layer)),
473
+ { tree: 'dev', candidates: 'none', projectName: 'absent' },
474
+ warnings
475
+ );
476
285
- expect(warnings).toEqual(expectedNotices(fixture));
286
- // Only the project --project-name names is resolved on the dev tree.
287
- const project =
288
- fixture.tree === 'dev' && fixture.projectName === 'absent'
289
- ? Option.none()
290
- : Option.some(expectedProjectId(fixture));
291
-
292
- expect(Option.map(user, ({ userId, source }) => ({ userId, source }))).toEqual(expected);
293
- expect(
294
- Option.map(user, found => Option.map(found.project, resolved => resolved.projectId))
295
- ).toEqual(Option.map(expected, () => project));
477
+ expect(result).toEqual({ project: Option.none(), user: Option.none() });
478
+ expect(warnings).toEqual([]);
479
})
480
);
481
});