"""Tests for M14 game/Proton/Steam log collection.""" import os import tempfile import time import unittest from pathlib import Path from unittest import mock from rigdoctor.core import gamelogs class TailTests(unittest.TestCase): def test_tail_returns_last_bytes(self): path = Path(tempfile.mkdtemp()) / "x.log" path.write_text("A" * 100 + "TAIL") out = gamelogs._tail(path, 4) self.assertEqual(out, "TAIL") def test_tail_short_file(self): path = Path(tempfile.mkdtemp()) / "x.log" path.write_text("short") self.assertEqual(gamelogs._tail(path, 9999), "short") def test_tail_missing(self): self.assertEqual(gamelogs._tail(Path("/nope/x.log"), 10), "") class CollectTests(unittest.TestCase): def test_collect_includes_proton_and_steam(self): tmp = Path(tempfile.mkdtemp()) proton = tmp / "steam-570.log" proton.write_text("err: vkd3d device lost") console = tmp / "console-linux.txt" console.write_text("Game removed AppID 570 ... exit") with mock.patch.object(gamelogs, "_proton_logs", return_value=[proton]), \ mock.patch.object(gamelogs, "_steam_console", return_value=console): out = gamelogs.collect() self.assertIn("Proton log", out) self.assertIn("vkd3d", out) self.assertIn("Steam log", out) self.assertIn("exit", out) def test_collect_empty_when_none(self): with mock.patch.object(gamelogs, "_proton_logs", return_value=[]), \ mock.patch.object(gamelogs, "_steam_console", return_value=None): self.assertEqual(gamelogs.collect(), "") class SinceScopingTests(unittest.TestCase): def test_since_filter_keeps_window_only(self): text = ( "[2026-05-22 13:00:00] old session line\n" "[2026-05-22 13:00:01] another old line\n" "[2026-05-22 14:30:00] new session launch\n" "[2026-05-22 14:30:05] new session error\n" ) since = time.mktime(time.strptime("2026-05-22 14:00:00", "%Y-%m-%d %H:%M:%S")) out = gamelogs._since_filter(text, since) self.assertIn("new session launch", out) self.assertIn("new session error", out) self.assertNotIn("old session", out) def test_collect_skips_stale_proton_log(self): tmp = Path(tempfile.mkdtemp()) proton = tmp / "steam-9999.log" proton.write_text("stale proton output from an earlier game") old_mtime = time.time() - 3600 os.utime(proton, (old_mtime, old_mtime)) since = time.time() - 60 # session started a minute ago with mock.patch.object(gamelogs, "_proton_logs", return_value=[proton]), \ mock.patch.object(gamelogs, "_steam_console", return_value=None): self.assertEqual(gamelogs.collect(since=since), "") # stale log excluded if __name__ == "__main__": unittest.main()