"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const fastify_1 = __importDefault(require("fastify"));
const characters_js_1 = __importDefault(require("./characters.js"));
function createMockCharacterEngine() {
    return {
        listCharacters: vitest_1.vi.fn(),
        getCharacter: vitest_1.vi.fn(),
        createCharacter: vitest_1.vi.fn(),
        updateCharacter: vitest_1.vi.fn(),
        deleteCharacter: vitest_1.vi.fn(),
        cloneCharacter: vitest_1.vi.fn(),
        activateCharacter: vitest_1.vi.fn(),
        deactivateCharacter: vitest_1.vi.fn(),
    };
}
function createMockDataStore() {
    return {
        getCharacter: vitest_1.vi.fn(),
        saveCharacter: vitest_1.vi.fn(),
        getConversationProfile: vitest_1.vi.fn(),
        saveConversationProfile: vitest_1.vi.fn(),
        getReferenceImagesDir: vitest_1.vi.fn(),
    };
}
const sampleCharacter = {
    id: 'char-1',
    createdAt: '2024-01-01T00:00:00.000Z',
    updatedAt: '2024-01-01T00:00:00.000Z',
    status: 'active',
    identity: { name: 'Test', age: 25, nationality: 'TR', city: 'Istanbul', occupation: 'Model', personality_tags: [], mbti: 'ENFP', backstory: '' },
    physicalMeasurements: { height_cm: 170, weight_kg: 55, body_type: 'slim', measurements: { bust: 85, waist: 60, hips: 90 } },
    skinMap: { base: '#F5D0A9', shadow: '#D4A574', highlight: '#FFE4C4' },
    faceAnatomy: { eye_shape: 'almond', eye_color: 'brown', eyebrow_shape: 'arched', nose_type: 'straight', lip_shape: 'full', cheekbones: 'high', jawline: 'soft', distinctive_marks: [] },
    hairDNA: { style: 'wavy', length: 'long', texture: 'smooth', root_color: '#3B2F2F', mid_color: '#5C4033', highlight_color: '#8B7355' },
    bodyBiometrics: { body_type_detail: 'ectomorph', posture: 'upright', skin_texture: 'smooth' },
    profileView: { best_angle: 'three-quarter', signature_pose: 'hand-on-hip' },
    expressionMotion: { default_expression: 'smile', smile_type: 'closed', gaze_style: 'direct' },
    makeupStyle: { philosophy: 'natural', skin_finish: 'dewy', colors: { primary: '#C48A6A', secondary: '#B5838D', accent: '#E8A87C' } },
    fashionStyle: { aesthetic: 'casual-chic', main_colors: ['#FFFFFF', '#000000'], signature_pieces: ['blazer'] },
    technicalSettings: { camera_body: 'Canon R5', portrait_lens: '85mm f/1.4', full_body_lens: '35mm f/1.4', film_stock: 'Portra 400' },
};
(0, vitest_1.describe)('characters API credentials endpoints', () => {
    let app;
    let mockEngine;
    let mockStore;
    (0, vitest_1.beforeEach)(async () => {
        vitest_1.vi.clearAllMocks();
        mockEngine = createMockCharacterEngine();
        mockStore = createMockDataStore();
        app = (0, fastify_1.default)();
        await app.register(characters_js_1.default, {
            characterEngine: mockEngine,
            dataStore: mockStore,
        });
        await app.ready();
    });
    (0, vitest_1.describe)('GET /characters/:id/api-credentials', () => {
        (0, vitest_1.it)('should return api credentials when they exist', async () => {
            const charWithCreds = { ...sampleCharacter, apiCredentials: { telegramApiId: 12345, telegramApiHash: 'abc123hash' } };
            mockStore.getCharacter.mockResolvedValue(charWithCreds);
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/api-credentials' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.success).toBe(true);
            (0, vitest_1.expect)(body.data).toEqual({ telegramApiId: 12345, telegramApiHash: 'abc123hash' });
        });
        (0, vitest_1.it)('should return null data when credentials are not set', async () => {
            mockStore.getCharacter.mockResolvedValue(sampleCharacter);
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/api-credentials' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.success).toBe(true);
            (0, vitest_1.expect)(body.data).toBeNull();
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({ method: 'GET', url: '/characters/nonexistent/api-credentials' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().success).toBe(false);
            (0, vitest_1.expect)(res.json().error).toBe('Character not found');
        });
    });
    (0, vitest_1.describe)('PUT /characters/:id/api-credentials', () => {
        (0, vitest_1.it)('should update api credentials successfully', async () => {
            mockStore.getCharacter.mockResolvedValue({ ...sampleCharacter });
            mockStore.saveCharacter.mockResolvedValue(undefined);
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiId: 99999, telegramApiHash: 'newhash456' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.success).toBe(true);
            (0, vitest_1.expect)(body.data).toEqual({ telegramApiId: 99999, telegramApiHash: 'newhash456' });
            (0, vitest_1.expect)(mockStore.saveCharacter).toHaveBeenCalledTimes(1);
            const savedChar = mockStore.saveCharacter.mock.calls[0][0];
            (0, vitest_1.expect)(savedChar.apiCredentials).toEqual({ telegramApiId: 99999, telegramApiHash: 'newhash456' });
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/nonexistent/api-credentials',
                payload: { telegramApiId: 12345, telegramApiHash: 'hash123' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should return 400 when body is missing', async () => {
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should return 400 when telegramApiId is missing', async () => {
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiHash: 'hash123' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should return 400 when telegramApiHash is missing', async () => {
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiId: 12345 },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should return 400 when telegramApiId is not a number', async () => {
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiId: 'not-a-number', telegramApiHash: 'hash123' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should return 400 when telegramApiHash is empty string', async () => {
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiId: 12345, telegramApiHash: '   ' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('should trim telegramApiHash whitespace', async () => {
            mockStore.getCharacter.mockResolvedValue({ ...sampleCharacter });
            mockStore.saveCharacter.mockResolvedValue(undefined);
            const res = await app.inject({
                method: 'PUT',
                url: '/characters/char-1/api-credentials',
                payload: { telegramApiId: 12345, telegramApiHash: '  hash123  ' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json().data.telegramApiHash).toBe('hash123');
        });
    });
});
//# sourceMappingURL=characters-api-credentials.test.js.map