from __future__ import annotations

from dataclasses import dataclass

from typer.testing import CliRunner

from eightmem.cli.main import app
import eightmem.cli.main as cli_main


runner = CliRunner()


@dataclass
class _FakeResult:
    model: str = "llama3.2:latest"
    text: str = "TEST_OK"
    text_source: str = "response"
    done: bool = True
    done_reason: str | None = "stop"
    eval_count: int = 12
    eval_duration: int = 123456
    raw: dict = None  # type: ignore[assignment]


def test_test_ollama_command_success(fake_home, monkeypatch):
    monkeypatch.setattr(cli_main, "generate_text", lambda **_: _FakeResult(raw={}))
    result = runner.invoke(app, ["test-ollama", "--no-memory", "--prompt", "Reply with exactly: TEST_OK"])
    assert result.exit_code == 0
    assert "text_source: response" in result.stdout
    assert "TEST_OK" in result.stdout


def test_ask_ollama_command_success(fake_home, monkeypatch):
    monkeypatch.setattr(
        cli_main,
        "generate_text",
        lambda **_: _FakeResult(text="Here is a concise plan.", raw={}),
    )
    result = runner.invoke(app, ["ask-ollama", "Give me launch steps", "--no-memory"])
    assert result.exit_code == 0
    assert "Here is a concise plan." in result.stdout
