"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
/*
 * INT-01 — Live integration test against Gemini API.
 *
 * Requires GEMINI_API_KEY in .env. Makes ONE real API call.
 * Cost: ~$0.0001 per run (negligible).
 *
 * NOT included in test:all. Run via: npm run test:integration-gemini
 */
const dotenv = __importStar(require("dotenv"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const Brain_1 = require("./orchestrator/Brain");
dotenv.config();
const LOG_PATH = path.join(process.cwd(), 'docs', 'audit', 'integrations', 'gemini.log');
function logLine(line) {
    fs.mkdirSync(path.dirname(LOG_PATH), { recursive: true });
    fs.appendFileSync(LOG_PATH, `${new Date().toISOString()} ${line}\n`);
}
async function main() {
    const key = process.env.GEMINI_API_KEY;
    if (!key) {
        throw new Error('MISSING_GEMINI_API_KEY: skipping live test');
    }
    const keyPrefix = key.slice(0, 6) + '...';
    logLine(`START gemini live test (key prefix=${keyPrefix})`);
    const brain = new Brain_1.Brain();
    // Force Gemini-only chain: override fallbacks to nothing
    brain.modelManager = {
        getModelConfig: () => ({
            primary: { provider: 'gemini-api', name: 'gemini-3.1-flash-lite-preview', raw: 'gemini-api/gemini-3.1-flash-lite-preview' },
            fallbacks: []
        })
    };
    const t0 = Date.now();
    const response = await brain.think('You are an audit assistant. Reply with the literal string AUDIT-OK and nothing else.', 'ping');
    const elapsed = Date.now() - t0;
    if (!response || response.length < 3) {
        logLine(`FAIL empty response`);
        throw new Error(`GEMINI_EMPTY_RESPONSE: "${response}"`);
    }
    if (response.includes('CRITICAL ERROR')) {
        logLine(`FAIL critical error: ${response.slice(0, 200)}`);
        throw new Error(`GEMINI_CRITICAL_ERROR: ${response.slice(0, 200)}`);
    }
    const containsAuditOk = response.toUpperCase().includes('AUDIT-OK') || response.toUpperCase().includes('AUDIT OK');
    logLine(`OK elapsed=${elapsed}ms response_length=${response.length} contains_audit_ok=${containsAuditOk}`);
    logLine(`  response_preview="${response.slice(0, 100).replace(/\n/g, ' ')}"`);
    console.log(`TEST_INTEGRATION_GEMINI_LIVE_OK (elapsed=${elapsed}ms, length=${response.length}, contains_audit_ok=${containsAuditOk})`);
    console.log(`  log: ${LOG_PATH}`);
    console.log(`  preview: ${response.slice(0, 80).replace(/\n/g, ' ')}`);
}
main().catch((err) => {
    const msg = err?.message || String(err);
    try {
        logLine(`FAIL ${msg.slice(0, 300)}`);
    }
    catch { }
    console.error('TEST_INTEGRATION_GEMINI_LIVE_FAIL:', msg);
    process.exit(1);
});
