import pytest from app.config import Settings from app.templates import TemplateError, check_mapping, find_broken_links, load_all def test_find_broken_links_detects_missing_node(): graph = { "1": {"class_type": "X", "inputs": {"m": ["9", 0]}}, } errors = find_broken_links(graph) assert len(errors) == 1 assert "missing node '9'" in errors[0] assert "node 1" in errors[0] def test_find_broken_links_ok_on_valid_graph(): graph = { "1": {"class_type": "X", "inputs": {"m": ["2", 0]}}, "2": {"class_type": "Y", "inputs": {}}, } assert find_broken_links(graph) == [] def test_check_mapping_missing_node(): graph = {"1": {"class_type": "X", "inputs": {"text": ""}}} mapping = {"create": {"prompt": {"node": "404", "field": "text"}}} errors = check_mapping(graph, mapping["create"], "create") assert any("not found" in e for e in errors) def test_check_mapping_non_scalar_field(): graph = {"1": {"class_type": "X", "inputs": {"text": ["2", 0]}}} mapping = {"create": {"prompt": {"node": "1", "field": "text"}}} errors = check_mapping(graph, mapping["create"], "create") assert any("no scalar input" in e for e in errors) def test_shipped_templates_load_cleanly(): s = Settings(_env_file=None) create, edit = load_all(s) assert create["83:28"]["class_type"] == "UNETLoader" assert edit["115:89"]["class_type"] == "LoraLoaderModelOnly" def test_broken_template_blocks_startup(tmp_path): bad = tmp_path / "bad.json" bad.write_text('{"1": {"class_type": "X", "inputs": {"m": ["9", 0]}}}', encoding="utf-8") good = tmp_path / "good.json" good.write_text( '{"1": {"class_type": "X", "inputs": {"m": ["2", 0]}}, "2": {"class_type": "Y", "inputs": {}}}', encoding="utf-8", ) s = Settings(_env_file=None, create_template_path=bad, edit_template_path=good) with pytest.raises(TemplateError) as exc: load_all(s) assert "missing node '9'" in str(exc.value)