| 1 | """Paket, Start-Skript, Menü-Eintrag und Stockfish-Download (ohne Netz).""" |
| 2 | |
| 3 | import glob |
| 4 | import io |
| 5 | import os |
| 6 | import tarfile |
| 7 | import tempfile |
| 8 | import tomllib |
| 9 | import unittest |
| 10 | import zipfile |
| 11 | from unittest import mock |
| 12 | |
| 13 | from schach import desktop, stockfish |
| 14 | |
| 15 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 16 | |
| 17 | |
| 18 | class PyprojectTest(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | with open(os.path.join(ROOT, "pyproject.toml"), "rb") as fh: |
| 21 | self.project = tomllib.load(fh) |
| 22 | |
| 23 | def test_all_data_files_are_packaged(self): |
| 24 | """Jede Nicht-Python-Datei unter schach/ muss ein package-data-Muster treffen.""" |
| 25 | package = os.path.join(ROOT, "schach") |
| 26 | patterns = self.project["tool"]["setuptools"]["package-data"]["schach"] |
| 27 | packaged = set() |
| 28 | for pattern in patterns: |
| 29 | packaged |= {os.path.normpath(p) for p in glob.glob(os.path.join(package, pattern), recursive=True) |
| 30 | if os.path.isfile(p)} |
| 31 | missing = [] |
| 32 | for folder, dirs, files in os.walk(package): |
| 33 | dirs[:] = [d for d in dirs if d != "__pycache__"] |
| 34 | missing += [os.path.relpath(os.path.join(folder, f), package) for f in files |
| 35 | if not f.endswith(".py") and os.path.normpath(os.path.join(folder, f)) not in packaged] |
| 36 | self.assertEqual(missing, []) |
| 37 | |
| 38 | def test_gui_script_points_to_main(self): |
| 39 | target = self.project["project"]["gui-scripts"]["schach"] |
| 40 | module, func = target.split(":") |
| 41 | self.assertEqual((module, func), ("schach.__main__", "main")) |
| 42 | from schach import __main__ |
| 43 | self.assertTrue(callable(__main__.main)) |
| 44 | |
| 45 | def test_webgpu_is_a_dependency(self): |
| 46 | deps = " ".join(self.project["project"]["dependencies"]) |
| 47 | self.assertIn("onnxruntime>=", deps) |
| 48 | self.assertIn("onnxruntime-ep-webgpu", deps) |
| 49 | |
| 50 | |
| 51 | class StockfishTest(unittest.TestCase): |
| 52 | def test_build_per_platform(self): |
| 53 | self.assertEqual(stockfish.build("linux", "x86_64")[0], "stockfish-linux-x86-64-universal.tar.gz") |
| 54 | self.assertEqual(stockfish.build("linux", "arm64")[0], "stockfish-linux-arm64-universal.tar.gz") |
| 55 | self.assertEqual(stockfish.build("win32", "AMD64")[0], "stockfish-windows-x86-64-universal.zip") |
| 56 | self.assertEqual(stockfish.build("darwin", "arm64")[0], "stockfish-macos-universal.tar.gz") |
| 57 | self.assertIsNone(stockfish.build("linux", "mips")) |
| 58 | |
| 59 | def test_checksums_look_like_sha256(self): |
| 60 | for archive, sha in stockfish.BUILDS.values(): |
| 61 | self.assertRegex(sha, r"^[0-9a-f]{64}$", archive) |
| 62 | |
| 63 | def test_binary_path(self): |
| 64 | self.assertTrue(stockfish.binary_path("stockfish-windows-x86-64-universal.zip", "/c") |
| 65 | .endswith(os.path.join("stockfish-sf_19", "stockfish-windows-x86-64-universal.exe"))) |
| 66 | |
| 67 | def test_env_wins(self): |
| 68 | with mock.patch.dict(os.environ, {"SCHACH_STOCKFISH": "/opt/sf"}): |
| 69 | self.assertEqual(stockfish.command(), (["/opt/sf"], None)) |
| 70 | |
| 71 | def test_without_system_stockfish_it_prepares(self): |
| 72 | with mock.patch.dict(os.environ, {}, clear=False), mock.patch("shutil.which", return_value=None): |
| 73 | os.environ.pop("SCHACH_STOCKFISH", None) |
| 74 | self.assertEqual(stockfish.command(), ([], stockfish.prepare)) |
| 75 | |
| 76 | def _archive(self, folder, kind, name): |
| 77 | body, licence = b"#!/bin/sh\necho stockfish\n", b"GPL" |
| 78 | path = os.path.join(folder, "sf." + kind) |
| 79 | if kind == "zip": |
| 80 | with zipfile.ZipFile(path, "w") as zf: |
| 81 | zf.writestr(f"stockfish/{name}", body) |
| 82 | zf.writestr("stockfish/Copying.txt", licence) |
| 83 | zf.writestr("stockfish/wiki/Home.md", b"x") |
| 84 | else: |
| 85 | with tarfile.open(path, "w:gz") as tf: |
| 86 | for member, data in ((f"stockfish/{name}", body), ("stockfish/Copying.txt", licence), |
| 87 | ("../boese", b"x")): |
| 88 | info = tarfile.TarInfo(member) |
| 89 | info.size = len(data) |
| 90 | tf.addfile(info, io.BytesIO(data)) |
| 91 | return path |
| 92 | |
| 93 | def test_extract_only_program_and_licence(self): |
| 94 | for kind in ("tar.gz", "zip"): |
| 95 | with self.subTest(kind=kind), tempfile.TemporaryDirectory() as tmp: |
| 96 | out = os.path.join(tmp, "out") |
| 97 | os.makedirs(out) |
| 98 | stockfish.extract(self._archive(tmp, kind, "sf-bin"), out, "sf-bin") |
| 99 | self.assertEqual(sorted(os.listdir(out)), ["COPYING.txt", "sf-bin"]) |
| 100 | self.assertTrue(os.access(os.path.join(out, "sf-bin"), os.X_OK)) |
| 101 | self.assertFalse(os.path.exists(os.path.join(tmp, "boese"))) |
| 102 | |
| 103 | def test_prepare_downloads_once(self): |
| 104 | with tempfile.TemporaryDirectory() as tmp: |
| 105 | archive, _ = stockfish.build("linux", "x86_64") |
| 106 | name = archive.removesuffix(".tar.gz") |
| 107 | |
| 108 | def fake_download(url, target, sha256, progress=None, what=""): |
| 109 | self.assertTrue(url.endswith(archive)) |
| 110 | os.makedirs(os.path.dirname(target), exist_ok=True) # wie das echte download() |
| 111 | os.replace(self._archive(tmp, "tar.gz", name), target) |
| 112 | |
| 113 | with mock.patch.object(stockfish, "build", return_value=(archive, "0" * 64)), \ |
| 114 | mock.patch.object(stockfish, "download", side_effect=fake_download) as dl: |
| 115 | first = stockfish.prepare(base=tmp) |
| 116 | second = stockfish.prepare(base=tmp) |
| 117 | self.assertEqual(first, second) |
| 118 | self.assertEqual(dl.call_count, 1) |
| 119 | self.assertTrue(os.path.isfile(first[0])) |
| 120 | self.assertFalse(os.path.exists(os.path.join(os.path.dirname(first[0]), archive))) |
| 121 | |
| 122 | def test_unknown_platform_explains(self): |
| 123 | with mock.patch.object(stockfish, "build", return_value=None): |
| 124 | with self.assertRaisesRegex(FileNotFoundError, "Stockfish installieren"): |
| 125 | stockfish.prepare() |
| 126 | |
| 127 | |
| 128 | class DesktopTest(unittest.TestCase): |
| 129 | def test_script_is_used_with_full_path(self): |
| 130 | with tempfile.TemporaryDirectory() as tmp: |
| 131 | script = os.path.join(tmp, "schach") |
| 132 | with open(script, "w") as fh: |
| 133 | fh.write("#!/bin/sh\n") |
| 134 | os.chmod(script, 0o755) |
| 135 | self.assertEqual(desktop.launch_command(script), (script, None)) |
| 136 | |
| 137 | def test_python_m_uses_project_folder(self): |
| 138 | exec_line, workdir = desktop.launch_command("/irgendwo/schach/__main__.py") |
| 139 | self.assertTrue(exec_line.endswith(" -m schach")) |
| 140 | self.assertEqual(workdir, ROOT) |
| 141 | |
| 142 | def test_quote(self): |
| 143 | self.assertEqual(desktop._quote("/usr/bin/schach"), "/usr/bin/schach") |
| 144 | self.assertEqual(desktop._quote("/home/a b/schach"), '"/home/a b/schach"') |
| 145 | self.assertEqual(desktop._quote('/x/"$y'), '"/x/\\"\\$y"') |
| 146 | |
| 147 | def test_install_and_uninstall(self): |
| 148 | with tempfile.TemporaryDirectory() as tmp: |
| 149 | entry, icon = desktop.install(base=tmp, argv0="/irgendwo/__main__.py") |
| 150 | with open(entry, encoding="utf-8") as fh: |
| 151 | text = fh.read() |
| 152 | self.assertIn("Icon=app.schach.Schach\n", text) |
| 153 | self.assertIn("StartupWMClass=app.schach.Schach\n", text) |
| 154 | self.assertIn(f"Path={ROOT}\n", text) |
| 155 | self.assertTrue(os.path.isfile(icon)) |
| 156 | self.assertEqual(sorted(desktop.uninstall(base=tmp)), sorted([entry, icon])) |
| 157 | |
| 158 | def test_icon_is_svg(self): |
| 159 | with open(desktop.ICON, encoding="utf-8") as fh: |
| 160 | self.assertIn("<svg", fh.read(200)) |
| 161 | |
| 162 | |
| 163 | class DownloadStatusTest(unittest.TestCase): |
| 164 | def test_subtitle_lists_running_downloads(self): |
| 165 | from schach.app import SchachApp |
| 166 | |
| 167 | class Stub: |
| 168 | maia_status = "" |
| 169 | downloads = {"Maia": (78_000_000, 156_000_000), "Stockfish": (8_000_000, None)} |
| 170 | get_windows = staticmethod(list) |
| 171 | |
| 172 | stub = Stub() |
| 173 | SchachApp._show_downloads(stub) |
| 174 | self.assertEqual(stub.maia_status, |
| 175 | "Einmaliger Download: Maia 50 % · 78 von 156 MB – Stockfish 8 MB") |
| 176 | stub.downloads = {} |
| 177 | SchachApp._show_downloads(stub) |
| 178 | self.assertEqual(stub.maia_status, "Maia wird geladen …") |
| 179 | |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | unittest.main() |