|
| 1 | +import io |
| 2 | +from comfy_api.input_impl.video_types import ( |
| 3 | + container_to_output_format, |
| 4 | + get_open_write_kwargs, |
| 5 | +) |
| 6 | +from comfy_api.util import VideoContainer |
| 7 | + |
| 8 | + |
| 9 | +def test_container_to_output_format_empty_string(): |
| 10 | + """Test that an empty string input returns None. `None` arg allows default auto-detection.""" |
| 11 | + assert container_to_output_format("") is None |
| 12 | + |
| 13 | + |
| 14 | +def test_container_to_output_format_none(): |
| 15 | + """Test that None input returns None.""" |
| 16 | + assert container_to_output_format(None) is None |
| 17 | + |
| 18 | + |
| 19 | +def test_container_to_output_format_comma_separated(): |
| 20 | + """Test that a comma-separated list returns a valid singular format from the list.""" |
| 21 | + comma_separated_format = "mp4,mov,m4a" |
| 22 | + output_format = container_to_output_format(comma_separated_format) |
| 23 | + assert output_format in comma_separated_format |
| 24 | + |
| 25 | + |
| 26 | +def test_container_to_output_format_single(): |
| 27 | + """Test that a single format string (not comma-separated list) is returned as is.""" |
| 28 | + assert container_to_output_format("mp4") == "mp4" |
| 29 | + |
| 30 | + |
| 31 | +def test_get_open_write_kwargs_filepath_no_format(): |
| 32 | + """Test that 'format' kwarg is NOT set when dest is a file path.""" |
| 33 | + kwargs_auto = get_open_write_kwargs("output.mp4", "mp4", VideoContainer.AUTO) |
| 34 | + assert "format" not in kwargs_auto, "Format should not be set for file paths (AUTO)" |
| 35 | + |
| 36 | + kwargs_specific = get_open_write_kwargs("output.avi", "mp4", "avi") |
| 37 | + fail_msg = "Format should not be set for file paths (Specific)" |
| 38 | + assert "format" not in kwargs_specific, fail_msg |
| 39 | + |
| 40 | + |
| 41 | +def test_get_open_write_kwargs_base_options_mode(): |
| 42 | + """Test basic kwargs for file path: mode and movflags.""" |
| 43 | + kwargs = get_open_write_kwargs("output.mp4", "mp4", VideoContainer.AUTO) |
| 44 | + assert kwargs["mode"] == "w", "mode should be set to write" |
| 45 | + |
| 46 | + fail_msg = "movflags should be set to preserve custom metadata tags" |
| 47 | + assert "movflags" in kwargs["options"], fail_msg |
| 48 | + assert kwargs["options"]["movflags"] == "use_metadata_tags", fail_msg |
| 49 | + |
| 50 | + |
| 51 | +def test_get_open_write_kwargs_bytesio_auto_format(): |
| 52 | + """Test kwargs for BytesIO dest with AUTO format.""" |
| 53 | + dest = io.BytesIO() |
| 54 | + container_fmt = "mov,mp4,m4a" |
| 55 | + kwargs = get_open_write_kwargs(dest, container_fmt, VideoContainer.AUTO) |
| 56 | + |
| 57 | + assert kwargs["mode"] == "w" |
| 58 | + assert kwargs["options"]["movflags"] == "use_metadata_tags" |
| 59 | + |
| 60 | + fail_msg = ( |
| 61 | + "Format should be a valid format from the container's format list when AUTO" |
| 62 | + ) |
| 63 | + assert kwargs["format"] in container_fmt, fail_msg |
| 64 | + |
| 65 | + |
| 66 | +def test_get_open_write_kwargs_bytesio_specific_format(): |
| 67 | + """Test kwargs for BytesIO dest with a specific single format.""" |
| 68 | + dest = io.BytesIO() |
| 69 | + container_fmt = "avi" |
| 70 | + to_fmt = VideoContainer.MP4 |
| 71 | + kwargs = get_open_write_kwargs(dest, container_fmt, to_fmt) |
| 72 | + |
| 73 | + assert kwargs["mode"] == "w" |
| 74 | + assert kwargs["options"]["movflags"] == "use_metadata_tags" |
| 75 | + |
| 76 | + fail_msg = "Format should be the specified format (lowercased) when output format is not AUTO" |
| 77 | + assert kwargs["format"] == "mp4", fail_msg |
| 78 | + |
| 79 | + |
| 80 | +def test_get_open_write_kwargs_bytesio_specific_format_list(): |
| 81 | + """Test kwargs for BytesIO dest with a specific comma-separated format.""" |
| 82 | + dest = io.BytesIO() |
| 83 | + container_fmt = "avi" |
| 84 | + to_fmt = "mov,mp4,m4a" # A format string that is a list |
| 85 | + kwargs = get_open_write_kwargs(dest, container_fmt, to_fmt) |
| 86 | + |
| 87 | + assert kwargs["mode"] == "w" |
| 88 | + assert kwargs["options"]["movflags"] == "use_metadata_tags" |
| 89 | + |
| 90 | + fail_msg = "Format should be a valid format from the specified format list when output format is not AUTO" |
| 91 | + assert kwargs["format"] in to_fmt, fail_msg |
0 commit comments