From 9f4bbd4066818ca1f6f9af0b963efa2e0a4d850b Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 27 Nov 2025 21:51:08 +0100 Subject: [PATCH] Added test for littlejs filter --- tests/unit/roles/web-app-littlejs/__init__.py | 0 .../filter_plugins/__init__.py | 0 .../filter_plugins/test_littlejs.py | 66 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 tests/unit/roles/web-app-littlejs/__init__.py create mode 100644 tests/unit/roles/web-app-littlejs/filter_plugins/__init__.py create mode 100644 tests/unit/roles/web-app-littlejs/filter_plugins/test_littlejs.py diff --git a/tests/unit/roles/web-app-littlejs/__init__.py b/tests/unit/roles/web-app-littlejs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/roles/web-app-littlejs/filter_plugins/__init__.py b/tests/unit/roles/web-app-littlejs/filter_plugins/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/roles/web-app-littlejs/filter_plugins/test_littlejs.py b/tests/unit/roles/web-app-littlejs/filter_plugins/test_littlejs.py new file mode 100644 index 00000000..b40287f6 --- /dev/null +++ b/tests/unit/roles/web-app-littlejs/filter_plugins/test_littlejs.py @@ -0,0 +1,66 @@ +import unittest +import importlib.util +from pathlib import Path + + +def _load_littlejs_module(): + """ + Load the littlejs filter plugin directly from the roles path. + Works even with hyphens in directory names. + """ + here = Path(__file__).resolve() + plugin_path = ( + here.parents[5] + / "roles" + / "web-app-littlejs" + / "filter_plugins" + / "littlejs.py" + ) + + spec = importlib.util.spec_from_file_location("littlejs_filter", plugin_path) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + spec.loader.exec_module(module) + return module + + +class TestLittlejsHref(unittest.TestCase): + + @classmethod + def setUpClass(cls): + module = _load_littlejs_module() + # Store original function; we will always access it via type(self) + cls._littlejs_href = module.littlejs_href + + def test_returns_hash_if_no_file(self): + href = type(self)._littlejs_href({}, "https", "littlejs.example.com") + self.assertEqual(href, "#") + + def test_full_example_project(self): + example = {"file": "starter", "is_project": True} + href = type(self)._littlejs_href(example, "https", "littlejs.example.com") + self.assertEqual( + href, + "https://littlejs.example.com/examples/starter/" + ) + + def test_short_example_uses_runner(self): + # no is_project → False by default + example = {"file": "clock.js"} + href = type(self)._littlejs_href(example, "https", "littlejs.example.com") + self.assertEqual( + href, + "https://littlejs.example.com/examples/shorts/run.html?file=clock.js" + ) + + def test_respects_protocol_and_domain(self): + example = {"file": "platformer", "is_project": True} + href = type(self)._littlejs_href(example, "http", "littlejs.infinito.nexus") + self.assertEqual( + href, + "http://littlejs.infinito.nexus/examples/platformer/" + ) + + +if __name__ == "__main__": + unittest.main()