@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 209 lines 7.45 KB
Raw
1 import type { ConnectedAccountListResponse as ApiConnectedAccountListResponse } from '@composio/client/resources/connected-accounts';
2 import { describe, expect, layer } from '@effect/vitest';
3 import { Console, Effect, Layer, Schema } from 'effect';
4 import {
5 decodeConnectedAccountItems,
6 decodeConnectedAccountList,
7 } from 'src/effects/decode-connected-account-list';
8 import { MockConsole } from 'test/__utils__';
9 import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
10
11 const TestLayer = Layer.mergeAll(Layer.effect(Console.Console, MockConsole.make), TerminalUITest);
12
13 // The server can send what the client's types do not describe (a newer status, a
14 // missing field, credentials under `state`); these fixtures model that payload.
15 const serverItems = (items: ReadonlyArray<unknown>) =>
16 items as ApiConnectedAccountListResponse['items'];
17 const serverResponse = (response: unknown) => response as ApiConnectedAccountListResponse;
18
19 const makeItem = (overrides?: Record<string, unknown>) => ({
20 id: 'con_decode_test',
21 alias: 'default',
22 word_id: 'castle',
23 status: 'ACTIVE',
24 status_reason: null,
25 is_disabled: false,
26 user_id: 'user_test',
27 toolkit: { slug: 'gmail' },
28 auth_config: {
29 id: 'ac_gmail_oauth',
30 auth_scheme: 'OAUTH2',
31 is_composio_managed: true,
32 is_disabled: false,
33 },
34 created_at: '2026-01-01T00:00:00Z',
35 updated_at: '2026-01-15T00:00:00Z',
36 test_request_endpoint: '',
37 ...overrides,
38 });
39
40 describe('decodeConnectedAccountItems', () => {
41 layer(TestLayer)('[Given] items with statuses this CLI build knows', it => {
42 it.effect('decodes without warning', () =>
43 Effect.gen(function* () {
44 const items = yield* decodeConnectedAccountItems(serverItems([makeItem()]));
45
46 expect(items).toStrictEqual([makeItem()]);
47 expect(yield* MockConsole.getLines()).toStrictEqual([]);
48 })
49 );
50 });
51
52 layer(TestLayer)('[Given] a status newer than this CLI build', it => {
53 it.effect('decodes, warns with only the status value, and still strips credential fields', () =>
54 Effect.gen(function* () {
55 const raw = makeItem({
56 status: 'QUANTUM_LINKED',
57 state: { access_token: 'must-not-leak' },
58 data: { refresh_token: 'must-not-leak' },
59 });
60
61 const items = yield* decodeConnectedAccountItems(serverItems([raw]));
62
63 expect(items).toStrictEqual([makeItem({ status: 'QUANTUM_LINKED' })]);
64 expect(JSON.stringify(items)).not.toContain('must-not-leak');
65
66 const output = (yield* MockConsole.getLines()).join('\n');
67 expect(output).toContain('QUANTUM_LINKED');
68 expect(output).toContain('composio upgrade');
69 expect(output).not.toContain('must-not-leak');
70 })
71 );
72 });
73
74 layer(TestLayer)('[Given] a state carrying a provider identity next to credentials', it => {
75 it.effect('keeps only the trimmed display name from state', () =>
76 Effect.gen(function* () {
77 const raw = makeItem({
78 state: {
79 authScheme: 'OAUTH2',
80 val: {
81 displayName: ' ada@example.com ',
82 access_token: 'must-not-leak',
83 refresh_token: 'must-not-leak',
84 },
85 },
86 data: { access_token: 'must-not-leak' },
87 });
88
89 const items = yield* decodeConnectedAccountItems(serverItems([raw]));
90
91 expect(items).toStrictEqual([makeItem({ display_name: 'ada@example.com' })]);
92 expect(JSON.stringify(items)).not.toContain('must-not-leak');
93 })
94 );
95
96 it.effect('keeps a display name of exactly 200 characters', () =>
97 Effect.gen(function* () {
98 const displayName = 'a'.repeat(200);
99
100 const items = yield* decodeConnectedAccountItems(
101 serverItems([makeItem({ state: { val: { displayName } } })])
102 );
103
104 expect(items).toStrictEqual([makeItem({ display_name: displayName })]);
105 })
106 );
107 });
108
109 layer(TestLayer)('[Given] no usable state.val.displayName', it => {
110 it.effect.each([
111 ['state is null', { state: null }],
112 ['state is not an object', { state: 'opaque' }],
113 ['state has no val', { state: { authScheme: 'OAUTH2' } }],
114 ['val has no displayName', { state: { val: { access_token: 'must-not-leak' } } }],
115 ['displayName is blank', { state: { val: { displayName: ' ' } } }],
116 ['displayName is not a string', { state: { val: { displayName: 42 } } }],
117 ['displayName is over 200 characters', { state: { val: { displayName: 'a'.repeat(201) } } }],
118 ['only a top-level display_name is sent', { display_name: 'ada@example.com' }],
119 ] as const)('leaves display_name out when %s', ([, overrides]) =>
120 Effect.gen(function* () {
121 const items = yield* decodeConnectedAccountItems(serverItems([makeItem(overrides)]));
122
123 expect(items).toStrictEqual([makeItem()]);
124 })
125 );
126 });
127
128 layer(TestLayer)('[Given] a malformed payload', it => {
129 it.effect('fails with SchemaError', () =>
130 Effect.gen(function* () {
131 const error = yield* Effect.flip(
132 decodeConnectedAccountItems(serverItems([{ definitely: 'not-an-account' }]))
133 );
134
135 expect(Schema.isSchemaError(error)).toBe(true);
136 })
137 );
138 });
139 });
140
141 describe('decodeConnectedAccountList', () => {
142 layer(TestLayer)('[Given] a response with a status newer than this CLI build', it => {
143 it.effect('decodes and warns', () =>
144 Effect.gen(function* () {
145 const result = yield* decodeConnectedAccountList(
146 serverResponse({
147 items: [makeItem({ status: 'QUANTUM_LINKED' })],
148 total_items: 1,
149 total_pages: 1,
150 current_page: 1,
151 next_cursor: null,
152 })
153 );
154
155 expect(result.items).toStrictEqual([makeItem({ status: 'QUANTUM_LINKED' })]);
156
157 const output = (yield* MockConsole.getLines()).join('\n');
158 expect(output).toContain('QUANTUM_LINKED');
159 expect(output).toContain('composio upgrade');
160 })
161 );
162 });
163
164 layer(TestLayer)('[Given] a response whose envelope does not match', it => {
165 it.effect('fails naming the mismatched field', () =>
166 Effect.gen(function* () {
167 const error = yield* Effect.flip(
168 decodeConnectedAccountList(
169 serverResponse({
170 items: [makeItem()],
171 total_items: '3',
172 total_pages: 1,
173 current_page: 1,
174 next_cursor: null,
175 })
176 )
177 );
178
179 expect(error._tag).toBe('effects/ConnectedAccountListShapeError');
180 expect(error.message).toContain('total_items');
181 expect(error.message).not.toContain('"3"');
182 })
183 );
184 });
185
186 layer(TestLayer)('[Given] a shape-skewed response carrying credential fields', it => {
187 it.effect('fails without echoing payload values', () =>
188 Effect.gen(function* () {
189 // `items` is not an array: the decode fails at the container level,
190 // where an unredacted formatter would print the whole actual value.
191 const error = yield* Effect.flip(
192 decodeConnectedAccountList(
193 serverResponse({
194 items: { 0: makeItem({ state: { access_token: 'must-not-leak' } }) },
195 total_items: 1,
196 total_pages: 1,
197 current_page: 1,
198 next_cursor: null,
199 })
200 )
201 );
202
203 expect(error.message).toContain('items');
204 expect(error.message).not.toContain('must-not-leak');
205 expect((yield* MockConsole.getLines()).join('\n')).not.toContain('must-not-leak');
206 })
207 );
208 });
209 });