1
+/**
2
+ * Tool execution E2E fixture for @composio/core in Deno.
3
+ *
4
+ * Exercises the runtime path, not just the import surface: session
5
+ * creation over fetch, custom-tool registration, Zod validation, and
6
+ * in-process local tool execution.
7
+ *
8
+ * The core import is a relative path into the workspace's built dist
9
+ * (baked into the e2e image by the CI Build step), so this suite tests
10
+ * the local Effect v4 build rather than whatever `npm:@composio/core`
11
+ * would resolve to from the registry. Requires COMPOSIO_API_KEY in the
12
+ * environment (the e2e runner passes it into the container).
13
+ */
14
+
15
+import {
16
+ Composio,
17
+ experimental_createTool as createCustomTool,
18
+} from '../../../../../packages/core/dist/index.mjs';
19
+import { z } from 'npm:zod/v3';
20
+
21
+const apiKey = Deno.env.get('COMPOSIO_API_KEY');
22
+if (!apiKey) {
23
+ console.error('COMPOSIO_API_KEY is required');
24
+ Deno.exit(1);
25
+}
26
+
27
+// ── Define custom tools (local execute fns) ──────────────────
28
+
29
+const getUserContext = createCustomTool('GET_USER_CONTEXT', {
30
+ name: 'Get user context',
31
+ description: 'Retrieve user preferences and history',
32
+ inputParams: z.object({
33
+ category: z.string().default('all'),
34
+ }),
35
+ execute: async input => ({ preferences: { category: input.category, source: 'local' } }),
36
+});
37
+
38
+const enrichedSearch = createCustomTool('ENRICHED_SEARCH', {
39
+ name: 'Enriched search',
40
+ description: 'Search and enrich results with user context',
41
+ inputParams: z.object({
42
+ query: z.string(),
43
+ }),
44
+ execute: async (input, ctx) => ({
45
+ query: input.query,
46
+ userId: ctx.userId,
47
+ }),
48
+});
49
+
50
+const throwingTool = createCustomTool('THROWING_TOOL', {
51
+ name: 'Throwing tool',
52
+ description: 'A tool that always throws an error',
53
+ inputParams: z.object({}),
54
+ execute: async () => {
55
+ throw new Error('intentional error for testing');
56
+ },
57
+});
58
+
59
+const strictTool = createCustomTool('STRICT_TOOL', {
60
+ name: 'Strict tool',
61
+ description: 'Tool with strict numeric input',
62
+ inputParams: z.object({
63
+ count: z.number(),
64
+ }),
65
+ execute: async input => ({ doubled: input.count * 2 }),
66
+});
67
+
68
+// ── Create session and run tests ─────────────────────────────
69
+
70
+const composio = new Composio({ apiKey });
71
+
72
+async function main() {
73
+ const userId = `e2e-deno-tools-${Date.now()}`;
74
+
75
+ const session = await composio.create(userId, {
76
+ toolkits: ['weathermap'],
77
+ manageConnections: false,
78
+ experimental: {
79
+ customTools: [getUserContext, enrichedSearch, throwingTool, strictTool],
80
+ },
81
+ });
82
+ if (!session.sessionId) {
83
+ throw new Error('SESSION_CREATE failed: missing sessionId');
84
+ }
85
+ console.log('SESSION_CREATE_OK');
86
+
87
+ // ── Test 1: Single local tool execution ──
88
+ {
89
+ const result = await session.execute('GET_USER_CONTEXT', { category: 'prefs' });
90
+ if (result.data?.preferences?.category !== 'prefs') {
91
+ throw new Error(`LOCAL_EXECUTE failed: ${JSON.stringify(result)}`);
92
+ }
93
+ if (result.data?.preferences?.source !== 'local') {
94
+ throw new Error(`LOCAL_EXECUTE source mismatch: ${JSON.stringify(result)}`);
95
+ }
96
+ console.log('LOCAL_EXECUTE_OK');
97
+ }
98
+
99
+ // ── Test 2: Zod defaults applied ──
100
+ {
101
+ const result = await session.execute('GET_USER_CONTEXT', {});
102
+ if (result.data?.preferences?.category !== 'all') {
103
+ throw new Error(`ZOD_DEFAULTS failed: ${JSON.stringify(result)}`);
104
+ }
105
+ console.log('ZOD_DEFAULTS_OK');
106
+ }
107
+
108
+ // ── Test 3: Error handling — throw wrapped into { data, error } ──
109
+ {
110
+ const result = await session.execute('THROWING_TOOL', {});
111
+ if (!result.error || !result.error.includes('intentional error')) {
112
+ throw new Error(`ERROR_HANDLING failed: ${JSON.stringify(result)}`);
113
+ }
114
+ if (result.data === undefined) {
115
+ throw new Error(`ERROR_HANDLING no data field: ${JSON.stringify(result)}`);
116
+ }
117
+ console.log('ERROR_HANDLING_OK');
118
+ }
119
+
120
+ // ── Test 4: Zod validation failure — wrong type wrapped into error ──
121
+ {
122
+ const result = await session.execute('STRICT_TOOL', { count: 'not-a-number' });
123
+ if (!result.error) {
124
+ throw new Error(`ZOD_VALIDATION_FAIL expected error: ${JSON.stringify(result)}`);
125
+ }
126
+ if (!result.error.toLowerCase().includes('validation')) {
127
+ throw new Error(`ZOD_VALIDATION_FAIL expected validation error: ${result.error}`);
128
+ }
129
+ console.log('ZOD_VALIDATION_FAIL_OK');
130
+ }
131
+
132
+ // ── Test 5: Multiple local tools route correctly ──
133
+ {
134
+ const r1 = await session.execute('GET_USER_CONTEXT', { category: 'a' });
135
+ const r2 = await session.execute('ENRICHED_SEARCH', { query: 'test' });
136
+ if (r1.data?.preferences?.category !== 'a') {
137
+ throw new Error(`MULTIPLE_TOOLS r1 failed: ${JSON.stringify(r1)}`);
138
+ }
139
+ if (r2.data?.query !== 'test') {
140
+ throw new Error(`MULTIPLE_TOOLS r2 failed: ${JSON.stringify(r2)}`);
141
+ }
142
+ console.log('MULTIPLE_TOOLS_OK');
143
+ }
144
+
145
+ // ── Test 6: Session context injection (userId) ──
146
+ {
147
+ const result = await session.execute('ENRICHED_SEARCH', { query: 'context-test' });
148
+ if (result.data?.userId !== userId) {
149
+ throw new Error(`SESSION_CONTEXT userId mismatch: expected ${userId}, got ${JSON.stringify(result)}`);
150
+ }
151
+ console.log('SESSION_CONTEXT_OK');
152
+ }
153
+
154
+ // ── Test 7: Case-insensitive slug ──
155
+ {
156
+ const result = await session.execute('get_user_context', { category: 'case-test' });
157
+ if (result.data?.preferences?.category !== 'case-test') {
158
+ throw new Error(`CASE_INSENSITIVE failed: ${JSON.stringify(result)}`);
159
+ }
160
+ console.log('CASE_INSENSITIVE_OK');
161
+ }
162
+
163
+ // ── Test 8: Prefixed slug (LOCAL_) ──
164
+ {
165
+ const result = await session.execute('LOCAL_GET_USER_CONTEXT', { category: 'prefix-test' });
166
+ if (result.data?.preferences?.category !== 'prefix-test') {
167
+ throw new Error(`PREFIXED_SLUG failed: ${JSON.stringify(result)}`);
168
+ }
169
+ console.log('PREFIXED_SLUG_OK');
170
+ }
171
+
172
+ console.log('ALL_OK');
173
+}
174
+
175
+main().catch((err: unknown) => {
176
+ const message = err instanceof Error ? err.message : String(err);
177
+ console.log('ERROR:', message);
178
+ Deno.exit(1);
179
+});