1
+import { AuthenticationError, InternalServerError } from '@composio/client/effect';
2
import { assert, describe, expect, layer } from '@effect/vitest';
3
import { isUsageFailure } from 'src/commands/errors';
4
import { ConfigProvider, Effect } from 'effect';
5
import { cli, TestLive, MockConsole } from 'test/__utils__';
6
+import { exitCodeOf } from 'test/__utils__/exit-code';
7
import { makeSessionInfo } from 'test/__utils__/models/account';
8
+import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
9
10
describe('CLI: composio whoami', () => {
11
layer(TestLive())('without a login', it => {
28
env: { COMPOSIO_USER_API_KEY: 'api_key_from_test_config_provider' },
29
});
30
28
- layer(TestLive({ baseConfigProvider: testConfigProvider }))('with config override', it => {
31
+ const readableSession = makeSessionInfo({
32
+ orgId: 'org_123',
33
+ orgName: 'Acme Org',
34
+ orgMemberId: 'om_123',
35
+ email: 'person@example.com',
36
+ name: 'Test Person',
37
+ });
38
+
39
+ layer(
40
+ TestLive({
41
+ baseConfigProvider: testConfigProvider,
42
+ accountData: { sessionInfo: readableSession },
43
+ })
44
+ )('with config override', it => {
45
it.effect('[Given] `COMPOSIO_USER_API_KEY` [Then] prints global user context JSON', () =>
46
Effect.gen(function* () {
47
const args = ['whoami'];
51
const output = lines.join('\n');
52
expect(output).not.toContain(`api_key_from_test_config_provider`);
53
expect(output).not.toContain(`global_user_api_key`);
38
- expect(output).toContain(`"email":null`);
39
- expect(output).toContain(`"current_org_name":null`);
54
+ expect(output).toContain(`"email":"person@example.com"`);
55
expect(output).not.toContain(`"test_user_id"`);
56
})
57
);
58
});
59
45
- layer(TestLive({ fixture: 'user-config-example' }))('with fixture', it => {
60
+ layer(
61
+ TestLive({ fixture: 'user-config-example', accountData: { sessionInfo: readableSession } })
62
+ )('with fixture', it => {
63
it.effect('[Given] user_data.json in fixture [Then] prints global user context JSON', () =>
64
Effect.gen(function* () {
65
const args = ['whoami'];
69
const output = lines.join('\n');
70
expect(output).not.toContain(`api_key_from_test_fixture`);
71
expect(output).not.toContain(`global_user_api_key`);
72
+ expect(output).toContain(`"email":"person@example.com"`);
73
+ expect(output).not.toContain(`"test_user_id"`);
74
+ })
75
+ );
76
+ });
77
+
78
+ // A 401 that is not the backend's `UserApiKey_Unauthorized`: the CLI cannot tell which
79
+ // credential it is about, so it names the login as the likely way out.
80
+ const rejectedKey = new AuthenticationError({
81
+ message: '401 Invalid API key',
82
+ method: 'GET',
83
+ url: 'https://mock.invalid/api/v3.1/auth/session/info',
84
+ status: 401,
85
+ requestId: undefined,
86
+ details: { message: 'Invalid API key', code: 401, slug: 'Auth_InvalidApiKey', status: 401 },
87
+ body: undefined,
88
+ });
89
+
90
+ layer(
91
+ TestLive({
92
+ fixture: 'user-config-example',
93
+ accountData: {
94
+ sessionInfo: () => {
95
+ throw rejectedKey;
96
+ },
97
+ },
98
+ })
99
+ )('with a stored key the backend rejects', it => {
100
+ it.effect('[Then] exits 1 with nothing on stdout and names `composio login` on stderr', () =>
101
+ Effect.gen(function* () {
102
+ const terminal = redirectedTerminalUI();
103
+ const exit = yield* Effect.exit(cli(['whoami']).pipe(Effect.provide(terminal.layer)));
104
+
105
+ expect(exitCodeOf(exit)).toBe(1);
106
+ expect(terminal.stdout()).toBe('');
107
+ expect(terminal.stderr()).toBe(
108
+ 'Invalid API key\nIf your login expired, log in again, then retry:\n> composio logout && composio login\n'
109
+ );
110
+ })
111
+ );
112
+ });
113
+
114
+ // A backend that cannot answer: the command cannot vouch for the login, so it fails.
115
+ const serverError = new InternalServerError({
116
+ message: '500 Internal Server Error',
117
+ method: 'GET',
118
+ url: 'https://mock.invalid/api/v3.1/auth/session/info',
119
+ status: 500,
120
+ requestId: undefined,
121
+ details: { message: 'Internal Server Error', code: 500, slug: 'Internal', status: 500 },
122
+ body: undefined,
123
+ });
124
+
125
+ layer(
126
+ TestLive({
127
+ fixture: 'user-config-example',
128
+ accountData: {
129
+ sessionInfo: () => {
130
+ throw serverError;
131
+ },
132
+ },
133
+ })
134
+ )('when the backend fails to answer', it => {
135
+ it.effect('[Then] exits 1 with nothing on stdout and the failure on stderr', () =>
136
+ Effect.gen(function* () {
137
+ const terminal = redirectedTerminalUI();
138
+ const exit = yield* Effect.exit(cli(['whoami']).pipe(Effect.provide(terminal.layer)));
139
+
140
+ expect(exitCodeOf(exit)).toBe(1);
141
+ expect(terminal.stdout()).toBe('');
142
+ expect(terminal.stderr()).toContain('Internal Server Error');
143
+ })
144
+ );
145
+ });
146
+
147
+ // A session the CLI cannot fully read (org-level credentials have no project): the key
148
+ // works, so the fields print as unknown.
149
+ layer(
150
+ TestLive({
151
+ fixture: 'user-config-example',
152
+ accountData: { sessionInfo: { ...readableSession, project: null } },
153
+ })
154
+ )('with a session the CLI cannot fully read', it => {
155
+ it.effect('[Then] exits 0 and prints the fields as unknown', () =>
156
+ Effect.gen(function* () {
157
+ yield* cli(['whoami']);
158
+
159
+ const output = (yield* MockConsole.getLines()).join('\n');
160
expect(output).toContain(`"email":null`);
161
expect(output).toContain(`"current_org_name":null`);
57
- expect(output).not.toContain(`"test_user_id"`);
162
})
163
);
164
});