import { describe, it, expect, beforeEach } from 'vitest';
import { testClient } from 'hono/testing';
import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
import { authRoutes } from '../../src/routes';

// 设置集成测试钩子
setupIntegrationDatabaseHooksWithEntities([])

describe('租户认证API集成测试', () => {
  let client: ReturnType<typeof testClient<typeof authRoutes>>;

  beforeEach(async () => {
    // 创建测试客户端
    client = testClient(authRoutes);
  });

  describe('POST /login - 登录接口', () => {
    it('应该使用正确的账号密码成功登录', async () => {
      const response = await client.login.$post({
        json: {
          username: 'superadmin',
          password: 'admin123'
        }
      });

      expect(response.status).toBe(200);
      const data = await response.json();
      expect(data.token).toBeDefined();
      expect(data.userId).toBe(1);
      expect(data.username).toBe('superadmin');
      expect(data.message).toBe('登录成功');
    });

    it('应该拒绝错误的用户名', async () => {
      const response = await client.login.$post({
        json: {
          username: 'wronguser',
          password: 'admin123'
        }
      });

      expect(response.status).toBe(401);
      const data = await response.json();
      expect(data.message).toBe('用户名或密码错误');
    });

    it('应该拒绝错误的密码', async () => {
      const response = await client.login.$post({
        json: {
          username: 'superadmin',
          password: 'wrongpassword'
        }
      });

      expect(response.status).toBe(401);
      const data = await response.json();
      expect(data.message).toBe('用户名或密码错误');
    });

    it('应该拒绝空的用户名', async () => {
      const response = await client.login.$post({
        json: {
          username: '',
          password: 'admin123'
        }
      });

      expect(response.status).toBe(400);
    });

    it('应该拒绝空的密码', async () => {
      const response = await client.login.$post({
        json: {
          username: 'superadmin',
          password: ''
        }
      });

      expect(response.status).toBe(400);
    });
  });
});