File size: 3,159 Bytes
910dadd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bcb9d8
910dadd
 
 
 
 
 
 
 
 
 
 
d6217aa
 
 
 
910dadd
 
 
1bcb9d8
910dadd
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient


@pytest.fixture()
def client(monkeypatch):
    monkeypatch.setenv("LANGFUSE_PUBLIC_KEY", "pk-lf-test")
    monkeypatch.setenv("LANGFUSE_SECRET_KEY", "sk-lf-test")
    monkeypatch.setenv("LANGFUSE_TRACING_ENABLED", "false")
    from app import config
    from app.main import app

    monkeypatch.setattr(config, "APP_PASSCODE", "sesame")
    return TestClient(app)


HEADERS = {"X-Contimp-Passcode": "sesame"}


class FakeMessage:
    def __init__(self, content, tool_calls=None):
        self.content = content
        self.tool_calls = tool_calls


def fake_completion(content):
    class Choice:
        message = FakeMessage(content)

    class Response:
        choices = [Choice()]

    return Response()


def test_health_unauthenticated(client):
    assert client.get("/api/health").json()["ok"] is True


def test_passcode_required(client):
    assert client.get("/api/tasks").status_code == 401
    assert client.get("/api/tasks", headers={"X-Contimp-Passcode": "wrong"}).status_code == 401
    assert client.get("/api/tasks", headers=HEADERS).status_code == 200


def test_tasks_and_sample(client):
    tasks = client.get("/api/tasks", headers=HEADERS).json()
    assert {t["id"] for t in tasks} >= {"pr-area", "config-copilot"}
    s = client.post("/api/tasks/pr-area/sample", headers=HEADERS).json()
    assert s["input_id"].startswith("pr-") and s["text"]
    assert client.post("/api/tasks/nope/sample", headers=HEADERS).status_code == 404


def test_run_pr_area_with_mocked_llm(client):
    from app.tasks.pr_area import _RECORDS

    input_id, record = next(iter(_RECORDS.items()))
    answer = json.dumps({"area": record["area"]})
    with patch("app.engine._openai") as mock_llm:
        mock_llm.return_value.chat.completions.create.return_value = fake_completion(answer)
        r = client.post(
            "/api/tasks/pr-area/run",
            headers=HEADERS,
            json={"text": "Title: x", "input_id": input_id, "user": "tester", "session_id": "s1"},
        )
    body = r.json()
    assert r.status_code == 200, body
    assert body["scores"] == {"format_ok": 1.0, "exact_match": 1.0}
    assert body["truth"]["area"] == record["area"]
    assert body["output"]["area"] == record["area"]
    # complete trajectory is returned: system -> user -> final assistant answer
    msgs = body["messages"]
    assert msgs[0]["role"] == "system" and msgs[1]["role"] == "user"
    assert msgs[-1] == {"role": "assistant", "content": answer}


def test_run_without_input_id_has_no_scores(client):
    with patch("app.engine._openai") as mock_llm:
        mock_llm.return_value.chat.completions.create.return_value = fake_completion(
            '{"area": "docs"}'
        )
        r = client.post(
            "/api/tasks/pr-area/run", headers=HEADERS, json={"text": "Title: my own PR"}
        )
    body = r.json()
    assert body["scores"] == {} and body["truth"] is None


def test_run_rejects_empty_text(client):
    r = client.post("/api/tasks/pr-area/run", headers=HEADERS, json={"text": "  "})
    assert r.status_code == 422