"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Brain_1 = require("./orchestrator/Brain");
const brain = new Brain_1.Brain();
function assertContains(haystack, needle, label) {
    if (!haystack.includes(needle))
        throw new Error(`MISSING_FIELD[${label}]: "${needle}" not in: ${haystack}`);
}
const openaiErr = brain.formatProviderError('openai-api', 'gpt-5.4-mini', {
    status: 401,
    error: { message: 'Incorrect API key provided' }
}, { keyEnvVar: 'OPENAI_API_KEY_TEST_NOT_SET', expectedKeyPrefix: 'sk-' });
assertContains(openaiErr.message, 'openai-api', 'provider-tag');
assertContains(openaiErr.message, 'HTTP 401', 'http-status');
assertContains(openaiErr.message, 'Incorrect API key provided', 'api-message');
process.env.OPENAI_API_KEY_TEST_WRONG_PREFIX = 'gqwen-fake-key-1234';
const wrongPrefix = brain.formatProviderError('openai-api', 'gpt-test', {
    status: 401,
    error: { message: 'bad key' }
}, { keyEnvVar: 'OPENAI_API_KEY_TEST_WRONG_PREFIX', expectedKeyPrefix: 'sk-' });
assertContains(wrongPrefix.message, 'key prefix:', 'key-prefix-warning');
assertContains(wrongPrefix.message, 'gqwen-fa', 'key-prefix-value');
delete process.env.OPENAI_API_KEY_TEST_WRONG_PREFIX;
process.env.OPENAI_API_KEY_TEST_OK_PREFIX = 'sk-proj-real-looking-key';
const okPrefix = brain.formatProviderError('openai-api', 'gpt-test', {
    status: 401, error: { message: 'rate limited' }
}, { keyEnvVar: 'OPENAI_API_KEY_TEST_OK_PREFIX', expectedKeyPrefix: 'sk-' });
if (okPrefix.message.includes('key prefix:'))
    throw new Error('SHOULD_NOT_WARN_ON_CORRECT_PREFIX');
delete process.env.OPENAI_API_KEY_TEST_OK_PREFIX;
const ollamaErr = brain.formatProviderError('ollama', 'llama3', {
    status: 503,
    message: 'Service Unavailable',
    error: { message: 'model not loaded' }
});
assertContains(ollamaErr.message, 'HTTP 503', 'ollama-status');
assertContains(ollamaErr.message, 'model not loaded', 'ollama-body');
const cliErr = brain.formatProviderError('openai-cli', 'codex-default', {
    stderr: 'codex: command not found\n',
    message: 'spawn codex ENOENT'
});
assertContains(cliErr.message, 'stderr:', 'cli-stderr');
assertContains(cliErr.message, 'codex: command not found', 'cli-stderr-content');
const genericErr = brain.formatProviderError('gemini-api', 'gemini-3.1', { message: 'Connection error.' });
assertContains(genericErr.message, 'gemini-api', 'generic-provider');
assertContains(genericErr.message, 'Connection error.', 'generic-message');
const causedBy = brain.formatProviderError('openai-api', 'gpt-x', { status: 500, message: 'boom' });
const cause = causedBy.cause;
if (!cause || cause.status !== 500)
    throw new Error('CAUSE_NOT_PRESERVED');
console.log('TEST_BRAIN_ERROR_DIAGNOSTICS_OK');
