"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const accounts_1 = require("../src/globalConfig/accounts");
const cloud_1 = require("../src/globalConfig/cloud");
const logger_1 = __importDefault(require("../src/logger"));
const share_1 = require("../src/share");
jest.mock('../src/logger');
jest.mock('../src/globalConfig/cloud');
jest.mock('../src/fetch', () => ({
    fetchWithProxy: jest.fn().mockResolvedValue({
        ok: true,
        json: jest.fn().mockResolvedValue({ id: 'mock-eval-id' }),
    }),
}));
jest.mock('../src/globalConfig/accounts', () => ({
    getUserEmail: jest.fn(),
    setUserEmail: jest.fn(),
}));
describe('stripAuthFromUrl', () => {
    it('removes username and password from URL', () => {
        const input = 'https://user:pass@example.com/path?query=value#hash';
        const expected = 'https://example.com/path?query=value#hash';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(expected);
    });
    it('handles URLs without auth info', () => {
        const input = 'https://example.com/path?query=value#hash';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(input);
    });
    it('handles URLs with only username', () => {
        const input = 'https://user@example.com/path';
        const expected = 'https://example.com/path';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(expected);
    });
    it('handles URLs with special characters in auth', () => {
        const input = 'https://user%40:p@ss@example.com/path';
        const expected = 'https://example.com/path';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(expected);
    });
    it('returns original string for invalid URLs', () => {
        const input = 'not a valid url';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(input);
    });
    it('handles URLs with IP addresses', () => {
        const input = 'http://user:pass@192.168.1.1:8080/path';
        const expected = 'http://192.168.1.1:8080/path';
        expect((0, share_1.stripAuthFromUrl)(input)).toBe(expected);
    });
});
describe('createShareableUrl', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('creates correct URL for cloud config and updates author', async () => {
        jest.mocked(cloud_1.cloudConfig.isEnabled).mockReturnValue(true);
        jest.mocked(cloud_1.cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
        jest.mocked(cloud_1.cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
        jest.mocked(cloud_1.cloudConfig.getApiKey).mockReturnValue('mock-api-key');
        jest.mocked(accounts_1.getUserEmail).mockReturnValue('logged-in@example.com');
        const mockEval = {
            config: {},
            author: 'original@example.com',
            useOldResults: jest.fn().mockReturnValue(false),
            loadResults: jest.fn().mockResolvedValue(undefined),
            save: jest.fn().mockResolvedValue(undefined),
        };
        const result = await (0, share_1.createShareableUrl)(mockEval);
        expect(result).toBe('https://app.example.com/eval/mock-eval-id');
        expect(mockEval.author).toBe('logged-in@example.com');
        expect(mockEval.save).toHaveBeenCalledWith();
    });
    it('throws error if cloud config enabled but no user email', async () => {
        jest.mocked(cloud_1.cloudConfig.isEnabled).mockReturnValue(true);
        jest.mocked(accounts_1.getUserEmail).mockReturnValue(null);
        const mockEval = {
            config: {},
            useOldResults: jest.fn().mockReturnValue(false),
            loadResults: jest.fn().mockResolvedValue(undefined),
        };
        await expect((0, share_1.createShareableUrl)(mockEval)).rejects.toThrow('User email is not set');
    });
    it('logs warning when changing author in cloud mode', async () => {
        jest.mocked(cloud_1.cloudConfig.isEnabled).mockReturnValue(true);
        jest.mocked(cloud_1.cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
        jest.mocked(cloud_1.cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
        jest.mocked(cloud_1.cloudConfig.getApiKey).mockReturnValue('mock-api-key');
        jest.mocked(accounts_1.getUserEmail).mockReturnValue('new@example.com');
        const mockEval = {
            config: {},
            author: 'original@example.com',
            useOldResults: jest.fn().mockReturnValue(false),
            loadResults: jest.fn().mockResolvedValue(undefined),
            save: jest.fn().mockResolvedValue(undefined),
        };
        await (0, share_1.createShareableUrl)(mockEval);
        expect(mockEval.author).toBe('new@example.com');
        expect(jest.mocked(logger_1.default.warn)).toHaveBeenCalledWith('Warning: Changing eval author from original@example.com to logged-in user new@example.com');
    });
});
//# sourceMappingURL=share.test.js.map