1
+import { describe, expect, it } from '@effect/vitest';
2
+import { Console, Effect, Layer, Option } from 'effect';
3
+import { runProjectsList } from 'src/commands/projects/list/projects.list.handler';
4
+import {
5
+ NO_CURRENT_ORG_MESSAGE,
6
+ NO_PROJECTS_MESSAGE,
7
+} from 'src/commands/projects/list/projects.list.format';
8
+import type { OrgProject } from 'src/services/composio-account';
9
+import { ComposioClientSingleton } from 'src/services/composio-clients';
10
+import { TerminalUI } from 'src/services/terminal-ui';
11
+import { ComposioUserContext } from 'src/services/user-context';
12
+import { MockConsole } from 'test/__utils__';
13
+import type { MockAccountRequest, MockRequestScope } from 'test/__utils__/layers/accounts';
14
+import { makeShadowClient } from 'test/__utils__/layers/shadow-client';
15
+import { makeOrgProject } from 'test/__utils__/models/account';
16
+import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test';
17
+
18
+const PROJECTS: ReadonlyArray<OrgProject> = [
19
+ makeOrgProject({ id: 'project_a', name: 'Project A', orgId: 'org_current' }),
20
+ makeOrgProject({ id: 'project_b', name: 'Project B', orgId: 'org_current' }),
21
+];
22
+
23
+interface ProjectsListFixture {
24
+ readonly apiKey?: Option.Option<string>;
25
+ readonly orgId?: Option.Option<string>;
26
+ readonly projects?: ReadonlyArray<OrgProject>;
27
+}
28
+
29
+/**
30
+ * What `runProjectsList` reads: a user context, the account family of the mock client, and a
31
+ * headless terminal whose stdout data lands in `outputs` and whose `org.project.list` requests
32
+ * land in `requests`.
33
+ */
34
+const projectsListTest = (fixture: ProjectsListFixture = {}) => {
35
+ const outputs: Array<string> = [];
36
+ const requests: Array<MockAccountRequest> = [];
37
+ const apiKey = fixture.apiKey ?? Option.some('uak_projects_list');
38
+ const accountData = {
39
+ projects: fixture.projects ?? PROJECTS,
40
+ onRequest: (request: MockAccountRequest) => {
41
+ requests.push(request);
42
+ },
43
+ };
44
+
45
+ const layer = Layer.mergeAll(
46
+ Layer.effect(Console.Console, MockConsole.make),
47
+ Layer.succeed(
48
+ TerminalUI,
49
+ TerminalUI.of({
50
+ ...terminalUITestImpl,
51
+ output: data =>
52
+ Effect.sync(() => {
53
+ outputs.push(data);
54
+ }),
55
+ })
56
+ ),
57
+ Layer.succeed(
58
+ ComposioUserContext,
59
+ ComposioUserContext.of({
60
+ data: {
61
+ apiKey,
62
+ baseURL: 'https://mock.invalid',
63
+ webURL: 'https://platform.mock.invalid/',
64
+ orgId: fixture.orgId ?? Option.some('org_current'),
65
+ projectId: Option.none(),
66
+ testUserId: Option.none(),
67
+ },
68
+ isLoggedIn: () => Option.isSome(apiKey),
69
+ logout: Effect.void,
70
+ login: () => Effect.void,
71
+ update: () => Effect.void,
72
+ })
73
+ ),
74
+ Layer.succeed(
75
+ ComposioClientSingleton,
76
+ ComposioClientSingleton.of({
77
+ get: () => Effect.succeed(makeShadowClient(accountData, {})),
78
+ getFor: (scope: MockRequestScope) => Effect.succeed(makeShadowClient(accountData, scope)),
79
+ getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }),
80
+ })
81
+ )
82
+ );
83
+ return { layer, outputs, requests };
84
+};
85
+
86
+describe('runProjectsList', () => {
87
+ it.effect('[Given] no --org-id [Then] lists the projects of the current org', () => {
88
+ const { layer, outputs, requests } = projectsListTest();
89
+ return Effect.gen(function* () {
90
+ yield* runProjectsList({ orgId: Option.none(), limit: 50 });
91
+
92
+ expect(requests.map(request => request.scope.orgId)).toEqual(['org_current']);
93
+ expect(outputs.map(output => JSON.parse(output))).toEqual([
94
+ [
95
+ { id: 'project_a', name: 'Project A', is_selected_global_project: false },
96
+ { id: 'project_b', name: 'Project B', is_selected_global_project: false },
97
+ ],
98
+ ]);
99
+ }).pipe(Effect.provide(layer));
100
+ });
101
+
102
+ it.effect('[Given] --org-id [Then] it wins over the current org', () => {
103
+ const { layer, requests } = projectsListTest();
104
+ return Effect.gen(function* () {
105
+ yield* runProjectsList({ orgId: Option.some('org_explicit'), limit: 50 });
106
+
107
+ expect(requests.map(request => request.scope.orgId)).toEqual(['org_explicit']);
108
+ }).pipe(Effect.provide(layer));
109
+ });
110
+
111
+ it.effect.each([
112
+ { orgId: Option.none<string>(), current: Option.none<string>(), label: 'no org at all' },
113
+ { orgId: Option.some(''), current: Option.some('org_current'), label: 'an empty --org-id' },
114
+ { orgId: Option.none<string>(), current: Option.some(''), label: 'an empty current org' },
115
+ ])('[Given] $label [Then] warns and sends no request', ({ orgId, current }) => {
116
+ const { layer, outputs, requests } = projectsListTest({ orgId: current });
117
+ return Effect.gen(function* () {
118
+ yield* runProjectsList({ orgId, limit: 50 });
119
+
120
+ expect(requests).toEqual([]);
121
+ expect(outputs).toEqual([]);
122
+ expect(yield* MockConsole.getLines({ stream: 'stderr' })).toContain(NO_CURRENT_ORG_MESSAGE);
123
+ }).pipe(Effect.provide(layer));
124
+ });
125
+
126
+ it.effect.each([
127
+ { limit: 50, expected: 50 },
128
+ { limit: 0, expected: 1 },
129
+ { limit: -3, expected: 1 },
130
+ { limit: 1001, expected: 1000 },
131
+ ])('[Given] limit $limit [Then] requests $expected projects', ({ limit, expected }) => {
132
+ const { layer, requests } = projectsListTest();
133
+ return Effect.gen(function* () {
134
+ yield* runProjectsList({ orgId: Option.none(), limit });
135
+
136
+ expect(requests.map(request => request.params)).toEqual([{ limit: expected }]);
137
+ }).pipe(Effect.provide(layer));
138
+ });
139
+
140
+ it.effect('[Given] no projects [Then] warns and outputs nothing', () => {
141
+ const { layer, outputs } = projectsListTest({ projects: [] });
142
+ return Effect.gen(function* () {
143
+ yield* runProjectsList({ orgId: Option.none(), limit: 50 });
144
+
145
+ expect(outputs).toEqual([]);
146
+ expect(yield* MockConsole.getLines({ stream: 'stderr' })).toEqual([NO_PROJECTS_MESSAGE]);
147
+ }).pipe(Effect.provide(layer));
148
+ });
149
+
150
+ it.effect(
151
+ '[Given] no stored API key [Then] fails with NotLoggedInError before any request',
152
+ () => {
153
+ const { layer, requests } = projectsListTest({ apiKey: Option.none() });
154
+ return Effect.gen(function* () {
155
+ const error = yield* Effect.flip(runProjectsList({ orgId: Option.none(), limit: 50 }));
156
+
157
+ expect(error).toMatchObject({ _tag: 'commands/NotLoggedInError' });
158
+ expect(requests).toEqual([]);
159
+ }).pipe(Effect.provide(layer));
160
+ }
161
+ );
162
+});