Skip to content

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

5.6 KB
Raw Blame
import { mkdirSync, mkdtempSync, writeFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import {
  findBatchLauncher,
  parseBatchLauncher,
  resolveCaseInsensitivePath
} from '../install'

describe('findBatchLauncher', () => {
  let dir: string

  beforeEach(() => {
    dir = mkdtempSync(join(tmpdir(), 'zoom-launcher-'))
  })

  afterEach(() => {
    rmSync(dir, { recursive: true, force: true })
  })

  it('prefers the launch batch file over other batches and exes', () => {
    writeFileSync(join(dir, 'setup.bat'), '')
    writeFileSync(join(dir, 'Launch The Colony (DOS).bat'), '')
    writeFileSync(join(dir, 'unins000.bat'), '')
    writeFileSync(join(dir, 'scummvm.exe'), '')

    expect(findBatchLauncher(dir)).toBe('Launch The Colony (DOS).bat')
  })

  it('accepts play and start batches', () => {
    writeFileSync(join(dir, 'Play Game.bat'), '')

    expect(findBatchLauncher(dir)).toBe('Play Game.bat')

    rmSync(join(dir, 'Play Game.bat'))
    writeFileSync(join(dir, 'Start Game.bat'), '')

    expect(findBatchLauncher(dir)).toBe('Start Game.bat')
  })

  it('ignores batches that only configure or register the game', () => {
    writeFileSync(join(dir, 'config.bat'), '')
    writeFileSync(join(dir, 'register.bat'), '')

    expect(findBatchLauncher(dir)).toBeUndefined()
  })

  it('accepts any batch name when the package bundles scummvm.exe', () => {
    writeFileSync(join(dir, 'The Colony (DOS).bat'), '')
    writeFileSync(join(dir, 'scummvm.exe'), '')

    expect(findBatchLauncher(dir)).toBe('The Colony (DOS).bat')
  })

  it('detects scummvm.exe inside a subfolder', () => {
    mkdirSync(join(dir, 'scummvm'))
    writeFileSync(join(dir, 'scummvm', 'scummvm.exe'), '')
    writeFileSync(join(dir, 'Run Game.bat'), '')

    expect(findBatchLauncher(dir)).toBe('Run Game.bat')
  })

  it('still refuses an unnamed batch when scummvm.exe is absent', () => {
    writeFileSync(join(dir, 'Game.bat'), '')

    expect(findBatchLauncher(dir)).toBeUndefined()
  })

  it('ignores setup and uninstall batches', () => {
    writeFileSync(join(dir, 'setup.bat'), '')
    writeFileSync(join(dir, 'unins000.bat'), '')

    expect(findBatchLauncher(dir)).toBeUndefined()
  })

  it('returns undefined when there is no batch file', () => {
    writeFileSync(join(dir, 'game.exe'), '')

    expect(findBatchLauncher(dir)).toBeUndefined()
  })

  it('ignores batch files inside subfolders', () => {
    mkdirSync(join(dir, 'ScummVM'))
    writeFileSync(join(dir, 'ScummVM', 'Launch Game.bat'), '')

    expect(findBatchLauncher(dir)).toBeUndefined()
  })
})

describe('parseBatchLauncher', () => {
  it('extracts the target and arguments from a Zoom launcher', () => {
    const line =
      'start .\\ScummVM\\scummvm.exe --zoom-splash=black --no-console -c "thecolony.ini" colony'

    expect(parseBatchLauncher(line)).toEqual({
      executable: 'ScummVM/scummvm.exe',
      args: [
        '--zoom-splash=black',
        '--no-console',
        '-c',
        'thecolony.ini',
        'colony'
      ]
    })
  })

  it('handles a quoted executable with spaces', () => {
    const line = 'start "" ".\\Game Files\\My Game.exe" -windowed'

    expect(parseBatchLauncher(line)).toEqual({
      executable: 'Game Files/My Game.exe',
      args: ['-windowed']
    })
  })

  it('returns undefined when there is no executable', () => {
    expect(parseBatchLauncher('echo hello')).toBeUndefined()
  })

  it('skips checks and only parses the launch line', () => {
    const script = [
      '@echo off',
      'if not exist "ScummVM\\scummvm.exe" goto missing',
      'cd /d "%~dp0"',
      'start "" "ScummVM\\scummvm.exe" -c "game.ini" gameid',
      'exit',
      ':missing'
    ].join('\r\n')

    expect(parseBatchLauncher(script)).toEqual({
      executable: 'ScummVM/scummvm.exe',
      args: ['-c', 'game.ini', 'gameid']
    })
  })

  it('refuses arguments with unresolved variables so cmd can run the batch', () => {
    expect(
      parseBatchLauncher('scummvm.exe -c "%GAME%.ini" gameid')
    ).toBeUndefined()
  })

  it('parses a quoted executable without a start prefix', () => {
    expect(parseBatchLauncher('"ScummVM\\scummvm.exe" --no-console')).toEqual({
      executable: 'ScummVM/scummvm.exe',
      args: ['--no-console']
    })
  })

  it('refuses drive-absolute and UNC targets so Wine runs the batch', () => {
    expect(parseBatchLauncher('start C:\\Game\\game.exe -x')).toBeUndefined()
    expect(parseBatchLauncher('"D:/Games/game.exe" -x')).toBeUndefined()
    expect(parseBatchLauncher('\\\\server\\share\\game.exe -x')).toBeUndefined()
  })
})

describe('resolveCaseInsensitivePath', () => {
  let dir: string

  beforeEach(() => {
    dir = mkdtempSync(join(tmpdir(), 'zoom-case-'))
    mkdirSync(join(dir, 'scummvm'))
    writeFileSync(join(dir, 'scummvm', 'scummvm.exe'), '')
    writeFileSync(join(dir, 'thecolony.ini'), '')
  })

  afterEach(() => {
    rmSync(dir, { recursive: true, force: true })
  })

  it('resolves a launcher path whose case differs from the archive', () => {
    expect(resolveCaseInsensitivePath(dir, 'ScummVM/scummvm.exe')).toBe(
      'scummvm/scummvm.exe'
    )
  })

  it('resolves backslash-separated Windows paths', () => {
    expect(resolveCaseInsensitivePath(dir, 'ScummVM\\scummvm.exe')).toBe(
      'scummvm/scummvm.exe'
    )
  })

  it('returns undefined when a segment is missing', () => {
    expect(
      resolveCaseInsensitivePath(dir, 'ScummVM/missing.exe')
    ).toBeUndefined()
    expect(resolveCaseInsensitivePath(dir, 'Nope/scummvm.exe')).toBeUndefined()
  })

  it('returns undefined for an empty path', () => {
    expect(resolveCaseInsensitivePath(dir, '')).toBeUndefined()
  })
})