import {
  describe,
  expect,
  it
} from 'vitest';

import {
  DESKTOP_APP_DISPLAY_NAME,
  DESKTOP_EXECUTABLE_NAME,
  LEGACY_APP_REGISTRY_NAMES,
  isLegacyLinuxAutostartEntry,
  patchLinuxAutostartDesktopEntryNameField
} from './desktop-branding.rules';

describe('desktop-branding.rules', () => {
  it('exposes the Toju desktop branding constants', () => {
    expect(DESKTOP_APP_DISPLAY_NAME).toBe('Toju');
    expect(DESKTOP_EXECUTABLE_NAME).toBe('toju');
    expect(LEGACY_APP_REGISTRY_NAMES).toContain('MetoYou');
    expect(LEGACY_APP_REGISTRY_NAMES).toContain('metoyou');
  });

  it('treats legacy linux autostart entries as removable', () => {
    expect(isLegacyLinuxAutostartEntry('metoyou.desktop', 'toju')).toBe(true);
    expect(isLegacyLinuxAutostartEntry('MetoYou.desktop', 'toju')).toBe(true);
    expect(isLegacyLinuxAutostartEntry('MetoYou-1.0.0-x86_64.AppImage.desktop', 'Toju-1.1.0-x86_64.AppImage')).toBe(true);
  });

  it('keeps the current launch entry when it already uses the Toju binary', () => {
    expect(isLegacyLinuxAutostartEntry('toju.desktop', 'toju')).toBe(false);
    expect(isLegacyLinuxAutostartEntry('Toju-1.1.0-x86_64.AppImage.desktop', 'Toju-1.1.0-x86_64.AppImage')).toBe(false);
  });

  it('rewrites the desktop entry display name to Toju', () => {
    const patched = patchLinuxAutostartDesktopEntryNameField([
      '[Desktop Entry]',
      'Type=Application',
      'Name=metoyou',
      'Exec=/opt/Toju/toju --no-sandbox %U'
    ].join('\n'));

    expect(patched).toContain('Name=Toju');
    expect(patched).not.toContain('Name=metoyou');
  });
});
