parser.test.ts
5.64 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
import { describe, it, expect } from 'vitest';
import { extractJSON, extractCode, extractFiles } from '../../src/utils/parser';
describe('parser utilities', () => {
describe('extractJSON', () => {
it('should extract JSON from markdown code blocks with json language tag', () => {
const input = '```json\n{"key": "value"}\n```';
const result = extractJSON(input);
expect(result).toBe('{"key": "value"}');
});
it('should extract JSON from generic markdown code blocks', () => {
const input = '```\n{"key": "value"}\n```';
const result = extractJSON(input);
expect(result).toBe('{"key": "value"}');
});
it('should return trimmed content when no code blocks found', () => {
const input = ' {"key": "value"} ';
const result = extractJSON(input);
expect(result).toBe('{"key": "value"}');
});
it('should handle multiline JSON in code blocks', () => {
const input = '```json\n{\n "key": "value",\n "nested": {\n "data": 123\n }\n}\n```';
const result = extractJSON(input);
expect(result).toBe('{\n "key": "value",\n "nested": {\n "data": 123\n }\n}');
});
it('should prefer json tagged blocks over generic blocks', () => {
const input = '```json\n{"json": "content"}\n```\n```\n{"generic": "content"}\n```';
const result = extractJSON(input);
expect(result).toBe('{"json": "content"}');
});
});
describe('extractCode', () => {
it('should extract code from tsx code blocks', () => {
const input = '```tsx\nconst App = () => <div>Hello</div>;\n```';
const result = extractCode(input);
expect(result).toBe('const App = () => <div>Hello</div>;');
});
it('should extract code from typescript code blocks', () => {
const input = '```typescript\nconst x: number = 42;\n```';
const result = extractCode(input);
expect(result).toBe('const x: number = 42;');
});
it('should extract code from javascript code blocks', () => {
const input = '```javascript\nconst x = 42;\n```';
const result = extractCode(input);
expect(result).toBe('const x = 42;');
});
it('should extract code from css code blocks', () => {
const input = '```css\n.class { color: red; }\n```';
const result = extractCode(input);
expect(result).toBe('.class { color: red; }');
});
it('should extract code from generic code blocks', () => {
const input = '```\nconst x = 42;\n```';
const result = extractCode(input);
expect(result).toBe('const x = 42;');
});
it('should clean up loose fence markers when no proper block found', () => {
const input = '```tsx\nconst x = 42;';
const result = extractCode(input);
expect(result).toBe('const x = 42;');
});
it('should handle code without any markdown fences', () => {
const input = 'const x = 42;';
const result = extractCode(input);
expect(result).toBe('const x = 42;');
});
it('should handle multiline code blocks', () => {
const input = '```tsx\nconst App = () => {\n return <div>Hello</div>;\n};\n```';
const result = extractCode(input);
expect(result).toBe('const App = () => {\n return <div>Hello</div>;\n};');
});
});
describe('extractFiles', () => {
it('should extract multiple files from content with file headers', () => {
const content = `
## App.tsx
\`\`\`tsx
const App = () => <div>Hello</div>;
\`\`\`
## styles.css
\`\`\`css
.app { color: red; }
\`\`\`
`.trim();
const result = extractFiles(content);
expect(result).toHaveLength(2);
expect(result[0]).toEqual({
filename: 'App.tsx',
content: 'const App = () => <div>Hello</div>;',
});
expect(result[1]).toEqual({
filename: 'styles.css',
content: '.app { color: red; }',
});
});
it('should handle files without language tags in code blocks', () => {
const content = `
## config.json
\`\`\`
{"key": "value"}
\`\`\`
`.trim();
const result = extractFiles(content);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
filename: 'config.json',
content: '{"key": "value"}',
});
});
it('should return empty array when no files found', () => {
const content = 'Just some text without file markers';
const result = extractFiles(content);
expect(result).toHaveLength(0);
});
it('should handle whitespace variations', () => {
const content = `
## App.tsx
\`\`\`tsx
const App = () => <div>Hello</div>;
\`\`\`
`.trim();
const result = extractFiles(content);
expect(result).toHaveLength(1);
expect(result[0]?.filename).toBe('App.tsx');
});
it('should handle multiline code content', () => {
const content = `
## Component.tsx
\`\`\`tsx
const Component = () => {
const [state, setState] = useState(0);
return <div>{state}</div>;
};
\`\`\`
`.trim();
const result = extractFiles(content);
expect(result).toHaveLength(1);
expect(result[0]?.content).toContain('useState');
});
});
});