import axios from 'axios';

const API_BASE_URL = 'http://localhost:3000/api';

const testPasswordValidation = async () => {
  console.log('🔒 测试密码验证修复...\n');

  try {
    // 创建测试用户
    const userData = {
      name: '密码测试用户',
      phone: '13800138002',
      password: 'correctpassword'
    };
    
    console.log('1. 创建测试用户...');
    const createResponse = await axios.post(`${API_BASE_URL}/users`, userData);
    console.log('✅ 用户创建成功');

    // 测试正确密码
    console.log('\n2. 测试正确密码验证...');
    const correctPasswordResponse = await axios.post(`${API_BASE_URL}/users/validate`, {
      name: userData.name,
      phone: userData.phone,
      password: userData.password
    });
    console.log('✅ 正确密码验证通过');

    // 测试错误密码
    console.log('\n3. 测试错误密码验证...');
    try {
      await axios.post(`${API_BASE_URL}/users/validate`, {
        name: userData.name,
        phone: userData.phone,
        password: 'wrongpassword'
      });
      console.log('❌ 错误：错误密码竟然通过了验证！');
    } catch (error) {
      if (error.response && error.response.status === 400) {
        console.log('✅ 错误密码正确被拒绝');
      } else {
        console.log('❌ 意外的错误:', error.message);
      }
    }

    console.log('\n🎉 密码验证修复测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.response?.data?.message || error.message);
  }
};

// 执行测试
testPasswordValidation();