import * as path from 'node:path';
import {
  resolveNodeRuntimeCommand,
} from '../src/modules/netaclaw/service/skill_executor';

describe('resolveNodeRuntimeCommand', () => {
  it('uses the bundled node.exe beside the installed Windows app in pkg mode', () => {
    const appDir = path.join('C:', 'Program Files', 'Neta');
    const bundledNode = path.join(appDir, 'node-runtime', 'node.exe');

    const cmd = resolveNodeRuntimeCommand({
      isPkg: true,
      platform: 'win32',
      execPath: path.join(appDir, 'backend.exe'),
      existsSync: p => p === bundledNode,
    });

    expect(cmd).toBe(bundledNode);
  });

  it('uses NETA_CONFIG_PATH to locate the installed app directory in pkg mode', () => {
    const appDir = path.join('D:', 'Neta');
    const bundledNode = path.join(appDir, 'node-runtime', 'node.exe');

    const cmd = resolveNodeRuntimeCommand({
      isPkg: true,
      platform: 'win32',
      configPath: path.join(appDir, 'config.yaml'),
      execPath: path.join('C:', 'temp', 'backend.exe'),
      existsSync: p => p === bundledNode,
    });

    expect(cmd).toBe(bundledNode);
  });

  it('throws a clear error when bundled node.exe is missing in Windows pkg mode', () => {
    const appDir = path.join('C:', 'Program Files', 'Neta');
    const bundledNode = path.join(appDir, 'node-runtime', 'node.exe');

    expect(() => resolveNodeRuntimeCommand({
      isPkg: true,
      platform: 'win32',
      execPath: path.join(appDir, 'backend.exe'),
      existsSync: () => false,
    })).toThrow(`Bundled Node runtime not found at ${bundledNode}`);
  });

  it('falls back to PATH node outside Windows pkg mode', () => {
    const cmd = resolveNodeRuntimeCommand({
      isPkg: false,
      platform: 'win32',
      execPath: path.join('C:', 'repo', 'node.exe'),
      existsSync: () => false,
    });

    expect(cmd).toBe('node');
  });
});
