"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 conversations_js_1 = __importDefault(require("./conversations.js"));
function createMockDataStore(overrides = {}) {
    return {
        getCharacter: vitest_1.vi.fn().mockResolvedValue({ id: 'char-1', status: 'active' }),
        listConversations: vitest_1.vi.fn().mockResolvedValue([]),
        getConversationHistory: vitest_1.vi.fn().mockResolvedValue(null),
        deleteConversation: vitest_1.vi.fn().mockResolvedValue(false),
        ...overrides,
    };
}
function createMockConversationEngine() {
    return {};
}
async function buildApp(dataStoreOverrides = {}) {
    const app = (0, fastify_1.default)();
    const dataStore = createMockDataStore(dataStoreOverrides);
    await app.register(conversations_js_1.default, {
        dataStore,
        conversationEngine: createMockConversationEngine(),
    });
    return { app, dataStore };
}
(0, vitest_1.describe)('conversationRoutes', () => {
    (0, vitest_1.describe)('GET /characters/:id/conversations', () => {
        (0, vitest_1.it)('returns 404 when character not found', async () => {
            const { app } = await buildApp({ getCharacter: vitest_1.vi.fn().mockResolvedValue(null) });
            const res = await app.inject({ method: 'GET', url: '/characters/unknown/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json()).toEqual({ success: false, error: 'Character not found: unknown' });
        });
        (0, vitest_1.it)('returns empty list when no conversations', async () => {
            const { app } = await buildApp();
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json()).toEqual({ success: true, data: [] });
        });
        (0, vitest_1.it)('returns conversation summaries', async () => {
            const conversations = [
                {
                    telegramUserId: 123,
                    userName: 'Alice',
                    userInfo: { name: 'Alice' },
                    totalMessages: 10,
                    totalStarsSpent: 5,
                    lastActivity: '2025-01-01T00:00:00.000Z',
                },
                {
                    telegramUserId: 456,
                    userInfo: { name: 'Bob' },
                    totalMessages: 3,
                    totalStarsSpent: 0,
                    lastActivity: '2025-01-02T00:00:00.000Z',
                },
            ];
            const { app } = await buildApp({ listConversations: vitest_1.vi.fn().mockResolvedValue(conversations) });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (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).toHaveLength(2);
            (0, vitest_1.expect)(body.data[0].telegramUserId).toBe(123);
            (0, vitest_1.expect)(body.data[0].userName).toBe('Alice');
            (0, vitest_1.expect)(body.data[1].userName).toBe('Bob');
        });
        (0, vitest_1.it)('returns 500 on internal error', async () => {
            const { app } = await buildApp({
                getCharacter: vitest_1.vi.fn().mockRejectedValue(new Error('disk failure')),
            });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(500);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
        (0, vitest_1.it)('returns lastMessagePreview with first 50 chars of last message', async () => {
            const longContent = 'A'.repeat(80);
            const conversations = [
                {
                    telegramUserId: 123,
                    userName: 'Alice',
                    userInfo: { name: 'Alice' },
                    totalMessages: 2,
                    totalStarsSpent: 0,
                    lastActivity: '2025-01-01T00:00:00.000Z',
                    messages: [
                        { id: 'm1', role: 'user', content: 'first message', timestamp: '2025-01-01T00:00:00.000Z' },
                        { id: 'm2', role: 'assistant', content: longContent, timestamp: '2025-01-01T00:01:00.000Z' },
                    ],
                },
            ];
            const { app } = await buildApp({ listConversations: vitest_1.vi.fn().mockResolvedValue(conversations) });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.data[0].lastMessagePreview).toBe('A'.repeat(50));
        });
        (0, vitest_1.it)('returns lastMessagePreview as null when no messages', async () => {
            const conversations = [
                {
                    telegramUserId: 123,
                    userName: 'Alice',
                    userInfo: { name: 'Alice' },
                    totalMessages: 0,
                    totalStarsSpent: 0,
                    lastActivity: '2025-01-01T00:00:00.000Z',
                    messages: [],
                },
            ];
            const { app } = await buildApp({ listConversations: vitest_1.vi.fn().mockResolvedValue(conversations) });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.data[0].lastMessagePreview).toBeNull();
        });
        (0, vitest_1.it)('returns full content as preview when message is shorter than 50 chars', async () => {
            const conversations = [
                {
                    telegramUserId: 123,
                    userName: 'Alice',
                    userInfo: { name: 'Alice' },
                    totalMessages: 1,
                    totalStarsSpent: 0,
                    lastActivity: '2025-01-01T00:00:00.000Z',
                    messages: [
                        { id: 'm1', role: 'user', content: 'Hello!', timestamp: '2025-01-01T00:00:00.000Z' },
                    ],
                },
            ];
            const { app } = await buildApp({ listConversations: vitest_1.vi.fn().mockResolvedValue(conversations) });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            const body = res.json();
            (0, vitest_1.expect)(body.data[0].lastMessagePreview).toBe('Hello!');
        });
    });
    (0, vitest_1.describe)('GET /characters/:id/conversations/:userId', () => {
        (0, vitest_1.it)('returns 404 when character not found', async () => {
            const { app } = await buildApp({ getCharacter: vitest_1.vi.fn().mockResolvedValue(null) });
            const res = await app.inject({ method: 'GET', url: '/characters/unknown/conversations/123' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
        (0, vitest_1.it)('returns 400 for non-numeric userId', async () => {
            const { app } = await buildApp();
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations/abc' });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().error).toContain('Invalid userId');
        });
        (0, vitest_1.it)('returns 404 when conversation not found', async () => {
            const { app } = await buildApp();
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations/999' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().error).toContain('Conversation not found');
        });
        (0, vitest_1.it)('returns conversation history', async () => {
            const history = {
                characterId: 'char-1',
                telegramUserId: 123,
                messages: [{ id: 'm1', role: 'user', content: 'hello', timestamp: '2025-01-01T00:00:00.000Z' }],
                totalMessages: 1,
                totalStarsSpent: 0,
                lastActivity: '2025-01-01T00:00:00.000Z',
                userInfo: {},
            };
            const { app } = await buildApp({
                getConversationHistory: vitest_1.vi.fn().mockResolvedValue(history),
            });
            const res = await app.inject({ method: 'GET', url: '/characters/char-1/conversations/123' });
            (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.characterId).toBe('char-1');
            (0, vitest_1.expect)(body.data.messages).toHaveLength(1);
        });
    });
    (0, vitest_1.describe)('DELETE /characters/:id/conversations/:userId', () => {
        (0, vitest_1.it)('returns 404 when character not found', async () => {
            const { app } = await buildApp({ getCharacter: vitest_1.vi.fn().mockResolvedValue(null) });
            const res = await app.inject({ method: 'DELETE', url: '/characters/unknown/conversations/123' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
        (0, vitest_1.it)('returns 400 for non-numeric userId', async () => {
            const { app } = await buildApp();
            const res = await app.inject({ method: 'DELETE', url: '/characters/char-1/conversations/abc' });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
        });
        (0, vitest_1.it)('returns 404 when conversation not found', async () => {
            const { app } = await buildApp();
            const res = await app.inject({ method: 'DELETE', url: '/characters/char-1/conversations/999' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
        (0, vitest_1.it)('deletes conversation successfully', async () => {
            const { app } = await buildApp({
                deleteConversation: vitest_1.vi.fn().mockResolvedValue(true),
            });
            const res = await app.inject({ method: 'DELETE', url: '/characters/char-1/conversations/123' });
            (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.message).toContain('123');
        });
        (0, vitest_1.it)('returns 500 on internal error', async () => {
            const { app } = await buildApp({
                deleteConversation: vitest_1.vi.fn().mockRejectedValue(new Error('io error')),
            });
            const res = await app.inject({ method: 'DELETE', url: '/characters/char-1/conversations/123' });
            (0, vitest_1.expect)(res.statusCode).toBe(500);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
});
//# sourceMappingURL=conversations.test.js.map