const fetch = require('node-fetch');

const API_BASE = 'http://localhost:8081/api';

async function testUserApi() {
  try {
    console.log('Testing user API...');
    
    // 测试获取用户列表（不需要身份验证？）
    console.log('1. Testing GET /admin/users');
    const usersResponse = await fetch(`${API_BASE}/admin/users`);
    console.log('Status:', usersResponse.status);
    
    if (usersResponse.ok) {
      const users = await usersResponse.json();
      console.log('Users:', users);
      
      // 如果有用户，测试获取单个用户
      if (users && users.length > 0) {
        const userId = users[0].id;
        console.log(`\n2. Testing GET /admin/users/${userId}`);
        const userResponse = await fetch(`${API_BASE}/admin/users/${userId}`);
        console.log('Status:', userResponse.status);
        
        if (userResponse.ok) {
          const user = await userResponse.json();
          console.log('User:', user);
          console.log('\n✅ API test passed!');
        } else {
          const error = await userResponse.json();
          console.log('Error:', error);
          console.log('\n⚠️ API test failed!');
        }
      }
    } else {
      const error = await usersResponse.json().catch(() => usersResponse.text());
      console.log('Error:', error);
      console.log('\n⚠️ API test failed!');
    }
  } catch (error) {
    console.error('Test error:', error);
    console.log('\n❌ API test failed!');
  }
}

testUserApi();