|
| 1 | +import importlib.util |
| 2 | +import json |
| 3 | +import pathlib |
| 4 | +import sys |
| 5 | +import unittest |
| 6 | +from typing import Any |
| 7 | + |
| 8 | + |
| 9 | +REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 10 | +NODE_LIST_PATH = REPO_ROOT / "node_list.json" |
| 11 | + |
| 12 | + |
| 13 | +def load_root_node_class_mappings() -> dict[str, Any]: |
| 14 | + parent = str(REPO_ROOT.parent) |
| 15 | + if parent not in sys.path: |
| 16 | + sys.path.insert(0, parent) |
| 17 | + |
| 18 | + spec = importlib.util.spec_from_file_location( |
| 19 | + "ComfyMath", |
| 20 | + REPO_ROOT / "__init__.py", |
| 21 | + submodule_search_locations=[str(REPO_ROOT)], |
| 22 | + ) |
| 23 | + if spec is None or spec.loader is None: |
| 24 | + raise RuntimeError("Unable to load root ComfyMath package") |
| 25 | + |
| 26 | + module = importlib.util.module_from_spec(spec) |
| 27 | + sys.modules[spec.name] = module |
| 28 | + spec.loader.exec_module(module) |
| 29 | + return module.NODE_CLASS_MAPPINGS |
| 30 | + |
| 31 | + |
| 32 | +def reject_duplicate_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]: |
| 33 | + result: dict[str, Any] = {} |
| 34 | + for key, value in pairs: |
| 35 | + if key in result: |
| 36 | + raise ValueError(f"Duplicate node_list.json key: {key}") |
| 37 | + result[key] = value |
| 38 | + return result |
| 39 | + |
| 40 | + |
| 41 | +class NodeListValidationTest(unittest.TestCase): |
| 42 | + def test_node_list_matches_effective_root_node_class_mappings(self) -> None: |
| 43 | + with NODE_LIST_PATH.open(encoding="utf-8") as node_list_file: |
| 44 | + node_list = json.load( |
| 45 | + node_list_file, object_pairs_hook=reject_duplicate_keys |
| 46 | + ) |
| 47 | + |
| 48 | + self.assertIs(type(node_list), dict) |
| 49 | + for node_key, description in node_list.items(): |
| 50 | + with self.subTest(node_key=node_key): |
| 51 | + self.assertIs(type(node_key), str) |
| 52 | + self.assertIs(type(description), str) |
| 53 | + self.assertTrue(description.strip()) |
| 54 | + |
| 55 | + registered_nodes = load_root_node_class_mappings() |
| 56 | + |
| 57 | + self.assertEqual(list(node_list), list(registered_nodes)) |
| 58 | + self.assertEqual(set(node_list), set(registered_nodes)) |
| 59 | + self.assertEqual(len(node_list), len(registered_nodes)) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + unittest.main() |
0 commit comments