Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

11.3 KB
Raw Blame
// Mock environment constants first, before any imports
const mockIsCLINoGui = jest.fn()
jest.mock('../constants/environment', () => ({
  get isCLINoGui() {
    return mockIsCLINoGui()
  }
}))

// Mock paths to avoid XDG_CONFIG_HOME issues
jest.mock('../constants/paths', () => ({
  windowIcon: 'mock-icon-path',
  appFolder: '/mock/app/folder',
  userHome: '/mock/home'
}))

// Mock GlobalConfig so we can toggle hideWindowOnProtocolLaunch per test
const mockHideWindowOnProtocolLaunch = jest.fn(() => false)
jest.mock('../config', () => ({
  GlobalConfig: {
    get: () => ({
      getSettings: () => ({
        hideWindowOnProtocolLaunch: mockHideWindowOnProtocolLaunch()
      })
    })
  }
}))

import {
  handleProtocol,
  isGameLaunchArgs,
  shouldHideWindowForProtocolArgs
} from '../protocol'
import { app, dialog } from 'electron'
import { libraryManagerMap } from '../storeManagers'
import { getMainWindow } from '../main_window'
import { sendFrontendMessage } from '../ipc'

// Mock electron modules
jest.mock('electron', () => ({
  app: {
    quit: jest.fn()
  },
  dialog: {
    showMessageBox: jest.fn()
  }
}))

// Mock other dependencies
jest.mock('../main_window', () => ({
  getMainWindow: jest.fn()
}))

jest.mock('../ipc', () => ({
  sendFrontendMessage: jest.fn()
}))

jest.mock('../storeManagers', () => ({
  libraryManagerMap: {
    legendary: {
      getGame: jest.fn()
    },
    gog: {
      getGame: jest.fn()
    },
    nile: {
      getGame: jest.fn()
    },
    zoom: {
      getGame: jest.fn()
    },
    sideload: {
      getGame: jest.fn()
    },
    steam: {
      getGame: jest.fn()
    },
    itchio: {
      getGame: jest.fn()
    }
  }
}))

jest.mock('../logger', () => ({
  logInfo: jest.fn(),
  logError: jest.fn(),
  LogPrefix: {
    ProtocolHandler: 'ProtocolHandler'
  }
}))

// Mock i18next
jest.mock('i18next', () => ({
  t: jest.fn((key: string, fallback?: string) => fallback || key)
}))

// Mock launcher
jest.mock('../launcher', () => ({
  launchEventCallback: jest.fn()
}))

// Mock additional dependencies to avoid import issues
jest.mock('../storeManagers/legendary/constants', () => ({
  legendaryConfigPath: '/mock/legendary/config',
  legendaryUserInfo: '/mock/legendary/user.json',
  legendaryInstalled: '/mock/legendary/installed.json'
}))

jest.mock('process', () => ({
  env: {
    XDG_CONFIG_HOME: '/mock/xdg/config'
  }
}))

