import { matchPatterns } from '../mechanic/core/patterns';
import * as fs from 'fs';
import * as path from 'path';

const componentPath = path.join(__dirname, 'test-artifacts/order-form-buggy.tsx');
const componentCode = fs.readFileSync(componentPath, 'utf-8');

console.log('\n=== Testing Pattern Matching ===\n');

// Test 1: async/loading issue
const request1 = {
  artifactId: 'test',
  errorType: 'behavior' as const,
  userDescription: 'When I click Submit Order, nothing happens. The button just stays there with no loading or feedback.',
  componentCode
};

const matches1 = matchPatterns(request1);
console.log('Test 1: "Submit does nothing"');
console.log('Matches:', matches1.length);
matches1.forEach(m => console.log(`  - ${m.patternName}: ${m.confidence}% (${m.matchedSignals.length} signals)`));

// Test 2: state mutation
const request2 = {
  artifactId: 'test',
  errorType: 'behavior' as const,
  userDescription: 'When I click Add Item, the item does not appear in the list. The UI is not updating.',
  componentCode
};

const matches2 = matchPatterns(request2);
console.log('\nTest 2: "Add Item doesn\'t show"');
console.log('Matches:', matches2.length);
matches2.forEach(m => console.log(`  - ${m.patternName}: ${m.confidence}% (${m.matchedSignals.length} signals)`));

// Test 3: undefined access
const request3 = {
  artifactId: 'test',
  errorType: 'runtime' as const,
  errorMessage: "TypeError: Cannot read property 'price' of undefined",
  userDescription: 'The page crashes when I try to see the total.',
  componentCode,
  browserConsole: [
    "TypeError: Cannot read property 'price' of undefined",
    "    at getTotal (OrderForm.tsx:52:32)"
  ]
};

const matches3 = matchPatterns(request3);
console.log('\nTest 3: "Cannot read property price"');
console.log('Matches:', matches3.length);
matches3.forEach(m => console.log(`  - ${m.patternName}: ${m.confidence}% (${m.matchedSignals.length} signals)`));
