|
| 1 | +/** |
| 2 | + * Integration test for DyadMarkdownParser to verify terminal command handling |
| 3 | + */ |
| 4 | + |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +// Test content that simulates what would come from AI |
| 9 | +const testContent = ` |
| 10 | +Let me help you check the current directory structure. |
| 11 | +
|
| 12 | +<run_terminal_cmd>ls -la</run_terminal_cmd> |
| 13 | +
|
| 14 | +I can see the files now. Let me also check if there are any backend processes running. |
| 15 | +
|
| 16 | +<dyad-run-backend-terminal-cmd>ps aux | grep node</dyad-run-backend-terminal-cmd> |
| 17 | +
|
| 18 | +And let me check the frontend build status. |
| 19 | +
|
| 20 | +<dyad-run-frontend-terminal-cmd>npm run build-status</dyad-run-frontend-terminal-cmd> |
| 21 | +
|
| 22 | +All commands executed successfully. The directory structure looks good. |
| 23 | +`; |
| 24 | + |
| 25 | +// Simulate the parseCustomTags function logic |
| 26 | +function testParseCustomTags(content) { |
| 27 | + console.log('🧪 Testing DyadMarkdownParser parseCustomTags function...\n'); |
| 28 | + |
| 29 | + const customTagNames = [ |
| 30 | + "dyad-write", |
| 31 | + "dyad-rename", |
| 32 | + "dyad-delete", |
| 33 | + "dyad-add-dependency", |
| 34 | + "dyad-execute-sql", |
| 35 | + "dyad-add-integration", |
| 36 | + "dyad-output", |
| 37 | + "dyad-problem-report", |
| 38 | + "dyad-chat-summary", |
| 39 | + "dyad-edit", |
| 40 | + "dyad-codebase-context", |
| 41 | + "think", |
| 42 | + "dyad-command", |
| 43 | + "dyad-run-backend-terminal-cmd", |
| 44 | + "dyad-run-frontend-terminal-cmd", |
| 45 | + "run_terminal_cmd", |
| 46 | + ]; |
| 47 | + |
| 48 | + const tagPattern = new RegExp( |
| 49 | + `<(${customTagNames.join("|")})\\s*([^>]*)>([\\s\\S]*?)<\\/\\1>`, |
| 50 | + "gs", |
| 51 | + ); |
| 52 | + |
| 53 | + const contentPieces = []; |
| 54 | + let lastIndex = 0; |
| 55 | + let match; |
| 56 | + |
| 57 | + console.log('📝 Parsing content with terminal command tags...\n'); |
| 58 | + |
| 59 | + while ((match = tagPattern.exec(content)) !== null) { |
| 60 | + const [fullMatch, tag, attributesStr, tagContent] = match; |
| 61 | + const startIndex = match.index; |
| 62 | + |
| 63 | + // Add markdown content before this tag |
| 64 | + if (startIndex > lastIndex) { |
| 65 | + const markdownContent = content.substring(lastIndex, startIndex); |
| 66 | + if (markdownContent.trim()) { |
| 67 | + contentPieces.push({ |
| 68 | + type: "markdown", |
| 69 | + content: markdownContent.trim() |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Parse attributes |
| 75 | + const attributes = {}; |
| 76 | + const attrPattern = /(\w+)="([^"]*)"/g; |
| 77 | + let attrMatch; |
| 78 | + while ((attrMatch = attrPattern.exec(attributesStr)) !== null) { |
| 79 | + attributes[attrMatch[1]] = attrMatch[2]; |
| 80 | + } |
| 81 | + |
| 82 | + // Add the tag info |
| 83 | + contentPieces.push({ |
| 84 | + type: "custom-tag", |
| 85 | + tagInfo: { |
| 86 | + tag, |
| 87 | + attributes, |
| 88 | + content: tagContent.trim(), |
| 89 | + fullMatch, |
| 90 | + inProgress: false, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + lastIndex = startIndex + fullMatch.length; |
| 95 | + } |
| 96 | + |
| 97 | + // Add remaining markdown content |
| 98 | + if (lastIndex < content.length) { |
| 99 | + const remainingContent = content.substring(lastIndex); |
| 100 | + if (remainingContent.trim()) { |
| 101 | + contentPieces.push({ |
| 102 | + type: "markdown", |
| 103 | + content: remainingContent.trim() |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return contentPieces; |
| 109 | +} |
| 110 | + |
| 111 | +// Simulate the renderCustomTag function logic |
| 112 | +function testRenderCustomTag(tagInfo) { |
| 113 | + const { tag, attributes, content, inProgress } = tagInfo; |
| 114 | + |
| 115 | + switch (tag) { |
| 116 | + case "think": |
| 117 | + return `<DyadThink>${content}</DyadThink>`; |
| 118 | + case "dyad-write": |
| 119 | + return `<DyadWrite path="${attributes.path}">${content}</DyadWrite>`; |
| 120 | + case "dyad-rename": |
| 121 | + return `<DyadRename from="${attributes.from}" to="${attributes.to}">${content}</DyadRename>`; |
| 122 | + case "dyad-delete": |
| 123 | + return `<DyadDelete path="${attributes.path}">${content}</DyadDelete>`; |
| 124 | + case "dyad-add-dependency": |
| 125 | + return `<DyadAddDependency packages="${attributes.packages}">${content}</DyadAddDependency>`; |
| 126 | + case "dyad-execute-sql": |
| 127 | + return `<DyadExecuteSql description="${attributes.description}">${content}</DyadExecuteSql>`; |
| 128 | + case "dyad-add-integration": |
| 129 | + return `<DyadAddIntegration provider="${attributes.provider}">${content}</DyadAddIntegration>`; |
| 130 | + case "dyad-edit": |
| 131 | + return `<DyadEdit path="${attributes.path}">${content}</DyadEdit>`; |
| 132 | + case "dyad-codebase-context": |
| 133 | + return `<DyadCodebaseContext files="${attributes.files}">${content}</DyadCodebaseContext>`; |
| 134 | + case "dyad-output": |
| 135 | + return `<DyadOutput type="${attributes.type}">${content}</DyadOutput>`; |
| 136 | + case "dyad-problem-report": |
| 137 | + return `<DyadProblemSummary summary="${attributes.summary}">${content}</DyadProblemSummary>`; |
| 138 | + case "dyad-chat-summary": |
| 139 | + return null; |
| 140 | + case "dyad-command": |
| 141 | + return null; |
| 142 | + case "run_terminal_cmd": |
| 143 | + return null; // Should return null (not render) |
| 144 | + case "dyad-run-backend-terminal-cmd": |
| 145 | + return null; // Should return null (not render) |
| 146 | + case "dyad-run-frontend-terminal-cmd": |
| 147 | + return null; // Should return null (not render) |
| 148 | + default: |
| 149 | + return null; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Main test function |
| 154 | +function runIntegrationTest() { |
| 155 | + console.log('🚀 DyadMarkdownParser Integration Test\n'); |
| 156 | + console.log('=' .repeat(60)); |
| 157 | + |
| 158 | + // Test 1: Parse the content |
| 159 | + console.log('📋 TEST 1: Content Parsing'); |
| 160 | + console.log('-'.repeat(40)); |
| 161 | + const contentPieces = testParseCustomTags(testContent); |
| 162 | + |
| 163 | + console.log(`✅ Content parsed into ${contentPieces.length} pieces:`); |
| 164 | + contentPieces.forEach((piece, index) => { |
| 165 | + if (piece.type === "markdown") { |
| 166 | + console.log(` ${index + 1}. [MARKDOWN]: "${piece.content.substring(0, 60)}${piece.content.length > 60 ? '...' : ''}"`); |
| 167 | + } else { |
| 168 | + console.log(` ${index + 1}. [${piece.tagInfo.tag.toUpperCase()} TAG]: ${piece.tagInfo.content}`); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + // Test 2: Render simulation |
| 173 | + console.log('\n📋 TEST 2: Render Simulation'); |
| 174 | + console.log('-'.repeat(40)); |
| 175 | + |
| 176 | + const terminalTags = contentPieces.filter(p => p.type === "custom-tag"); |
| 177 | + const terminalCommandTags = terminalTags.filter(p => |
| 178 | + p.tagInfo.tag === "run_terminal_cmd" || |
| 179 | + p.tagInfo.tag === "dyad-run-backend-terminal-cmd" || |
| 180 | + p.tagInfo.tag === "dyad-run-frontend-terminal-cmd" |
| 181 | + ); |
| 182 | + |
| 183 | + console.log(`✅ Found ${terminalCommandTags.length} terminal command tags to render:`); |
| 184 | + |
| 185 | + let allReturnNull = true; |
| 186 | + terminalCommandTags.forEach((piece, index) => { |
| 187 | + const rendered = testRenderCustomTag(piece.tagInfo); |
| 188 | + console.log(` ${index + 1}. <${piece.tagInfo.tag}> renders as: ${rendered}`); |
| 189 | + |
| 190 | + if (rendered !== null) { |
| 191 | + allReturnNull = false; |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + // Test 3: Verify markdown content is preserved |
| 196 | + console.log('\n📋 TEST 3: Markdown Content Preservation'); |
| 197 | + console.log('-'.repeat(40)); |
| 198 | + |
| 199 | + const markdownPieces = contentPieces.filter(p => p.type === "markdown"); |
| 200 | + console.log(`✅ Found ${markdownPieces.length} markdown sections:`); |
| 201 | + |
| 202 | + markdownPieces.forEach((piece, index) => { |
| 203 | + console.log(` ${index + 1}. "${piece.content.substring(0, 80)}${piece.content.length > 80 ? '...' : ''}"`); |
| 204 | + }); |
| 205 | + |
| 206 | + // Summary |
| 207 | + console.log('\n' + '=' .repeat(60)); |
| 208 | + console.log('📊 INTEGRATION TEST SUMMARY'); |
| 209 | + console.log('-'.repeat(40)); |
| 210 | + |
| 211 | + const parsingSuccess = contentPieces.length === 7; // Should have 4 markdown + 3 terminal tags |
| 212 | + const renderingSuccess = allReturnNull; // All terminal tags should return null |
| 213 | + const markdownPreserved = markdownPieces.length === 4; // Should have 4 markdown sections |
| 214 | + |
| 215 | + console.log(`✅ Content Parsing: ${parsingSuccess ? 'PASS' : 'FAIL'}`); |
| 216 | + console.log(`✅ Terminal Tag Rendering: ${renderingSuccess ? 'PASS' : 'FAIL'}`); |
| 217 | + console.log(`✅ Markdown Preservation: ${markdownPreserved ? 'PASS' : 'FAIL'}`); |
| 218 | + |
| 219 | + const allTestsPassed = parsingSuccess && renderingSuccess && markdownPreserved; |
| 220 | + |
| 221 | + console.log(`\nOverall Result: ${allTestsPassed ? '✅ ALL TESTS PASSED' : '❌ SOME TESTS FAILED'}`); |
| 222 | + |
| 223 | + if (allTestsPassed) { |
| 224 | + console.log('\n🎉 SUCCESS: DyadMarkdownParser correctly handles terminal commands!'); |
| 225 | + console.log(' - Terminal command tags are parsed correctly'); |
| 226 | + console.log(' - Tags return null (not rendered in UI)'); |
| 227 | + console.log(' - Markdown content is preserved'); |
| 228 | + console.log(' - Commands will execute silently in terminals'); |
| 229 | + } else { |
| 230 | + console.log('\n⚠️ WARNING: Some tests failed. Check the implementation.'); |
| 231 | + } |
| 232 | + |
| 233 | + console.log('=' .repeat(60)); |
| 234 | + |
| 235 | + return allTestsPassed; |
| 236 | +} |
| 237 | + |
| 238 | +// Run the integration test |
| 239 | +if (require.main === module) { |
| 240 | + runIntegrationTest(); |
| 241 | +} |
| 242 | + |
| 243 | +module.exports = { |
| 244 | + testParseCustomTags, |
| 245 | + testRenderCustomTag, |
| 246 | + runIntegrationTest |
| 247 | +}; |
0 commit comments