@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 114 lines 3.16 KB
Raw
1 import { describe, it, expect, vi, beforeEach } from 'vitest';
2 import { z } from 'zod/v3';
3
4 const mockLogger = vi.hoisted(() => ({
5 warn: vi.fn(),
6 error: vi.fn(),
7 info: vi.fn(),
8 debug: vi.fn(),
9 }));
10
11 vi.mock('../../src/utils/logger', () => ({
12 default: mockLogger,
13 }));
14
15 vi.mock('../../src/index', () => ({
16 logger: mockLogger,
17 }));
18
19 import { transform } from '../../src/utils/transform';
20
21 describe('transform', () => {
22 beforeEach(() => {
23 vi.clearAllMocks();
24 });
25
26 it('should return validated data when schema matches', () => {
27 const schema = z.object({
28 name: z.string(),
29 age: z.number(),
30 });
31
32 const result = transform({ raw_name: 'Alice', raw_age: 30 })
33 .with(schema)
34 .using(raw => ({ name: raw.raw_name, age: raw.raw_age }));
35
36 expect(result).toEqual({ name: 'Alice', age: 30 });
37 });
38
39 it('should strip extra fields via Zod parsing on valid data', () => {
40 const schema = z.object({
41 name: z.string(),
42 });
43
44 const result = transform({ name: 'Alice', extra: 'field' })
45 .with(schema)
46 .using(raw => raw as unknown);
47
48 expect(result).toEqual({ name: 'Alice' });
49 expect(result).not.toHaveProperty('extra');
50 });
51
52 it('should return untransformed data and log a warning on validation failure', () => {
53 const schema = z.object({
54 name: z.string(),
55 age: z.number(),
56 });
57
58 const result = transform({ name: 123, age: 'not a number' })
59 .with(schema)
60 .using(raw => raw as unknown);
61
62 expect(result).toEqual({ name: 123, age: 'not a number' });
63
64 expect(mockLogger.warn).toHaveBeenCalledTimes(1);
65 expect(mockLogger.error).not.toHaveBeenCalled();
66
67 const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
68 expect(warnMessage).toContain('Transform validation failed');
69 expect(warnMessage).toContain('name');
70 expect(warnMessage).toContain('age');
71 });
72
73 it('should include label in the warning message when provided', () => {
74 const schema = z.object({ id: z.string() });
75
76 transform({ id: 42 })
77 .with(schema)
78 .using(raw => raw as unknown, { label: 'ConnectedAccount' });
79
80 expect(mockLogger.warn).toHaveBeenCalledTimes(1);
81 const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
82 expect(warnMessage).toContain('ConnectedAccount');
83 });
84
85 it('should not log anything when validation succeeds', () => {
86 const schema = z.object({ id: z.string() });
87
88 const result = transform({ id: 'abc' })
89 .with(schema)
90 .using(raw => raw as unknown);
91
92 expect(result).toEqual({ id: 'abc' });
93 expect(mockLogger.warn).not.toHaveBeenCalled();
94 expect(mockLogger.error).not.toHaveBeenCalled();
95 });
96
97 it('should handle nested schema validation failures', () => {
98 const schema = z.object({
99 user: z.object({
100 email: z.string().email(),
101 }),
102 });
103
104 const result = transform({ user: { email: 'not-an-email' } })
105 .with(schema)
106 .using(raw => raw as unknown);
107
108 expect(result).toEqual({ user: { email: 'not-an-email' } });
109 expect(mockLogger.warn).toHaveBeenCalledTimes(1);
110
111 const warnMessage = mockLogger.warn.mock.calls[0][0] as string;
112 expect(warnMessage).toContain('user.email');
113 });
114 });