/**
 * Tests for passwordService
 */

import { forgotPassword, resetPassword } from './passwordService';
import http from '@/http';

describe('passwordService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('forgotPassword', () => {
    it('shouldCallApiForgotPasswordEndpoint', async () => {
      (http.post as jest.Mock).mockResolvedValueOnce({ data: {} });

      await forgotPassword('test@example.com');

      expect(http.post).toHaveBeenCalledWith('/forgot-password/front', {
        email: 'test@example.com',
      });
    });

    it('shouldThrowErrorWhenEmailIsEmpty', async () => {
      await expect(forgotPassword('')).rejects.toThrow('Email is required');
    });
  });

  describe('resetPassword', () => {
    it('shouldCallApiResetPasswordEndpoint', async () => {
      (http.post as jest.Mock).mockResolvedValueOnce({ data: {} });

      await resetPassword('valid-token', 'NewStrongP@ss1');

      expect(http.post).toHaveBeenCalledWith('/reset-password/front', {
        token: 'valid-token',
        newPassword: 'NewStrongP@ss1',
      });
    });

    it('shouldThrowErrorWhenTokenIsEmpty', async () => {
      await expect(resetPassword('', 'NewPassword1')).rejects.toThrow('Token is required');
    });

    it('shouldThrowErrorWhenPasswordIsEmpty', async () => {
      await expect(resetPassword('token', '')).rejects.toThrow('New password is required');
    });

    it('shouldThrowErrorWhenPasswordIsTooShort', async () => {
      await expect(resetPassword('token', 'short')).rejects.toThrow(
        'Password must be at least 8 characters'
      );
    });
  });
});
