Initial ai typewriter implementation
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
from ai_typewriter.ai_client import ask_ai
|
||||
from ai_typewriter.config import AppConfig
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload):
|
||||
self.payload = payload
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
def json(self):
|
||||
return self.payload
|
||||
|
||||
|
||||
def test_ollama_payload_contains_strict_system_prompt(monkeypatch):
|
||||
seen = {}
|
||||
def fake_post(url, json, timeout, **kwargs):
|
||||
seen["url"] = url
|
||||
seen["json"] = json
|
||||
return FakeResponse({"message": {"content": "ok"}})
|
||||
monkeypatch.setattr("ai_typewriter.ai_client.requests.post", fake_post)
|
||||
|
||||
result = ask_ai("texte", AppConfig(provider="ollama", model="m"))
|
||||
|
||||
assert result == "ok"
|
||||
assert seen["url"].endswith("/api/chat")
|
||||
assert seen["json"]["messages"][0]["role"] == "system"
|
||||
assert "Uniquement la réponse brute" in seen["json"]["messages"][0]["content"]
|
||||
|
||||
|
||||
def test_gemini_payload(monkeypatch):
|
||||
seen = {}
|
||||
def fake_post(url, params, json, timeout):
|
||||
seen["url"] = url
|
||||
seen["params"] = params
|
||||
seen["json"] = json
|
||||
return FakeResponse({"candidates": [{"content": {"parts": [{"text": "brut"}]}}]})
|
||||
monkeypatch.setattr("ai_typewriter.ai_client.requests.post", fake_post)
|
||||
|
||||
result = ask_ai("texte", AppConfig(provider="gemini", model="gemini-1.5-flash", api_key="k"))
|
||||
|
||||
assert result == "brut"
|
||||
assert seen["params"] == {"key": "k"}
|
||||
assert seen["url"].endswith("/v1beta/models/gemini-1.5-flash:generateContent")
|
||||
assert "systemInstruction" in seen["json"]
|
||||
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from ai_typewriter.config import load_config
|
||||
|
||||
|
||||
def test_load_default_config(tmp_path):
|
||||
path = tmp_path / "config.json"
|
||||
path.write_text(json.dumps({"provider": "ollama"}), encoding="utf-8")
|
||||
|
||||
cfg = load_config(path)
|
||||
|
||||
assert cfg.provider == "ollama"
|
||||
assert cfg.hotkey == "ctrl+alt+a"
|
||||
assert "Réponds directement" in cfg.system_prompt
|
||||
|
||||
|
||||
def test_reject_invalid_provider(tmp_path):
|
||||
path = tmp_path / "config.json"
|
||||
path.write_text(json.dumps({"provider": "bad"}), encoding="utf-8")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
load_config(path)
|
||||
Reference in New Issue
Block a user