embark-mcp
Version: 
MCP server proxy for Embark code search
137 lines (118 loc) • 3.47 kB
JavaScript
/**
 * Test script for snippet extraction functionality
 */
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { extractSnippetFromGit } from './dist/git-utils.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Colors for terminal output
const colors = {
  reset: '\x1b[0m',
  green: '\x1b[32m',
  red: '\x1b[31m',
  yellow: '\x1b[33m',
  blue: '\x1b[34m',
  gray: '\x1b[90m',
};
function log(message, color = colors.reset) {
  console.log(`${color}${message}${colors.reset}`);
}
function logSuccess(message) {
  log(`✓ ${message}`, colors.green);
}
function logError(message) {
  log(`✗ ${message}`, colors.red);
}
function logInfo(message) {
  log(`ℹ ${message}`, colors.blue);
}
/**
 * Test snippet extraction from this repository
 */
async function testSnippetExtraction() {
  log('='.repeat(60), colors.blue);
  log('Testing Snippet Extraction', colors.blue);
  log('='.repeat(60), colors.blue);
  const repoPath = __dirname;
  const testCases = [
    {
      name: 'Extract from git-utils.ts',
      relativePath: 'src/git-utils.ts',
      revision: 'HEAD',
      // Extract a reasonable portion from the middle of the file
      startOffset: 1500,
      endOffset: 1700,
      description: 'Extract 200 bytes from git-utils.ts'
    },
    {
      name: 'Extract from package.json',
      relativePath: 'package.json',
      revision: null, // Will use HEAD
      startOffset: 0,
      endOffset: 100,
      description: 'Extract first 100 bytes of package.json'
    },
    {
      name: 'Extract beginning of git-utils.ts',
      relativePath: 'src/git-utils.ts',
      revision: 'HEAD',
      startOffset: 0,
      endOffset: 150,
      description: 'Extract file header'
    }
  ];
  let allTestsPassed = true;
  for (const testCase of testCases) {
    log(`\n--- ${testCase.name} ---`, colors.yellow);
    logInfo(testCase.description);
    logInfo(`File: ${testCase.relativePath}`);
    logInfo(`Offsets: ${testCase.startOffset}-${testCase.endOffset}`);
    logInfo(`Revision: ${testCase.revision || 'HEAD'}`);
    try {
      const snippet = extractSnippetFromGit(
        repoPath,
        testCase.relativePath,
        testCase.revision,
        testCase.startOffset,
        testCase.endOffset
      );
      if (snippet === null) {
        logError('Failed to extract snippet (returned null)');
        allTestsPassed = false;
        continue;
      }
      if (snippet.length === 0) {
        logError('Extracted snippet is empty');
        allTestsPassed = false;
        continue;
      }
      logSuccess(`Successfully extracted ${snippet.length} bytes`);
      log('\nSnippet preview:', colors.gray);
      log('---', colors.gray);
      log(snippet.slice(0, 200) + (snippet.length > 200 ? '...' : ''), colors.gray);
      log('---', colors.gray);
    } catch (error) {
      logError(`Test failed with error: ${error.message}`);
      allTestsPassed = false;
    }
  }
  // Summary
  log('\n' + '='.repeat(60), colors.blue);
  if (allTestsPassed) {
    log('✓ All tests passed!', colors.green);
    log('='.repeat(60), colors.blue);
    process.exit(0);
  } else {
    log('✗ Some tests failed', colors.red);
    log('='.repeat(60), colors.blue);
    process.exit(1);
  }
}
// Run tests
testSnippetExtraction().catch((error) => {
  logError(`Fatal error: ${error.message}`);
  console.error(error);
  process.exit(1);
});