"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 images_js_1 = __importDefault(require("./images.js"));
// Stubs
function createMockImageEngine() {
    return {
        generateImage: vitest_1.vi.fn(),
    };
}
function createMockDataStore() {
    return {
        getCharacter: vitest_1.vi.fn(),
        getGeneratedImagesDir: vitest_1.vi.fn().mockReturnValue('/tmp/images'),
        getQualityReportsDir: vitest_1.vi.fn().mockReturnValue('/tmp/reports'),
    };
}
// Mock file-ops
vitest_1.vi.mock('../modules/data-store/file-ops.js', () => ({
    listFiles: vitest_1.vi.fn().mockResolvedValue([]),
    readJSON: vitest_1.vi.fn().mockResolvedValue(null),
    deleteFile: vitest_1.vi.fn().mockResolvedValue(true),
    fileExists: vitest_1.vi.fn().mockReturnValue(false),
}));
const file_ops_js_1 = require("../modules/data-store/file-ops.js");
(0, vitest_1.describe)('imageRoutes', () => {
    let app;
    let mockImageEngine;
    let mockDataStore;
    (0, vitest_1.beforeEach)(async () => {
        vitest_1.vi.clearAllMocks();
        mockImageEngine = createMockImageEngine();
        mockDataStore = createMockDataStore();
        app = (0, fastify_1.default)();
        await app.register(images_js_1.default, {
            imageEngine: mockImageEngine,
            dataStore: mockDataStore,
        });
        await app.ready();
    });
    (0, vitest_1.describe)('POST /characters/:id/images/generate', () => {
        (0, vitest_1.it)('should return 201 when image generation is queued', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            // Mock dynamic imports used by the route
            vitest_1.vi.doMock('../modules/data-store/file-ops.js', () => ({
                listFiles: vitest_1.vi.fn().mockResolvedValue([]),
                readJSON: vitest_1.vi.fn().mockResolvedValue({ negative_prompt: 'blurry' }),
                deleteFile: vitest_1.vi.fn().mockResolvedValue(true),
                fileExists: vitest_1.vi.fn().mockReturnValue(false),
                ensureDir: vitest_1.vi.fn().mockResolvedValue(undefined),
            }));
            // This endpoint now requires prompt (not templateId) and does KIE + download
            // Since it uses dynamic imports internally, we test the validation path instead
            const res = await app.inject({
                method: 'POST',
                url: '/characters/char-1/images/generate',
                payload: {},
            });
            // Should return 400 because prompt is required
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
            (0, vitest_1.expect)(res.json().error).toContain('prompt');
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({
                method: 'POST',
                url: '/characters/missing/images/generate',
                payload: { prompt: 'test prompt' },
            });
            (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 prompt is missing', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            const res = await app.inject({
                method: 'POST',
                url: '/characters/char-1/images/generate',
                payload: {},
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
            (0, vitest_1.expect)(res.json().error).toContain('prompt');
        });
        (0, vitest_1.it)('should return 400 when prompt is empty string', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            const res = await app.inject({
                method: 'POST',
                url: '/characters/char-1/images/generate',
                payload: { prompt: '  ' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
    (0, vitest_1.describe)('GET /characters/:id/images', () => {
        (0, vitest_1.it)('should return list of images', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.listFiles).mockResolvedValue(['img-1.jpg', 'img-2.jpg']);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/char-1/images',
            });
            (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);
            // Images are sorted descending by imageId
            (0, vitest_1.expect)(body.data[0].imageId).toBe('img-2');
            (0, vitest_1.expect)(body.data[1].imageId).toBe('img-1');
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/missing/images',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
        (0, vitest_1.it)('should return empty array when no images exist', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.listFiles).mockResolvedValue([]);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/char-1/images',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json().data).toEqual([]);
        });
    });
    (0, vitest_1.describe)('GET /characters/:id/images/:imageId', () => {
        (0, vitest_1.it)('should return image detail with quality report', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.fileExists).mockReturnValue(true);
            vitest_1.vi.mocked(file_ops_js_1.readJSON).mockResolvedValue({
                imageId: 'img-1',
                totalScore: 8.5,
                passed: true,
            });
            const res = await app.inject({
                method: 'GET',
                url: '/characters/char-1/images/img-1',
            });
            (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.imageId).toBe('img-1');
            (0, vitest_1.expect)(body.data.qualityReport.totalScore).toBe(8.5);
        });
        (0, vitest_1.it)('should return null qualityReport when no report exists', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.fileExists).mockReturnValue(true);
            vitest_1.vi.mocked(file_ops_js_1.readJSON).mockResolvedValue(null);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/char-1/images/img-1',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json().data.qualityReport).toBeNull();
        });
        (0, vitest_1.it)('should return 404 when image not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.fileExists).mockReturnValue(false);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/char-1/images/nonexistent',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().error).toContain('Image not found');
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({
                method: 'GET',
                url: '/characters/missing/images/img-1',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
    });
    (0, vitest_1.describe)('DELETE /characters/:id/images/:imageId', () => {
        (0, vitest_1.it)('should delete image and quality report', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.fileExists).mockReturnValue(true);
            vitest_1.vi.mocked(file_ops_js_1.deleteFile).mockResolvedValue(true);
            const res = await app.inject({
                method: 'DELETE',
                url: '/characters/char-1/images/img-1',
            });
            (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.imageId).toBe('img-1');
            (0, vitest_1.expect)(body.data.deleted).toBe(true);
            // Should delete both image and quality report
            (0, vitest_1.expect)(file_ops_js_1.deleteFile).toHaveBeenCalledTimes(2);
        });
        (0, vitest_1.it)('should return 404 when image not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue({ id: 'char-1' });
            vitest_1.vi.mocked(file_ops_js_1.fileExists).mockReturnValue(false);
            const res = await app.inject({
                method: 'DELETE',
                url: '/characters/char-1/images/nonexistent',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
        (0, vitest_1.it)('should return 404 when character not found', async () => {
            mockDataStore.getCharacter.mockResolvedValue(null);
            const res = await app.inject({
                method: 'DELETE',
                url: '/characters/missing/images/img-1',
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
        });
    });
});
//# sourceMappingURL=images.test.js.map