import os import tempfile import tkinter as tk import unittest from pathlib import Path from unittest.mock import call, patch from copienator_gui.manual_resolution import ManualResolutionPanel @unittest.skipUnless(os.environ.get("DISPLAY"), "requires a display") class ManualResolutionPanelTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.evaluation = Path(self.temp.name) self.root = tk.Tk() self.addCleanup(self.root.destroy) self.path = self.evaluation / "manual_resolutions.txt" def panel(self): panel = ManualResolutionPanel(self.root, lambda: self.evaluation) panel.pack() self.root.update() return panel def test_preview_editor_and_each_pdf_pair_use_shared_resolution_rules(self): content = "### Instructions\nCopie01 Ex 1 x> |Ex 2\n\nCopie02 Ex 3 ss Ex 4|\n" self.path.write_text(content, encoding="utf-8") paths = [] for copy, label in (("01", "Ex 1_new"), ("01", "Ex 2_old"), ("02", "Ex 3"), ("02", "Ex 4")): path = self.evaluation / "Copies" / f"Copie{copy}" / f"{label}.pdf" path.parent.mkdir(parents=True, exist_ok=True) path.touch() paths.append(path) panel = self.panel() self.assertEqual(panel.text.get("1.0", "end-1c"), content) self.assertEqual(len(panel.pdf_buttons), 4) self.assertEqual([button.cget("text") for button in panel.pdf_buttons], ["PDF source", "PDF cible"] * 2) with patch("copienator_gui.manual_resolution.open_path") as opened: panel.editor_button.invoke() for button in panel.pdf_buttons: button.invoke() self.assertEqual(opened.call_args_list, [call(self.path), *map(call, paths)]) def test_reload_keeps_invalid_lines_visible_and_removes_stale_actions(self): self.path.write_text("Copie01 A -> B|\n", encoding="utf-8") panel = self.panel() self.path.write_text("bad instruction\nCopie02 C sx D\n", encoding="utf-8") panel.reload() self.assertIn("bad instruction", panel.text.get("1.0", "end")) self.assertIn("lignes invalides : 1", panel.status.cget("text")) self.assertEqual(len(panel.pdf_buttons), 2) self.path.unlink() panel.reload() self.assertFalse(panel.pdf_buttons) self.assertEqual(str(panel.editor_button.cget("state")), "disabled") def test_missing_source_does_not_prevent_opening_destination(self): self.path.write_text("Copie01 A -> B|\n", encoding="utf-8") destination = self.evaluation / "Copies" / "Copie01" / "B.pdf" destination.parent.mkdir(parents=True) destination.touch() panel = self.panel() with patch("copienator_gui.manual_resolution.open_path") as opened, patch( "copienator_gui.manual_resolution.messagebox.showerror" ) as error: panel.pdf_buttons[0].invoke() opened.assert_not_called() panel.pdf_buttons[1].invoke() opened.assert_called_once_with(destination) self.assertIn("A.pdf", error.call_args.args[1])