import { test, expect } from 'vitest';
import { DIContainer } from '@apps/companion/main/di-container';

test('renderer -> preload -> main: set/get updates BrowserModeConfigLoader (reproduces headless-toggle bug)', () => {
  // Ensure environment is development so toggle is available
  (process.env as any).NODE_ENV = 'development';

  // Provide a minimal electron.app mock so DIContainer can resolve paths in node test environment
  // This avoids calling the real Electron runtime during unit/runner tests.
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const electron = require('electron');
    electron.app = electron.app || {};
    electron.app.getAppPath = electron.app.getAppPath || (() => process.cwd());
    electron.app.isPackaged = electron.app.isPackaged || false;
    electron.app.getPath = electron.app.getPath || ((p: string) => process.cwd());
  } catch {
    // If require('electron') fails, ignore; DIContainer will still attempt to access app and may error.
  }

  // Reset and get fresh DI container for test isolation
  DIContainer.resetInstance();
  const container = DIContainer.getInstance();
  const loader = container.getBrowserModeConfigLoader();

  // Sanity: toggle visible and default is 'headed' in development
  expect(process.env.NODE_ENV).toBe('development');
  expect(loader.getDevelopmentMode()).toBe('headed');

  // Simulate renderer setting to 'headless' via IPC (which should call loader.setDevelopmentMode)
  loader.setDevelopmentMode('headless');

  // After setting, the loader must reflect new value
  expect(loader.getDevelopmentMode()).toBe('headless');

  // loader.load() should report the GUI source in development and the updated mode
  const config = loader.load();
  expect(config.mode).toBe('headless');
  expect(config.source).toBe('GUI');
});