22
LIST_WITH_AUTH_CONFIG_MESSAGE,
23
ROOT_AUTH_CONFIG_MESSAGE,
24
TOOLKIT_AND_AUTH_CONFIG_MESSAGE,
25
+ formatNonInteractiveAuthorizationInstructions,
26
missingArgumentMessage,
27
} from 'src/commands/connected-accounts/link/connected-accounts.link.format';
28
import { CommandSurface, MissingActingUserError } from 'src/commands/surface';
33
import { ComposioClientSingleton } from 'src/services/composio-clients';
34
import { NodeOs } from 'src/services/node-os';
35
import { NodeProcess } from 'src/services/node-process';
35
-import type { TerminalUI } from 'src/services/terminal-ui';
36
+import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
37
import { ProjectContext } from 'src/services/project-context';
38
import { ComposioUserContext } from 'src/services/user-context';
39
import { ConnectedAccountsTest, MockConsole } from 'test/__utils__';
46
import { makeOrgProject } from 'test/__utils__/models/account';
47
import { exitCodeOf } from 'test/__utils__/exit-code';
48
import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
48
-import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
49
+import { TerminalUITest, terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
50
import open from 'open';
51
import { afterEach, vi } from 'vitest';
52
122
readonly connectedAccountsListFails?: boolean;
123
/** Makes the first N `connectedAccounts.retrieve` calls fail, for the polling retries. */
124
readonly retrieveFailsFirst?: number;
125
+ /** Runs before every `connectedAccounts.retrieve`, i.e. at every poll while the link waits. */
126
+ readonly onRetrieve?: () => void;
127
/** Replaces the headless test terminal, e.g. with `redirectedTerminalUI`. */
128
readonly terminal?: Layer.Layer<TerminalUI>;
129
}
191
? () => Stream.fail(new Error('connectedAccounts.list is down'))
192
: connectedAccounts.connectedAccounts.listStream,
193
retrieve: (nanoid: string) => {
194
+ fixture.onRetrieve?.();
195
retrieveAttempts += 1;
196
return retrieveAttempts <= (fixture.retrieveFailsFirst ?? 0)
197
? Effect.fail(new Error(`transient retrieve failure ${retrieveAttempts}`))
371
return result;
372
});
373
374
+/** The headless test terminal with stderr a TTY, so the link's decoration renders. */
375
+const decoratingTerminal = Layer.succeed(
376
+ TerminalUI,
377
+ TerminalUI.of({
378
+ ...terminalUITestImpl,
379
+ capabilities: Effect.succeed(
380
+ getTerminalCapabilities({
381
+ stdin: { isTTY: false },
382
+ stdout: { isTTY: false },
383
+ stderr: { isTTY: true },
384
+ })
385
+ ),
386
+ })
387
+);
388
+
389
const devSurface = (flags: { userId?: string; projectName?: string } = {}) =>
390
CommandSurface.dev({
391
userId: Option.fromUndefinedOr(flags.userId),
1032
accounts: [account({ id: 'con_new', user_id: 'poll-source', alias: null, word_id: null })],
1033
toolRouterLink: () =>
1034
Effect.succeed({ connected_account_id: 'con_new', redirect_url: 'notaurl' }),
1035
+ terminal: decoratingTerminal,
1036
});
1037
const output = yield* handleToolRouterLink(
1038
params({ toolkit: Option.some('gmail'), noWait: false })
1063
vi.mocked(open).mockRejectedValueOnce(new Error('no browser'));
1064
const { layer } = linkHandlerTest({
1065
accounts: [account({ id: 'con_new', user_id: 'poll-source', alias: null, word_id: null })],
1066
+ terminal: decoratingTerminal,
1067
});
1068
const output = yield* handleToolRouterLink(
1069
params({ toolkit: Option.some('gmail'), noWait: false, noBrowser: false })
1147
})
1148
);
1149
});
1150
+
1151
+describe('connected-accounts link handler: waiting off a terminal', () => {
1152
+ // Every stream redirected, as an agent's shell runs the command. The first two polls find
1153
+ // nothing, so the link is still waiting when `stderrWhileWaiting` is read, at the first one.
1154
+ it.effect.each([
1155
+ {
1156
+ flow: 'a toolkit',
1157
+ account: 'con_new',
1158
+ redirectUrl: TOOL_ROUTER_LINK_TARGET.redirect_url,
1159
+ surface: undefined,
1160
+ directoryProject: undefined,
1161
+ link: () => handleToolRouterLink(params({ toolkit: Option.some('gmail'), noWait: false })),
1162
+ },
1163
+ {
1164
+ flow: 'an auth config',
1165
+ account: LEGACY_LINK_TARGET.connected_account_id,
1166
+ redirectUrl: LEGACY_LINK_TARGET.redirect_url,
1167
+ surface: devSurface(),
1168
+ directoryProject: directory(Option.some('user_directory')),
1169
+ link: () =>
1170
+ handleLegacyAuthConfigLink(params({ authConfig: Option.some('ac_gmail'), noWait: false })),
1171
+ },
1172
+ ])(
1173
+ '[Given] --no-browser and $flow [Then] stderr has the URL before the wait, stdout the JSON after',
1174
+ ({ account: accountId, redirectUrl, surface, directoryProject, link }) =>
1175
+ Effect.gen(function* () {
1176
+ const terminal = redirectedTerminalUI();
1177
+ let stderrWhileWaiting: string | undefined;
1178
+ const { layer } = linkHandlerTest({
1179
+ ...(surface ? { surface } : {}),
1180
+ ...(directoryProject ? { directoryProject } : {}),
1181
+ accounts: [
1182
+ account({ id: accountId, user_id: 'poll-source', alias: null, word_id: null }),
1183
+ ],
1184
+ retrieveFailsFirst: 2,
1185
+ onRetrieve: () => {
1186
+ stderrWhileWaiting ??= terminal.stderr();
1187
+ },
1188
+ terminal: terminal.layer,
1189
+ });
1190
+
1191
+ yield* runWithAdvancingClock(link().pipe(Effect.provide(layer)));
1192
+
1193
+ expect(stderrWhileWaiting).toBe(
1194
+ `${formatNonInteractiveAuthorizationInstructions(redirectUrl)}\n`
1195
+ );
1196
+ const stdout = terminal.stdout();
1197
+ expect(JSON.parse(stdout)).toMatchObject({ status: 'success', redirect_url: redirectUrl });
1198
+ expect(vi.mocked(open)).not.toHaveBeenCalled();
1199
+ })
1200
+ );
1201
+
1202
+ it.effect('[Given] --no-wait [Then] stderr stays empty: the URL is in the stdout JSON', () =>
1203
+ Effect.gen(function* () {
1204
+ const terminal = redirectedTerminalUI();
1205
+ const { layer } = linkHandlerTest({ terminal: terminal.layer });
1206
+
1207
+ yield* handleToolRouterLink(params({ toolkit: Option.some('gmail') })).pipe(
1208
+ Effect.provide(layer)
1209
+ );
1210
+
1211
+ expect(terminal.stderr()).toBe('');
1212
+ expect(JSON.parse(terminal.stdout())).toMatchObject({
1213
+ status: 'pending',
1214
+ redirect_url: TOOL_ROUTER_LINK_TARGET.redirect_url,
1215
+ });
1216
+ })
1217
+ );
1218
+});