import json
import pytest
from aiassistcli import history

@pytest.fixture
def temp_history_file(tmp_path, monkeypatch):
    fake_history = tmp_path / "history.json"
    monkeypatch.setattr(history, "HISTORY_PATH", fake_history)
    return fake_history

def test_save_and_show_history(temp_history_file, capsys):
    history.save_history("ls", "ls -la", "run")

    # fichier bien créé
    assert temp_history_file.exists()
    data = json.loads(temp_history_file.read_text())
    assert data[0]["query"] == "ls"
    assert data[0]["command"] == "ls -la"
    assert data[0]["action"] == "run"

    # affichage
    history.show_history()
    captured = capsys.readouterr()
    assert "ls -la" in captured.out
