-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_app.py
More file actions
169 lines (154 loc) · 5.34 KB
/
test_api_app.py
File metadata and controls
169 lines (154 loc) · 5.34 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
from fastapi.testclient import TestClient
from core_config import Config
from video_rss_aggregator.api import create_app
from video_rss_aggregator.application.use_cases.ingest_feed import IngestReport
from video_rss_aggregator.bootstrap import AppRuntime, AppUseCases
from video_rss_aggregator.domain.models import PreparedMedia, SummaryResult
from video_rss_aggregator.domain.outcomes import PartialSuccess
@dataclass
class RecordingUseCase:
result: Any
calls: list[tuple[Any, ...]] = field(default_factory=list)
async def execute(self, *args: Any) -> Any:
self.calls.append(args)
return self.result
def _build_runtime() -> tuple[AppRuntime, dict[str, RecordingUseCase]]:
runtime_status = RecordingUseCase(
{
"ollama_version": "0.6.0",
"local_models": {"qwen": {"size": 1}},
"reachable": True,
"database_path": ".data/runtime.db",
"storage_dir": ".data",
"models": ["qwen", "qwen:min"],
}
)
bootstrap_runtime = RecordingUseCase({"models": ["qwen"]})
ingest_feed = RecordingUseCase(
IngestReport(feed_title="Example Feed", item_count=2, processed_count=1)
)
process_source = RecordingUseCase(
PartialSuccess(
media=PreparedMedia(
source_url="https://example.com/watch?v=1",
title="Example Title",
transcript="transcript text",
media_path="/tmp/video.mp4",
frame_paths=("/tmp/frame-1.jpg", "/tmp/frame-2.jpg"),
),
reason="model degraded",
summary=SummaryResult(
summary="Compact summary",
key_points=("one", "two"),
visual_highlights=("frame",),
model_used="qwen",
vram_mb=512.0,
transcript_chars=15,
frame_count=2,
error="model degraded",
),
)
)
render_rss_feed = RecordingUseCase("<rss>feed</rss>")
use_cases = {
"get_runtime_status": runtime_status,
"bootstrap_runtime": bootstrap_runtime,
"ingest_feed": ingest_feed,
"process_source": process_source,
"render_rss_feed": render_rss_feed,
}
return (
AppRuntime(
config=Config(),
use_cases=AppUseCases(**use_cases),
close=lambda: None,
),
use_cases,
)
def test_routes_delegate_to_runtime_use_cases_and_keep_http_shapes() -> None:
runtime, use_cases = _build_runtime()
client = TestClient(create_app(runtime))
ingest = client.post(
"/ingest",
json={
"feed_url": "https://example.com/feed.xml",
"process": True,
"max_items": 3,
},
)
process = client.post(
"/process",
json={
"source_url": "https://example.com/watch?v=1",
"title": "Example Title",
},
)
rss = client.get("/rss?limit=5")
runtime_response = client.get("/runtime")
bootstrap = client.post("/setup/bootstrap")
runtime_payload = runtime_response.json()
bootstrap_payload = bootstrap.json()
assert ingest.status_code == 200
assert ingest.json() == {
"feed_title": "Example Feed",
"item_count": 2,
"processed_count": 1,
}
assert process.status_code == 200
assert process.json() == {
"source_url": "https://example.com/watch?v=1",
"title": "Example Title",
"transcript_chars": 15,
"frame_count": 2,
"summary": {
"summary": "Compact summary",
"key_points": ["one", "two"],
"visual_highlights": ["frame"],
"model_used": "qwen",
"vram_mb": 512.0,
"error": "model degraded",
},
}
assert rss.status_code == 200
assert rss.text == "<rss>feed</rss>"
assert runtime_response.status_code == 200
assert runtime_payload == {
"ollama_version": "0.6.0",
"local_models": {"qwen": {"size": 1}},
"reachable": True,
"database_path": ".data/runtime.db",
"storage_dir": ".data",
"models": ["qwen", "qwen:min"],
"setup_view": {
"state": "blocked",
"missing_models": ["qwen:min"],
"next_action": "Bootstrap required models",
},
}
assert runtime_payload["setup_view"] == {
"state": "blocked",
"missing_models": ["qwen:min"],
"next_action": "Bootstrap required models",
}
assert bootstrap.status_code == 200
assert bootstrap_payload == {
"models_prepared": ["qwen"],
"runtime": {
"ollama_version": "0.6.0",
"local_models": {"qwen": {"size": 1}},
"reachable": True,
"database_path": ".data/runtime.db",
"storage_dir": ".data",
"models": ["qwen", "qwen:min"],
},
}
assert use_cases["ingest_feed"].calls == [("https://example.com/feed.xml", True, 3)]
assert use_cases["process_source"].calls == [
("https://example.com/watch?v=1", "Example Title")
]
assert use_cases["render_rss_feed"].calls == [(5,)]
assert use_cases["get_runtime_status"].calls == [(), ()]
assert use_cases["bootstrap_runtime"].calls == [()]