Skip to content

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

3.8 KB
Raw Blame
import { sendGameStatusUpdate } from 'backend/utils'
import type LogWriter from 'backend/logger/log_writer'
import type { ChildProcess } from 'child_process'
import {
  linuxRuntimeOutputHandler,
  parseLinuxRuntimeEvent,
  withLinuxRuntimeEvents
} from '../linux_runtime'

jest.mock('backend/utils', () => ({ sendGameStatusUpdate: jest.fn() }))
jest.mock('../aurelia', () => ({}))

const applied =
  '{"event":"linux_runtime","name":"Steam Linux Runtime 1.0 (scout)","runtime":"scout","runtime_app_id":1070560,"state":"applied"}'

function writer() {
  return {
    logInfo: jest.fn(async () => {}),
    logWarning: jest.fn(async () => {})
  }
}

describe('parseLinuxRuntimeEvent', () => {
  it('reads an applied event', () => {
    expect(parseLinuxRuntimeEvent(applied)).toMatchObject({
      state: 'applied',
      runtime: 'scout'
    })
  })

  it('ignores other lines', () => {
    expect(parseLinuxRuntimeEvent('{"event":"progress"}')).toBeUndefined()
    expect(parseLinuxRuntimeEvent('Running in "linux_runtime"')).toBeUndefined()
  })
})

describe('linuxRuntimeOutputHandler', () => {
  beforeEach(() => jest.mocked(sendGameStatusUpdate).mockClear())

  it('logs the runtime across split chunks', () => {
    const log = writer()
    const handle = linuxRuntimeOutputHandler('70', log as unknown as LogWriter)
    handle(applied.slice(0, 40))
    expect(log.logInfo).not.toHaveBeenCalled()
    handle(`${applied.slice(40)}\n`)
    expect(log.logInfo).toHaveBeenCalledWith(
      'Running in Steam Linux Runtime 1.0 (scout)'
    )
  })

  it('keeps stdout out of a stderr line', () => {
    const log = writer()
    const handle = linuxRuntimeOutputHandler('70', log as unknown as LogWriter)
    handle(applied.slice(0, 40), 'stderr')
    handle('game output\n', 'stdout')
    handle(`${applied.slice(40)}\n`, 'stderr')
    expect(log.logInfo).toHaveBeenCalledWith(
      'Running in Steam Linux Runtime 1.0 (scout)'
    )
  })

  it('shows a download as a pre-launch step', () => {
    const log = writer()
    const handle = linuxRuntimeOutputHandler('70', log as unknown as LogWriter)
    const name = 'Steam Linux Runtime 2.0 (soldier)'
    handle(
      [
        `{"event":"linux_runtime","state":"installing","runtime":"soldier","name":"${name}"}`,
        `{"event":"linux_runtime","state":"progress","runtime":"soldier","name":"${name}","percent":42.4}`,
        `{"event":"linux_runtime","state":"installed","runtime":"soldier","name":"${name}"}`,
        ''
      ].join('\n')
    )
    const updates = jest
      .mocked(sendGameStatusUpdate)
      .mock.calls.map(([update]) => [update.status, update.context])
    expect(updates).toEqual([
      ['redist', name],
      ['redist', `${name} · 42%`],
      ['playing', undefined]
    ])
  })

  it('warns when a download fails', () => {
    const log = writer()
    const handle = linuxRuntimeOutputHandler('70', log as unknown as LogWriter)
    handle(
      '{"event":"linux_runtime","state":"install_failed","runtime":"sniper","name":"SLR 3.0","error":"offline"}\n'
    )
    expect(log.logWarning).toHaveBeenCalledWith(
      'Could not install SLR 3.0: offline'
    )
  })
})

describe('withLinuxRuntimeEvents', () => {
  const child = {} as ChildProcess

  it('leaves other commands alone', () => {
    const options = { abortId: '70', logWriters: [] }
    expect(withLinuxRuntimeEvents(['install', '70'], options)).toBe(options)
  })

  it('follows play output and keeps the caller handler', () => {
    const log = writer()
    const onOutput = jest.fn()
    const options = withLinuxRuntimeEvents(['play', '70'], {
      abortId: '70',
      logWriters: [log as unknown as LogWriter],
      onOutput
    })
    options.onOutput?.(`${applied}\n`, child, 'stderr')
    expect(log.logInfo).toHaveBeenCalledWith(
      'Running in Steam Linux Runtime 1.0 (scout)'
    )
    expect(onOutput).toHaveBeenCalledWith(`${applied}\n`, child, 'stderr')
  })
})