import validateEndpoint from './endpoint-validator';
import { INVALID_ENDPOINT } from "../constants/message-constants.js";
import { showError } from "../helpers/common/message-helper.js";


jest.mock('../helpers/common/message-helper.js', () => ({
    showError: jest.fn(),
}));

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

    it('should return valid endpoints for valid HTTP and HTTPS URLs', async () => {
        const input = 'http://valid.com, https://secure.com';
        const result = await validateEndpoint(input);
        expect(result).toEqual(['http://valid.com', 'https://secure.com']);
    });

    it('should call showError with the correct message for invalid URLs', async () => {
        const input = 'gchhjv://ftp.example.com';
        const result = await validateEndpoint(input);
        expect(showError).toHaveBeenCalledWith(INVALID_ENDPOINT('gchhjv://ftp.example.com'));
        expect(result).toBe(false);
    });

    it('should return false if no valid endpoints are provided', async () => {
        const input = 'gchhjv://invalid.com, ftp://another.com';
        const result = await validateEndpoint(input);
        expect(showError).toHaveBeenCalledTimes(2);
        expect(result).toBe(false);
    });

    it('should ignore invalid URLs and return valid ones', async () => {
        const input = 'http://valid.com, gchhjv://invalid.com';
        const result = await validateEndpoint(input);
        expect(result).toEqual(['http://valid.com']);
        expect(showError).toHaveBeenCalledWith(INVALID_ENDPOINT('gchhjv://invalid.com'));
    });

    it('should handle a single valid endpoint correctly', async () => {
        const input = 'https://single-valid.com';
        const result = await validateEndpoint(input);
        expect(result).toEqual(['https://single-valid.com']);
    });

    it('should return false if all endpoints are invalid', async () => {
        const input = 'invalid-url, gchhjv://another-invalid.com';
        const result = await validateEndpoint(input);
        expect(showError).toHaveBeenCalledTimes(2);
        expect(result).toBe(false);
    });

    it('should trim spaces around URLs', async () => {
        const input = '   http://valid.com   ,   https://secure.com   ';
        const result = await validateEndpoint(input);
        expect(result).toEqual(['http://valid.com', 'https://secure.com']);
    });

    it('should return false for a single invalid URL', async () => {
        const input = 'gchhjv://invalid-url.com';
        const result = await validateEndpoint(input);
        expect(showError).toHaveBeenCalledWith(INVALID_ENDPOINT('gchhjv://invalid-url.com'));
        expect(result).toBe(false);
    });

    it('should call showError for a single invalid URL with unsupported protocol', async () => {
        const input = 'ftp://invalid-url.com';
        const result = await validateEndpoint(input);
        expect(showError).toHaveBeenCalledWith(INVALID_ENDPOINT('ftp://invalid-url.com'));
        expect(result).toBe(false);
    });
});
