|
| 1 | +"""Tests for multimedia logging tools.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +import importlib.util |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +MODULE_PATH = Path(__file__).resolve().parent.parent / "tools" / "media_logging.py" |
| 12 | +SPEC = importlib.util.spec_from_file_location("media_logging", MODULE_PATH) |
| 13 | +assert SPEC and SPEC.loader |
| 14 | +MODULE = importlib.util.module_from_spec(SPEC) |
| 15 | +SPEC.loader.exec_module(MODULE) |
| 16 | + |
| 17 | +MediaLogging = MODULE.MediaLogging |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def toolset() -> MediaLogging: |
| 22 | + return MediaLogging() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def media_files(tmp_path: Path) -> dict[str, Path]: |
| 27 | + image_path = tmp_path / "sample.png" |
| 28 | + image_path.write_bytes(b"png-bytes") |
| 29 | + |
| 30 | + audio_path = tmp_path / "sample.wav" |
| 31 | + audio_path.write_bytes(b"wav-bytes") |
| 32 | + |
| 33 | + video_path = tmp_path / "sample.mp4" |
| 34 | + video_path.write_bytes(b"mp4-bytes") |
| 35 | + |
| 36 | + artifact_path = tmp_path / "notes.txt" |
| 37 | + artifact_path.write_text("artifact", encoding="utf-8") |
| 38 | + |
| 39 | + return { |
| 40 | + "image": image_path, |
| 41 | + "audio": audio_path, |
| 42 | + "video": video_path, |
| 43 | + "artifact": artifact_path, |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +class TestToolDiscovery: |
| 48 | + def test_tools_discovered(self, toolset: MediaLogging) -> None: |
| 49 | + names = {tool.name for tool in toolset.get_tools()} |
| 50 | + assert names == { |
| 51 | + "log_image_output", |
| 52 | + "log_audio_output", |
| 53 | + "log_video_output", |
| 54 | + "log_file_artifact", |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +class TestMediaLogging: |
| 59 | + @pytest.mark.asyncio |
| 60 | + async def test_log_image_output( |
| 61 | + self, |
| 62 | + toolset: MediaLogging, |
| 63 | + media_files: dict[str, Path], |
| 64 | + monkeypatch: pytest.MonkeyPatch, |
| 65 | + ) -> None: |
| 66 | + captured: dict[str, object] = {} |
| 67 | + |
| 68 | + def fake_log_output(name: str, value: object, **_: object) -> None: |
| 69 | + captured["name"] = name |
| 70 | + captured["value"] = value |
| 71 | + |
| 72 | + monkeypatch.setattr(MODULE.dn, "log_output", fake_log_output) |
| 73 | + |
| 74 | + result = await toolset.log_image_output( |
| 75 | + "screenshot/home", str(media_files["image"]), caption="Home" |
| 76 | + ) |
| 77 | + assert result == { |
| 78 | + "kind": "image", |
| 79 | + "path": str(media_files["image"]), |
| 80 | + "name": "screenshot/home", |
| 81 | + "caption": "Home", |
| 82 | + } |
| 83 | + assert captured["name"] == "screenshot/home" |
| 84 | + assert isinstance(captured["value"], MODULE.dn.Image) |
| 85 | + assert captured["value"].data == media_files["image"] |
| 86 | + |
| 87 | + @pytest.mark.asyncio |
| 88 | + async def test_log_audio_output( |
| 89 | + self, |
| 90 | + toolset: MediaLogging, |
| 91 | + media_files: dict[str, Path], |
| 92 | + monkeypatch: pytest.MonkeyPatch, |
| 93 | + ) -> None: |
| 94 | + captured: dict[str, object] = {} |
| 95 | + |
| 96 | + def fake_log_output(name: str, value: object, **_: object) -> None: |
| 97 | + captured["name"] = name |
| 98 | + captured["value"] = value |
| 99 | + |
| 100 | + monkeypatch.setattr(MODULE.dn, "log_output", fake_log_output) |
| 101 | + |
| 102 | + result = await toolset.log_audio_output( |
| 103 | + "audio/sample", str(media_files["audio"]), caption="Sample" |
| 104 | + ) |
| 105 | + assert result == { |
| 106 | + "kind": "audio", |
| 107 | + "path": str(media_files["audio"]), |
| 108 | + "name": "audio/sample", |
| 109 | + "caption": "Sample", |
| 110 | + } |
| 111 | + assert captured["name"] == "audio/sample" |
| 112 | + assert isinstance(captured["value"], MODULE.dn.Audio) |
| 113 | + assert captured["value"].data == media_files["audio"] |
| 114 | + |
| 115 | + @pytest.mark.asyncio |
| 116 | + async def test_log_video_output( |
| 117 | + self, |
| 118 | + toolset: MediaLogging, |
| 119 | + media_files: dict[str, Path], |
| 120 | + monkeypatch: pytest.MonkeyPatch, |
| 121 | + ) -> None: |
| 122 | + captured: dict[str, object] = {} |
| 123 | + |
| 124 | + def fake_log_output(name: str, value: object, **_: object) -> None: |
| 125 | + captured["name"] = name |
| 126 | + captured["value"] = value |
| 127 | + |
| 128 | + monkeypatch.setattr(MODULE.dn, "log_output", fake_log_output) |
| 129 | + |
| 130 | + result = await toolset.log_video_output( |
| 131 | + "video/demo", str(media_files["video"]), caption="Demo" |
| 132 | + ) |
| 133 | + assert result == { |
| 134 | + "kind": "video", |
| 135 | + "path": str(media_files["video"]), |
| 136 | + "name": "video/demo", |
| 137 | + "caption": "Demo", |
| 138 | + } |
| 139 | + assert captured["name"] == "video/demo" |
| 140 | + assert isinstance(captured["value"], MODULE.dn.Video) |
| 141 | + assert captured["value"].data == media_files["video"] |
| 142 | + |
| 143 | + @pytest.mark.asyncio |
| 144 | + async def test_log_file_artifact( |
| 145 | + self, |
| 146 | + toolset: MediaLogging, |
| 147 | + media_files: dict[str, Path], |
| 148 | + monkeypatch: pytest.MonkeyPatch, |
| 149 | + ) -> None: |
| 150 | + captured: dict[str, object] = {} |
| 151 | + |
| 152 | + def fake_log_artifact(local_uri: object, **kwargs: object) -> None: |
| 153 | + captured["path"] = local_uri |
| 154 | + captured["name"] = kwargs.get("name") |
| 155 | + |
| 156 | + monkeypatch.setattr(MODULE.dn, "log_artifact", fake_log_artifact) |
| 157 | + |
| 158 | + result = await toolset.log_file_artifact( |
| 159 | + str(media_files["artifact"]), name="notes.txt" |
| 160 | + ) |
| 161 | + assert result == { |
| 162 | + "kind": "artifact", |
| 163 | + "path": str(media_files["artifact"]), |
| 164 | + "name": "notes.txt", |
| 165 | + } |
| 166 | + assert captured["path"] == media_files["artifact"] |
| 167 | + assert captured["name"] == "notes.txt" |
| 168 | + |
| 169 | + @pytest.mark.asyncio |
| 170 | + async def test_missing_file_raises(self, toolset: MediaLogging) -> None: |
| 171 | + with pytest.raises(FileNotFoundError): |
| 172 | + await toolset.log_image_output("missing", "/tmp/nope.png") |
| 173 | + |
| 174 | + with pytest.raises(FileNotFoundError): |
| 175 | + await toolset.log_audio_output("missing", "/tmp/nope.wav") |
| 176 | + |
| 177 | + with pytest.raises(FileNotFoundError): |
| 178 | + await toolset.log_video_output("missing", "/tmp/nope.mp4") |
| 179 | + |
| 180 | + with pytest.raises(FileNotFoundError): |
| 181 | + await toolset.log_file_artifact("/tmp/nope.bin") |
| 182 | + |
| 183 | + @pytest.mark.asyncio |
| 184 | + async def test_directory_path_raises( |
| 185 | + self, toolset: MediaLogging, tmp_path: Path |
| 186 | + ) -> None: |
| 187 | + directory = tmp_path / "dir" |
| 188 | + directory.mkdir() |
| 189 | + |
| 190 | + with pytest.raises(ValueError): |
| 191 | + await toolset.log_file_artifact(str(directory)) |
0 commit comments