#!/usr/bin/env node
import fs from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
import os from "os";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class TafaTests {
constructor() {
this.testDir = path.join(os.tmpdir(), 'tafa-mcp-tests');
this.testResults = [];
}
async runTests() {
console.log('π§ͺ Running Tafa MCP Basic Tests...\n');
// Setup test environment
await this.setupTestEnv();
// Run tests
await this.testFileOperations();
await this.testDirectoryOperations();
await this.testSearchOperations();
await this.testSecurity();
// Cleanup
await this.cleanupTestEnv();
// Report results
this.reportResults();
}
async setupTestEnv() {
console.log('π§ Setting up test environment...');
await fs.ensureDir(this.testDir);
// Create test files
await fs.writeFile(path.join(this.testDir, 'test1.txt'), 'Hello World\nThis is a test file');
await fs.writeFile(path.join(this.testDir, 'test2.js'), 'console.log("Hello from JS");');
await fs.writeFile(path.join(this.testDir, 'README.md'), '# Test README\nThis is a test readme');
// Create test subdirectory
await fs.ensureDir(path.join(this.testDir, 'subdir'));
await fs.writeFile(path.join(this.testDir, 'subdir', 'nested.txt'), 'Nested file content');
console.log('β
Test environment ready\n');
}
async testFileOperations() {
console.log('π Testing File Operations...');
// Test file reading
await this.runTest('File Reading', async () => {
const content = await fs.readFile(path.join(this.testDir, 'test1.txt'), 'utf8');
return content.includes('Hello World');
});
// Test file writing
await this.runTest('File Writing', async () => {
const testFile = path.join(this.testDir, 'write-test.txt');
await fs.writeFile(testFile, 'Test content');
const content = await fs.readFile(testFile, 'utf8');
return content === 'Test content';
});
// Test file copying
await this.runTest('File Copying', async () => {
const source = path.join(this.testDir, 'test1.txt');
const dest = path.join(this.testDir, 'copy-test.txt');
await fs.copy(source, dest);
return await fs.pathExists(dest);
});
console.log('');
}
async testDirectoryOperations() {
console.log('π Testing Directory Operations...');
// Test directory creation
await this.runTest('Directory Creation', async () => {
const testDir = path.join(this.testDir, 'new-dir');
await fs.ensureDir(testDir);
return await fs.pathExists(testDir);
});
// Test directory listing
await this.runTest('Directory Listing', async () => {
const entries = await fs.readdir(this.testDir);
return entries.length > 0;
});
console.log('');
}
async testSearchOperations() {
console.log('π Testing Search Operations...');
// Test file search by pattern
await this.runTest('File Pattern Search', async () => {
const files = await fs.readdir(this.testDir);
const jsFiles = files.filter(file => file.endsWith('.js'));
return jsFiles.length > 0;
});
// Test content search
await this.runTest('Content Search', async () => {
const content = await fs.readFile(path.join(this.testDir, 'test1.txt'), 'utf8');
return content.includes('Hello');
});
console.log('');
}
async testSecurity() {
console.log('π Testing Security Features...');
// Test path validation
await this.runTest('Path Validation', async () => {
const testPath = path.join(this.testDir, 'test1.txt');
const resolved = path.resolve(testPath);
return resolved.startsWith(this.testDir);
});
// Test file permissions
await this.runTest('File Permissions', async () => {
const testFile = path.join(this.testDir, 'test1.txt');
try {
await fs.access(testFile, fs.constants.R_OK);
return true;
} catch {
return false;
}
});
console.log('');
}
async runTest(name, testFn) {
try {
const result = await testFn();
if (result) {
console.log(`β
${name}`);
this.testResults.push({ name, status: 'PASS' });
} else {
console.log(`β ${name}`);
this.testResults.push({ name, status: 'FAIL' });
}
} catch (error) {
console.log(`β ${name} - Error: ${error.message}`);
this.testResults.push({ name, status: 'ERROR', error: error.message });
}
}
async cleanupTestEnv() {
console.log('π§Ή Cleaning up test environment...');
await fs.remove(this.testDir);
console.log('β
Cleanup complete\n');
}
reportResults() {
console.log('π Test Results Summary:');
console.log('========================');
const passed = this.testResults.filter(r => r.status === 'PASS').length;
const failed = this.testResults.filter(r => r.status === 'FAIL').length;
const errors = this.testResults.filter(r => r.status === 'ERROR').length;
console.log(`β
Passed: ${passed}`);
console.log(`β Failed: ${failed}`);
console.log(`π₯ Errors: ${errors}`);
console.log(`π Total: ${this.testResults.length}`);
if (failed > 0 || errors > 0) {
console.log('\nβ Some tests failed. Check the output above for details.');
process.exit(1);
} else {
console.log('\nπ All tests passed!');
}
}
}
// Run tests if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
const tests = new TafaTests();
tests.runTests().catch(console.error);
}