@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 255 lines 6.79 KB
Raw
1 import { spawnSync } from 'child_process';
2 import { describe, it, expect } from 'vitest';
3
4 interface AssertPythonIsValidInput {
5 files: {
6 [filename: string]: string;
7 };
8 }
9
10 /**
11 * Asserts that the provided Python code is syntactically valid.
12 */
13 export function assertPythonIsValid({ files }: AssertPythonIsValidInput) {
14 for (const [filename, code] of Object.entries(files)) {
15 try {
16 // Use spawnSync to avoid shell quote escaping issues by passing code via stdin
17 const {
18 status: exitCode,
19 stderr,
20 stdout,
21 } = spawnSync(
22 'python3',
23 ['-c', 'import sys; compile(sys.stdin.read(), "<string>", "exec")'],
24 {
25 input: code,
26 encoding: 'utf8',
27 }
28 );
29
30 if (exitCode !== 0) {
31 throw new Error(stderr || stdout || 'Python compilation failed');
32 }
33 } catch (error) {
34 if (error instanceof Error) {
35 throw new Error(`Invalid Python code in ${filename}:\n${error.message}`);
36 }
37 throw error;
38 }
39 }
40 }
41
42 if (import.meta.vitest) {
43 describe('assertPythonIsValid', () => {
44 it('[Given] valid Python code [Then] no errors are found', () => {
45 const code = /* python */ `
46 def greet(name):
47 return f"Hello, {name}!"
48
49 result = greet("World")
50 print(result)
51 `;
52 assertPythonIsValid({ files: { 'main.py': code } });
53 });
54
55 it('[Given] valid Python code with imports [Then] no errors are found', () => {
56 const mainSource = /* python */ `
57 import math
58
59 def calculate_area(radius):
60 return math.pi * radius ** 2
61
62 area = calculate_area(5)
63 print(f"Area: {area}")
64 `;
65
66 assertPythonIsValid({
67 files: { 'main.py': mainSource },
68 });
69 });
70
71 it('[Given] valid Python code with class definition [Then] no errors are found', () => {
72 const code = /* python */ `
73 class Calculator:
74 def __init__(self):
75 self.result = 0
76
77 def add(self, x, y):
78 self.result = x + y
79 return self.result
80
81 calc = Calculator()
82 calc.add(2, 3)
83 `;
84 assertPythonIsValid({ files: { 'calculator.py': code } });
85 });
86
87 it('[Given] valid Python code with string escaping [Then] no errors are found', () => {
88 const code = /* python */ `
89 message = "Hello \\"World\\" with 'quotes'"
90 template = '''This is a multiline
91 string with "double" and 'single' quotes'''
92 formatted = f"Template: {template}"
93 print(formatted)
94 `;
95 assertPythonIsValid({ files: { 'strings.py': code } });
96 });
97
98 it('[Given] invalid Python code [Then] errors are found', () => {
99 const code = /* python */ `
100 def broken_function(
101 print("This is missing a closing parenthesis"
102 `;
103 expect(() => {
104 assertPythonIsValid({ files: { 'broken.py': code } });
105 }).toThrowError();
106 });
107
108 it('[Given] Python code with syntax error [Then] errors are found', () => {
109 const code = /* python */ `
110 def invalid_syntax():
111 if True
112 print("Missing colon")
113 `;
114 expect(() => {
115 assertPythonIsValid({ files: { 'syntax_error.py': code } });
116 }).toThrowError();
117 });
118
119 it('[Given] Python code with indentation error [Then] errors are found', () => {
120 const code = /* python */ `
121 def bad_indentation():
122 x = 1
123 y = 2 # Wrong indentation
124 return x + y
125 `;
126 expect(() => {
127 assertPythonIsValid({ files: { 'indentation_error.py': code } });
128 }).toThrowError();
129 });
130
131 it('[Given] multiple valid Python files [Then] no errors are found', () => {
132 const mainSource = /* python */ `
133 from math_utils import add, multiply
134
135 def main():
136 result = multiply(add(2, 3), 4)
137 print(f"Result: {result}")
138
139 if __name__ == "__main__":
140 main()
141 `;
142
143 const mathUtilsSource = /* python */ `
144 def add(x, y):
145 return x + y
146
147 def multiply(x, y):
148 return x * y
149 `;
150
151 assertPythonIsValid({
152 files: { 'main.py': mainSource, 'math_utils.py': mathUtilsSource },
153 });
154 });
155
156 it('[Given] valid Python code with decorators [Then] no errors are found', () => {
157 const code = /* python */ `
158 def my_decorator(func):
159 def wrapper(*args, **kwargs):
160 print(f"Calling {func.__name__}")
161 return func(*args, **kwargs)
162 return wrapper
163
164 @my_decorator
165 def greet(name):
166 return f"Hello, {name}!"
167
168 result = greet("World")
169 print(result)
170 `;
171 assertPythonIsValid({ files: { 'decorators.py': code } });
172 });
173
174 it('[Given] valid Python code with async/await [Then] no errors are found', () => {
175 const code = /* python */ `
176 import asyncio
177
178 async def fetch_data():
179 await asyncio.sleep(1)
180 return "data"
181
182 async def main():
183 result = await fetch_data()
184 print(result)
185
186 if __name__ == "__main__":
187 asyncio.run(main())
188 `;
189 assertPythonIsValid({ files: { 'async_example.py': code } });
190 });
191
192 it('[Given] valid Python code with type hints [Then] no errors are found', () => {
193 const code = /* python */ `
194 from typing import List, Optional, Dict
195
196 def process_items(items: List[str], config: Optional[Dict[str, bool]] = None) -> str:
197 if config is None:
198 config = {}
199
200 processed = [item.upper() for item in items if config.get("uppercase", True)]
201 return ", ".join(processed)
202
203 result: str = process_items(["hello", "world"])
204 print(result)
205 `;
206 assertPythonIsValid({ files: { 'type_hints.py': code } });
207 });
208
209 it('[Given] Python code with complex string literals [Then] no errors are found', () => {
210 const code = /* python */ `
211 # Test various string literal formats
212 single_quotes = 'This is a single-quoted string'
213 double_quotes = "This is a double-quoted string"
214 triple_single = '''This is a
215 multi-line string
216 with single quotes'''
217 triple_double = """This is a
218 multi-line string
219 with double quotes"""
220
221 # Raw strings
222 raw_string = r"This is a raw string with \\n and \\t"
223 f_string = f"Hello, {single_quotes}"
224
225 # Mixed quotes
226 mixed = "She said, 'Hello there!' to him"
227 escaped = "This has \\"escaped\\" quotes"
228
229 print(f_string)
230 `;
231 assertPythonIsValid({ files: { 'string_literals.py': code } });
232 });
233
234 it('[Given] invalid Python code with undefined variable [Then] errors are found', () => {
235 const code = /* python */ `
236 def test_function():
237 return undefined_variable
238 `;
239 // Note: Python syntax validation won't catch undefined variables - only syntax errors
240 // This should pass syntax validation but would fail at runtime
241 assertPythonIsValid({ files: { 'undefined_var.py': code } });
242 });
243
244 it('[Given] Python code with syntax error in function call [Then] errors are found', () => {
245 const code = /* python */ `
246 def test_function():
247 print("Hello",) # This trailing comma is valid
248 print("Hello",,) # This double comma is invalid
249 `;
250 expect(() => {
251 assertPythonIsValid({ files: { 'function_call_error.py': code } });
252 }).toThrowError();
253 });
254 });
255 }