Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

7.8 KB
Raw Blame
import importlib.util
import os
from pathlib import Path
import subprocess
import tempfile
import unittest

ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('library', ROOT / 'scripts/game-library.py')
library = importlib.util.module_from_spec(spec)
spec.loader.exec_module(library)
CLIENT = 'drive_c/Program Files (x86)/Steam/steam.exe'


class SteamGPTKTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory(prefix="soju steam gptk ' ")
        self.addCleanup(self.temp.cleanup)
        self.base = Path(self.temp.name)

    def put(self, relative):
        path = self.base / relative
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text('')

    def gptk(self, *args):
        env = dict(os.environ, SOJU_BASE=str(self.base))
        return subprocess.run(['bash', str(ROOT / 'scripts/steam-gptk.sh'), *args], env=env,
                              capture_output=True, text=True, timeout=60)

    def test_status_is_off_until_flag_and_bottle(self):
        self.assertEqual(self.gptk('status').stdout.strip(), 'off')
        self.put('steam-gptk')
        self.assertEqual(self.gptk('status').stdout.strip(), 'off')   # no bottle yet
        self.put('steam-gptk-bottle/' + CLIENT)
        self.assertEqual(self.gptk('status').stdout.strip(), 'on')

    def test_off_waits_for_steam_open_on_the_launcher_engine(self):
        # Steam started for a Rockstar game runs on the launcher engine (#236); switching modes must still see it.
        self.put('steam-bottle/' + CLIENT)
        self.put('steam-gptk-bottle/' + CLIENT)
        self.put('steam-gptk')
        stub = self.base / 'stub-bin'
        stub.mkdir()
        (stub / 'pgrep').write_text('#!/bin/sh\ncase "$*" in *steam*exe*) exit 0 ;; esac\nexit 1\n')
        (stub / 'pgrep').chmod(0o755)
        env = dict(os.environ, SOJU_BASE=str(self.base), PATH=f'{stub}:{os.environ["PATH"]}')
        run = lambda: subprocess.run(['bash', str(ROOT / 'scripts/steam-gptk.sh'), 'off'], env=env,
                                     capture_output=True, text=True, timeout=60)
        (self.base / 'steam-gptk-bottle/.soju-steam-engine').write_text(str(self.base / 'cx26-engine-launchers') + '\n')
        result = run()
        self.assertNotEqual(result.returncode, 0)
        self.assertIn('quit Steam first', result.stderr)
        self.assertTrue((self.base / 'steam-gptk').exists())
        # Steam on the GPTK engine is found by its wineserver instead; the mark alone does not count.
        (self.base / 'steam-gptk-bottle/.soju-steam-engine').write_text(str(self.base / 'cx26-engine-steam') + '\n')
        self.assertEqual(run().returncode, 0)
        self.assertFalse((self.base / 'steam-gptk').exists())

    def test_on_needs_steam_installed(self):
        result = self.gptk('on')
        self.assertNotEqual(result.returncode, 0)
        self.assertIn('install Steam first', result.stderr)
        self.assertFalse((self.base / 'steam-gptk').exists())

    def test_library_reads_the_bottle_steam_runs_from(self):
        self.put('steam-bottle/' + CLIENT)
        self.assertEqual(library.Library(self.base).folders['steam'], 'steam-bottle')
        self.put('steam-gptk-bottle/' + CLIENT)
        self.assertEqual(library.Library(self.base).folders['steam'], 'steam-bottle')   # not switched on
        self.put('steam-gptk')
        self.assertEqual(library.Library(self.base).folders['steam'], 'steam-gptk-bottle')

    def test_gptk_games_point_to_the_steam_mode(self):
        self.put('steam-bottle/' + CLIENT)
        root = self.base / 'steam-bottle/drive_c/Program Files (x86)/Steam'
        for uid, folder in (('2357570', 'Overwatch'), ('70', 'Half-Life')):
            (root / 'steamapps/common' / folder).mkdir(parents=True)
            (root / 'steamapps' / f'appmanifest_{uid}.acf').write_text(
                f'"AppState" {{ "appid" "{uid}" "name" "{folder}" "StateFlags" "4" "installdir" "{folder}" }}')
        lib = library.Library(self.base); lib.steam()
        # No message to act on: Play itself switches Steam to the mode the game needs.
        self.assertIsNone(lib.games['steam:2357570']['issue'])
        self.assertIsNone(lib.games['steam:70']['issue'])
        self.assertEqual(lib.command('steam:2357570')[1:], [str(ROOT / 'scripts/steam-gptk.sh'), 'play', 'steam://rungameid/2357570'])
        self.assertEqual(lib.command('steam:70')[1:], [str(ROOT / 'scripts/play.sh'), 'steam', 'steam://rungameid/70'])

    def test_a_game_whose_store_page_asks_for_directx_12_goes_to_the_gptk_mode(self):
        calls = []
        def fetch(uid):
            calls.append(uid)
            dx = {'2050650': '12', '70': '9'}.get(uid)
            if dx is None: return '{"%s": {"success": false}}' % uid
            return '{"%s": {"success": true, "data": {"pc_requirements": {"minimum": "<li><strong>DirectX:</strong> Version %s<br></li>"}}}}' % (uid, dx)
        self.assertTrue(library.needs_dx12(self.base, '2050650', fetch))
        self.assertFalse(library.needs_dx12(self.base, '70', fetch))
        self.assertFalse(library.needs_dx12(self.base, '1', fetch))
        self.assertTrue(library.needs_dx12(self.base, '2050650', fetch))   # asked once, then kept
        self.assertEqual(calls.count('2050650'), 1)
        self.assertFalse(library.needs_dx12(self.base, '3', lambda uid: (_ for _ in ()).throw(OSError('offline'))))

    def test_turning_the_mode_on_brings_over_games_installed_since(self):
        self.put('steam-bottle/' + CLIENT); self.put('steam-gptk-bottle/' + CLIENT)
        apps = 'drive_c/Program Files (x86)/Steam/steamapps'
        (self.base / 'steam-bottle' / apps / 'common/Sinking Star').mkdir(parents=True)
        (self.base / 'steam-bottle' / apps / 'common/Sinking Star/game.exe').write_text('x')
        (self.base / 'steam-bottle' / apps / 'appmanifest_499170.acf').write_text(
            '"AppState"\n{\n\t"appid"\t\t"499170"\n\t"installdir"\t\t"Sinking Star"\n}\n')
        (self.base / 'steam-gptk-bottle' / apps).mkdir(parents=True, exist_ok=True)
        text = (ROOT / 'scripts/steam-gptk.sh').read_text()
        body = text.split('sync_games(){', 1)[1].split('\n}\n', 1)[0]
        script = 'PLAIN=%r; GPTK=%r\nsync_games(){%s\n}\nsync_games\n' % (str(self.base / 'steam-bottle'), str(self.base / 'steam-gptk-bottle'), body)
        result = subprocess.run(['/bin/bash', '-c', script], capture_output=True, text=True)
        self.assertEqual(result.returncode, 0, result.stderr)
        self.assertTrue((self.base / 'steam-gptk-bottle' / apps / 'appmanifest_499170.acf').is_file())
        self.assertTrue((self.base / 'steam-gptk-bottle' / apps / 'common/Sinking Star/game.exe').is_file())

    def test_play_sh_picks_the_gptk_bottle(self):
        text = (ROOT / 'scripts/play.sh').read_text()
        self.assertIn('steam-gptk.sh" status', text)
        self.assertIn('steam-gptk-engine.sh', text)
        # The GPTK branch must keep the webhelper wrapper: without it Steam's window is black.
        branch = text.split('SOJU_STEAM_GPTK:-0}" = 1 ]; then', 1)[1].split('WINESTABLE=', 1)[0]
        self.assertIn('steamwebhelper-wrapper.exe', branch)
        self.assertIn('-no-cef-sandbox', branch)

    def test_app_action_is_wired(self):
        # The app runs `steam-gptk on|off` through desktop-action.sh, not the soju command.
        self.assertIn('"steam-gptk", on ? "on" : "off"', (ROOT / 'app/Accounts.swift').read_text())
        self.assertIn('steam-gptk) exec bash "$ROOT/scripts/steam-gptk.sh"', (ROOT / 'scripts/desktop-action.sh').read_text())

    def test_engine_keeps_apple_shims(self):
        text = (ROOT / 'scripts/steam-gptk-engine.sh').read_text()
        self.assertIn('winemac.so', text)
        for swapped in ('d3d11.dll', 'dxgi.dll', 'dxmt'):
            self.assertNotIn(swapped, text.split('cp -Rc', 1)[1])


if __name__ == '__main__':
    unittest.main()