"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 templates_js_1 = __importDefault(require("./templates.js"));
function createMockTemplateManager() {
    return {
        listTemplates: vitest_1.vi.fn(),
        getTemplate: vitest_1.vi.fn(),
        fillTemplate: vitest_1.vi.fn(),
        initDefaults: vitest_1.vi.fn(),
    };
}
function createMockDataStore() {
    return {
        saveTemplate: vitest_1.vi.fn(),
        getTemplate: vitest_1.vi.fn(),
        listTemplates: vitest_1.vi.fn(),
        deleteTemplate: vitest_1.vi.fn(),
    };
}
const sampleTemplate = {
    id: 'beach-123',
    name: 'Beach',
    category: 'outdoor',
    promptTemplate: '{subject} on a beach, {camera}',
    environment: { lighting: 'golden hour', timeOfDay: 'sunset' },
    defaultAspectRatio: '16:9',
    createdAt: '2024-01-01T00:00:00.000Z',
    updatedAt: '2024-01-01T00:00:00.000Z',
};
(0, vitest_1.describe)('templateRoutes', () => {
    let app;
    let mockTmplManager;
    let mockDataStore;
    (0, vitest_1.beforeEach)(async () => {
        vitest_1.vi.clearAllMocks();
        mockTmplManager = createMockTemplateManager();
        mockDataStore = createMockDataStore();
        app = (0, fastify_1.default)();
        await app.register(templates_js_1.default, {
            templateManager: mockTmplManager,
            dataStore: mockDataStore,
        });
        await app.ready();
    });
    (0, vitest_1.describe)('GET /templates', () => {
        (0, vitest_1.it)('should return all templates', async () => {
            mockTmplManager.listTemplates.mockResolvedValue([sampleTemplate]);
            const res = await app.inject({ method: 'GET', url: '/templates' });
            (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(1);
            (0, vitest_1.expect)(body.data[0].name).toBe('Beach');
        });
        (0, vitest_1.it)('should filter by category query param', async () => {
            // Category filter now uses prompt-library getPromptsByCategory, not tmplManager
            const res = await app.inject({ method: 'GET', url: '/templates?category=outdoor' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json().success).toBe(true);
        });
        (0, vitest_1.it)('should return empty array when no templates exist', async () => {
            mockTmplManager.listTemplates.mockResolvedValue([]);
            const res = await app.inject({ method: 'GET', url: '/templates' });
            (0, vitest_1.expect)(res.statusCode).toBe(200);
            (0, vitest_1.expect)(res.json().data).toEqual([]);
        });
    });
    (0, vitest_1.describe)('POST /templates', () => {
        (0, vitest_1.it)('should create a new template and return 201', async () => {
            mockDataStore.saveTemplate.mockResolvedValue(undefined);
            const res = await app.inject({
                method: 'POST',
                url: '/templates',
                payload: {
                    name: 'Studio',
                    category: 'professional',
                    promptTemplate: '{subject} in studio, {camera}',
                    environment: { lighting: 'studio', timeOfDay: 'daytime' },
                    defaultAspectRatio: '3:4',
                },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(201);
            const body = res.json();
            (0, vitest_1.expect)(body.success).toBe(true);
            (0, vitest_1.expect)(body.data.name).toBe('Studio');
            (0, vitest_1.expect)(body.data.category).toBe('professional');
            (0, vitest_1.expect)(body.data.id).toContain('studio-');
            (0, vitest_1.expect)(mockDataStore.saveTemplate).toHaveBeenCalledTimes(1);
        });
        (0, vitest_1.it)('should return 400 when required fields are missing', async () => {
            const res = await app.inject({
                method: 'POST',
                url: '/templates',
                payload: { name: 'Incomplete' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(400);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
    (0, vitest_1.describe)('GET /templates/:id', () => {
        (0, vitest_1.it)('should return template detail', async () => {
            mockDataStore.getTemplate.mockResolvedValue(sampleTemplate);
            const res = await app.inject({ method: 'GET', url: '/templates/beach-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.id).toBe('beach-123');
        });
        (0, vitest_1.it)('should return 404 when template not found', async () => {
            mockDataStore.getTemplate.mockResolvedValue(null);
            const res = await app.inject({ method: 'GET', url: '/templates/nonexistent' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
    (0, vitest_1.describe)('PUT /templates/:id', () => {
        (0, vitest_1.it)('should update an existing template', async () => {
            mockDataStore.getTemplate.mockResolvedValue(sampleTemplate);
            mockDataStore.saveTemplate.mockResolvedValue(undefined);
            const res = await app.inject({
                method: 'PUT',
                url: '/templates/beach-123',
                payload: { name: 'Tropical Beach' },
            });
            (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.name).toBe('Tropical Beach');
            (0, vitest_1.expect)(body.data.category).toBe('outdoor'); // unchanged
        });
        (0, vitest_1.it)('should return 404 when template not found', async () => {
            mockDataStore.getTemplate.mockResolvedValue(null);
            const res = await app.inject({
                method: 'PUT',
                url: '/templates/nonexistent',
                payload: { name: 'Updated' },
            });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
    (0, vitest_1.describe)('DELETE /templates/:id', () => {
        (0, vitest_1.it)('should delete an existing template', async () => {
            mockDataStore.getTemplate.mockResolvedValue(sampleTemplate);
            mockDataStore.deleteTemplate.mockResolvedValue(true);
            const res = await app.inject({ method: 'DELETE', url: '/templates/beach-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.id).toBe('beach-123');
            (0, vitest_1.expect)(body.data.deleted).toBe(true);
        });
        (0, vitest_1.it)('should return 404 when template not found', async () => {
            mockDataStore.getTemplate.mockResolvedValue(null);
            const res = await app.inject({ method: 'DELETE', url: '/templates/nonexistent' });
            (0, vitest_1.expect)(res.statusCode).toBe(404);
            (0, vitest_1.expect)(res.json().success).toBe(false);
        });
    });
});
//# sourceMappingURL=templates.test.js.map