describe('protocol.ts --no-gui behavior', () => {
  const mockMainWindow = {
    show: jest.fn(),
    hide: jest.fn(),
    isVisible: jest.fn(() => true)
  }

  const mockGameInfo = {
    app_name: 'test-game',
    title: 'Test Game',
    runner: 'legendary' as const,
    is_installed: false
  }

  const mockGameSettings = {
    ignoreGameUpdates: false
  }
  const emptyGameInfoMock = { getGameInfo: () => ({}) }

  beforeEach(() => {
    jest.clearAllMocks()
    mockHideWindowOnProtocolLaunch.mockReturnValue(false)
    mockMainWindow.isVisible.mockReturnValue(true)

    const gameMock = {
      getGameInfo: () => mockGameInfo,
      getSettings: () => mockGameSettings
    }

    ;(getMainWindow as jest.Mock).mockReturnValue(mockMainWindow)
    ;(libraryManagerMap.legendary.getGame as jest.Mock).mockReturnValue(
      gameMock
    )

    // Mock other game managers to return empty objects
    ;(libraryManagerMap.gog.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
    ;(libraryManagerMap.nile.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
    ;(libraryManagerMap.zoom.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
    ;(libraryManagerMap.sideload.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
    ;(libraryManagerMap.steam.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
    ;(libraryManagerMap.itchio.getGame as jest.Mock).mockReturnValue(
      emptyGameInfoMock
    )
  })

  describe('when game is not installed', () => {
    beforeEach(() => {
      mockGameInfo.is_installed = false
    })

    describe('with --no-gui flag', () => {
      beforeEach(() => {
        mockIsCLINoGui.mockReturnValue(true)
      })

      test('should exit app when user clicks No', async () => {
        ;(dialog.showMessageBox as jest.Mock).mockResolvedValue({ response: 1 })

        await handleProtocol(['heroic://launch/test-game'])

        expect(app.quit).toHaveBeenCalled()
      })

      test('should show GUI and install when user clicks Yes', async () => {
        ;(dialog.showMessageBox as jest.Mock).mockResolvedValue({ response: 0 })

        await handleProtocol(['heroic://launch/test-game'])

        expect(mockMainWindow.show).toHaveBeenCalled()
        expect(sendFrontendMessage).toHaveBeenCalledWith(
          'installGame',
          'test-game',
          'legendary'
        )
        expect(app.quit).not.toHaveBeenCalled()
      })
    })

    describe('without --no-gui flag', () => {
      beforeEach(() => {
        mockIsCLINoGui.mockReturnValue(false)
      })

      test('should not exit app when user clicks No', async () => {
        ;(dialog.showMessageBox as jest.Mock).mockResolvedValue({ response: 1 })

        await handleProtocol(['heroic://launch/test-game'])

        expect(app.quit).not.toHaveBeenCalled()
        expect(mockMainWindow.show).not.toHaveBeenCalled()
      })

      test('should install when user clicks Yes (normal behavior)', async () => {
        ;(dialog.showMessageBox as jest.Mock).mockResolvedValue({ response: 0 })

        await handleProtocol(['heroic://launch/test-game'])

        expect(sendFrontendMessage).toHaveBeenCalledWith(
          'installGame',
          'test-game',
          'legendary'
        )
        expect(app.quit).not.toHaveBeenCalled()
        expect(mockMainWindow.show).not.toHaveBeenCalled()
      })
    })
  })

  describe('when game is installed', () => {
    beforeEach(() => {
      mockGameInfo.is_installed = true
    })

    test('should launch game directly regardless of --no-gui flag', async () => {
      mockIsCLINoGui.mockReturnValue(true)

      // Mock launchEventCallback to avoid complex setup
      const mockLaunchEventCallback = jest.fn()
      jest.doMock('../launcher', () => ({
        launchEventCallback: mockLaunchEventCallback
      }))

      await handleProtocol(['heroic://launch/test-game'])

      // Should not show dialog for installed games
      expect(dialog.showMessageBox).not.toHaveBeenCalled()
      expect(app.quit).not.toHaveBeenCalled()
    })
  })

  describe('edge cases', () => {
    test('should handle invalid URLs gracefully', async () => {
      await handleProtocol(['not-a-heroic-url'])

      // Should not crash or call any dialog/app methods
      expect(dialog.showMessageBox).not.toHaveBeenCalled()
      expect(app.quit).not.toHaveBeenCalled()
    })

    test('should handle missing game info', async () => {
      // Mock gameManagerMap to return empty object for missing games
      ;(libraryManagerMap.legendary.getGame as jest.Mock).mockReturnValue(
        emptyGameInfoMock
      )

      await handleProtocol(['heroic://launch/nonexistent-game'])

      // Should not show dialog if game info cannot be found
      expect(dialog.showMessageBox).not.toHaveBeenCalled()
      expect(app.quit).not.toHaveBeenCalled()
    })

    test('should handle missing main window', async () => {
      ;(getMainWindow as jest.Mock).mockReturnValue(null)
      mockIsCLINoGui.mockReturnValue(true)

      await handleProtocol(['heroic://launch/test-game'])

      // Should return early if no main window available
      expect(dialog.showMessageBox).not.toHaveBeenCalled()
      expect(app.quit).not.toHaveBeenCalled()
    })
  })

  describe('hide window on protocol launch', () => {
    beforeEach(() => {
      mockIsCLINoGui.mockReturnValue(false)
      mockGameInfo.is_installed = true
    })

    test('hides window when URL carries gui=false', async () => {
      await handleProtocol([
        'heroic://launch?appName=test-game&runner=legendary&gui=false'
      ])

      expect(mockMainWindow.hide).toHaveBeenCalled()
    })

    test('hides window when hideWindowOnProtocolLaunch setting is enabled', async () => {
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)

      await handleProtocol(['heroic://launch/test-game'])

      expect(mockMainWindow.hide).toHaveBeenCalled()
    })

    test('does not hide window when neither setting nor URL param is set', async () => {
      await handleProtocol(['heroic://launch/test-game'])

      expect(mockMainWindow.hide).not.toHaveBeenCalled()
    })

    test('does not hide window that is not visible', async () => {
      mockMainWindow.isVisible.mockReturnValue(false)
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)

      await handleProtocol(['heroic://launch/test-game'])

      expect(mockMainWindow.hide).not.toHaveBeenCalled()
    })

    test('shows window when not-installed game needs install dialog, regardless of hide setting', async () => {
      mockGameInfo.is_installed = false
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)
      ;(dialog.showMessageBox as jest.Mock).mockResolvedValue({ response: 0 })

      await handleProtocol(['heroic://launch/test-game'])

      expect(mockMainWindow.show).toHaveBeenCalled()
      expect(sendFrontendMessage).toHaveBeenCalledWith(
        'installGame',
        'test-game',
        'legendary'
      )
    })
  })

  describe('shouldHideWindowForProtocolArgs', () => {
    beforeEach(() => {
      mockHideWindowOnProtocolLaunch.mockReturnValue(false)
    })

    test('returns true when URL has gui=false', () => {
      expect(
        shouldHideWindowForProtocolArgs([
          'heroic://launch?appName=foo&gui=false'
        ])
      ).toBe(true)
    })

    test('accepts gui=0 and gui=no as equivalent to false', () => {
      expect(
        shouldHideWindowForProtocolArgs(['heroic://launch?appName=foo&gui=0'])
      ).toBe(true)
      expect(
        shouldHideWindowForProtocolArgs(['heroic://launch?appName=foo&gui=no'])
      ).toBe(true)
    })

    test('returns true when setting is enabled', () => {
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)
      expect(
        shouldHideWindowForProtocolArgs(['heroic://launch/test-game'])
      ).toBe(true)
    })

    test('returns false when setting is off and URL has no gui param', () => {
      expect(
        shouldHideWindowForProtocolArgs(['heroic://launch/test-game'])
      ).toBe(false)
    })

    test('returns false for non-launch protocol URLs even with setting on', () => {
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)
      expect(shouldHideWindowForProtocolArgs(['heroic://ping'])).toBe(false)
    })

    test('returns false when no heroic URL is present', () => {
      mockHideWindowOnProtocolLaunch.mockReturnValue(true)
      expect(
        shouldHideWindowForProtocolArgs(['/path/to/heroic', '--some-flag'])
      ).toBe(false)
    })
  })

  describe('isGameLaunchArgs', () => {
    test('returns true for old-style launch URLs', () => {
      expect(isGameLaunchArgs(['heroic://launch/test-game'])).toBe(true)
    })

    test('returns true for new-style launch URLs', () => {
      expect(
        isGameLaunchArgs(['heroic://launch?appName=test-game&runner=legendary'])
      ).toBe(true)
    })

    test('returns false for non-launch protocol URLs', () => {
      expect(isGameLaunchArgs(['heroic://ping'])).toBe(false)
    })

    test('returns false when no heroic URL is present', () => {
      expect(isGameLaunchArgs(['/path/to/heroic', '--some-flag'])).toBe(false)
    })
  })
})