import os from 'os';
import path from 'path';
import { loadConfig } from './src/utils/config.js';

let passed = 0;
let failed = 0;

function test(name, fn) {
  return Promise.resolve()
    .then(fn)
    .then(() => {
      console.log(`  ✓ ${name}`);
      passed++;
    })
    .catch((err) => {
      console.log(`  ✗ ${name}: ${err.message}`);
      failed++;
    });
}

function assertEqual(actual, expected, msg) {
  if (actual !== expected) {
    throw new Error(msg || `Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
  }
}

function assert(condition, msg) {
  if (!condition) throw new Error(msg || 'Assertion failed');
}

console.log('\n── Empty Window Config ──');

await test('web mode without explicit --dir starts in no-workspace mode', async () => {
  const config = loadConfig(['--web']);
  assertEqual(config.noWorkspace, true);
  assert(config.workDir.endsWith(path.join('.vl-code', 'empty-window')));
});

await test('explicit --dir disables no-workspace mode', async () => {
  const customDir = path.join(os.tmpdir(), 'vlcode-lite-config-test');
  const config = loadConfig(['--web', '--dir', customDir]);
  assertEqual(config.noWorkspace, false);
  assertEqual(config.workDir, customDir);
});

console.log(`\n── Results ──\n\n  ${passed} passed, ${failed} failed\n`);
process.exit(failed > 0 ? 1 : 0);
