UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

211 lines (202 loc) โ€ข 7.67 kB
import { FileWriteTool } from '../services/tools/FileWriteTool.js'; import { FileService } from '../services/fileService.js'; import fs from 'fs/promises'; import path from 'path'; async function testFileWriteTool() { console.log('๐Ÿงช Testing FileWriteTool...\n'); const testDir = path.join(process.cwd(), 'test-file-write'); const fileWriteTool = new FileWriteTool(process.cwd()); const fileService = new FileService(process.cwd()); try { // Setup test environment await fs.mkdir(testDir, { recursive: true }); console.log('โœ… Created test directory:', testDir); // Test 1: File Creation console.log('\n๐Ÿ“ Test 1: File Creation'); const createResult = await fileWriteTool.execute({ operation_type: 'create', file_path: 'test-file-write/new-file.md', content: `# New Test File This is a newly created file for testing. ## Features - File creation - Content writing - Markdown support ## Code Example \`\`\`javascript console.log("Hello from new file!"); \`\`\` ` }); if (createResult.success) { console.log('โœ… File creation successful!'); console.log('๐Ÿ“„ Result:', createResult.data); console.log('๐Ÿ’ฌ Message:', createResult.message); } else { console.log('โŒ File creation failed:', createResult.error); } // Test 2: Try to create the same file again (should fail) console.log('\n๐Ÿšซ Test 2: Duplicate File Creation (should fail)'); const duplicateResult = await fileWriteTool.execute({ operation_type: 'create', file_path: 'test-file-write/new-file.md', content: 'This should fail' }); if (!duplicateResult.success) { console.log('โœ… Duplicate creation correctly prevented!'); console.log('โŒ Error message:', duplicateResult.error); } else { console.log('โŒ Duplicate creation should have failed!'); } // Test 3: Full File Rewrite console.log('\n๐Ÿ”„ Test 3: Full File Rewrite'); const rewriteResult = await fileWriteTool.execute({ operation_type: 'rewrite', file_path: 'test-file-write/new-file.md', content: `# Rewritten Test File This file has been completely rewritten. ## New Content - Complete replacement - Backup creation - New structure ## Updated Code \`\`\`python print("Hello from rewritten file!") \`\`\` ## Additional Section This is new content that wasn't in the original file. `, create_backup: true }); if (rewriteResult.success) { console.log('โœ… File rewrite successful!'); console.log('๐Ÿ“„ Result:', rewriteResult.data); console.log('๐Ÿ’ฌ Message:', rewriteResult.message); } else { console.log('โŒ File rewrite failed:', rewriteResult.error); } // Test 4: Partial Write - Replace specific lines console.log('\nโœ‚๏ธ Test 4: Partial Write - Replace Lines 3-5'); const partialResult = await fileWriteTool.execute({ operation_type: 'partial', file_path: 'test-file-write/new-file.md', content: `This content replaces lines 3-5. ## Partially Updated Section - This replaces the original content - Lines 3-5 have been updated`, start_line: 3, end_line: 5, create_backup: true }); if (partialResult.success) { console.log('โœ… Partial write successful!'); console.log('๐Ÿ“„ Result:', partialResult.data); console.log('๐Ÿ’ฌ Message:', partialResult.message); } else { console.log('โŒ Partial write failed:', partialResult.error); } // Test 5: Partial Write - Insert at specific line console.log('\nโž• Test 5: Partial Write - Insert at Line 8'); const insertResult = await fileWriteTool.execute({ operation_type: 'partial', file_path: 'test-file-write/new-file.md', content: ` ## Inserted Section This content was inserted at line 8. - New bullet point - Another new point `, start_line: 8, end_line: 8, create_backup: false }); if (insertResult.success) { console.log('โœ… Partial insert successful!'); console.log('๐Ÿ“„ Result:', insertResult.data); console.log('๐Ÿ’ฌ Message:', insertResult.message); } else { console.log('โŒ Partial insert failed:', insertResult.error); } // Test 6: Read the final file to verify all operations console.log('\n๐Ÿ“– Test 6: Reading Final File Content'); try { const finalContent = await fileService.readFile('test-file-write/new-file.md'); console.log('โœ… Final file content:'); console.log('โ”€'.repeat(50)); console.log(finalContent); console.log('โ”€'.repeat(50)); } catch (error) { console.log('โŒ Failed to read final file:', error); } // Test 7: Error Handling - Invalid operation console.log('\n๐Ÿšซ Test 7: Error Handling - Invalid Operation'); const invalidResult = await fileWriteTool.execute({ operation_type: 'invalid_operation', file_path: 'test-file-write/test.md', content: 'test' }); if (!invalidResult.success) { console.log('โœ… Invalid operation correctly rejected!'); console.log('โŒ Error message:', invalidResult.error); } else { console.log('โŒ Invalid operation should have failed!'); } // Test 8: Error Handling - Unsupported file type console.log('\n๐Ÿšซ Test 8: Error Handling - Unsupported File Type'); const unsupportedResult = await fileWriteTool.execute({ operation_type: 'create', file_path: 'test-file-write/test.exe', content: 'binary content' }); if (!unsupportedResult.success) { console.log('โœ… Unsupported file type correctly rejected!'); console.log('โŒ Error message:', unsupportedResult.error); } else { console.log('โŒ Unsupported file type should have failed!'); } // List backup files created console.log('\n๐Ÿ’พ Backup Files Created:'); try { const files = await fs.readdir(testDir); const backupFiles = files.filter(file => file.includes('.backup.')); if (backupFiles.length > 0) { backupFiles.forEach(file => { console.log(`๐Ÿ“ ${file}`); }); } else { console.log('No backup files found'); } } catch (error) { console.log('Error listing backup files:', error); } console.log('\n๐ŸŽ‰ All FileWriteTool tests completed!'); } catch (error) { console.error('โŒ Test failed:', error); } finally { // Cleanup try { await fs.rm(testDir, { recursive: true, force: true }); console.log('\n๐Ÿงน Cleaned up test files'); } catch (cleanupError) { console.error('Failed to cleanup:', cleanupError); } } } // Run the test if this file is executed directly if (import.meta.url === `file://${process.argv[1]}`) { testFileWriteTool().catch(console.error); } export { testFileWriteTool };