/**
 * Test script for apiforge-analyzer (CommonJS)
 */

const express = require('express')
const { analyze } = require('./dist/cjs/index.js')

console.log('🧪 Testing apiforge-analyzer...\n')

const app = express()
app.use(express.json())

// Add routes
app.get('/api/users', (req, res) => res.json({ users: [] }))
app.get('/api/users/:id', (req, res) => res.json({ id: req.params.id }))
app.post('/api/users', (req, res) => res.json({ created: true }))
app.put('/api/users/:id', (req, res) => res.json({ updated: req.params.id }))
app.delete('/api/users/:id', (req, res) => res.json({ deleted: req.params.id }))
app.get('/api/posts', (req, res) => res.json({ posts: [] }))

analyze(app, {
  projectName: 'Test API',
  framework: 'express',
  saveJson: true,
  jsonPath: './test-output.json',
  upload: false
})
  .then((routes) => {
    console.log(`✅ Analysis complete!`)
    console.log(`📊 Found ${routes.length} routes\n`)
    
    if (routes.length > 0) {
      console.log('Routes:')
      routes.forEach(route => {
        console.log(`  ${route.method.padEnd(6)} ${route.path}`)
      })
      console.log('\n💾 Saved to test-output.json')
      console.log('\n✅ TEST PASSED!')
    } else {
      console.log('⚠️  No routes found - this might be expected if Express router not initialized')
      console.log('Try using the package in a real Express app!')
    }
  })
  .catch(err => {
    console.error('\n❌ Test failed:', err.message)
    process.exit(1)
  })
