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';
34
+import { NodeProcess } from 'src/services/node-process';
35
import type { TerminalUI } from 'src/services/terminal-ui';
36
import { ProjectContext } from 'src/services/project-context';
37
import { ComposioUserContext } from 'src/services/user-context';
43
} from 'test/__utils__/layers/connected-accounts';
44
import { hostOnlyEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
45
import { makeOrgProject } from 'test/__utils__/models/account';
45
-import { recordingNodeProcess } from 'test/__utils__/services/node-process-test';
46
+import { exitCodeOf } from 'test/__utils__/exit-code';
47
import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
48
import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
49
import open from 'open';
167
/**
168
* What a link handler needs and nothing more: the account and connected-account families of
169
* the mock client (`test/__utils__/layers`), a Tool Router session that answers, and doubles
169
- * for the user, the directory, and the platform. `requests` records every link request, and
170
- * `exitCode` the exit code a handler sets.
170
+ * for the user, the directory, and the platform. `requests` records every link request.
171
*/
172
const linkHandlerTest = (fixture: LinkFixture = {}) => {
173
const requests: Array<LinkRequest> = [];
239
}) as unknown as ComposioClientShape;
240
241
const dir = tempy.temporaryDirectory({ prefix: 'link-handler' });
242
- const proc = recordingNodeProcess(dir);
242
const layer = Layer.mergeAll(
243
Layer.effect(Console.Console, MockConsole.make),
244
fixture.terminal ?? TerminalUITest,
280
NodeOs,
281
NodeOs.of({ homedir: dir, tmpdir: dir, arch: 'arm64', platform: 'darwin' })
282
),
284
- proc.layer,
283
+ Layer.succeed(
284
+ NodeProcess,
285
+ NodeProcess.of({ cwd: dir, execPath: `${dir}/composio`, platform: 'darwin', arch: 'arm64' })
286
+ ),
287
Layer.succeed(ComposioCliUserConfig, cliUserConfig),
288
ConfigProvider.layer(hostOnlyEnvConfigProvider)
289
);
288
- return { layer, requests, sessionUsers, exitCode: proc.exitCode };
290
+ return { layer, requests, sessionUsers };
291
};
292
293
const lines = MockConsole.getLines({ stripAnsi: true });
294
295
+/** Runs `handler` to its exit, then reads the lines it printed. */
296
+const exitAndLines = <A, E, R>(handler: Effect.Effect<A, E, R>) =>
297
+ Effect.gen(function* () {
298
+ const exit = yield* Effect.exit(handler);
299
+ return { exit, output: yield* lines };
300
+ });
301
+
302
/** The command's JSON result: `ui.output` prints it as the last console line. */
303
const lastJson = lines.pipe(Effect.map(all => JSON.parse(String(all.at(-1))) as unknown));
304
767
'[Given] the link request fails [Then] reports the message and the auth-config hint, and exits 1',
768
() =>
769
Effect.gen(function* () {
761
- const { layer, requests, exitCode } = linkHandlerTest({
770
+ const { layer, requests } = linkHandlerTest({
771
surface: devSurface({ userId: 'user_1' }),
772
directoryProject: directory(Option.none()),
773
legacyLinkCreate: () => Effect.fail(new Error('quota exhausted')),
774
});
766
- const output = yield* handleLegacyAuthConfigLink(
767
- params({ authConfig: Option.some('ac_gmail') })
768
- ).pipe(Effect.andThen(lines), Effect.provide(layer));
775
+ const { exit, output } = yield* exitAndLines(
776
+ handleLegacyAuthConfigLink(params({ authConfig: Option.some('ac_gmail') }))
777
+ ).pipe(Effect.provide(layer));
778
779
expect(output).toEqual(['quota exhausted', BROWSE_AUTH_CONFIGS_HINT]);
771
- expect(exitCode()).toBe(1);
780
+ expect(exitCodeOf(exit)).toBe(1);
781
expect(requests).toEqual([
782
{
783
operation: 'link.create',
791
'[Given] an alias already in use [Then] points at the dev commands, sends nothing, and exits 1',
792
() =>
793
Effect.gen(function* () {
785
- const { layer, requests, exitCode } = linkHandlerTest({
794
+ const { layer, requests } = linkHandlerTest({
795
surface: devSurface({ userId: 'user_1' }),
796
directoryProject: directory(Option.none()),
797
accounts: [account({ user_id: 'user_1', alias: 'Work' })],
798
});
790
- const output = yield* handleLegacyAuthConfigLink(
791
- params({ authConfig: Option.some('ac_gmail'), alias: Option.some(' work ') })
792
- ).pipe(Effect.andThen(lines), Effect.provide(layer));
799
+ const { exit, output } = yield* exitAndLines(
800
+ handleLegacyAuthConfigLink(
801
+ params({ authConfig: Option.some('ac_gmail'), alias: Option.some(' work ') })
802
+ )
803
+ ).pipe(Effect.provide(layer));
804
805
expect(output).toEqual([
806
'Alias "work" is already in use by connected account "con_existing".',
807
"[Tips] Use the existing alias: composio dev playground-execute <TOOL_SLUG> --account work -d '{ ... }'\nList connected accounts: composio dev connected-accounts list",
808
]);
798
- expect(exitCode()).toBe(1);
809
+ expect(exitCodeOf(exit)).toBe(1);
810
expect(requests).toEqual([]);
811
})
812
);
836
);
837
838
it.effect(
828
- '[Given] dev and an alias already in use [Then] points at the dev commands and sends nothing',
839
+ '[Given] dev and an alias already in use [Then] points at the dev commands, sends nothing, and exits 1',
840
() =>
841
Effect.gen(function* () {
842
const { layer, requests } = linkHandlerTest({
844
directoryProject: directory(Option.none()),
845
accounts: [account({ user_id: 'user_1', alias: 'Work' })],
846
});
836
- const output = yield* handleToolRouterLink(
837
- params({ toolkit: Option.some('gmail'), alias: Option.some('work') })
838
- ).pipe(Effect.andThen(lines), Effect.provide(layer));
847
+ const { exit, output } = yield* exitAndLines(
848
+ handleToolRouterLink(
849
+ params({ toolkit: Option.some('gmail'), alias: Option.some('work') })
850
+ )
851
+ ).pipe(Effect.provide(layer));
852
853
expect(output).toEqual([
854
'Alias "work" is already in use by connected account "con_existing".',
855
"[Tips] Use the existing alias: composio dev playground-execute <TOOL_SLUG> --account work -d '{ ... }'\nList connected accounts: composio dev connected-accounts list --toolkits gmail",
856
]);
857
+ expect(exitCodeOf(exit)).toBe(1);
858
expect(requests).toEqual([]);
859
})
860
);
863
'[Given] the consumer project and --alias [Then] links with the alias and prints the pending link',
864
() =>
865
Effect.gen(function* () {
852
- const { layer, requests, exitCode } = linkHandlerTest({ accounts: [account()] });
866
+ const { layer, requests } = linkHandlerTest({ accounts: [account()] });
867
const output = yield* handleToolRouterLink(
868
params({ toolkit: Option.some('gmail'), alias: Option.some(' work ') })
869
).pipe(Effect.andThen(lines), Effect.provide(layer));
875
},
876
]);
877
expect(String(output.at(-1))).toBe(toolRouterPendingJson());
864
- expect(exitCode()).toBeUndefined();
878
})
879
);
880
938
it.effect('[Given] the link request fails [Then] reports the cause and the browse hint', () =>
939
Effect.gen(function* () {
940
const terminal = redirectedTerminalUI();
928
- const { layer, exitCode } = linkHandlerTest({
941
+ const { layer } = linkHandlerTest({
942
toolRouterLink: () => Effect.fail(new Error('offline')),
943
terminal: terminal.layer,
944
});
932
- yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
933
- Effect.provide(layer)
945
+ const exit = yield* Effect.exit(
946
+ handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(Effect.provide(layer))
947
);
948
949
// `failedWith` keeps the client error in `.cause`, and the message prefers it. Plain
950
// lines, so a script, CI or an agent reading a redirected stderr sees them.
951
expect(terminal.stderr()).toBe(`offline\n${BROWSE_TOOLKITS_HINT}\n`);
952
expect(terminal.stdout()).toBe('');
940
- expect(exitCode()).toBe(1);
953
+ expect(exitCodeOf(exit)).toBe(1);
954
})
955
);
956
957
it.effect('[Given] an incomplete link response [Then] reports it and exits 1', () =>
958
Effect.gen(function* () {
946
- const { layer, exitCode } = linkHandlerTest({
959
+ const { layer } = linkHandlerTest({
960
toolRouterLink: () =>
961
Effect.succeed({ connected_account_id: 'con_new', link_token: 'lt', redirect_url: '' }),
962
});
950
- const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
951
- Effect.andThen(lines),
952
- Effect.provide(layer)
953
- );
963
+ const { exit, output } = yield* exitAndLines(
964
+ handleToolRouterLink(params({ toolkit: Option.some('gmail') }))
965
+ ).pipe(Effect.provide(layer));
966
967
expect(output).toEqual(['Linking gmail...', INCOMPLETE_LINK_RESPONSE_MESSAGE]);
956
- expect(exitCode()).toBe(1);
968
+ expect(exitCodeOf(exit)).toBe(1);
969
})
970
);
971
1110
'[Given] an active account and no --alias [Then] lists it, prints no link, and exits 1',
1111
() =>
1112
Effect.gen(function* () {
1101
- const { layer, exitCode } = linkHandlerTest({ accounts: [account({ alias: 'default' })] });
1102
- const output = yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
1103
- Effect.andThen(lines),
1104
- Effect.provide(layer)
1105
- );
1113
+ const { layer } = linkHandlerTest({ accounts: [account({ alias: 'default' })] });
1114
+ const { exit, output } = yield* exitAndLines(
1115
+ handleToolRouterLink(params({ toolkit: Option.some('gmail') }))
1116
+ ).pipe(Effect.provide(layer));
1117
1118
expect(output.slice(-2)).toEqual([
1119
`A connected account already exists for user "${CONSUMER_USER_ID}" in toolkit "gmail". Pass --alias to create another one.`,
1120
'[Existing accounts] con_existing (default, castle)',
1121
]);
1111
- expect(exitCode()).toBe(1);
1122
+ expect(exitCodeOf(exit)).toBe(1);
1123
})
1124
);
1125
});