1
import { describe, expect, it } from 'vitest';
2
+import { it as effectIt } from '@effect/vitest';
3
+import { Effect } from 'effect';
4
+import type { Composio } from '@composio/client';
5
import {
6
formatConnectedAccountChoices,
7
groupCachedConnectedAccountsByToolkit,
8
+ listConnectedAccountsForToolkit,
9
resolveConnectedAccountSelection,
10
resolveDefaultConnectedAccountsByToolkit,
11
} from 'src/services/connected-account-selection';
98
).toEqual(['default / castle (con_default)']);
99
});
100
});
101
+
102
+type ListParams = {
103
+ readonly toolkit_slugs?: ReadonlyArray<string>;
104
+ readonly limit?: number;
105
+};
106
+
107
+// A stand-in for the SDK client: one page of accounts, plus a log of every
108
+// `connectedAccounts.list` call so a test can see which path ran.
109
+const makeListClient = (
110
+ items: ReadonlyArray<ConnectedAccountItem>,
111
+ page?: { readonly total_items?: number; readonly next_cursor?: string | null }
112
+) => {
113
+ const calls: ListParams[] = [];
114
+ const client = {
115
+ connectedAccounts: {
116
+ list: async (params: ListParams) => {
117
+ calls.push(params);
118
+ const filtered = params.toolkit_slugs
119
+ ? items.filter(item =>
120
+ params.toolkit_slugs!.some(
121
+ slug => slug.trim().toLowerCase() === item.toolkit.slug.trim().toLowerCase()
122
+ )
123
+ )
124
+ : items;
125
+ return {
126
+ items: filtered.slice(0, params.limit ?? 30),
127
+ total_items: page?.total_items ?? filtered.length,
128
+ total_pages: 1,
129
+ current_page: 1,
130
+ next_cursor: page?.next_cursor ?? null,
131
+ };
132
+ },
133
+ },
134
+ } as unknown as Composio;
135
+ return { client, calls };
136
+};
137
+
138
+describe('listConnectedAccountsForToolkit', () => {
139
+ effectIt.effect('matches toolkit slugs the way the grouping helpers do, on both paths', () =>
140
+ Effect.gen(function* () {
141
+ const items = [
142
+ makeAccount({ id: 'con_github', toolkit: { slug: 'GitHub ' } }),
143
+ makeAccount({ id: 'con_gmail', toolkit: { slug: 'gmail' } }),
144
+ ];
145
+
146
+ const derived = makeListClient(items);
147
+ const fromSharedList = yield* listConnectedAccountsForToolkit({
148
+ client: derived.client,
149
+ userId: 'default',
150
+ toolkitSlug: ' github',
151
+ });
152
+ expect(fromSharedList.map(item => item.id)).toEqual(['con_github']);
153
+ expect(derived.calls).toHaveLength(1);
154
+
155
+ const fallback = makeListClient(items, { next_cursor: 'more' });
156
+ const fromServer = yield* listConnectedAccountsForToolkit({
157
+ client: fallback.client,
158
+ userId: 'default',
159
+ toolkitSlug: ' github',
160
+ });
161
+ expect(fromServer.map(item => item.id)).toEqual(['con_github']);
162
+ expect(fallback.calls[1]?.toolkit_slugs).toEqual(['github']);
163
+ })
164
+ );
165
+});