feat: add featured/locked columns and auto-rotation logic to news pipeline
- Add `ai_grade`, `featured`, `featured_locked`, `landing_locked`, `author`, `article_published`, `image_url`, `has_unique_image` columns to news table - Extend `news_images` schema with `alt_text`, `phash`, `width`, `height`, `is_placeholder` columns - Create `idx_news_featured` index for efficient featured queries - Update admin toggle endpoints to set `featured_locked`/`landing_locked` when manually toggling - Propagate `featured` and `image_url` fields through landing page, news list, and detail page rendering - Update `get_featured_news` to return `featured` and `image_url` fields - Document in AGENTS.md the full zero-maintenance pipeline: image perceptual hashing, placeholder detection, AI grading on cleaned text, reliability gate, effective score computation, and post-loop landing rotation - Update README.md service description to reflect automatic image comparison and landing rotation capabilities - Clarify docs_api.py summaries that toggling featured/landing now locks articles from auto-rotation
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
@@ -0,0 +1,69 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from devplacepy.database import get_table, refresh_snapshot
|
||||
from tests.conftest import BASE_URL
|
||||
|
||||
JSON_gateway = {"Accept": "application/json"}
|
||||
_counter_gateway = [0]
|
||||
|
||||
|
||||
def _db_user_gateway(name):
|
||||
refresh_snapshot()
|
||||
return get_table("users").find_one(username=name)
|
||||
|
||||
|
||||
def _unique_gateway(prefix="gw"):
|
||||
_counter_gateway[0] += 1
|
||||
return f"{prefix}{int(time.time() * 1000)}{_counter_gateway[0]}"
|
||||
|
||||
|
||||
def admin_session(seeded_db):
|
||||
session = requests.Session()
|
||||
session.headers.update(
|
||||
{"X-API-KEY": _db_user_gateway("alice_test")["api_key"], **JSON_gateway}
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
def member_key():
|
||||
name = _unique_gateway("gwmem")
|
||||
requests.post(
|
||||
f"{BASE_URL}/auth/signup",
|
||||
data={
|
||||
"username": name,
|
||||
"email": f"{name}@t.dev",
|
||||
"password": "secret123",
|
||||
"confirm_password": "secret123",
|
||||
},
|
||||
allow_redirects=True,
|
||||
)
|
||||
return _db_user_gateway(name)["api_key"]
|
||||
|
||||
|
||||
def test_gateway_page_requires_admin(seeded_db):
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway", headers=JSON_gateway, allow_redirects=False
|
||||
).status_code
|
||||
== 401
|
||||
)
|
||||
key = member_key()
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway",
|
||||
headers={**JSON_gateway, "X-API-KEY": key},
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
|
||||
|
||||
def test_gateway_page_renders_for_admin(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
response = admin.get(f"{BASE_URL}/admin/gateway", headers={})
|
||||
assert response.status_code == 200
|
||||
assert "Gateway routing" in response.text
|
||||
@@ -0,0 +1,85 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
|
||||
from tests.conftest import BASE_URL
|
||||
from tests.api.admin.gateway.index import (
|
||||
JSON_gateway,
|
||||
admin_session,
|
||||
member_key,
|
||||
_unique_gateway,
|
||||
)
|
||||
|
||||
|
||||
def test_models_require_admin(seeded_db):
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
headers=JSON_gateway,
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 401
|
||||
)
|
||||
key = member_key()
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
headers={**JSON_gateway, "X-API-KEY": key},
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert (
|
||||
admin_session(seeded_db).get(f"{BASE_URL}/admin/gateway/models").status_code
|
||||
== 200
|
||||
)
|
||||
|
||||
|
||||
def test_model_route_create_and_economy(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
source = _unique_gateway("src")
|
||||
|
||||
created = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": source,
|
||||
"provider": "",
|
||||
"target_model": "vendor/y",
|
||||
"kind": "chat",
|
||||
"price_output_per_m": 5.0,
|
||||
"price_cache_miss_per_m": 2.0,
|
||||
"context_window": 32000,
|
||||
},
|
||||
)
|
||||
assert created.status_code == 200, created.text[:300]
|
||||
assert created.json()["model"]["target_model"] == "vendor/y"
|
||||
|
||||
listed = admin.get(f"{BASE_URL}/admin/gateway/models").json()
|
||||
row = next(m for m in listed["models"] if m["source_model"] == source)
|
||||
assert row["kind"] == "chat"
|
||||
assert row["price_output_per_m"] == 5.0
|
||||
assert row["context_window"] == 32000
|
||||
|
||||
deleted = admin.delete(f"{BASE_URL}/admin/gateway/models/{source}")
|
||||
assert deleted.status_code == 200
|
||||
assert admin.delete(f"{BASE_URL}/admin/gateway/models/{source}").status_code == 404
|
||||
|
||||
|
||||
def test_model_route_validation(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
missing_target = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={"source_model": _unique_gateway("src")},
|
||||
)
|
||||
assert missing_target.status_code == 400
|
||||
assert missing_target.json()["ok"] is False
|
||||
|
||||
bad_kind = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/models",
|
||||
json={
|
||||
"source_model": _unique_gateway("src"),
|
||||
"target_model": "vendor/z",
|
||||
"kind": "bogus",
|
||||
},
|
||||
)
|
||||
assert bad_kind.status_code == 400
|
||||
@@ -0,0 +1,84 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import requests
|
||||
|
||||
from tests.conftest import BASE_URL
|
||||
from tests.api.admin.gateway.index import (
|
||||
JSON_gateway,
|
||||
admin_session,
|
||||
member_key,
|
||||
_unique_gateway,
|
||||
)
|
||||
|
||||
|
||||
def test_providers_require_admin(seeded_db):
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
headers=JSON_gateway,
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 401
|
||||
)
|
||||
key = member_key()
|
||||
assert (
|
||||
requests.get(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
headers={**JSON_gateway, "X-API-KEY": key},
|
||||
allow_redirects=False,
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert (
|
||||
admin_session(seeded_db)
|
||||
.get(f"{BASE_URL}/admin/gateway/providers")
|
||||
.status_code
|
||||
== 200
|
||||
)
|
||||
|
||||
|
||||
def test_provider_create_update_delete(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
name = _unique_gateway("prov").lower()
|
||||
|
||||
created = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
json={
|
||||
"name": name,
|
||||
"base_url": "https://x.example/v1/chat/completions",
|
||||
"api_key": "sk-x",
|
||||
"is_active": True,
|
||||
},
|
||||
)
|
||||
assert created.status_code == 200, created.text[:300]
|
||||
assert created.json()["ok"] is True
|
||||
|
||||
listed = admin.get(f"{BASE_URL}/admin/gateway/providers").json()
|
||||
match = next((p for p in listed["providers"] if p["name"] == name), None)
|
||||
assert match is not None
|
||||
assert match["base_url"] == "https://x.example/v1/chat/completions"
|
||||
|
||||
updated = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
json={"name": name, "base_url": "https://y.example/v1/chat/completions"},
|
||||
)
|
||||
assert updated.status_code == 200
|
||||
relisted = admin.get(f"{BASE_URL}/admin/gateway/providers").json()
|
||||
match = next(p for p in relisted["providers"] if p["name"] == name)
|
||||
assert match["base_url"] == "https://y.example/v1/chat/completions"
|
||||
|
||||
deleted = admin.delete(f"{BASE_URL}/admin/gateway/providers/{name}")
|
||||
assert deleted.status_code == 200 and deleted.json()["ok"] is True
|
||||
assert (
|
||||
admin.delete(f"{BASE_URL}/admin/gateway/providers/{name}").status_code == 404
|
||||
)
|
||||
|
||||
|
||||
def test_provider_name_validation(seeded_db):
|
||||
admin = admin_session(seeded_db)
|
||||
bad = admin.post(
|
||||
f"{BASE_URL}/admin/gateway/providers",
|
||||
json={"name": "has spaces!", "base_url": "https://z.example/v1/chat/completions"},
|
||||
)
|
||||
assert bad.status_code == 400
|
||||
assert bad.json()["ok"] is False
|
||||
Reference in New Issue
Block a user