|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + formatObjectToMarkdownBlock, |
| 4 | + escapeBackslashes, |
| 5 | + formatPlaceholder, |
| 6 | + customAstToString, |
| 7 | + processContextCommand |
| 8 | +} from '../../lib/utils/markdown' |
| 9 | +import { remark } from 'remark' |
| 10 | +import remarkStringify from 'remark-stringify' |
| 11 | + |
| 12 | +describe('escapeBackslashes', () => { |
| 13 | + it('should escape backslashes in Windows paths', () => { |
| 14 | + const windowsPath = 'C:\\Users\\test\\Documents\\project\\file.js' |
| 15 | + const expected = 'C:\\\\Users\\\\test\\\\Documents\\\\project\\\\file.js' |
| 16 | + expect(escapeBackslashes(windowsPath)).toBe(expected) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should handle unix paths properly', () => { |
| 20 | + const unixPath = '/home/user/projects/file.js' |
| 21 | + expect(escapeBackslashes(unixPath)).toBe(unixPath) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should handle empty strings', () => { |
| 25 | + expect(escapeBackslashes('')).toBe('') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should handle null or undefined', () => { |
| 29 | + expect(escapeBackslashes(null as any)).toBe('') |
| 30 | + expect(escapeBackslashes(undefined as any)).toBe('') |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('formatPlaceholder', () => { |
| 35 | + it('should format a file placeholder with Windows path', () => { |
| 36 | + const objStr = JSON.stringify({ filepath: 'C:\\Users\\test\\file.js' }) |
| 37 | + const expected = `[[file:${JSON.stringify({ filepath: 'C:\\\\Users\\\\test\\\\file.js' })}]]` |
| 38 | + expect(formatPlaceholder('file', objStr)).toBe(expected) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should format a symbol placeholder with Unix path', () => { |
| 42 | + const objStr = JSON.stringify({ filepath: '/home/user/file.js', range: { start: 1, end: 5 } }) |
| 43 | + expect(formatPlaceholder('symbol', objStr)).toBe(`[[symbol:${objStr}]]`) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should handle empty object string', () => { |
| 47 | + expect(formatPlaceholder('file', '')).toBe('') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should handle null or undefined', () => { |
| 51 | + expect(formatPlaceholder('file', null as any)).toBe('') |
| 52 | + expect(formatPlaceholder('symbol', undefined as any)).toBe('') |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('formatObjectToMarkdownBlock', () => { |
| 57 | + it('should format object with JavaScript content', () => { |
| 58 | + const obj = { filepath: '/path/to/file.js' } |
| 59 | + const jsContent = `function test() { |
| 60 | + console.log("Hello World"); |
| 61 | + return true; |
| 62 | +}` |
| 63 | + const result = formatObjectToMarkdownBlock('file', obj, jsContent) |
| 64 | + expect(result).toContain('```context label=file') |
| 65 | + expect(result).toContain(JSON.stringify(obj)) |
| 66 | + expect(result).toContain(jsContent) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should format object with Rust content', () => { |
| 70 | + const obj = { filepath: 'C:\\path\\to\\file.rs' } |
| 71 | + const rustContent = `fn main() { |
| 72 | + println!("Hello, world!"); |
| 73 | + let x = 5; |
| 74 | + let y = 10; |
| 75 | + println!("x + y = {}", x + y); |
| 76 | +}` |
| 77 | + const result = formatObjectToMarkdownBlock('file', obj, rustContent) |
| 78 | + expect(result).toContain('```context label=file') |
| 79 | + expect(result).toContain(JSON.stringify(obj)) |
| 80 | + expect(result).toContain(rustContent) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should handle content with markdown code blocks', () => { |
| 84 | + const obj = { filepath: '/path/to/README.md' } |
| 85 | + const markdownContent = `# Title |
| 86 | +Some text here |
| 87 | +\`\`\`js |
| 88 | +const x = 5; |
| 89 | +\`\`\` |
| 90 | +More text` |
| 91 | + const result = formatObjectToMarkdownBlock('file', obj, markdownContent) |
| 92 | + expect(result).toContain('```context label=file') |
| 93 | + // Verify backticks are properly handled |
| 94 | + expect(result).not.toContain('```js') |
| 95 | + expect(result).toContain('` ` `js') |
| 96 | + }) |
| 97 | + |
| 98 | + it('should handle content ending with backtick', () => { |
| 99 | + const obj = { filepath: '/path/to/file.md' } |
| 100 | + const content = 'This is some text with a backtick at the end: `' |
| 101 | + const result = formatObjectToMarkdownBlock('file', obj, content) |
| 102 | + // Should append a space after the backtick |
| 103 | + expect(result.includes('backtick at the end: ` \n```')).toBeTruthy() |
| 104 | + }) |
| 105 | + |
| 106 | + it('should handle complex nested code blocks', () => { |
| 107 | + const obj = { filepath: '/path/to/doc.md' } |
| 108 | + const complexContent = `# Documentation |
| 109 | +\`\`\`html |
| 110 | +<div> |
| 111 | + <pre> |
| 112 | + \`\`\`typescript |
| 113 | + function test() { |
| 114 | + return true; |
| 115 | + } |
| 116 | + \`\`\` |
| 117 | + </pre> |
| 118 | +</div> |
| 119 | +\`\`\` |
| 120 | +` |
| 121 | + const result = formatObjectToMarkdownBlock('file', obj, complexContent) |
| 122 | + // Check that nested code blocks are handled properly |
| 123 | + expect(result).toContain('` ` `html') |
| 124 | + expect(result).toContain('` ` `typescript') |
| 125 | + }) |
| 126 | + |
| 127 | + it('should handle Windows paths in object properties', () => { |
| 128 | + const obj = { filepath: 'C:\\Users\\test\\file.txt' } |
| 129 | + const content = 'Simple text content' |
| 130 | + const result = formatObjectToMarkdownBlock('file', obj, content) |
| 131 | + expect(result).toContain(JSON.stringify(obj)) |
| 132 | + expect(result).toContain(content) |
| 133 | + }) |
| 134 | + |
| 135 | + it('should handle error in object serialization', () => { |
| 136 | + // Create a circular reference that will cause JSON.stringify to fail |
| 137 | + const circularObj: any = { name: 'test' } |
| 138 | + circularObj.self = circularObj |
| 139 | + |
| 140 | + const content = 'Some content' |
| 141 | + const result = formatObjectToMarkdownBlock('file', circularObj, content) |
| 142 | + expect(result).toBe('\n*Error formatting file*\n') |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +describe('customAstToString', () => { |
| 147 | + it('should properly stringify a simple markdown AST', () => { |
| 148 | + // 使用标准的remark处理器解析文本 |
| 149 | + const markdownText = '# Title\n\nThis is a paragraph.\n\n* List item 1\n* List item 2' |
| 150 | + const ast = remark().parse(markdownText) |
| 151 | + |
| 152 | + const result = customAstToString(ast) |
| 153 | + |
| 154 | + // 验证最基本的结构被保留 |
| 155 | + expect(result).toContain('# Title') |
| 156 | + expect(result).toContain('This is a paragraph') |
| 157 | + expect(result).toContain('* List item 1') |
| 158 | + expect(result).toContain('* List item 2') |
| 159 | + }) |
| 160 | + |
| 161 | + it('should preserve code blocks in the AST', () => { |
| 162 | + // 使用标准的remark处理器解析文本 |
| 163 | + const markdownText = '```js\nconst x = 5;\n```' |
| 164 | + const ast = remark().parse(markdownText) |
| 165 | + |
| 166 | + const result = customAstToString(ast) |
| 167 | + |
| 168 | + expect(result).toContain('```js') |
| 169 | + expect(result).toContain('const x = 5;') |
| 170 | + expect(result).toContain('```') |
| 171 | + }) |
| 172 | + |
| 173 | + it('should maintain processContextCommand functionality', () => { |
| 174 | + const input = '```context label=file object={"filepath":"/path/to/file.js"}\nconst x = 5;\n```' |
| 175 | + const result = processContextCommand(input) |
| 176 | + |
| 177 | + expect(result).toContain('[[file:') |
| 178 | + expect(result).toContain('{"filepath":"/path/to/file.js"}') |
| 179 | + }) |
| 180 | +}) |
0 commit comments