import test from 'node:test';
import assert from 'node:assert';
import { safeImport } from '../_helpers/path.js';
import { FakeLLMClient } from '../_helpers/fakeLLMClient.js';
import { AutoReporter } from '../reporters/AutoReporter.js';

function skip(msg){ console.warn('[SKIP]', msg); }


// Auto-Reporter Configuration
const autoReporter = new AutoReporter();

test('LLMManager: retries on transient (429), not on logical (400)', async () => {
  const res = await safeImport('LLMManager');
  if (!res.ok) { skip(res.reason); return; }
  const L = res.mod;

  if (typeof L.__setClient !== 'function' || typeof L.__setRetryPolicy !== 'function') {
    skip('Missing __setClient/__setRetryPolicy hooks');
    return;
  }

  L.__setRetryPolicy({
    retries: 2,
    baseMs: 1,
    isTransient: (code) => [429,500,502,503,504].includes(code)
  });

  const fakeTransient = new FakeLLMClient({
    plan: [{err:{statusCode:429}}, {err:{statusCode:429}}, 'ok']
  });
  L.__setClient('mock', fakeTransient);
  const ok = await L.callModel({ provider:'mock', model:'fake-1', input:'X' });
  assert.ok(ok.completion, 'should succeed after retries');

  const fakeLogical = new FakeLLMClient({
    plan: [{err:{statusCode:400}}]
  });
  L.__setClient('mock', fakeLogical);
  let threw = false;
  try { await L.callModel({ provider:'mock', model:'fake-1', input:'Y' }); } catch(e){ threw = true; }
  assert.ok(threw, 'should not retry logical errors (400)');
});
