"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const database_1 = require("../../src/database");
const accounts_1 = require("../../src/globalConfig/accounts");
const migrate_1 = require("../../src/migrate");
const eval_1 = __importStar(require("../../src/models/eval"));
const evalFactory_1 = __importDefault(require("../factories/evalFactory"));
jest.mock('../../src/globalConfig/accounts', () => ({
    ...jest.requireActual('../../src/globalConfig/accounts'),
    getUserEmail: jest.fn(),
}));
describe('evaluator', () => {
    beforeAll(async () => {
        await (0, migrate_1.runDbMigrations)();
    });
    beforeEach(async () => {
        // Clear all tables before each test
        const db = (0, database_1.getDb)();
        // Delete related tables first
        await db.run('DELETE FROM eval_results');
        await db.run('DELETE FROM evals_to_datasets');
        await db.run('DELETE FROM evals_to_prompts');
        await db.run('DELETE FROM evals_to_tags');
        // Then delete from main table
        await db.run('DELETE FROM evals');
    });
    describe('summaryResults', () => {
        it('should return all evaluations', async () => {
            const eval1 = await evalFactory_1.default.create();
            const eval2 = await evalFactory_1.default.create();
            await evalFactory_1.default.createOldResult();
            const evaluations = await (0, eval_1.getSummaryOfLatestEvals)();
            expect(evaluations).toHaveLength(2);
            expect(evaluations).toContainEqual(expect.objectContaining({
                evalId: eval1.id,
                createdAt: eval1.createdAt,
                numTests: 2,
            }));
            expect(evaluations).toContainEqual(expect.objectContaining({
                evalId: eval2.id,
                createdAt: eval2.createdAt,
                description: eval2.description || null,
                numTests: 2,
            }));
        });
    });
    describe('delete', () => {
        it('should delete an evaluation', async () => {
            const eval1 = await evalFactory_1.default.create();
            const eval_ = await eval_1.default.findById(eval1.id);
            expect(eval_).toBeDefined();
            await eval1.delete();
            const eval_2 = await eval_1.default.findById(eval1.id);
            expect(eval_2).toBeUndefined();
        });
    });
    describe('create', () => {
        it('should use provided author when available', async () => {
            const providedAuthor = 'provided@example.com';
            const config = { description: 'Test eval' };
            const renderedPrompts = [
                { raw: 'Test prompt', display: 'Test prompt', label: 'Test label' },
            ];
            const evaluation = await eval_1.default.create(config, renderedPrompts, { author: providedAuthor });
            expect(evaluation.author).toBe(providedAuthor);
            const persistedEval = await eval_1.default.findById(evaluation.id);
            expect(persistedEval?.author).toBe(providedAuthor);
        });
        it('should use default author from getUserEmail when not provided', async () => {
            const mockEmail = 'default@example.com';
            jest.mocked(accounts_1.getUserEmail).mockReturnValue(mockEmail);
            const config = { description: 'Test eval' };
            const renderedPrompts = [
                { raw: 'Test prompt', display: 'Test prompt', label: 'Test label' },
            ];
            const evaluation = await eval_1.default.create(config, renderedPrompts);
            const persistedEval = await eval_1.default.findById(evaluation.id);
            expect(persistedEval?.author).toBe(mockEmail);
        });
    });
});
//# sourceMappingURL=eval.test.js.map