"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;
    };
})();
describe('CostOptimizer', () => {
    let module;
    beforeEach(async () => {
        module = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/decisions/cost-optimizer')));
        module.clearCostLog();
    });
    test('getAvailableModels returns default models', () => {
        const models = module.getAvailableModels();
        expect(models.length).toBeGreaterThanOrEqual(5);
        expect(models.some((m) => m.modelId === 'gpt-4o')).toBe(true);
        expect(models.some((m) => m.modelId === 'deepseek-coder')).toBe(true);
    });
    test('routeTask returns cheapest for low complexity', () => {
        const decision = module.routeTask({ taskType: 'simple_edit', complexity: 'low', estimatedTokens: 500 });
        expect(decision.recommendedModel).toBeTruthy();
        expect(decision.estimatedCost).toBeGreaterThanOrEqual(0);
        expect(decision.alternatives.length).toBeGreaterThan(0);
    });
    test('routeTask respects budget parameter', () => {
        const best = module.routeTask({ taskType: 'code_gen', complexity: 'high', estimatedTokens: 5000 }, 'best');
        expect(best.recommendedModel).toBeTruthy();
        const cheapest = module.routeTask({ taskType: 'code_gen', complexity: 'high', estimatedTokens: 5000 }, 'cheapest');
        expect(cheapest.recommendedModel).toBeTruthy();
    });
    test('logModelCall and getCostStats', () => {
        module.logModelCall('gpt-4o', 1000, 'code_gen');
        module.logModelCall('deepseek-coder', 2000, 'code_review');
        const stats = module.getCostStats();
        expect(stats.totalRequests).toBe(2);
        expect(stats.totalTokens).toBe(3000);
        expect(stats.perModel.length).toBe(2);
    });
    test('registerCustomModel adds a new model', () => {
        module.registerCustomModel({ modelId: 'custom-v1', name: 'Custom V1', provider: 'custom', costPerToken: 0.000001, contextWindow: 16000, suitableFor: ['code_gen'], avgLatencyMs: 500, qualityScore: 0.7 });
        const models = module.getAvailableModels();
        expect(models.some((m) => m.modelId === 'custom-v1')).toBe(true);
    });
    test('clearCostLog resets all data', () => {
        module.logModelCall('gpt-4o', 100, 'test');
        expect(module.getCostStats().totalRequests).toBe(1);
        module.clearCostLog();
        expect(module.getCostStats().totalRequests).toBe(0);
    });
    test('recordBenchmark and getBestModelForTask', () => {
        module.recordBenchmark({ modelId: 'gpt-4o', taskType: 'code_gen', tokensUsed: 100, latencyMs: 200, quality: 0.9, cost: 0.005 });
        module.recordBenchmark({ modelId: 'gpt-4o-mini', taskType: 'code_gen', tokensUsed: 100, latencyMs: 400, quality: 0.6, cost: 0.001 });
        const best = module.getBestModelForTask('code_gen', 'quality');
        expect(best).not.toBeNull();
        expect(best.modelId).toBe('gpt-4o');
    });
    test('getBenchmarkStats returns per-task summaries', () => {
        module.recordBenchmark({ modelId: 'gpt-4o', taskType: 'review', tokensUsed: 200, latencyMs: 300, quality: 0.8, cost: 0.01 });
        const stats = module.getBenchmarkStats();
        expect(stats.totalBenchmarks).toBeGreaterThanOrEqual(1);
        expect(stats.perTask.some((t) => t.taskType === 'review')).toBe(true);
    });
});
//# sourceMappingURL=cost-optimizer.test.js.map