diff --git a/tests-unit/comfy_extras_test/camera_info_contract_test.py b/tests-unit/comfy_extras_test/camera_info_contract_test.py new file mode 100644 index 00000000000..3e1288fce25 --- /dev/null +++ b/tests-unit/comfy_extras_test/camera_info_contract_test.py @@ -0,0 +1,89 @@ +import math + +import pytest +import torch + +from comfy.cli_args import args + +if not torch.cuda.is_available(): + args.cpu = True + +from comfy_extras.nodes_camera import CreateCameraInfo +from comfy_extras.nodes_camera_angle import ( + CAMERA_FOV, + MAX_ZOOM_FACTOR, + MIN_ZOOM_FACTOR, + SUBJECT_CENTER, + SUBJECT_DISTANCE, + CameraAngle, + build_camera_info, + vertical_term, +) +from comfy_extras.nodes_gaussian_splat import _camera_basis, _lookat_camera_info, _quat_camera_info + +DEVICE = torch.device("cpu") +EYE = [0.0, 0.0, 4.0] +ORIGIN = [0.0, 0.0, 0.0] +ROLLED = [0.0, 0.0, math.sin(math.pi / 8), math.cos(math.pi / 8)] + + +@pytest.mark.parametrize("node", [CameraAngle, CreateCameraInfo]) +def test_camera_nodes_agree_on_the_camera_info_output_type(node): + outputs = [o for o in node.define_schema().outputs if o.display_name == "camera_info"] + assert [o.get_io_type() for o in outputs] == ["LOAD3D_CAMERA"] + + +def test_camera_angle_keeps_the_inputs_the_frontend_picker_binds_to(): + # The picker is selected by widgetType, writes back through these ids, and hand-duplicates the + # limits, defaults, steps and scene constants in src/extensions/core/cameraAngle/types.ts, which + # it uses to place the preview camera. Changing either side alone leaves the node and its 3D + # preview disagreeing, with nothing negotiating the difference at runtime. + inputs = {i.id: i for i in CameraAngle.define_schema().inputs} + assert set(inputs) >= {"horizontal_angle", "vertical_angle", "zoom", "image", "view"} + assert inputs["view"].extra_dict["widgetType"] == "CAMERA_ANGLE_VIEW" + assert [(inputs[i].default, inputs[i].min, inputs[i].max, inputs[i].step) + for i in ("horizontal_angle", "vertical_angle", "zoom")] == [ + (0, 0, 360, 1), (0, -30, 60, 1), (5.0, 0.0, 10.0, 0.1), + ] + assert (SUBJECT_CENTER, CAMERA_FOV, SUBJECT_DISTANCE, MIN_ZOOM_FACTOR, MAX_ZOOM_FACTOR) == \ + ((0.0, 0.0, 0.0), 35.0, 6.0, 1.0, 1.875) + + +@pytest.mark.parametrize("horizontal, vertical", [(0, 0), (90, 0), (180, 0), (270, 0), (45, 30), (0, -30)]) +def test_camera_angle_aims_the_splat_renderer_where_it_was_asked_to(horizontal, vertical): + # CameraAngle emits no quaternion, so _camera_basis takes its look-at path. The view direction + # it recovers there has to be the angle the node was given. + yaw, pitch = math.radians(horizontal), math.radians(vertical) + _, _, _, _, fwd = _camera_basis(build_camera_info(horizontal, vertical, 5.0), DEVICE) + assert [float(v) for v in fwd] == pytest.approx( + [-math.cos(pitch) * math.sin(yaw), math.sin(pitch), math.cos(pitch) * math.cos(yaw)], abs=1e-5) + + +def test_camera_angle_always_aims_at_the_subject_centre(): + _, target, _, _, _ = _camera_basis(build_camera_info(45, 30, 5.0), DEVICE) + assert [float(v) for v in target] == pytest.approx(ORIGIN, abs=1e-6) + + +@pytest.mark.parametrize("vertical, term", [ + (-30, "low-angle shot"), (-15, "eye-level shot"), (0, "eye-level shot"), + (15, "elevated shot"), (45, "high-angle shot"), (60, "high-angle shot"), +]) +def test_camera_angle_elevation_wording_matches_the_rendered_view(vertical, term): + # The splat frame is Y-down, so a camera above the subject looks along a positive forward Y. + _, _, _, _, fwd = _camera_basis(build_camera_info(0, vertical, 5.0), DEVICE) + assert vertical_term(vertical) == term + assert (float(fwd[1]) > 0) == (vertical > 0) + + +def test_camera_info_flags_custom_up_when_world_up_would_be_wrong(): + # The viewer reads the quaternion's up only when useCustomUp is set, so a rolled camera and a + # supplied rotation that tilts up both say so. A plain look-at leaves it off and keeps world up. + assert "useCustomUp" not in _lookat_camera_info(EYE, ORIGIN, 35.0, DEVICE) + assert _lookat_camera_info(EYE, ORIGIN, 35.0, DEVICE, roll=30.0)["useCustomUp"] is True + assert _quat_camera_info(EYE, ROLLED, 35.0, DEVICE)["useCustomUp"] is True + + +def test_camera_angle_leaves_the_viewer_up_vector_alone(): + info = build_camera_info(90, 0, 5.0) + assert "quaternion" not in info + assert "useCustomUp" not in info diff --git a/tests-unit/nodes_test/test_bundled_node_contracts.py b/tests-unit/nodes_test/test_bundled_node_contracts.py new file mode 100644 index 00000000000..ae3fd0bd713 --- /dev/null +++ b/tests-unit/nodes_test/test_bundled_node_contracts.py @@ -0,0 +1,89 @@ +import asyncio + +import pytest +import torch + +from comfy.cli_args import args + +if not torch.cuda.is_available(): + args.cpu = True + +import nodes +from comfy_api.latest import io +from comfy_api.latest._io import _DynamicGroup + +# Startup only logs an import failure and carries on. The sweep below is only meaningful over a +# complete node list, so here a failure is fatal instead. nodes_replacements.py is exempt: it needs +# a running PromptServer, and registers no nodes of its own, so skipping it costs no coverage. +ALLOWED_IMPORT_FAILURES = {"nodes_replacements.py"} + + +async def _load_bundled_nodes(): + return await nodes.init_builtin_extra_nodes() + await nodes.init_builtin_api_nodes() + + +@pytest.fixture(scope="module") +def bundled_nodes(): + class_mappings = dict(nodes.NODE_CLASS_MAPPINGS) + display_name_mappings = dict(nodes.NODE_DISPLAY_NAME_MAPPINGS) + try: + # Both halves of a default startup, so the sweep below sees every node a user gets. The + # loaded modules themselves stay resident; only the mappings are put back. + import_failed = asyncio.run(_load_bundled_nodes()) + assert set(import_failed) <= ALLOWED_IMPORT_FAILURES, \ + "a bundled module failed to import, so the checks below would skip its nodes" + yield nodes.NODE_CLASS_MAPPINGS + finally: + nodes.NODE_CLASS_MAPPINGS.clear() + nodes.NODE_CLASS_MAPPINGS.update(class_mappings) + nodes.NODE_DISPLAY_NAME_MAPPINGS.clear() + nodes.NODE_DISPLAY_NAME_MAPPINGS.update(display_name_mappings) + + +def test_both_halves_of_a_default_startup_are_loaded(bundled_nodes): + # The sweep below is only as good as its node list, and the api nodes are a third of it. Pin one + # from each half so a glob that stops matching shows up here instead of quietly shrinking it. + assert "KSampler" in bundled_nodes + assert "CameraAngle" in bundled_nodes + assert "OpenAIGPTImage1" in bundled_nodes + + +def test_camera_nodes_register_under_the_ids_workflows_reference(bundled_nodes): + # CreateCameraInfo has shipped since v0.23.0 and just moved out of nodes_gaussian_splat.py into + # nodes_camera.py, so saved workflows already carry its id. CameraAngle is new, and shipped + # under a display name agreed after its node id was already in use. + assert "CreateCameraInfo" in bundled_nodes + assert nodes.NODE_DISPLAY_NAME_MAPPINGS["CameraAngle"] == "Compose Camera Angle Prompt" + + +def _strings(value): + if isinstance(value, str): + yield value + elif isinstance(value, dict): + for item in value.values(): + yield from _strings(item) + elif isinstance(value, (list, tuple)): + for item in value: + yield from _strings(item) + + +def test_a_dynamic_group_widget_is_detectable(): + class Grouped(io.ComfyNode): + @classmethod + def define_schema(cls): + return io.Schema(node_id=cls.__name__, inputs=[ + _DynamicGroup.Input("rows", template=[io.Float.Input("x")]), + ], outputs=[]) + + assert _DynamicGroup.io_type in _strings(Grouped.INPUT_TYPES()) + + +def test_no_bundled_node_offers_a_dynamic_group_widget(bundled_nodes): + # comfyui-frontend-package 1.55.x has no DynamicGroup renderer, so a bundled node offering one + # would show an unusable widget. Frontend support landed in 1.57.0; drop this once the pin in + # requirements.txt reaches a version that has it. + offenders = sorted( + name for name, node in bundled_nodes.items() + if _DynamicGroup.io_type in _strings(node.INPUT_TYPES()) + ) + assert offenders == []