forked from codinit-dev/codinit-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-validator.test.ts
More file actions
247 lines (221 loc) · 6.74 KB
/
Copy pathcode-validator.test.ts
File metadata and controls
247 lines (221 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { describe, it, expect } from 'vitest';
import { validateCode } from '~/lib/runtime/code-validator';
describe('code-validator', () => {
describe('validateCode - General', () => {
it('should detect placeholder comments', () => {
const code = `
function test() {
// rest of code here
return true;
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors).toContain('Code contains placeholder comment "// rest of code here"');
});
it('should detect ellipsis placeholders', () => {
const code = `
function test() {
const x = 1;
/* ... */
return x;
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors).toContain('Code contains ellipsis placeholders');
});
it('should warn about markdown code blocks', () => {
const code = '```javascript\nconst x = 1;\n```';
const result = validateCode('test.js', code);
expect(result.warnings).toContain('File contains markdown code block syntax (```)');
});
it('should pass valid code without placeholders', () => {
const code = `
function add(a, b) {
return a + b;
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(true);
expect(result.errors).toHaveLength(0);
});
});
describe('validateCode - JavaScript/TypeScript', () => {
it('should detect mismatched braces', () => {
const code = `
function test() {
if (true) {
console.log('test');
// missing closing brace
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('Mismatched braces'))).toBe(true);
});
it('should detect mismatched parentheses', () => {
const code = 'function test(a, b {\n return a + b;\n}';
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('parentheses'))).toBe(true);
});
it('should detect mismatched brackets', () => {
const code = 'const arr = [1, 2, 3;';
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('brackets'))).toBe(true);
});
it('should pass valid JavaScript', () => {
const code = `
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
const result = fibonacci(10);
console.log(result);
`;
const result = validateCode('fib.js', code);
expect(result.isValid).toBe(true);
});
it('should pass valid TypeScript', () => {
const code = `
interface User {
name: string;
age: number;
}
function greet(user: User): string {
return \`Hello, \${user.name}!\`;
}
`;
const result = validateCode('greet.ts', code);
expect(result.isValid).toBe(true);
});
it('should detect mixed Python/JS syntax', () => {
const code = 'import from http';
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('mixing JavaScript and Python'))).toBe(true);
});
it('should ignore comments when counting braces', () => {
const code = `
function test() {
// This is a comment with {braces}
/* Another comment { with } braces */
return true;
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(true);
});
});
describe('validateCode - Python', () => {
it('should warn about tabs instead of spaces', () => {
const code = 'def test():\n\treturn True';
const result = validateCode('test.py', code);
expect(result.warnings.some((w) => w.includes('tabs instead of spaces'))).toBe(true);
});
it('should detect JavaScript keywords in Python', () => {
const code = `
const x = 5
let y = 10
var z = 15
`;
const result = validateCode('test.py', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('JavaScript variable declarations'))).toBe(true);
});
it('should warn about function keyword', () => {
const code = 'function greet(name):\n return f"Hello {name}"';
const result = validateCode('test.py', code);
expect(result.warnings.some((w) => w.includes('function'))).toBe(true);
});
it('should pass valid Python code', () => {
const code = `
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
result = factorial(5)
print(result)
`;
const result = validateCode('factorial.py', code);
expect(result.isValid).toBe(true);
});
it('should handle Python classes', () => {
const code = `
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, {self.name}!"
`;
const result = validateCode('user.py', code);
expect(result.isValid).toBe(true);
});
});
describe('validateCode - JSON', () => {
it('should detect invalid JSON', () => {
const code = '{ "name": "test", invalid }';
const result = validateCode('config.json', code);
expect(result.isValid).toBe(false);
expect(result.errors.some((e) => e.includes('Invalid JSON'))).toBe(true);
});
it('should pass valid JSON', () => {
const code = `{
"name": "test-project",
"version": "1.0.0",
"dependencies": {
"react": "^18.0.0"
}
}`;
const result = validateCode('package.json', code);
expect(result.isValid).toBe(true);
});
it('should detect trailing commas', () => {
const code = '{ "name": "test", }';
const result = validateCode('config.json', code);
expect(result.isValid).toBe(false);
});
});
describe('validateCode - Complex scenarios', () => {
it('should handle nested structures', () => {
const code = `
const obj = {
nested: {
deep: {
value: [1, 2, [3, 4]]
}
}
};
`;
const result = validateCode('nested.js', code);
expect(result.isValid).toBe(true);
});
it('should handle JSX syntax', () => {
const code = `
export function Button({ children, onClick }) {
return (
<button onClick={onClick}>
{children}
</button>
);
}
`;
const result = validateCode('Button.jsx', code);
expect(result.isValid).toBe(true);
});
it('should detect multiple errors', () => {
const code = `
function test( {
const x = [1, 2, 3;
// rest of code here
}
`;
const result = validateCode('test.js', code);
expect(result.isValid).toBe(false);
expect(result.errors.length).toBeGreaterThan(1);
});
});
});