#!/usr/bin/env tsx
/**
 * Test jail runtime behavior and cleanup logic
 * Run: tsx scripts/test-jail-runtime.ts
 */

import {
  detectRuntime,
  cleanupOrphans,
  RuntimeType,
} from '../src/container-runtime.js';

console.log('=== Jail Runtime Test ===\n');

// 1. Detect runtime
const runtime = detectRuntime();
console.log(`Detected runtime: ${runtime || 'none'}`);

if (runtime !== 'jail') {
  console.log('\n⚠️  Not running on FreeBSD with jail support');
  console.log('   This is expected on macOS/Linux\n');
  process.exit(0);
}

// 2. Check if we're in a jail
import { JailRuntime } from '../src/jail-runtime.js';
const jailRuntime = JailRuntime.getInstance();

if (jailRuntime.isInJail()) {
  const info = jailRuntime.getJailInfo();
  console.log('\n✅ Running inside jail:');
  console.log(`   Name: ${info?.name}`);
  console.log(`   JID: ${info?.jid}`);
  console.log(`   Hostname: ${info?.hostname}`);
  console.log(`   IP: ${info?.ip}`);
  console.log(`   Path: ${info?.path}`);
} else {
  console.log('\n✅ Running on FreeBSD host (not in jail)');
}

// 3. Test orphan cleanup
console.log('\n=== Testing Orphan Cleanup ===');
cleanupOrphans();
console.log('✅ Orphan cleanup completed\n');

// 4. Check for stale mounts
import { execSync } from 'child_process';

console.log('=== Checking for Stale Mounts ===');
try {
  const mounts = execSync('mount | grep clawdie-cp || echo "No stale mounts"', {
    encoding: 'utf-8',
  });
  console.log(mounts);
} catch {
  console.log('No stale mounts found');
}

console.log('\n✅ Test complete!');
