"use strict";
// src/test_embedding_providers.ts
// v1.13.0 — Embedding provider/registry regression.
Object.defineProperty(exports, "__esModule", { value: true });
const EmbeddingRegistry_1 = require("./orchestrator/rag/EmbeddingRegistry");
function assert(cond, msg) {
    if (!cond)
        throw new Error(`assertion failed: ${msg}`);
}
async function withEnv(vars, fn) {
    const saved = {};
    for (const k of Object.keys(vars)) {
        saved[k] = process.env[k];
        if (vars[k] === undefined)
            delete process.env[k];
        else
            process.env[k] = vars[k];
    }
    try {
        return await fn();
    }
    finally {
        for (const k of Object.keys(saved)) {
            if (saved[k] === undefined)
                delete process.env[k];
            else
                process.env[k] = saved[k];
        }
    }
}
async function main() {
    // 1. selectName default
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: undefined }, () => {
        assert(EmbeddingRegistry_1.EmbeddingRegistry.selectName() === 'ollama', 'default provider should be ollama');
    });
    console.log(`✓ scenario 1: default provider is ollama`);
    // 2. case-insensitive known providers
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: 'OPENAI' }, () => {
        assert(EmbeddingRegistry_1.EmbeddingRegistry.selectName() === 'openai', 'OPENAI uppercase resolved');
    });
    console.log(`✓ scenario 2: case-insensitive resolution`);
    // 3. unknown provider falls back to ollama with a warning
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: 'cohere' }, () => {
        const captured = [];
        const origWarn = console.warn;
        console.warn = (msg) => { captured.push(String(msg)); };
        try {
            assert(EmbeddingRegistry_1.EmbeddingRegistry.selectName() === 'ollama', 'unknown provider falls back to ollama');
            assert(captured.some(m => m.includes('cohere')), 'warning mentions the unknown value');
        }
        finally {
            console.warn = origWarn;
        }
    });
    console.log(`✓ scenario 3: unknown provider falls back to ollama with warning`);
    // 4. Ollama provider properties
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: 'ollama' }, () => {
        const p = EmbeddingRegistry_1.EmbeddingRegistry.create();
        assert(p.name() === 'ollama', 'name = ollama');
        assert(p.dim() === 768, `dim default 768, got ${p.dim()}`);
        assert(p.model() === 'nomic-embed-text', `default model nomic-embed-text, got ${p.model()}`);
    });
    console.log(`✓ scenario 4: Ollama provider defaults (dim=768, model=nomic-embed-text)`);
    // 5. OpenAI provider properties — does not require API key to instantiate
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: 'openai', OPENAI_API_KEY: undefined }, async () => {
        const p = EmbeddingRegistry_1.EmbeddingRegistry.create();
        assert(p.name() === 'openai', 'name = openai');
        assert(p.dim() === 1536, 'dim default 1536');
        assert(p.model() === 'text-embedding-3-small', 'default model text-embedding-3-small');
        const avail = await p.isAvailable();
        assert(avail === false, 'unavailable when OPENAI_API_KEY missing');
    });
    console.log(`✓ scenario 5: OpenAI provider defaults + availability gate`);
    // 6. Ollama availability probe never throws (Ollama not running in this sandbox)
    await withEnv({ OPENLIFE_EMBEDDING_PROVIDER: 'ollama', OLLAMA_URL: 'http://127.0.0.1:1' }, async () => {
        const p = EmbeddingRegistry_1.EmbeddingRegistry.create();
        const avail = await p.isAvailable();
        assert(avail === false, 'isAvailable returns false on connection refused without throwing');
    });
    console.log(`✓ scenario 6: Ollama isAvailable never throws on connect refused`);
    console.log('TEST_EMBEDDING_PROVIDERS_OK');
}
main().catch(err => {
    console.error(`TEST_EMBEDDING_PROVIDERS_FAIL: ${err instanceof Error ? err.message : String(err)}`);
    process.exit(1);
});
