"""Offline checks using temporary category files; run with unittest discovery.""" import argparse import contextlib import io import tempfile import unittest from pathlib import Path from unittest.mock import patch import games class GamesTests(unittest.TestCase): def setUp(self): temporary = tempfile.TemporaryDirectory() self.addCleanup(temporary.cleanup) self.root = Path(temporary.name) override = patch.object(games, "SRC", self.root) override.start() self.addCleanup(override.stop) self.paths = {name: self.root / f"{name}.md" for name in ("news", "action")} for name, path in self.paths.items(): path.write_text(f"# {name}\n", encoding="utf-8") self.block = " ### Test Game\n ![Game](images/game.jpg)\n * **Avis**: Très bien !\n\n" self.paths["news"].write_text("# News\nIntro\n\n" + self.block + "### Other\nNotes\n", encoding="utf-8") self.silence = contextlib.redirect_stdout(io.StringIO()) self.silence.__enter__() self.addCleanup(self.silence.__exit__, None, None, None) def move_args(self, **overrides): return argparse.Namespace(**dict(dict(game="test game", category="action", source=None, dry_run=False), **overrides)) def test_move_preserves_block_neighbors_and_intro(self): games.move(self.move_args(), self.paths) self.assertEqual(self.paths["action"].read_text(), "# action\n\n" + self.block) self.assertEqual(self.paths["news"].read_text(), "# News\nIntro\n\n### Other\nNotes\n") def test_duplicate_requires_source_and_refuses_duplicate_destination(self): self.paths["action"].write_text(self.block) before = {name: path.read_bytes() for name, path in self.paths.items()} with self.assertRaisesRegex(ValueError, "multiple categories"): games.move(self.move_args(), self.paths) with self.assertRaisesRegex(ValueError, "already contains"): games.move(self.move_args(source="news"), self.paths) self.assertEqual(before, {name: path.read_bytes() for name, path in self.paths.items()}) def test_move_dry_run_changes_nothing(self): before = {name: path.read_bytes() for name, path in self.paths.items()} games.move(self.move_args(dry_run=True), self.paths) self.assertEqual(before, {name: path.read_bytes() for name, path in self.paths.items()}) def test_steam_inputs_and_ambiguous_search(self): self.assertEqual(games.app_id("https://store.steampowered.com/app/620/Portal_2/?l=french"), "620") self.assertEqual(games.app_id("620"), "620") with patch.object(games, "api", return_value={"items": [{"id": 620, "name": "Portal 2"}]}): self.assertEqual(games.app_id("portal 2"), "620") with self.assertRaisesRegex(ValueError, "No unique exact name"): games.app_id("Portal") with self.assertRaises(ValueError): games.app_id("https://example.com/app/620") def test_add_downloads_image_and_rejects_duplicate(self): args = argparse.Namespace(game="620", category="news", owners="Dan, Vky", tags=None, dry_run=False) data = {"620": {"success": True, "data": { "name": "Portal 2", "short_description": "A test & puzzles.", "genres": [{"description": "Adventure"}], "header_image": "https://example.com/header.jpg", }}} with patch.object(games, "api", return_value=data), patch.object(games, "fetch", return_value=b"\xff\xd8\xffimage") as download: games.add(args, self.paths) text = self.paths["news"].read_text() self.assertIn("### Portal 2\n![Portal 2](images/portal_2_620.jpg)", text) self.assertIn("**Description**: A test & puzzles.", text) self.assertEqual((self.root / "images/portal_2_620.jpg").read_bytes(), b"\xff\xd8\xffimage") with self.assertRaisesRegex(ValueError, "already exists"): games.add(args, self.paths) download.assert_called_once() def test_download_failure_does_not_change_markdown(self): args = argparse.Namespace(game="620", category="news", owners="Dan", tags="Puzzle", dry_run=False) data = {"620": {"success": True, "data": { "name": "Portal 2", "short_description": "Puzzles", "header_image": "https://example.com/header.jpg", }}} before = self.paths["news"].read_bytes() with patch.object(games, "api", return_value=data), patch.object(games, "fetch", side_effect=OSError("offline")): with self.assertRaises(OSError): games.add(args, self.paths) self.assertEqual(before, self.paths["news"].read_bytes()) self.assertFalse((self.root / "images").exists()) if __name__ == "__main__": unittest.main()