Files
DroidDeck/tools/tests/test_curseforge_helper.py
T
MaxsTechReview 210c127446 feat(addons): add a CurseForge source to the addon browser (#3028)
* feat(addons): add a CurseForge source to the addon browser

Search and install CurseForge addons from the Download addons window by
driving the installed CurseForge app in the background: it starts minimized
on the session's X display with its inspector bound to 127.0.0.1, and the
window's own renderer services handle game detection, search, install and
shutdown. Sign-in stays inside the CurseForge app.

The Install buttons in the addon list now size to their label, so the label
is centered and the button matches the other compact buttons.

* fix(curseforge): secret inspector URL, hold mode and serialised starts

- Start CurseForge with its Node inspector on 127.0.0.1:0 and publish only
  the UUID websocket URL, kept in the 0700 state directory, so no fixed
  unauthenticated debug port is exposed.
- The agent evaluates through that URL instead of a fixed port.
- Add a hold mode that runs as a session program and stops the app on
  exit, and a lock so hold and start never launch twice.

* feat(addons): run CurseForge in a hidden session when none is running

- Opening the CurseForge source with no session starts one headlessly
  (offscreen compositor output, shared start path with SessionActivity via
  CompositorHost) running the CurseForge hold helper; leaving the dialog
  stops the app and that session. A session the user already had is
  reused and left running.
- Drop the "start a session" notice from every locale.
- Addon browser link: cleanup shares the link's lock off the UI thread,
  stops the app at once on close, refuses work after close and stops a
  launch that finishes after close.
- CurseForgeApp.start stops the app before every failed return.
- instanceFor returns no instance when the populated list has no match.

* Harden headless CurseForge session and helper lifecycle

Scope the hidden session to its id, roll back partial starts, release the
offscreen reader once the session ends, re-acquire the inspector when its
endpoint goes away, bound DevTools calls by one deadline, key the installed
snapshot by target, track the helper launch by pid start time and directory,
fail on lock errors, and update the HDR gate test to its new location.

* Map DevTools send failures and recheck the hidden session id

* fix(addons): bind the hidden session to the id it claims at launch

* Keep the CurseForge helper quiet when no launch is recorded
2026-11-22 17:02:35 -05:00

174 lines
7.4 KiB
Python

import os
import shlex
import shutil
import signal
import subprocess
import tempfile
import time
import unittest
from pathlib import Path
HELPER = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-curseforge"
URL = "ws://127.0.0.1:43117/0f8fad5b-d9cb-469f-a165-70867728950e"
class CurseForgeHelperTest(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.tmp, True)
self.proc = self.tmp / "proc"
self.proc.mkdir()
self.dir = self.tmp / "curseforge"
(self.dir / "app").mkdir(parents=True)
self.state = self.tmp / "state"
self.runner = self.tmp / "runner"
self.seen = self.tmp / "seen"
self.runner.write_text(
"#!/bin/bash\n"
"printf '%%s\\n' \"DISPLAY=$DISPLAY\" \"XAUTHORITY=${XAUTHORITY-}\" \"WAYLAND=${WAYLAND_DISPLAY-}\" "
"\"DBUS=${DBUS_SESSION_BUS_ADDRESS-}\" \"$@\" > %s\n"
"mkdir -p %s/$$ && printf '%%s\\0' bash \"$0\" \"$@\" > %s/$$/cmdline\n"
"echo 'Debugger listening on %s'\n"
"echo 'For help, see: https://nodejs.org/en/docs/inspector'\n"
"exec sleep 60\n"
% (shlex.quote(str(self.seen)), shlex.quote(str(self.proc)), shlex.quote(str(self.proc)), URL))
self.runner.chmod(0o755)
def fake(self, pid, *argv):
d = self.proc / str(pid)
d.mkdir()
(d / "cmdline").write_bytes(b"".join(a.encode() + b"\0" for a in argv))
def env(self, extra=None):
base = {k: v for k, v in os.environ.items() if k not in ("DISPLAY", "XAUTHORITY")}
base.update(DD_PROC=str(self.proc), DD_CURSEFORGE_STATE=str(self.state), DD_APPIMAGE_RUN=str(self.runner),
DD_CURSEFORGE_WAIT="10", WAYLAND_DISPLAY="wayland-0", DBUS_SESSION_BUS_ADDRESS="unix:path=/x")
base.update(extra or {})
return base
def run_helper(self, *args, env=None):
return subprocess.run(["bash", str(HELPER), *args], env=self.env(env), capture_output=True, text=True, timeout=60)
def stop(self):
self.run_helper("stop", str(self.dir))
def pid(self):
return int((self.state / "pid").read_text().split()[0])
def gone(self, pid):
for _ in range(50):
try:
os.kill(pid, 0)
except ProcessLookupError:
return True
time.sleep(0.1)
return False
def test_the_desktop_x_display_is_found_from_xwayland(self):
self.fake(40, "/usr/bin/kwin_wayland", ":9")
self.fake(41, "/usr/bin/Xwayland", ":0", "-rootless", "-auth", "/run/droiddeck/xauth_abc", "-listenfd", "5")
out = self.run_helper("display", "")
self.assertEqual(0, out.returncode, out.stderr)
self.assertEqual([":0", "/run/droiddeck/xauth_abc"], out.stdout.splitlines())
def test_the_session_display_wins(self):
self.fake(41, "/usr/bin/Xwayland", ":3")
out = self.run_helper("display", "", env={"DISPLAY": ":0"})
self.assertEqual(":0", out.stdout.splitlines()[0])
def test_no_display_is_reported(self):
out = self.run_helper("start", str(self.dir))
self.assertEqual(4, out.returncode)
def test_a_running_instance_without_the_inspector_is_busy(self):
self.fake(50, "/usr/bin/FEX", str(self.dir) + "/app/curseforge", "--no-sandbox")
out = self.run_helper("start", str(self.dir), env={"DISPLAY": ":0"})
self.assertEqual(3, out.returncode)
self.assertEqual("busy", out.stdout.strip())
self.stop()
self.assertTrue((self.proc / "50").exists())
def test_start_uses_a_random_port_and_a_secret_inspector_url_kept_private(self):
self.fake(41, "/usr/bin/Xwayland", ":0", "-auth", "/run/droiddeck/xauth_abc")
out = self.run_helper("start", str(self.dir))
self.addCleanup(self.stop)
self.assertEqual(0, out.returncode, out.stderr)
self.assertEqual(URL, out.stdout.strip())
seen = self.seen.read_text().splitlines()
self.assertEqual(["DISPLAY=:0", "XAUTHORITY=/run/droiddeck/xauth_abc", "WAYLAND=", "DBUS=", str(self.dir),
"--minimized", "--inspect=127.0.0.1:0", "--inspect-publish-uid=stderr"], seen)
self.assertEqual(0o700, self.state.stat().st_mode & 0o777)
self.assertEqual(0o600, (self.state / "log").stat().st_mode & 0o777)
again = self.run_helper("start", str(self.dir))
self.assertEqual(URL, again.stdout.strip())
pid = self.pid()
self.stop()
self.assertFalse((self.state / "pid").exists())
self.assertTrue(self.gone(pid), "the started instance is still running")
def test_hold_keeps_the_app_until_it_is_signalled(self):
held = subprocess.Popen(["bash", str(HELPER), "hold", str(self.dir)], env=self.env({"DISPLAY": ":0"}),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
self.addCleanup(self.end, held)
for _ in range(100):
if (self.state / "pid").exists() and self.seen.exists():
break
time.sleep(0.1)
pid = self.pid()
time.sleep(1)
self.assertIsNone(held.poll())
held.send_signal(signal.SIGTERM)
self.assertEqual(0, held.wait(timeout=30))
self.assertTrue(self.gone(pid), "hold left the app running")
self.assertFalse((self.state / "pid").exists())
def end(self, held):
if held.poll() is None:
held.send_signal(signal.SIGTERM)
try:
held.wait(timeout=15)
except subprocess.TimeoutExpired:
held.kill()
held.wait()
record = self.state / "pid"
if record.exists():
try:
os.killpg(self.pid(), signal.SIGKILL)
except (ProcessLookupError, PermissionError):
pass
def test_a_launch_for_another_directory_is_busy(self):
other = self.tmp / "other"
(other / "app").mkdir(parents=True)
first = self.run_helper("start", str(self.dir), env={"DISPLAY": ":0"})
self.addCleanup(self.stop)
self.assertEqual(0, first.returncode, first.stderr)
out = self.run_helper("start", str(other), env={"DISPLAY": ":0"})
self.assertEqual(3, out.returncode)
self.assertEqual("busy", out.stdout.strip())
pid = self.pid()
self.run_helper("stop", str(other))
self.assertEqual(pid, self.pid())
os.kill(pid, 0)
def test_hold_ends_when_the_app_exits(self):
proc = shlex.quote(str(self.proc))
self.runner.write_text("#!/bin/bash\nmkdir -p %s/$$ && printf '%%s\\0' bash \"$0\" \"$@\" > %s/$$/cmdline\n"
"echo 'Debugger listening on %s'\nsleep 2\nrm -r %s/$$\n" % (proc, proc, URL, proc))
out = subprocess.run(["bash", str(HELPER), "hold", str(self.dir)], env=self.env({"DISPLAY": ":0"}),
capture_output=True, text=True, timeout=60)
self.assertEqual(0, out.returncode, out.stderr)
def test_stop_without_a_launch_is_quiet(self):
out = self.run_helper("stop", str(self.dir))
self.assertEqual(0, out.returncode)
self.assertEqual("", out.stderr)
def test_bad_arguments_are_refused(self):
self.assertEqual(2, self.run_helper("start", str(self.tmp / "missing")).returncode)
self.assertEqual(2, self.run_helper("launch", str(self.dir)).returncode)
if __name__ == "__main__":
unittest.main()