Skip to content

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

3.0 KB
Raw Blame
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'graceful-fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { ZoomDownloadFile } from 'common/types/zoom'
import { applyLinuxGameFiles } from '../linuxInstall'

const file = (id: string, filename: string): ZoomDownloadFile => ({
  id,
  name: filename,
  filename,
  size: '1 GB',
  total_size: 1024
})

const ok = { stdout: '', stderr: '' }

describe('applyLinuxGameFiles', () => {
  it('keeps the executable found before a patch adds a second folder', async () => {
    const root = mkdtempSync(join(tmpdir(), 'zoom-linux-'))
    const installPath = join(root, 'game')
    mkdirSync(installPath)

    const run = async (gameFile: ZoomDownloadFile) => {
      if (gameFile.id === 'base') {
        mkdirSync(join(installPath, 'base'))
        writeFileSync(join(installPath, 'base', 'start.sh'), '')
      } else {
        mkdirSync(join(installPath, 'patch'))
      }
      return ok
    }

    const result = await applyLinuxGameFiles(
      [file('base', 'base.tar.xz'), file('patch', 'patch.tar.xz')],
      installPath,
      run,
      () => undefined
    )

    expect(result.executable).toBe(join(installPath, 'base', 'start.sh'))
    expect(result.error).toBeUndefined()

    rmSync(root, { recursive: true, force: true })
  })

  it('resolves start.sh for a non-tar installer instead of the download', async () => {
    const root = mkdtempSync(join(tmpdir(), 'zoom-linux-'))
    const installPath = join(root, 'game')
    mkdirSync(installPath)

    const result = await applyLinuxGameFiles(
      [file('run', 'installer.run')],
      installPath,
      async () => {
        writeFileSync(join(installPath, 'start.sh'), '')
        return ok
      },
      () => undefined
    )

    expect(result.executable).toBe(join(installPath, 'start.sh'))

    rmSync(root, { recursive: true, force: true })
  })

  it('reports no executable when no file provides a start script', async () => {
    const root = mkdtempSync(join(tmpdir(), 'zoom-linux-'))
    const installPath = join(root, 'game')
    mkdirSync(installPath)
    mkdirSync(join(installPath, 'base'))

    const result = await applyLinuxGameFiles(
      [file('base', 'base.tar.xz')],
      installPath,
      async () => ok,
      () => undefined
    )

    expect(result.executable).toBeUndefined()
    expect(result.error).toBeUndefined()

    rmSync(root, { recursive: true, force: true })
  })

  it('stops when a game file fails', async () => {
    const root = mkdtempSync(join(tmpdir(), 'zoom-linux-'))
    const installPath = join(root, 'game')
    mkdirSync(installPath)

    const second = jest.fn(async () => ok)
    const result = await applyLinuxGameFiles(
      [file('base', 'base.tar.xz'), file('patch', 'patch.tar.xz')],
      installPath,
      async (gameFile) =>
        gameFile.id === 'base'
          ? { stdout: '', stderr: '', error: 'boom' }
          : second(),
      () => undefined
    )

    expect(result.error).toBe('boom')
    expect(second).not.toHaveBeenCalled()

    rmSync(root, { recursive: true, force: true })
  })
})