|
2 | 2 | from aiohttp import web
|
3 | 3 | from unittest.mock import patch
|
4 | 4 | from app.custom_node_manager import CustomNodeManager
|
| 5 | +import json |
5 | 6 |
|
6 | 7 | pytestmark = (
|
7 | 8 | pytest.mark.asyncio
|
8 | 9 | ) # This applies the asyncio mark to all test functions in the module
|
9 | 10 |
|
| 11 | + |
10 | 12 | @pytest.fixture
|
11 | 13 | def custom_node_manager():
|
12 | 14 | return CustomNodeManager()
|
13 | 15 |
|
| 16 | + |
14 | 17 | @pytest.fixture
|
15 | 18 | def app(custom_node_manager):
|
16 | 19 | app = web.Application()
|
17 | 20 | routes = web.RouteTableDef()
|
18 |
| - custom_node_manager.add_routes(routes, app, [("ComfyUI-TestExtension1", "ComfyUI-TestExtension1")]) |
| 21 | + custom_node_manager.add_routes( |
| 22 | + routes, app, [("ComfyUI-TestExtension1", "ComfyUI-TestExtension1")] |
| 23 | + ) |
19 | 24 | app.add_routes(routes)
|
20 | 25 | return app
|
21 | 26 |
|
| 27 | + |
22 | 28 | async def test_get_workflow_templates(aiohttp_client, app, tmp_path):
|
23 | 29 | client = await aiohttp_client(app)
|
24 | 30 | # Setup temporary custom nodes file structure with 1 workflow file
|
25 | 31 | custom_nodes_dir = tmp_path / "custom_nodes"
|
26 |
| - example_workflows_dir = custom_nodes_dir / "ComfyUI-TestExtension1" / "example_workflows" |
| 32 | + example_workflows_dir = ( |
| 33 | + custom_nodes_dir / "ComfyUI-TestExtension1" / "example_workflows" |
| 34 | + ) |
27 | 35 | example_workflows_dir.mkdir(parents=True)
|
28 | 36 | template_file = example_workflows_dir / "workflow1.json"
|
29 |
| - template_file.write_text('') |
| 37 | + template_file.write_text("") |
30 | 38 |
|
31 |
| - with patch('folder_paths.folder_names_and_paths', { |
32 |
| - 'custom_nodes': ([str(custom_nodes_dir)], None) |
33 |
| - }): |
34 |
| - response = await client.get('/workflow_templates') |
| 39 | + with patch( |
| 40 | + "folder_paths.folder_names_and_paths", |
| 41 | + {"custom_nodes": ([str(custom_nodes_dir)], None)}, |
| 42 | + ): |
| 43 | + response = await client.get("/workflow_templates") |
35 | 44 | assert response.status == 200
|
36 | 45 | workflows_dict = await response.json()
|
37 | 46 | assert isinstance(workflows_dict, dict)
|
38 | 47 | assert "ComfyUI-TestExtension1" in workflows_dict
|
39 | 48 | assert isinstance(workflows_dict["ComfyUI-TestExtension1"], list)
|
40 | 49 | assert workflows_dict["ComfyUI-TestExtension1"][0] == "workflow1"
|
| 50 | + |
| 51 | + |
| 52 | +async def test_build_translations_empty_when_no_locales(custom_node_manager, tmp_path): |
| 53 | + custom_nodes_dir = tmp_path / "custom_nodes" |
| 54 | + custom_nodes_dir.mkdir(parents=True) |
| 55 | + |
| 56 | + with patch("folder_paths.get_folder_paths", return_value=[str(custom_nodes_dir)]): |
| 57 | + translations = custom_node_manager.build_translations() |
| 58 | + assert translations == {} |
| 59 | + |
| 60 | + |
| 61 | +async def test_build_translations_loads_all_files(custom_node_manager, tmp_path): |
| 62 | + # Setup test directory structure |
| 63 | + custom_nodes_dir = tmp_path / "custom_nodes" / "test-extension" |
| 64 | + locales_dir = custom_nodes_dir / "locales" / "en" |
| 65 | + locales_dir.mkdir(parents=True) |
| 66 | + |
| 67 | + # Create test translation files |
| 68 | + main_content = {"title": "Test Extension"} |
| 69 | + (locales_dir / "main.json").write_text(json.dumps(main_content)) |
| 70 | + |
| 71 | + node_defs = {"node1": "Node 1"} |
| 72 | + (locales_dir / "nodeDefs.json").write_text(json.dumps(node_defs)) |
| 73 | + |
| 74 | + commands = {"cmd1": "Command 1"} |
| 75 | + (locales_dir / "commands.json").write_text(json.dumps(commands)) |
| 76 | + |
| 77 | + settings = {"setting1": "Setting 1"} |
| 78 | + (locales_dir / "settings.json").write_text(json.dumps(settings)) |
| 79 | + |
| 80 | + with patch( |
| 81 | + "folder_paths.get_folder_paths", return_value=[tmp_path / "custom_nodes"] |
| 82 | + ): |
| 83 | + translations = custom_node_manager.build_translations() |
| 84 | + |
| 85 | + assert translations == { |
| 86 | + "en": { |
| 87 | + "title": "Test Extension", |
| 88 | + "nodeDefs": {"node1": "Node 1"}, |
| 89 | + "commands": {"cmd1": "Command 1"}, |
| 90 | + "settings": {"setting1": "Setting 1"}, |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +async def test_build_translations_handles_invalid_json(custom_node_manager, tmp_path): |
| 96 | + # Setup test directory structure |
| 97 | + custom_nodes_dir = tmp_path / "custom_nodes" / "test-extension" |
| 98 | + locales_dir = custom_nodes_dir / "locales" / "en" |
| 99 | + locales_dir.mkdir(parents=True) |
| 100 | + |
| 101 | + # Create valid main.json |
| 102 | + main_content = {"title": "Test Extension"} |
| 103 | + (locales_dir / "main.json").write_text(json.dumps(main_content)) |
| 104 | + |
| 105 | + # Create invalid JSON file |
| 106 | + (locales_dir / "nodeDefs.json").write_text("invalid json{") |
| 107 | + |
| 108 | + with patch( |
| 109 | + "folder_paths.get_folder_paths", return_value=[tmp_path / "custom_nodes"] |
| 110 | + ): |
| 111 | + translations = custom_node_manager.build_translations() |
| 112 | + |
| 113 | + assert translations == { |
| 114 | + "en": { |
| 115 | + "title": "Test Extension", |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +async def test_build_translations_merges_multiple_extensions( |
| 121 | + custom_node_manager, tmp_path |
| 122 | +): |
| 123 | + # Setup test directory structure for two extensions |
| 124 | + custom_nodes_dir = tmp_path / "custom_nodes" |
| 125 | + ext1_dir = custom_nodes_dir / "extension1" / "locales" / "en" |
| 126 | + ext2_dir = custom_nodes_dir / "extension2" / "locales" / "en" |
| 127 | + ext1_dir.mkdir(parents=True) |
| 128 | + ext2_dir.mkdir(parents=True) |
| 129 | + |
| 130 | + # Create translation files for extension 1 |
| 131 | + ext1_main = {"title": "Extension 1", "shared": "Original"} |
| 132 | + (ext1_dir / "main.json").write_text(json.dumps(ext1_main)) |
| 133 | + |
| 134 | + # Create translation files for extension 2 |
| 135 | + ext2_main = {"description": "Extension 2", "shared": "Override"} |
| 136 | + (ext2_dir / "main.json").write_text(json.dumps(ext2_main)) |
| 137 | + |
| 138 | + with patch("folder_paths.get_folder_paths", return_value=[str(custom_nodes_dir)]): |
| 139 | + translations = custom_node_manager.build_translations() |
| 140 | + |
| 141 | + assert translations == { |
| 142 | + "en": { |
| 143 | + "title": "Extension 1", |
| 144 | + "description": "Extension 2", |
| 145 | + "shared": "Override", # Second extension should override first |
| 146 | + } |
| 147 | + } |
0 commit comments