/**
 * Test suite for messageUtils
 */
import { getMessageObject } from '../messageUtils';

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

    it('should create a message object with all parameters', () => {
      const result = getMessageObject('success', 'Operation completed', false);

      expect(result).toHaveProperty('id');
      expect(result).toHaveProperty('text', 'Operation completed');
      expect(result).toHaveProperty('type', 'success');
      expect(result).toHaveProperty('hideProgressBar', false);
    });

    it('should create a message object with type and message only', () => {
      const result = getMessageObject('error', 'Something went wrong');

      expect(result.type).toBe('error');
      expect(result.text).toBe('Something went wrong');
      expect(result.hideProgressBar).toBe(false);
    });

    it('should create a message object with hideProgressBar set to true', () => {
      const result = getMessageObject('info', 'Loading', true);

      expect(result.type).toBe('info');
      expect(result.text).toBe('Loading');
      expect(result.hideProgressBar).toBe(true);
    });

    it('should generate a numeric id string based on Date.now()', () => {
      const mockTime = 1612267539410;
      jest.spyOn(Date, 'now').mockReturnValue(mockTime);

      const result = getMessageObject('success', 'Test');

      expect(result.id).toBe(`${mockTime}`);
      expect(/^\d+$/.test(result.id)).toBe(true);

      Date.now.mockRestore();
    });

    it('should generate different ids for consecutive calls', () => {
      jest.spyOn(Date, 'now')
        .mockReturnValueOnce(1000)
        .mockReturnValueOnce(2000);

      const result1 = getMessageObject('success', 'First');
      const result2 = getMessageObject('success', 'Second');

      expect(result1.id).not.toBe(result2.id);
      expect(result1.id).toBe('1000');
      expect(result2.id).toBe('2000');

      Date.now.mockRestore();
    });

    it('should handle empty message string', () => {
      const result = getMessageObject('warning', '');

      expect(result.text).toBe('');
      expect(result.type).toBe('warning');
    });

    it('should support different message types', () => {
      const types = ['success', 'error', 'warning', 'info', 'default'];

      types.forEach((type) => {
        const result = getMessageObject(type, 'Test message');
        expect(result.type).toBe(type);
      });
    });

    it('should handle long message strings', () => {
      const longMessage = 'A'.repeat(1000);
      const result = getMessageObject('info', longMessage);

      expect(result.text).toBe(longMessage);
      expect(result.text.length).toBe(1000);
    });

    it('should handle special characters in message', () => {
      const specialMessage = 'Error: "Failed" & <Retry>';
      const result = getMessageObject('error', specialMessage);

      expect(result.text).toBe(specialMessage);
    });
  });
});
