1
+import { Auth, make, type ComposioClientShape } from '@composio/client/effect';
2
+import { describe, expect, it } from '@effect/vitest';
3
+import { Console, Effect, Layer, Option } from 'effect';
4
+import { HttpClient } from 'effect/http';
5
+import { runOrgsList } from 'src/commands/orgs/list/orgs.list.handler';
6
+import { NO_ORGANIZATIONS_MESSAGE } from 'src/commands/orgs/list/orgs.list.format';
7
+import type { OrganizationSummary } from 'src/services/composio-account';
8
+import { ComposioClientSingleton } from 'src/services/composio-clients';
9
+import { TerminalUI } from 'src/services/terminal-ui';
10
+import { ComposioUserContext } from 'src/services/user-context';
11
+import { MockConsole } from 'test/__utils__';
12
+import { mockAccountResources, type MockRequestScope } from 'test/__utils__/layers/accounts';
13
+import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
14
+
15
+const ORGANIZATIONS: ReadonlyArray<OrganizationSummary> = [
16
+ { id: 'org_first', name: 'First Org' },
17
+ { id: 'org_second', name: 'Second Org' },
18
+];
19
+
20
+// A resource the account mocks do not shadow reaches this transport and dies with its URL.
21
+const baseClient = make(
22
+ { auth: Auth.project('test'), baseURL: 'https://mock.invalid' },
23
+ HttpClient.make(request =>
24
+ Effect.die(new Error(`unexpected network call from the orgs list handler test: ${request.url}`))
25
+ )
26
+);
27
+
28
+interface OrgsListFixture {
29
+ readonly apiKey?: Option.Option<string>;
30
+ readonly orgId?: Option.Option<string>;
31
+ readonly organizations?: ReadonlyArray<OrganizationSummary>;
32
+}
33
+
34
+/**
35
+ * What `runOrgsList` reads: a user context, the account family of the mock client, and a
36
+ * headless terminal whose stdout data lands in `outputs` and whose `org.list` limits land in
37
+ * `limits`.
38
+ */
39
+const orgsListTest = (fixture: OrgsListFixture = {}) => {
40
+ const outputs: Array<string> = [];
41
+ const limits: Array<unknown> = [];
42
+ const apiKey = fixture.apiKey ?? Option.some('uak_orgs_list');
43
+ const accountData = {
44
+ organizations: fixture.organizations ?? ORGANIZATIONS,
45
+ onRequest: (request: { readonly params?: unknown }) => {
46
+ limits.push((request.params as { readonly limit?: number } | undefined)?.limit);
47
+ },
48
+ };
49
+ const clientFor = (scope: MockRequestScope): ComposioClientShape =>
50
+ // The shadows answer only the fields the handler reads, as in `test-layer.ts`.
51
+ ({
52
+ ...baseClient,
53
+ ...mockAccountResources(accountData, scope),
54
+ }) as unknown as ComposioClientShape;
55
+
56
+ const layer = Layer.mergeAll(
57
+ Layer.effect(Console.Console, MockConsole.make),
58
+ Layer.succeed(
59
+ TerminalUI,
60
+ TerminalUI.of({
61
+ ...terminalUITestImpl,
62
+ output: data =>
63
+ Effect.sync(() => {
64
+ outputs.push(data);
65
+ }),
66
+ })
67
+ ),
68
+ Layer.succeed(
69
+ ComposioUserContext,
70
+ ComposioUserContext.of({
71
+ data: {
72
+ apiKey,
73
+ baseURL: 'https://mock.invalid',
74
+ webURL: 'https://platform.mock.invalid/',
75
+ orgId: fixture.orgId ?? Option.none(),
76
+ projectId: Option.none(),
77
+ testUserId: Option.none(),
78
+ },
79
+ isLoggedIn: () => Option.isSome(apiKey),
80
+ logout: Effect.void,
81
+ login: () => Effect.void,
82
+ update: () => Effect.void,
83
+ })
84
+ ),
85
+ Layer.succeed(
86
+ ComposioClientSingleton,
87
+ ComposioClientSingleton.of({
88
+ get: () => Effect.succeed(clientFor({})),
89
+ getFor: (scope: MockRequestScope) => Effect.succeed(clientFor(scope)),
90
+ getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }),
91
+ })
92
+ )
93
+ );
94
+ return { layer, outputs, limits };
95
+};
96
+
97
+describe('runOrgsList', () => {
98
+ it.effect('[Given] a current org [Then] flags only that org as selected', () => {
99
+ const { layer, outputs } = orgsListTest({ orgId: Option.some('org_second') });
100
+ return Effect.gen(function* () {
101
+ yield* runOrgsList({ limit: 50 });
102
+
103
+ expect(outputs.map(output => JSON.parse(output))).toEqual([
104
+ [
105
+ { id: 'org_first', name: 'First Org', is_selected_global_org: false },
106
+ { id: 'org_second', name: 'Second Org', is_selected_global_org: true },
107
+ ],
108
+ ]);
109
+ }).pipe(Effect.provide(layer));
110
+ });
111
+
112
+ it.effect('[Given] no current org [Then] flags no org as selected', () => {
113
+ const { layer, outputs } = orgsListTest();
114
+ return Effect.gen(function* () {
115
+ yield* runOrgsList({ limit: 50 });
116
+
117
+ expect(JSON.parse(outputs[0] ?? '[]')).toEqual([
118
+ { id: 'org_first', name: 'First Org', is_selected_global_org: false },
119
+ { id: 'org_second', name: 'Second Org', is_selected_global_org: false },
120
+ ]);
121
+ }).pipe(Effect.provide(layer));
122
+ });
123
+
124
+ it.effect.each([
125
+ { limit: 50, expected: 50 },
126
+ { limit: 0, expected: 1 },
127
+ { limit: -3, expected: 1 },
128
+ { limit: 1001, expected: 1000 },
129
+ ])('[Given] limit $limit [Then] requests $expected organizations', ({ limit, expected }) => {
130
+ const { layer, limits } = orgsListTest();
131
+ return Effect.gen(function* () {
132
+ yield* runOrgsList({ limit });
133
+
134
+ expect(limits).toEqual([expected]);
135
+ }).pipe(Effect.provide(layer));
136
+ });
137
+
138
+ it.effect('[Given] no organizations [Then] warns and outputs nothing', () => {
139
+ const { layer, outputs } = orgsListTest({ organizations: [] });
140
+ return Effect.gen(function* () {
141
+ yield* runOrgsList({ limit: 50 });
142
+
143
+ expect(outputs).toEqual([]);
144
+ expect(yield* MockConsole.getLines({ stream: 'stderr' })).toEqual([NO_ORGANIZATIONS_MESSAGE]);
145
+ }).pipe(Effect.provide(layer));
146
+ });
147
+
148
+ it.effect(
149
+ '[Given] no stored API key [Then] fails with NotLoggedInError before any request',
150
+ () => {
151
+ const { layer, limits } = orgsListTest({ apiKey: Option.none() });
152
+ return Effect.gen(function* () {
153
+ const error = yield* Effect.flip(runOrgsList({ limit: 50 }));
154
+
155
+ expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
156
+ expect(limits).toEqual([]);
157
+ }).pipe(Effect.provide(layer));
158
+ }
159
+ );
160
+});