forked from codinit-dev/codinit-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage-parser.test.ts
More file actions
229 lines (196 loc) · 7.43 KB
/
Copy pathmessage-parser.test.ts
File metadata and controls
229 lines (196 loc) · 7.43 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
import { describe, it, expect } from 'vitest';
function cleanoutMarkdownSyntax(content: string) {
const codeBlockRegex = /^\s*```[\w-]*\s*\n?([\s\S]*?)\n?\s*```\s*$/;
const match = content.match(codeBlockRegex);
if (match) {
return match[1].trim();
}
const multilineCodeBlockRegex = /```[\w-]*\s*\n([\s\S]*?)```/g;
let cleaned = content.replace(multilineCodeBlockRegex, (_match, code) => code.trim());
const inlineCodeBlockRegex = /^```[\w-]*\s*\n?|```\s*$/gm;
cleaned = cleaned.replace(inlineCodeBlockRegex, '');
return cleaned.trim() !== content.trim() ? cleaned.trim() : content;
}
describe('message-parser - cleanoutMarkdownSyntax', () => {
describe('Basic markdown removal', () => {
it('should remove markdown code block with language', () => {
const input = '```javascript\nconst x = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should remove markdown code block without language', () => {
const input = '```\nconst x = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should handle code with no markdown', () => {
const input = 'const x = 1;\nconsole.log(x);';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe(input);
});
it('should preserve newlines in code', () => {
const input = '```javascript\nfunction test() {\n return true;\n}\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('function test() {\n return true;\n}');
});
});
describe('Language identifiers', () => {
it('should handle typescript language', () => {
const input = '```typescript\ninterface User { name: string; }\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('interface User { name: string; }');
});
it('should handle python language', () => {
const input = '```python\ndef hello():\n print("Hello")\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('def hello():\n print("Hello")');
});
it('should handle language with hyphens', () => {
const input = '```type-script\nconst x: number = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x: number = 1;');
});
it('should handle jsx/tsx', () => {
const input = '```tsx\nexport const Button = () => <button>Click</button>;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('export const Button = () => <button>Click</button>;');
});
});
describe('Whitespace handling', () => {
it('should trim leading whitespace', () => {
const input = ' ```javascript\nconst x = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should trim trailing whitespace', () => {
const input = '```javascript\nconst x = 1;\n``` ';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should handle extra newlines', () => {
const input = '```javascript\n\nconst x = 1;\n\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should handle no newline after opening backticks', () => {
const input = '```javascriptconst x = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('x = 1;');
});
});
describe('Complex scenarios', () => {
it('should handle multiline code', () => {
const input = `\`\`\`javascript
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('function fibonacci');
expect(result).toContain('return fibonacci');
});
it('should handle code with backticks inside', () => {
const input = '```javascript\nconst str = `template ${var}`;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const str = `template ${var}`;');
});
it('should handle nested structures', () => {
const input = `\`\`\`typescript
const obj = {
nested: {
value: [1, 2, 3]
}
};
\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('const obj = {');
expect(result).toContain('nested:');
});
it('should handle incomplete markdown (no closing backticks)', () => {
const input = '```javascript\nconst x = 1;';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
it('should handle incomplete markdown (no opening backticks)', () => {
const input = 'const x = 1;\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('const x = 1;');
});
});
describe('Multiple code blocks', () => {
it('should handle multiple code blocks in sequence', () => {
const input = `\`\`\`javascript
const a = 1;
\`\`\`
\`\`\`python
b = 2
\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('const a = 1;');
expect(result).toContain('b = 2');
});
it('should clean multiple inline code blocks', () => {
const input = 'Some text ```js\ncode1\n``` more text ```py\ncode2\n``` end';
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('code1');
expect(result).toContain('code2');
});
});
describe('Edge cases', () => {
it('should handle empty code block', () => {
const input = '```javascript\n\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('');
});
it('should handle just backticks', () => {
const input = '```\n```';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe('');
});
it('should preserve code with no markdown at all', () => {
const input = 'function test() { return 42; }';
const result = cleanoutMarkdownSyntax(input);
expect(result).toBe(input);
});
it('should handle very long code blocks', () => {
const code = 'const x = 1;\n'.repeat(100);
const input = `\`\`\`javascript\n${code}\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result.split('\n').length).toBe(100);
});
});
describe('Real-world scenarios', () => {
it('should clean AI-generated React component', () => {
const input = `\`\`\`tsx
import React from 'react';
export function Button({ children, onClick }: ButtonProps) {
return (
<button onClick={onClick}>
{children}
</button>
);
}
\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('import React');
expect(result).toContain('export function Button');
expect(result).toContain('<button onClick={onClick}>');
});
it('should clean AI-generated Python script', () => {
const input = `\`\`\`python
def calculate_fibonacci(n):
"""Calculate the nth Fibonacci number."""
if n <= 1:
return n
return calculate_fibonacci(n - 1) + calculate_fibonacci(n - 2)
if __name__ == "__main__":
print(calculate_fibonacci(10))
\`\`\``;
const result = cleanoutMarkdownSyntax(input);
expect(result).toContain('def calculate_fibonacci');
expect(result).toContain('if __name__');
expect(result).not.toContain('```');
});
});
});