17
AUTH_CONFIG_SCOPE_MESSAGE,
18
BROWSE_AUTH_CONFIGS_HINT,
19
BROWSE_TOOLKITS_HINT,
20
+ INCOMPLETE_LINK_RESPONSE_MESSAGE,
21
LIST_REQUIRES_TOOLKIT_MESSAGE,
22
LIST_WITH_AUTH_CONFIG_MESSAGE,
23
ROOT_AUTH_CONFIG_MESSAGE,
31
import { ComposioCliUserConfig } from 'src/services/cli-user-config';
32
import { ComposioClientSingleton } from 'src/services/composio-clients';
33
import { NodeOs } from 'src/services/node-os';
33
-import { NodeProcess } from 'src/services/node-process';
34
+import type { TerminalUI } from 'src/services/terminal-ui';
35
import { ProjectContext } from 'src/services/project-context';
36
import { ComposioUserContext } from 'src/services/user-context';
37
import { ConnectedAccountsTest, MockConsole } from 'test/__utils__';
42
} from 'test/__utils__/layers/connected-accounts';
43
import { hostOnlyEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
44
import { makeOrgProject } from 'test/__utils__/models/account';
45
+import { recordingNodeProcess } from 'test/__utils__/services/node-process-test';
46
+import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
47
import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
48
import open from 'open';
49
import { afterEach, vi } from 'vitest';
118
readonly connectedAccountsListFails?: boolean;
119
/** Makes the first N `connectedAccounts.retrieve` calls fail, for the polling retries. */
120
readonly retrieveFailsFirst?: number;
121
+ /** Replaces the headless test terminal, e.g. with `redirectedTerminalUI`. */
122
+ readonly terminal?: Layer.Layer<TerminalUI>;
123
}
124
125
/** A request that creates a link, as the mock client saw it. */
164
/**
165
* What a link handler needs and nothing more: the account and connected-account families of
166
* the mock client (`test/__utils__/layers`), a Tool Router session that answers, and doubles
162
- * for the user, the directory, and the platform. `requests` records every link request.
167
+ * for the user, the directory, and the platform. `requests` records every link request, and
168
+ * `exitCode` the exit code a handler sets.
169
*/
170
const linkHandlerTest = (fixture: LinkFixture = {}) => {
171
const requests: Array<LinkRequest> = [];
229
}) as unknown as ComposioClientShape;
230
231
const dir = tempy.temporaryDirectory({ prefix: 'link-handler' });
232
+ const proc = recordingNodeProcess(dir);
233
const layer = Layer.mergeAll(
234
Layer.effect(Console.Console, MockConsole.make),
228
- TerminalUITest,
235
+ fixture.terminal ?? TerminalUITest,
236
fixture.surface ?? CommandSurface.root,
237
Layer.succeed(
238
ComposioUserContext,
271
NodeOs,
272
NodeOs.of({ homedir: dir, tmpdir: dir, arch: 'arm64', platform: 'darwin' })
273
),
267
- Layer.succeed(
268
- NodeProcess,
269
- NodeProcess.of({
270
- cwd: dir,
271
- execPath: `${dir}/composio`,
272
- platform: 'darwin',
273
- arch: 'arm64',
274
- setExitCode: () => Effect.void,
275
- })
276
- ),
274
+ proc.layer,
275
Layer.succeed(ComposioCliUserConfig, cliUserConfig),
276
ConfigProvider.layer(hostOnlyEnvConfigProvider)
277
);
280
- return { layer, requests };
278
+ return { layer, requests, exitCode: proc.exitCode };
279
};
280
281
const lines = MockConsole.getLines({ stripAnsi: true });
359
input: { alias: Option.some(' '), authConfig: Option.some('ac_1') },
360
option: '--alias',
361
message: '`--alias` cannot be empty.',
362
+ command: 'link',
363
},
364
{
365
name: '--auth-config on the root tree, which is consumer-only',
367
input: { toolkit: Option.some('gmail'), authConfig: Option.some('ac_1') },
368
option: '--auth-config',
369
message: ROOT_AUTH_CONFIG_MESSAGE,
370
+ command: 'link',
371
},
372
- ])('[Given] $name [Then] fails with a CommandInputError and sends nothing', c =>
373
- Effect.gen(function* () {
374
- const { layer, requests } = linkHandlerTest({ surface: c.surface });
375
- const error = yield* runConnectedAccountsLink(params(c.input)).pipe(
376
- Effect.flip,
377
- Effect.provide(layer)
378
- );
379
-
380
- expect(error).toMatchObject({
381
- _tag: 'commands/CommandInputError',
382
- command: 'link',
383
- option: c.option,
384
- message: c.message,
385
- });
386
- expect(requests).toEqual([]);
387
- })
388
- );
389
-
390
- it.effect.each([
372
{
373
name: 'a toolkit and --auth-config under dev',
374
surface: devSurface(),
375
input: { toolkit: Option.some('gmail'), authConfig: Option.some('ac_1') },
376
+ option: '--auth-config',
377
message: TOOLKIT_AND_AUTH_CONFIG_MESSAGE,
378
+ command: 'dev connected-accounts link',
379
},
380
{
381
name: 'neither a toolkit nor --auth-config on the root tree',
382
surface: CommandSurface.root,
383
input: {},
384
+ option: '<toolkit>',
385
message: missingArgumentMessage('root'),
386
+ command: 'link',
387
},
388
{
389
name: 'neither a toolkit nor --auth-config under dev',
390
surface: devSurface(),
391
input: {},
392
+ option: '<toolkit>',
393
message: missingArgumentMessage('dev'),
394
+ command: 'dev connected-accounts link',
395
},
409
- ])('[Given] $name [Then] prints the usage message and exits 0', c =>
396
+ ])('[Given] $name [Then] fails with a CommandInputError and sends nothing', c =>
397
Effect.gen(function* () {
398
const { layer, requests } = linkHandlerTest({ surface: c.surface });
412
- const output = yield* runConnectedAccountsLink(params(c.input)).pipe(
413
- Effect.andThen(lines),
399
+ const error = yield* runConnectedAccountsLink(params(c.input)).pipe(
400
+ Effect.flip,
401
Effect.provide(layer)
402
);
403
417
- expect(output).toEqual([c.message]);
404
+ expect(error).toMatchObject({
405
+ _tag: 'commands/CommandInputError',
406
+ command: c.command,
407
+ option: c.option,
408
+ message: c.message,
409
+ });
410
expect(requests).toEqual([]);
411
})
412
);
455
input: { list: true, toolkit: Option.some('gmail'), authConfig: Option.some('ac_1') },
456
message: LIST_WITH_AUTH_CONFIG_MESSAGE,
457
},
466
- ])('[Given] $name [Then] prints why --list cannot run', c =>
458
+ ])('[Given] $name [Then] fails with a CommandInputError saying why --list cannot run', c =>
459
Effect.gen(function* () {
468
- const { layer } = linkHandlerTest();
469
- const output = yield* handleListConnectedAccounts(params(c.input)).pipe(
470
- Effect.andThen(lines),
460
+ const { layer, requests } = linkHandlerTest();
461
+ const error = yield* handleListConnectedAccounts(params(c.input)).pipe(
462
+ Effect.flip,
463
Effect.provide(layer)
464
);
465
474
- expect(output).toEqual([c.message]);
466
+ expect(error).toMatchObject({
467
+ _tag: 'commands/CommandInputError',
468
+ command: 'link',
469
+ option: '--list',
470
+ message: c.message,
471
+ });
472
+ expect(requests).toEqual([]);
473
})
474
);
475
602
})
603
);
604
605
+ it.effect('[Given] no --auth-config [Then] fails with a usage error and sends nothing', () =>
606
+ Effect.gen(function* () {
607
+ const { layer, requests } = linkHandlerTest({ surface: devSurface() });
608
+ const error = yield* handleLegacyAuthConfigLink(params()).pipe(
609
+ Effect.flip,
610
+ Effect.provide(layer)
611
+ );
612
+
613
+ expect(error).toMatchObject({
614
+ _tag: 'commands/CommandInputError',
615
+ command: 'dev connected-accounts link',
616
+ option: '--auth-config',
617
+ message: missingArgumentMessage('dev'),
618
+ });
619
+ expect(requests).toEqual([]);
620
+ })
621
+ );
622
+
623
it.effect('[Given] no user anywhere [Then] fails pointing at dev init', () =>
624
Effect.gen(function* () {
625
const { layer } = linkHandlerTest({
638
})
639
);
640
625
- it.effect('[Given] no developer project [Then] explains the scope and sends nothing', () =>
626
- Effect.gen(function* () {
627
- const { layer, requests } = linkHandlerTest({ surface: devSurface({ userId: 'user_1' }) });
628
- const output = yield* handleLegacyAuthConfigLink(
629
- params({ authConfig: Option.some('ac_gmail') })
630
- ).pipe(Effect.andThen(lines), Effect.provide(layer));
641
+ it.effect(
642
+ '[Given] no developer project [Then] fails with a CommandInputError explaining the scope and sends nothing',
643
+ () =>
644
+ Effect.gen(function* () {
645
+ const { layer, requests } = linkHandlerTest({ surface: devSurface({ userId: 'user_1' }) });
646
+ const error = yield* handleLegacyAuthConfigLink(
647
+ params({ authConfig: Option.some('ac_gmail') })
648
+ ).pipe(Effect.flip, Effect.provide(layer));
649
632
- expect(output).toEqual([AUTH_CONFIG_SCOPE_MESSAGE]);
633
- expect(requests).toEqual([]);
634
- })
650
+ expect(error).toMatchObject({
651
+ _tag: 'commands/CommandInputError',
652
+ command: 'dev connected-accounts link',
653
+ option: '--auth-config',
654
+ message: AUTH_CONFIG_SCOPE_MESSAGE,
655
+ });
656
+ expect(requests).toEqual([]);
657
+ })
658
);
659
660
it.effect(
638
- '[Given] the link request fails [Then] reports the message and the auth-config hint',
661
+ '[Given] the link request fails [Then] reports the message and the auth-config hint, and exits 1',
662
() =>
663
Effect.gen(function* () {
641
- const { layer, requests } = linkHandlerTest({
664
+ const { layer, requests, exitCode } = linkHandlerTest({
665
surface: devSurface({ userId: 'user_1' }),
666
directoryProject: directory(Option.none()),
667
legacyLinkCreate: () => Effect.fail(new Error('quota exhausted')),
671
).pipe(Effect.andThen(lines), Effect.provide(layer));
672
673
expect(output).toEqual(['quota exhausted', BROWSE_AUTH_CONFIGS_HINT]);
674
+ expect(exitCode()).toBe(1);
675
expect(requests).toEqual([
676
{
677
operation: 'link.create',
682
);
683
684
it.effect(
661
- '[Given] an alias already in use [Then] points at the dev commands and sends nothing',
685
+ '[Given] an alias already in use [Then] points at the dev commands, sends nothing, and exits 1',
686
() =>
687
Effect.gen(function* () {
664
- const { layer, requests } = linkHandlerTest({
688
+ const { layer, requests, exitCode } = linkHandlerTest({
689
surface: devSurface({ userId: 'user_1' }),
690
directoryProject: directory(Option.none()),
691
accounts: [account({ user_id: 'user_1', alias: 'Work' })],
698
'Alias "work" is already in use by connected account "con_existing".',
699
"[Tips] Use the existing alias: composio dev playground-execute <TOOL_SLUG> --account work -d '{ ... }'\nList connected accounts: composio dev connected-accounts list",
700
]);
701
+ expect(exitCode()).toBe(1);
702
expect(requests).toEqual([]);
703
})
704
);
709
'[Given] the consumer project and --alias [Then] links with the alias and prints the pending link',
710
() =>
711
Effect.gen(function* () {
687
- const { layer, requests } = linkHandlerTest({ accounts: [account()] });
712
+ const { layer, requests, exitCode } = linkHandlerTest({ accounts: [account()] });
713
const output = yield* handleToolRouterLink(
714
params({ toolkit: Option.some('gmail'), alias: Option.some(' work ') })
715
).pipe(Effect.andThen(lines), Effect.provide(layer));
721
},
722
]);
723
expect(String(output.at(-1))).toBe(toolRouterPendingJson());
724
+ expect(exitCode()).toBeUndefined();
725
})
726
);
727
784
785
it.effect('[Given] the link request fails [Then] reports the cause and the browse hint', () =>
786
Effect.gen(function* () {
761
- const { layer } = linkHandlerTest({
787
+ const terminal = redirectedTerminalUI();
788
+ const { layer, exitCode } = linkHandlerTest({
789
toolRouterLink: () => Effect.fail(new Error('offline')),
790
+ terminal: terminal.layer,
791
+ });
792
+ yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
793
+ Effect.provide(layer)
794
+ );
795
+
796
+ // `failedWith` keeps the client error in `.cause`, and the message prefers it. Plain
797
+ // lines, so a script, CI or an agent reading a redirected stderr sees them.
798
+ expect(terminal.stderr()).toBe(`offline\n${BROWSE_TOOLKITS_HINT}\n`);
799
+ expect(terminal.stdout()).toBe('');
800
+ expect(exitCode()).toBe(1);
801
+ })
802
+ );
803
+
804
+ it.effect('[Given] an incomplete link response [Then] reports it and exits 1', () =>
805
+ Effect.gen(function* () {
806
+ const { layer, exitCode } = linkHandlerTest({
807
+ toolRouterLink: () =>
808
+ Effect.succeed({ connected_account_id: 'con_new', link_token: 'lt', redirect_url: '' }),
809
});
810
const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
811
Effect.andThen(lines),
812
Effect.provide(layer)
813
);
814
769
- // `failedWith` keeps the client error in `.cause`, and the message prefers it.
770
- expect(output).toEqual(['offline', BROWSE_TOOLKITS_HINT]);
815
+ expect(output).toEqual(['Linking gmail...', INCOMPLETE_LINK_RESPONSE_MESSAGE]);
816
+ expect(exitCode()).toBe(1);
817
+ })
818
+ );
819
+
820
+ it.effect('[Given] no toolkit [Then] fails with a usage error and sends nothing', () =>
821
+ Effect.gen(function* () {
822
+ const { layer, requests } = linkHandlerTest();
823
+ const error = yield* handleToolRouterLink(params()).pipe(Effect.flip, Effect.provide(layer));
824
+
825
+ expect(error).toMatchObject({
826
+ _tag: 'commands/CommandInputError',
827
+ command: 'link',
828
+ option: '<toolkit>',
829
+ message: missingArgumentMessage('root'),
830
+ });
831
+ expect(requests).toEqual([]);
832
})
833
);
834
934
})
935
);
936
876
- it.effect('[Given] an active account and no --alias [Then] lists it and prints no link', () =>
877
- Effect.gen(function* () {
878
- const { layer } = linkHandlerTest({ accounts: [account({ alias: 'default' })] });
879
- const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
880
- Effect.andThen(lines),
881
- Effect.provide(layer)
882
- );
937
+ it.effect(
938
+ '[Given] an active account and no --alias [Then] lists it, prints no link, and exits 1',
939
+ () =>
940
+ Effect.gen(function* () {
941
+ const { layer, exitCode } = linkHandlerTest({ accounts: [account({ alias: 'default' })] });
942
+ const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
943
+ Effect.andThen(lines),
944
+ Effect.provide(layer)
945
+ );
946
884
- expect(output.slice(-2)).toEqual([
885
- `A connected account already exists for user "${CONSUMER_USER_ID}" in toolkit "gmail". Pass --alias to create another one.`,
886
- '[Existing accounts] con_existing (default, castle)',
887
- ]);
888
- })
947
+ expect(output.slice(-2)).toEqual([
948
+ `A connected account already exists for user "${CONSUMER_USER_ID}" in toolkit "gmail". Pass --alias to create another one.`,
949
+ '[Existing accounts] con_existing (default, castle)',
950
+ ]);
951
+ expect(exitCode()).toBe(1);
952
+ })
953
);
954
});