'use strict';
const Loopuman = require('./index');
const { LoopumanError, LoopumanTimeoutError, TaskResult } = require('./index');
let passed = 0, failed = 0;
function assert(c, n) { if (c) { passed++; console.log(`  ✅ ${n}`); } else { failed++; console.log(`  ❌ ${n}`); } }
console.log('\n🧪 Loopuman Node.js SDK Tests\n');
console.log('Constructor:');
assert(typeof Loopuman === 'function', 'Loopuman is a constructor');
try { new Loopuman(); assert(false, 'Throws without API key'); } catch (e) { assert(e instanceof LoopumanError, 'Throws LoopumanError without API key'); assert(e.message.includes('API key required'), 'Error message mentions API key'); }
const client = new Loopuman({ apiKey: 'lp_test_key' });
assert(client.apiKey === 'lp_test_key', 'Sets API key');
assert(client.baseUrl === 'https://api.loopuman.com', 'Default base URL');
assert(client.timeout === 30000, 'Default timeout');
const custom = new Loopuman({ apiKey: 'lp_custom', baseUrl: 'https://custom.api.com/', timeout: 5000 });
assert(custom.baseUrl === 'https://custom.api.com', 'Custom base URL (trailing slash stripped)');
assert(custom.timeout === 5000, 'Custom timeout');
console.log('\nEnv var fallback:');
process.env.LOOPUMAN_API_KEY = 'lp_from_env';
const envClient = new Loopuman();
assert(envClient.apiKey === 'lp_from_env', 'Reads from LOOPUMAN_API_KEY env var');
delete process.env.LOOPUMAN_API_KEY;
console.log('\nAPI methods:');
['health','ask','createTask','bulkCreate','getBatch','getResults','waitForBatch'].forEach(m => assert(typeof client[m] === 'function', `${m}() exists`));
console.log('\nError classes:');
const err = new LoopumanError('test', 400, { detail: 'bad' });
assert(err instanceof Error, 'LoopumanError extends Error');
assert(err.name === 'LoopumanError', 'name correct');
assert(err.statusCode === 400, 'statusCode set');
assert(err.body.detail === 'bad', 'body set');
const tout = new LoopumanTimeoutError('task_123', 60);
assert(tout instanceof LoopumanError, 'TimeoutError extends LoopumanError');
assert(tout.taskId === 'task_123', 'taskId set');
assert(tout.timeoutSeconds === 60, 'timeoutSeconds set');
console.log('\nTaskResult:');
const r = new TaskResult({ task_id: 'abc', status: 'completed', response: 'Yes', worker_id: 'w1', completed_at: '2026-02-06' });
assert(r.taskId === 'abc', 'taskId mapped');
assert(r.status === 'completed', 'status mapped');
assert(r.response === 'Yes', 'response mapped');
assert(r.workerId === 'w1', 'workerId mapped');
const alt = new TaskResult({ id: 'alt', content: 'fb' });
assert(alt.taskId === 'alt', 'id fallback');
assert(alt.response === 'fb', 'content fallback');
console.log('\nExports:');
const mod = require('./index');
assert(mod === Loopuman, 'Default export');
assert(mod.Loopuman === Loopuman, 'Named: Loopuman');
assert(mod.LoopumanError === LoopumanError, 'Named: LoopumanError');
assert(mod.LoopumanTimeoutError === LoopumanTimeoutError, 'Named: LoopumanTimeoutError');
assert(mod.TaskResult === TaskResult, 'Named: TaskResult');
console.log(`\n${'─'.repeat(30)}`);
console.log(`Results: ${passed} passed, ${failed} failed`);
console.log(`${'─'.repeat(30)}\n`);
process.exit(failed > 0 ? 1 : 0);
