24
TOOLKIT_AND_AUTH_CONFIG_MESSAGE,
25
missingArgumentMessage,
26
} from 'src/commands/connected-accounts/link/connected-accounts.link.format';
27
-import { CommandSurface } from 'src/commands/surface';
28
-import { MissingLinkUserIdError } from 'src/commands/connected-accounts/link/connected-accounts.link.errors';
27
+import { CommandSurface, MissingActingUserError } from 'src/commands/surface';
28
import { CliUserConfig } from 'src/models/cli-user-config';
29
import type { ConnectedAccountItem } from 'src/models/connected-accounts';
30
import type { ProjectKeys } from 'src/models/project-keys';
171
*/
172
const linkHandlerTest = (fixture: LinkFixture = {}) => {
173
const requests: Array<LinkRequest> = [];
174
+ /** The `user_id` of every Tool Router session the handler created. */
175
+ const sessionUsers: Array<string> = [];
176
const connectedAccounts = mockConnectedAccountResources(
177
resolveConnectedAccountsTestData({ items: [...(fixture.accounts ?? [])] })
178
);
218
...baseClient.toolRouter,
219
session: {
220
...baseClient.toolRouter.session,
220
- create: () => Effect.succeed({ session_id: 'trs_handler_test' }),
221
+ create: (sessionParams: { user_id: string }) =>
222
+ Effect.sync(() => sessionUsers.push(sessionParams.user_id)).pipe(
223
+ Effect.as({ session_id: 'trs_handler_test' })
224
+ ),
225
link: (sessionId: string, linkParams: { toolkit: string; alias?: string }) =>
226
record('toolRouter.session.link', { sessionId, ...linkParams }).pipe(
227
Effect.andThen(
285
Layer.succeed(ComposioCliUserConfig, cliUserConfig),
286
ConfigProvider.layer(hostOnlyEnvConfigProvider)
287
);
284
- return { layer, requests, exitCode: proc.exitCode };
288
+ return { layer, requests, sessionUsers, exitCode: proc.exitCode };
289
};
290
291
const lines = MockConsole.getLines({ stripAnsi: true });
319
});
320
321
/** The pending payload of a Tool Router link, byte for byte. */
318
-const toolRouterPendingJson = () =>
322
+const toolRouterPendingJson = (projectType: 'CONSUMER' | 'DEVELOPER' = 'CONSUMER') =>
323
toJson({
324
status: 'pending',
325
message: 'Complete authorization by opening the URL',
326
connected_account_id: TOOL_ROUTER_LINK_TARGET.connected_account_id,
327
redirect_url: TOOL_ROUTER_LINK_TARGET.redirect_url,
328
toolkit: 'gmail',
325
- project_type: 'CONSUMER',
329
+ project_type: projectType,
330
});
331
332
/** The payload once polling saw `con_new` ACTIVE, byte for byte. */
427
{ flow: 'tool-router', input: { toolkit: Option.some('gmail') } },
428
])('[Given] the flags select the $flow flow [Then] it runs that flow', c =>
429
Effect.gen(function* () {
426
- const fixtureAccount = account({ alias: 'work' });
430
+ // On the dev tree every flow acts as --user-id on the directory's developer project.
431
+ const fixtureAccount = account({ alias: 'work', user_id: 'user_1' });
432
const { layer } = linkHandlerTest({
433
surface: devSurface({ userId: 'user_1' }),
434
accounts: [fixtureAccount],
448
? toJson({ toolkit: 'gmail', total: 1, items: [fixtureAccount] })
449
: c.flow === 'legacy'
450
? legacyPendingJson()
446
- : toolRouterPendingJson()
451
+ : toolRouterPendingJson('DEVELOPER')
452
);
453
})
454
);
556
})
557
);
558
559
+ it.effect.each([
560
+ { name: '--user-id', userId: 'user_flag', expectedUser: 'user_flag' },
561
+ { name: 'the directory’s test user', userId: undefined, expectedUser: 'user_directory' },
562
+ ])(
563
+ '[Given] dev without --project-name and $name [Then] lists that user’s accounts in the directory’s project',
564
+ c =>
565
+ Effect.gen(function* () {
566
+ const { layer } = linkHandlerTest({
567
+ surface: devSurface({ userId: c.userId }),
568
+ directoryProject: directory(Option.some('user_directory')),
569
+ accounts: [CONSUMER_USER_ID, 'user_flag', 'user_directory'].map(userId =>
570
+ account({ id: `con_${userId}`, user_id: userId })
571
+ ),
572
+ });
573
+ const result = yield* handleListConnectedAccounts(
574
+ params({ toolkit: Option.some('gmail'), list: true })
575
+ ).pipe(Effect.andThen(lastJson), Effect.provide(layer));
576
+
577
+ expect(result).toMatchObject({ total: 1, items: [{ user_id: c.expectedUser }] });
578
+ })
579
+ );
580
+
581
+ it.effect(
582
+ '[Given] dev without --project-name outside a developer project [Then] fails with ProjectResolutionError',
583
+ () =>
584
+ Effect.gen(function* () {
585
+ const { layer, requests } = linkHandlerTest({
586
+ surface: devSurface({ userId: 'user_flag' }),
587
+ });
588
+ const error = yield* handleListConnectedAccounts(
589
+ params({ toolkit: Option.some('gmail'), list: true })
590
+ ).pipe(Effect.flip, Effect.provide(layer));
591
+
592
+ expect(error).toMatchObject({
593
+ _tag: 'services/ProjectResolutionError',
594
+ reason: 'missing_developer_project',
595
+ });
596
+ expect(requests).toEqual([]);
597
+ })
598
+ );
599
+
600
it.effect('[Given] the list request fails [Then] fails with the request error', () =>
601
Effect.gen(function* () {
602
const { layer } = linkHandlerTest({ connectedAccountsListFails: true });
611
})
612
);
613
614
+ it.effect.each([
615
+ {
616
+ tree: 'root',
617
+ surface: CommandSurface.root,
618
+ message: 'Missing consumer user id. Run `composio login` and try again.',
619
+ },
620
+ {
621
+ tree: 'dev',
622
+ surface: devSurface(),
623
+ message:
624
+ 'Missing user id. Provide --user-id or run `composio dev init` to set a playground test user id.',
625
+ },
626
+ ])('[Given] no user on the $tree tree [Then] fails with that tree’s message', c =>
627
+ Effect.gen(function* () {
628
+ const { layer } = linkHandlerTest({
629
+ surface: c.surface,
630
+ directoryProject: directory(Option.none()),
631
+ consumerUserId: '',
632
+ });
633
+ const error = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
634
+ Effect.flip,
635
+ Effect.provide(layer)
636
+ );
637
+
638
+ expect(error).toMatchObject({ message: c.message });
639
+ })
640
+ );
641
+
642
it.effect(
643
'[Given] dev --project-name and no user anywhere [Then] fails pointing at --user-id',
644
() =>
649
).pipe(Effect.flip, Effect.provide(layer));
650
651
expect(error).toMatchObject({
578
- _tag: 'commands/MissingLinkUserIdError',
579
- flow: 'list',
652
+ _tag: 'commands/MissingActingUserError',
653
message:
654
'Missing user id. Provide --user-id, or run `composio login` to store a global test user id.',
655
});
710
).pipe(Effect.flip, Effect.provide(layer));
711
712
expect(error).toMatchObject({
640
- _tag: 'commands/MissingLinkUserIdError',
641
- flow: 'legacy',
642
- message: 'Missing user id. Provide --user-id or run composio dev init to set test_user_id.',
713
+ _tag: 'commands/MissingActingUserError',
714
+ message:
715
+ 'Missing user id. Provide --user-id or run `composio dev init` to set a playground test user id.',
716
});
717
})
718
);
719
720
+ it.effect(
721
+ '[Given] no developer project and no user [Then] reports the missing project first',
722
+ () =>
723
+ Effect.gen(function* () {
724
+ const { layer, requests } = linkHandlerTest({ surface: devSurface() });
725
+ const error = yield* handleLegacyAuthConfigLink(
726
+ params({ authConfig: Option.some('ac_gmail') })
727
+ ).pipe(Effect.flip, Effect.provide(layer));
728
+
729
+ expect(error).toMatchObject({
730
+ _tag: 'commands/CommandInputError',
731
+ option: '--auth-config',
732
+ message: AUTH_CONFIG_SCOPE_MESSAGE,
733
+ });
734
+ expect(requests).toEqual([]);
735
+ })
736
+ );
737
+
738
it.effect(
739
'[Given] no developer project [Then] fails with a CommandInputError explaining the scope and sends nothing',
740
() =>
802
});
803
804
describe('connected-accounts link handler: handleToolRouterLink', () => {
805
+ it.effect.each([
806
+ { name: '--user-id', userId: 'user_flag', expectedUser: 'user_flag' },
807
+ { name: 'the directory’s test user', userId: undefined, expectedUser: 'user_directory' },
808
+ ])(
809
+ '[Given] dev without --project-name and $name [Then] links that user in the directory’s project',
810
+ c =>
811
+ Effect.gen(function* () {
812
+ const { layer, sessionUsers } = linkHandlerTest({
813
+ surface: devSurface({ userId: c.userId }),
814
+ directoryProject: directory(Option.some('user_directory')),
815
+ globalTestUserId: Option.some('user_global'),
816
+ });
817
+ const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
818
+ Effect.andThen(lines),
819
+ Effect.provide(layer)
820
+ );
821
+
822
+ expect(sessionUsers).toEqual([c.expectedUser]);
823
+ expect(String(output.at(-1))).toBe(toolRouterPendingJson('DEVELOPER'));
824
+ })
825
+ );
826
+
827
+ it.effect(
828
+ '[Given] dev and an alias already in use [Then] points at the dev commands and sends nothing',
829
+ () =>
830
+ Effect.gen(function* () {
831
+ const { layer, requests } = linkHandlerTest({
832
+ surface: devSurface({ userId: 'user_1' }),
833
+ directoryProject: directory(Option.none()),
834
+ accounts: [account({ user_id: 'user_1', alias: 'Work' })],
835
+ });
836
+ const output = yield* handleToolRouterLink(
837
+ params({ toolkit: Option.some('gmail'), alias: Option.some('work') })
838
+ ).pipe(Effect.andThen(lines), Effect.provide(layer));
839
+
840
+ expect(output).toEqual([
841
+ 'Alias "work" is already in use by connected account "con_existing".',
842
+ "[Tips] Use the existing alias: composio dev playground-execute <TOOL_SLUG> --account work -d '{ ... }'\nList connected accounts: composio dev connected-accounts list --toolkits gmail",
843
+ ]);
844
+ expect(requests).toEqual([]);
845
+ })
846
+ );
847
+
848
it.effect(
849
'[Given] the consumer project and --alias [Then] links with the alias and prints the pending link',
850
() =>
972
})
973
);
974
841
- it.effect(
842
- '[Given] no consumer user and no test user [Then] fails with the tool-router missing-user error',
843
- () =>
844
- Effect.gen(function* () {
845
- const { layer, requests } = linkHandlerTest({ consumerUserId: null });
846
- const failure = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
847
- Effect.flip,
848
- Effect.provide(layer)
849
- );
850
-
851
- // The flow re-fails `actingUser`'s expected failure as its own expected error, so
852
- // `cli.command.failed` keeps reporting `commands/MissingLinkUserIdError`.
853
- expect(failure).toBeInstanceOf(MissingLinkUserIdError);
854
- expect(failure).toMatchObject({
855
- flow: 'tool-router',
856
- message:
857
- 'Missing user id. Provide --user-id or run composio login to set global test_user_id.',
858
- });
859
- expect(requests).toEqual([]);
860
- })
861
- );
862
-
863
- it.effect('[Given] an empty consumer user id [Then] the flow still acts as it and links', () =>
975
+ it.effect.each([
976
+ { case: 'no consumer user id at all', consumerUserId: null },
977
+ { case: 'an empty consumer user id', consumerUserId: '' },
978
+ ])('[Given] $case and no test user [Then] fails before any link request', c =>
979
Effect.gen(function* () {
865
- const { layer, requests } = linkHandlerTest({ consumerUserId: '' });
866
- yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
867
- Effect.andThen(lines),
980
+ const { layer, requests } = linkHandlerTest({ consumerUserId: c.consumerUserId });
981
+ const failure = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
982
+ Effect.flip,
983
Effect.provide(layer)
984
);
985
871
- // As before `actingUser`, the flow's legacy options accept the empty id as the user.
872
- expect(requests).toEqual([
873
- {
874
- operation: 'toolRouter.session.link',
875
- params: { sessionId: 'trs_handler_test', toolkit: 'gmail' },
876
- },
877
- ]);
986
+ // An empty id counts as no user, so both shapes fail with the shared missing-user error.
987
+ expect(failure).toBeInstanceOf(MissingActingUserError);
988
+ expect(failure).toMatchObject({
989
+ message: 'Missing consumer user id. Run `composio login` and try again.',
990
+ });
991
+ expect(requests).toEqual([]);
992
})
993
);
994