"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const manager_js_1 = require("./manager.js");
// Mock the UserbotClient to avoid real Telegram connections
vitest_1.vi.mock('./client.js', () => ({
    UserbotClient: vitest_1.vi.fn().mockImplementation((cfg) => ({
        characterId: cfg.characterId,
        _cfg: cfg,
        onMessage: vitest_1.vi.fn(),
        start: vitest_1.vi.fn().mockResolvedValue('mock-session-string'),
        reconnect: vitest_1.vi.fn().mockResolvedValue(undefined),
        disconnect: vitest_1.vi.fn().mockResolvedValue(undefined),
        isConnected: vitest_1.vi.fn().mockReturnValue(true),
    })),
}));
// Mock file-ops to avoid real file I/O
vitest_1.vi.mock('../data-store/file-ops.js', () => ({
    readJSON: vitest_1.vi.fn().mockResolvedValue([]),
    atomicWriteJSON: vitest_1.vi.fn().mockResolvedValue(undefined),
    ensureDir: vitest_1.vi.fn().mockResolvedValue(undefined),
}));
function createMockDataStore(character = null) {
    return {
        getCharacter: vitest_1.vi.fn().mockResolvedValue(character),
        saveCharacter: vitest_1.vi.fn(),
        listCharacters: vitest_1.vi.fn(),
        init: vitest_1.vi.fn(),
    };
}
const sampleCharacterWithCreds = {
    id: 'char-1',
    identity: { name: 'TestChar' },
    apiCredentials: {
        telegramApiId: 12345,
        telegramApiHash: 'abc123hash',
    },
};
const sampleCharacterNoCreds = {
    id: 'char-2',
    identity: { name: 'NoCreds' },
};
(0, vitest_1.describe)('UserbotManager - character-based credentials', () => {
    (0, vitest_1.beforeEach)(() => {
        vitest_1.vi.clearAllMocks();
    });
    (0, vitest_1.describe)('login()', () => {
        (0, vitest_1.it)('should use character apiCredentials when available', async () => {
            const mockStore = createMockDataStore(sampleCharacterWithCreds);
            const manager = new manager_js_1.UserbotManager(mockStore);
            const { UserbotClient } = await import('./client.js');
            await manager.login('char-1', '+905551234567', async () => '12345');
            (0, vitest_1.expect)(mockStore.getCharacter).toHaveBeenCalledWith('char-1');
            (0, vitest_1.expect)(UserbotClient).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
                characterId: 'char-1',
                phoneNumber: '+905551234567',
                telegramApiId: 12345,
                telegramApiHash: 'abc123hash',
            }));
        });
        (0, vitest_1.it)('should throw meaningful error when character not found', async () => {
            const mockStore = createMockDataStore(null);
            const manager = new manager_js_1.UserbotManager(mockStore);
            await (0, vitest_1.expect)(manager.login('nonexistent', '+905551234567', async () => '12345')).rejects.toThrow('Character "nonexistent" not found');
        });
        (0, vitest_1.it)('should throw meaningful error when apiCredentials are missing', async () => {
            const mockStore = createMockDataStore(sampleCharacterNoCreds);
            const manager = new manager_js_1.UserbotManager(mockStore);
            await (0, vitest_1.expect)(manager.login('char-2', '+905551234567', async () => '12345')).rejects.toThrow('API credentials are not configured for character "char-2"');
        });
        (0, vitest_1.it)('should throw meaningful error when telegramApiId is 0', async () => {
            const charWithZeroId = {
                ...sampleCharacterWithCreds,
                apiCredentials: { telegramApiId: 0, telegramApiHash: 'hash' },
            };
            const mockStore = createMockDataStore(charWithZeroId);
            const manager = new manager_js_1.UserbotManager(mockStore);
            await (0, vitest_1.expect)(manager.login('char-1', '+905551234567', async () => '12345')).rejects.toThrow('API credentials are not configured');
        });
        (0, vitest_1.it)('should throw meaningful error when telegramApiHash is empty', async () => {
            const charWithEmptyHash = {
                ...sampleCharacterWithCreds,
                apiCredentials: { telegramApiId: 12345, telegramApiHash: '' },
            };
            const mockStore = createMockDataStore(charWithEmptyHash);
            const manager = new manager_js_1.UserbotManager(mockStore);
            await (0, vitest_1.expect)(manager.login('char-1', '+905551234567', async () => '12345')).rejects.toThrow('API credentials are not configured');
        });
        (0, vitest_1.it)('should throw error when DataStore is not provided', async () => {
            const manager = new manager_js_1.UserbotManager();
            await (0, vitest_1.expect)(manager.login('char-1', '+905551234567', async () => '12345')).rejects.toThrow('DataStore is not available');
        });
    });
});
//# sourceMappingURL=manager.test.js.map