from __future__ import annotations import json import pytest from ComfyUI_H3_Continuum_Join.prompt_builder import ( DumasH3SequencePromptBuilder, default_sequence, format_sequence_prompt, normalize_sequence, ) def test_builder_emits_one_absolute_timeline_prompt_from_clip_local_slots(): sequence = normalize_sequence( {"clips": [ {"duration": 4, "slots": [{"start": 0, "end": 2, "text": "Walk forward."}]}, {"duration": 5, "slots": [{"start": 1, "end": 4, "text": "Turn toward the light."}]}, ]}, clip_count=2, default_seconds=4, ) result = format_sequence_prompt( shared_directive="Preserve the same character and environment.", sequence=sequence, soundscape="Quiet street ambience.", music="Low cinematic pulse.", ) assert "[0-4s]\n0-2s: First." in result assert "[4-8s]\n1-4s: Turn toward the light." in result assert "[0-2s]" not in result assert "[5-9s]" not in result assert "overall_soundscape: Quiet street ambience." in result assert "non_diegetic_music: Low cinematic pulse." in result assert result.count("Preserve the same character and environment.") == 2 assert result.count("overall_soundscape: Quiet street ambience.") == 2 assert result.count("non_diegetic_music: Low cinematic pulse.") == 2 def test_normalizer_orders_slots_fills_missing_clips_and_shares_duration(): value = normalize_sequence( json.dumps({"clips": [{"duration": 6, "slots": [ {"start": 3, "end": 6, "text": "Second."}, {"start": 0, "end": 3, "text": "First."}, ]}]}), clip_count=2, default_seconds=4, ) assert value["clips"][0]["slots"][0]["text"] == "First." assert value["clips"][0]["duration"] == 4.0 assert value["clips"][1]["duration"] == 4.0 @pytest.mark.parametrize("bad", [ {"clips": [{"duration": 4, "slots": [{"start": 3, "end": 2, "text": "bad"}]}]}, {"clips": [{"duration": 4, "slots": [{"start": 0, "end": 5, "text": "bad"}]}]}, ]) def test_normalizer_rejects_invalid_slot_ranges(bad): with pytest.raises(ValueError): normalize_sequence(bad, clip_count=1, default_seconds=4) def test_node_schema_has_single_string_output_and_serialized_editor_state(): schema = DumasH3SequencePromptBuilder.INPUT_TYPES()["required"] assert DumasH3SequencePromptBuilder.RETURN_TYPES == ("STRING",) assert schema["timeline_json"][1]["multiline"] is True result = DumasH3SequencePromptBuilder().build(1, 4, "Directive", json.dumps(default_sequence(1, 4)), "N/A", "N/A") assert result[0].startswith("[0-4s]\nDirective") assert result[0].count("overall_soundscape:") == 1