@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 161 lines 5.4 KB
Raw
1 import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2 import { serve, type ServerType } from '@hono/node-server';
3 import { Hono } from 'hono';
4 import { z } from 'zod/v3';
5 import { BadRequestError } from '@composio/client';
6 import { Composio } from '../../src/composio';
7 import { ComposioSessionConfigConflictError } from '../../src/errors/ToolRouterErrors';
8
9 // Production rejects unknown PATCH keys. Keep this boundary independent of
10 // the SDK's input schema so accidental additions fail through the HTTP client.
11 const patchSchema = z.union([
12 z.object({ experimental: z.object({ session_config_id: z.string() }).strict() }).strict(),
13 z.object({ toolkits: z.object({ enable: z.array(z.string()) }).strict() }).strict(),
14 ]);
15
16 describe('Session configs over HTTP', () => {
17 let server: ServerType;
18 let composio: Composio;
19 let patchRequests: unknown[];
20 let listQueries: Record<string, string>[];
21 let conflict: boolean;
22 const originalConfig = {
23 user_id: 'session-config-test',
24 toolkits: { enabled: ['github'] },
25 preload: { tools: [] },
26 };
27 const updatedConfig = {
28 ...originalConfig,
29 toolkits: { enabled: ['gmail'] },
30 tags: { enabled: ['readOnlyHint'] },
31 };
32
33 beforeEach(async () => {
34 patchRequests = [];
35 listQueries = [];
36 conflict = false;
37 const app = new Hono();
38 app.get('/api/v3.1/session_configs', c => {
39 listQueries.push(c.req.query());
40 return c.json({ items: [], next_cursor: null });
41 });
42 const sessionPath = '/api/v3.1/tool_router/session/trs_test';
43 app.get(sessionPath, c =>
44 c.json({
45 session_id: 'trs_test',
46 config_version: 7,
47 config: originalConfig,
48 tool_router_tools: [],
49 mcp: { type: 'http', url: new URL('/mcp', c.req.url).href },
50 })
51 );
52 app.patch(sessionPath, async c => {
53 const payload: unknown = await c.req.json();
54 patchRequests.push(payload);
55 const parsed = patchSchema.safeParse(payload);
56 if (!parsed.success || conflict) {
57 return c.json(
58 { error: { message: conflict ? 'Config changed' : 'Unrecognized PATCH field' } },
59 conflict ? 409 : 400
60 );
61 }
62 return c.json({
63 session_id: 'trs_test',
64 config_version: 8,
65 config: updatedConfig,
66 warnings: [],
67 ...('experimental' in parsed.data && {
68 experimental: {
69 source_session_config: { id: parsed.data.experimental.session_config_id },
70 },
71 }),
72 });
73 });
74 app.notFound(c => c.json({ error: { message: 'Unexpected endpoint' } }, 404));
75 const baseURL = await new Promise<string>(resolve => {
76 server = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' }, address =>
77 resolve(`http://127.0.0.1:${address.port}`)
78 );
79 });
80 composio = new Composio({
81 apiKey: 'test-key',
82 baseURL,
83 allowTracking: false,
84 allowTracing: false,
85 });
86 });
87
88 afterEach(async () => {
89 await new Promise<void>((resolve, reject) =>
90 server.close(error => (error ? reject(error) : resolve()))
91 );
92 });
93
94 it('omits undefined list parameters while preserving false', async () => {
95 await composio.sessionConfigs.list({
96 search: undefined,
97 archived: false,
98 limit: undefined,
99 cursor: undefined,
100 });
101
102 expect(listQueries).toEqual([{ archived: 'false' }]);
103 });
104
105 it('applies the documented saved-config update without extra wire fields', async () => {
106 const session = await composio.sessions.use('trs_test');
107
108 await session.update({ experimental: { sessionConfigId: 'sc_test' } });
109
110 expect(patchRequests).toEqual([{ experimental: { session_config_id: 'sc_test' } }]);
111 expect(session.config).toEqual(updatedConfig);
112 expect(session.configVersion).toBe(8);
113 expect(session.experimental.sourceSessionConfig).toEqual({ id: 'sc_test' });
114 });
115
116 it('also applies inline updates without an implicit precondition', async () => {
117 const session = await composio.sessions.use('trs_test');
118
119 await session.update({ toolkits: ['gmail'] });
120
121 expect(patchRequests).toEqual([{ toolkits: { enable: ['gmail'] } }]);
122 expect(session.config.toolkits).toEqual({ enabled: ['gmail'] });
123 });
124
125 it('preserves an explicit version check and surfaces rejection without retrying', async () => {
126 const session = await composio.sessions.use('trs_test');
127 const before = session.config;
128
129 await expect(
130 session.update({
131 experimental: { sessionConfigId: 'sc_test' },
132 expectedConfigVersion: 7,
133 })
134 ).rejects.toBeInstanceOf(BadRequestError);
135
136 expect(patchRequests).toEqual([
137 {
138 experimental: { session_config_id: 'sc_test' },
139 expected_config_version: 7,
140 },
141 ]);
142 expect(session.config).toBe(before);
143 expect(session.configVersion).toBe(7);
144 expect(session.experimental.sourceSessionConfig).toBeUndefined();
145 });
146
147 it('surfaces an apply-time conflict without retrying or refreshing local state', async () => {
148 const session = await composio.sessions.use('trs_test');
149 const before = session.config;
150 conflict = true;
151
152 await expect(
153 session.update({ experimental: { sessionConfigId: 'sc_test' } })
154 ).rejects.toBeInstanceOf(ComposioSessionConfigConflictError);
155
156 expect(patchRequests).toHaveLength(1);
157 expect(session.config).toBe(before);
158 expect(session.configVersion).toBe(7);
159 expect(session.experimental.sourceSessionConfig).toBeUndefined();
160 });
161 });