from pathlib import Path

from wsi_embed_client import cli


def test_cli_success(monkeypatch, capsys):
    def fake_embed(slide_path, model, key, server_url, output_dir, timeout_seconds):  # noqa: ANN001
        assert slide_path == "/tmp/slide.svs"
        assert model == "uni-v1"
        assert key == "abc123"
        assert server_url == "http://localhost:8000"
        assert output_dir is None
        assert timeout_seconds == 300
        return {
            "embedding_id": "id-1",
            "saved_path": str(Path("/tmp/out.embedding.json")),
            "model": "uni-v1",
            "embedding": [0.1, 0.2],
        }

    monkeypatch.setattr("wsi_embed_client.cli.embed", fake_embed)
    monkeypatch.setattr(
        "sys.argv",
        [
            "wsi-embed",
            "/tmp/slide.svs",
            "--model",
            "uni-v1",
            "--key",
            "abc123",
            "--server-url",
            "http://localhost:8000",
        ],
    )

    code = cli.main()
    assert code == 0
    out = capsys.readouterr().out
    assert "Embedding ID: id-1" in out


def test_cli_failure(monkeypatch, capsys):
    def fake_embed(*args, **kwargs):  # noqa: ANN002, ANN003
        raise RuntimeError("boom")

    monkeypatch.setattr("wsi_embed_client.cli.embed", fake_embed)
    monkeypatch.setattr(
        "sys.argv",
        ["wsi-embed", "/tmp/slide.svs", "--model", "uni-v1", "--key", "abc123"],
    )

    code = cli.main()
    assert code == 1
    err = capsys.readouterr().err
    assert "Error: boom" in err
