Spaces:
Sleeping
Sleeping
File size: 1,859 Bytes
63d4e3a | 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 | import json
import pytest
from fastapi.testclient import TestClient
from app import main
from app.model_runtime import Completion
VALID_REQUEST = {
"version": "v1",
"knowledge_submission": "I completed the task.",
"wisdom": {
"content": "I learned something before.",
"chapterTitle": "Past learning",
"BookTypeName": "Testimony",
"BookType": "Personal Record",
},
"village_goal": "Learn the skill",
"previous_activity_title": "Practice the skill",
}
VALID_OUTPUT = {
"progression": {
"knowledge_state": "COMPLETED",
"demonstrated_state": ["Completed the task"],
"next_gap": "Apply it in a new setting",
"activity_plan": {
"action": "Apply",
"target": "A new setting",
"avoid_repeating": ["Completing the same task"],
},
}
}
class FakeRuntime:
"""Stands in for ModelRuntime so tests never load the GGUF."""
def __init__(self) -> None:
self.is_ready = True
self.output = json.dumps(VALID_OUTPUT)
self.prompts: list[str] = []
async def start(self) -> None:
pass
async def stop(self) -> None:
pass
def ready(self) -> bool:
return self.is_ready
async def complete(self, prompt: str) -> Completion:
self.prompts.append(prompt)
return Completion(
text=self.output,
input_tokens=len(prompt.split()),
output_tokens=len(self.output.split()),
)
@pytest.fixture
def fake_runtime(monkeypatch):
fake = FakeRuntime()
monkeypatch.setattr(main, "runtime", fake)
return fake
@pytest.fixture
def client(fake_runtime):
with TestClient(main.app) as test_client:
yield test_client
@pytest.fixture
def valid_request():
return json.loads(json.dumps(VALID_REQUEST))
|