|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) Alibaba, Inc. and its affiliates. |
| 3 | +# pylint: disable=protected-access |
| 4 | + |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from dashscope.acli.tools import vision |
| 10 | +from dashscope.acli.tools.registry import registry |
| 11 | + |
| 12 | + |
| 13 | +class FakeProvider: |
| 14 | + def __init__(self): |
| 15 | + self.messages = None |
| 16 | + |
| 17 | + async def chat(self, messages): |
| 18 | + self.messages = messages |
| 19 | + return SimpleNamespace(content="transcribed table text") |
| 20 | + |
| 21 | + |
| 22 | +def _config(): |
| 23 | + return SimpleNamespace( |
| 24 | + provider="tongyi", |
| 25 | + model="qwen3.8-max", |
| 26 | + api_key="sk-test", |
| 27 | + base_url="", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def _register(fake_provider): |
| 32 | + captured = {} |
| 33 | + |
| 34 | + def get_provider_fn(provider, model, api_key, base_url=None): |
| 35 | + captured.update( |
| 36 | + provider=provider, |
| 37 | + model=model, |
| 38 | + api_key=api_key, |
| 39 | + base_url=base_url, |
| 40 | + ) |
| 41 | + return fake_provider |
| 42 | + |
| 43 | + vision.register_vision_tools( |
| 44 | + _config(), |
| 45 | + get_provider_fn=get_provider_fn, |
| 46 | + ) |
| 47 | + return captured |
| 48 | + |
| 49 | + |
| 50 | +class TestViewImage: |
| 51 | + async def test_sends_image_to_vision_model(self, tmp_path, monkeypatch): |
| 52 | + monkeypatch.setattr( |
| 53 | + vision, |
| 54 | + "is_vision_model", |
| 55 | + lambda model: False, |
| 56 | + ) |
| 57 | + monkeypatch.setattr( |
| 58 | + "dashscope.acli.extensions.find_provider", |
| 59 | + lambda name: SimpleNamespace(vision_models=["qwen-vl-plus"]), |
| 60 | + ) |
| 61 | + fake_provider = FakeProvider() |
| 62 | + captured = _register(fake_provider) |
| 63 | + |
| 64 | + image = tmp_path / "shot.png" |
| 65 | + image.write_bytes(b"\x89PNG fake bytes") |
| 66 | + |
| 67 | + result = await registry.get("view_image").func( |
| 68 | + image_path=str(image), |
| 69 | + ) |
| 70 | + |
| 71 | + assert result == "transcribed table text" |
| 72 | + assert captured["model"] == "qwen-vl-plus" |
| 73 | + content = fake_provider.messages[0]["content"] |
| 74 | + assert content[0]["type"] == "text" |
| 75 | + assert content[1]["image_url"]["url"].startswith( |
| 76 | + "data:image/png;base64,", |
| 77 | + ) |
| 78 | + |
| 79 | + async def test_missing_file_returns_error(self): |
| 80 | + _register(FakeProvider()) |
| 81 | + result = await registry.get("view_image").func( |
| 82 | + image_path=str("no-such-image.png"), |
| 83 | + ) |
| 84 | + assert result.startswith("Error: failed to read image") |
| 85 | + |
| 86 | + async def test_vision_call_failure_returns_error(self, tmp_path): |
| 87 | + class BrokenProvider: |
| 88 | + async def chat(self, messages): |
| 89 | + raise RuntimeError("boom") |
| 90 | + |
| 91 | + _register(BrokenProvider()) |
| 92 | + image = tmp_path / "shot.png" |
| 93 | + image.write_bytes(b"\x89PNG fake bytes") |
| 94 | + result = await registry.get("view_image").func( |
| 95 | + image_path=str(image), |
| 96 | + ) |
| 97 | + assert result == "Error: vision call failed: boom" |
| 98 | + |
| 99 | + |
| 100 | +class TestPickVisionModel: |
| 101 | + def test_active_model_when_vision_capable(self, monkeypatch): |
| 102 | + monkeypatch.setattr(vision, "is_vision_model", lambda m: True) |
| 103 | + assert vision.pick_vision_model(_config()) == "qwen3.8-max" |
| 104 | + |
| 105 | + def test_extension_vision_models(self, monkeypatch): |
| 106 | + monkeypatch.setattr(vision, "is_vision_model", lambda m: False) |
| 107 | + monkeypatch.setattr( |
| 108 | + "dashscope.acli.extensions.find_provider", |
| 109 | + lambda name: SimpleNamespace(vision_models=["qwen-vl-max"]), |
| 110 | + ) |
| 111 | + assert vision.pick_vision_model(_config()) == "qwen-vl-max" |
| 112 | + |
| 113 | + def test_fallback_per_provider(self, monkeypatch): |
| 114 | + monkeypatch.setattr(vision, "is_vision_model", lambda m: False) |
| 115 | + monkeypatch.setattr( |
| 116 | + "dashscope.acli.extensions.find_provider", |
| 117 | + lambda name: None, |
| 118 | + ) |
| 119 | + assert vision.pick_vision_model(_config()) == "qwen-vl-max" |
| 120 | + cfg = _config() |
| 121 | + cfg.provider = "openai" |
| 122 | + assert vision.pick_vision_model(cfg) == "gpt-4o" |
| 123 | + |
| 124 | + |
| 125 | +class TestSchemaExposure: |
| 126 | + def test_view_image_always_offered(self): |
| 127 | + vision.register_vision_tools( |
| 128 | + _config(), |
| 129 | + get_provider_fn=lambda *a, **k: FakeProvider(), |
| 130 | + ) |
| 131 | + names = [t["name"] for t in registry.to_schema_list()] |
| 132 | + assert "view_image" in names |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + pytest.main([__file__, "-v"]) |
0 commit comments