import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as path from 'path';

const ORIG_ENV = { ...process.env };
const MOCK_HOME = '/home/testuser';

vi.mock('os', async () => {
  const actual = await vi.importActual<typeof import('os')>('os');
  return {
    ...actual,
    platform: () => 'linux',
    homedir: () => MOCK_HOME,
  };
});

describe('resolveProxyInstallDir()', () => {
  beforeEach(() => {
    vi.resetModules();
    delete process.env.LOCALAPPDATA;
  });

  afterEach(() => {
    process.env = { ...ORIG_ENV };
  });

  it('resolves macOS path', async () => {
    const { resolveProxyInstallDir } = await import('../../scripts/configure-mcp');
    expect(resolveProxyInstallDir('darwin')).toBe(
      path.join(MOCK_HOME, 'Library', 'Application Support', 'priority-forge')
    );
  });

  it('resolves Linux path (XDG data home)', async () => {
    const { resolveProxyInstallDir } = await import('../../scripts/configure-mcp');
    expect(resolveProxyInstallDir('linux')).toBe(
      path.join(MOCK_HOME, '.local', 'share', 'priority-forge')
    );
  });

  it('resolves WSL path same as Linux (NOT /mnt/c)', async () => {
    const { resolveProxyInstallDir } = await import('../../scripts/configure-mcp');
    expect(resolveProxyInstallDir('linux-wsl')).toBe(
      path.join(MOCK_HOME, '.local', 'share', 'priority-forge')
    );
  });

  it('resolves Windows path using %LOCALAPPDATA% when set', async () => {
    process.env.LOCALAPPDATA = 'C:\\Users\\fried\\AppData\\Local';
    vi.resetModules();
    const { resolveProxyInstallDir } = await import('../../scripts/configure-mcp');
    expect(resolveProxyInstallDir('win32')).toBe(
      path.join('C:\\Users\\fried\\AppData\\Local', 'priority-forge')
    );
  });

  it('falls back to ~/AppData/Local when %LOCALAPPDATA% is missing', async () => {
    const { resolveProxyInstallDir } = await import('../../scripts/configure-mcp');
    expect(resolveProxyInstallDir('win32')).toBe(
      path.join(MOCK_HOME, 'AppData', 'Local', 'priority-forge')
    );
  });
});
