188 lines
8.0 KiB
Python
188 lines
8.0 KiB
Python
"""Tests für scripts/orca_senden.py ohne laufenden Server."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from scripts import orca_senden as orca # noqa: E402
|
|
|
|
|
|
class OrcaSendenHelpersTest(unittest.TestCase):
|
|
def test_send_page_url(self):
|
|
self.assertEqual(
|
|
orca.send_page_url("http://127.0.0.1:8770/", 42),
|
|
"http://127.0.0.1:8770/dateien?file=42",
|
|
)
|
|
|
|
def test_app_page_url(self):
|
|
self.assertEqual(
|
|
orca.app_page_url("http://127.0.0.1:8770/"),
|
|
"http://127.0.0.1:8770/dateien?orca=1",
|
|
)
|
|
|
|
def test_browser_key_collapses_duplicates(self):
|
|
self.assertEqual(
|
|
orca.browser_key("1018-1648-Round Clip PLA + PETG(01)_PLA_0.2_30m34s.gcode"),
|
|
orca.browser_key("1018-1648-Round_Clip_PLA_+_PETG(01)_PLA_0.2_30m34s-2.gcode"),
|
|
)
|
|
|
|
def test_browser_key_strips_time_prefix(self):
|
|
self.assertEqual(
|
|
orca.browser_key("1018-1706-Round Clip PLA + PETG(01)_PLA_0.2_30m34s.gcode"),
|
|
orca.browser_key("1018-1709-Round_Clip_PLA_+_PETG(01)_PLA_0.2_30m34s.gcode"),
|
|
)
|
|
self.assertEqual(
|
|
orca.browser_key("1018-1709-Round Clip PLA + PETG(01)_PLA_0.2_30m34s.gcode"),
|
|
"roundclipplapetg01pla0230m34s",
|
|
)
|
|
|
|
def test_open_browser_debounces(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
lock = Path(tmp) / "browser.lock"
|
|
focus = Path(tmp) / "focus.json"
|
|
with patch.object(orca, "BROWSER_LOCK", lock):
|
|
with patch.object(orca, "FOCUS_PATH", focus):
|
|
with patch.object(orca, "open_as_app", return_value=False):
|
|
with patch.object(orca, "webbrowser") as browser:
|
|
orca.open_browser(
|
|
"http://127.0.0.1:8770/dateien?file=1",
|
|
key="roundclip",
|
|
file_id=1,
|
|
mode="tab",
|
|
)
|
|
orca.open_browser(
|
|
"http://127.0.0.1:8770/dateien?file=2",
|
|
key="roundclip",
|
|
file_id=2,
|
|
mode="tab",
|
|
)
|
|
self.assertEqual(browser.open.call_count, 1)
|
|
data = json.loads(focus.read_text(encoding="utf-8"))
|
|
self.assertEqual(data["file_id"], 2)
|
|
|
|
def test_open_browser_app_mode(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
lock = Path(tmp) / "browser.lock"
|
|
focus = Path(tmp) / "focus.json"
|
|
with patch.object(orca, "BROWSER_LOCK", lock):
|
|
with patch.object(orca, "FOCUS_PATH", focus):
|
|
with patch.object(orca, "app_already_running", return_value=False):
|
|
with patch.object(orca, "open_as_app", return_value=True) as app:
|
|
with patch.object(orca, "webbrowser") as browser:
|
|
orca.open_browser(
|
|
"http://127.0.0.1:8770/dateien?orca=1",
|
|
key="roundclip",
|
|
file_id=9,
|
|
mode="app",
|
|
)
|
|
app.assert_called_once()
|
|
browser.open.assert_not_called()
|
|
|
|
def test_open_browser_reuses_running_app(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
lock = Path(tmp) / "browser.lock"
|
|
focus = Path(tmp) / "focus.json"
|
|
with patch.object(orca, "BROWSER_LOCK", lock):
|
|
with patch.object(orca, "FOCUS_PATH", focus):
|
|
with patch.object(orca, "app_already_running", return_value=True):
|
|
with patch.object(orca, "open_as_app", return_value=True) as app:
|
|
with patch.object(orca, "webbrowser") as browser:
|
|
orca.open_browser(
|
|
"http://127.0.0.1:8770/dateien?orca=1",
|
|
key="andere-datei",
|
|
file_id=11,
|
|
mode="app",
|
|
)
|
|
app.assert_not_called()
|
|
browser.open.assert_not_called()
|
|
data = json.loads(focus.read_text(encoding="utf-8"))
|
|
self.assertEqual(data["file_id"], 11)
|
|
|
|
def test_find_gcode_arg_last_matching(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
folder = Path(tmp)
|
|
gcode = folder / "teil.gcode"
|
|
gcode.write_text("; test\n", encoding="utf-8")
|
|
other = folder / "notes.txt"
|
|
other.write_text("x", encoding="utf-8")
|
|
found = orca.find_gcode_arg(["orca_senden.py", str(other), str(gcode)])
|
|
self.assertEqual(found, gcode.resolve())
|
|
|
|
def test_find_gcode_arg_pp_temp(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
pp = Path(tmp) / "platte.gcode.pp"
|
|
pp.write_text("; temp\n", encoding="utf-8")
|
|
found = orca.find_gcode_arg(["orca_senden.py", str(pp)])
|
|
self.assertEqual(found, pp.resolve())
|
|
self.assertEqual(orca.upload_name_for(pp), "platte.gcode")
|
|
|
|
def test_under_watch_subfolder(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp) / "slice"
|
|
dest = root / "Gridfinity" / "teil.gcode"
|
|
dest.parent.mkdir(parents=True)
|
|
dest.write_text("; x\n", encoding="utf-8")
|
|
placed = orca.under_watch(root, dest)
|
|
self.assertEqual(placed, ("Gridfinity", "teil.gcode"))
|
|
|
|
def test_under_watch_outside(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp) / "slice"
|
|
root.mkdir()
|
|
other = Path(tmp) / "elsewhere" / "teil.gcode"
|
|
other.parent.mkdir()
|
|
other.write_text("; x\n", encoding="utf-8")
|
|
self.assertIsNone(orca.under_watch(root, other))
|
|
|
|
def test_find_gcode_arg_missing(self):
|
|
with self.assertRaises(ValueError):
|
|
orca.find_gcode_arg(["orca_senden.py"])
|
|
|
|
def test_find_gcode_arg_file_gone(self):
|
|
with self.assertRaises(FileNotFoundError):
|
|
orca.find_gcode_arg(["orca_senden.py", r"C:\gibt\es\nicht.gcode"])
|
|
|
|
def test_load_config_ok(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "orca-senden.json"
|
|
path.write_text(
|
|
json.dumps({"url": "http://127.0.0.1:8770/", "token": "grt_abc"}),
|
|
encoding="utf-8",
|
|
)
|
|
cfg = orca.load_config(path)
|
|
self.assertEqual(cfg["url"], "http://127.0.0.1:8770")
|
|
self.assertEqual(cfg["token"], "grt_abc")
|
|
|
|
def test_load_config_missing(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "fehlt.json"
|
|
with self.assertRaises(FileNotFoundError):
|
|
orca.load_config(path)
|
|
|
|
def test_load_config_empty_token(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "orca-senden.json"
|
|
path.write_text(json.dumps({"url": "http://x", "token": ""}), encoding="utf-8")
|
|
with self.assertRaises(ValueError):
|
|
orca.load_config(path)
|
|
|
|
def test_main_shows_error_without_config(self):
|
|
with patch.object(orca, "CONFIG_PATH", Path("/tmp/does-not-exist-orca.json")):
|
|
with patch.object(orca, "show_error") as shown:
|
|
code = orca.main(["orca_senden.py", "x.gcode"])
|
|
self.assertEqual(code, 1)
|
|
self.assertTrue(shown.called)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|