Add thread notifications, SEO topic pages, and fix quiz auto-advance
Notifications: a new "thread" type notifies every other commenter on a
post whenever anyone comments on it, disregarding reply hierarchy -
excluding the actor and whoever already got a comment/reply
notification for that same event, so no one is double-notified.
Implemented via a background-deferred fan-out mirroring the existing
mention-notification pattern.
SEO: discussion_forum_posting() now embeds up to 20 of a post's
comments as nested schema.org Comment entities (not just an aggregate
count), and a new /topics hub plus /topics/{topic} pages give the
feed's topic filter real, independently crawlable/indexable URLs -
/feed?topic=X was never indexable since its canonical strips the
query string back to bare /feed. Both are wired end to end (schemas,
Devii actions, docs API, sitemap, locustfile load-test coverage).
Quiz player: the auto-advance to the next question used to hide the
just-answered slide in the same tick as rendering the grade, so on
any multi-question quiz the Correct/Not correct feedback was never
actually visible before the view moved on. Delayed via setTimeout,
with the pending timer cleared on manual navigation and on
disconnect so it can't race or fire on a removed component.
Also includes other local changes already in progress in this
working tree before this session (messaging, push delivery,
deepsearch jobs, game economy, quiz builder) - verified by the full
suite passing (3467 tests) but not authored or individually reviewed
in this session.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VL9Xn57W5UR3HZbbuuzxdK
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
from devplacepy.database import get_table, init_db
|
||||
|
||||
FILTERED_COLUMNS = {
|
||||
"messages": ("uid", "sender_uid", "receiver_uid", "content", "read", "created_at"),
|
||||
"messages": ("uid", "sender_uid", "receiver_uid", "content", "read", "created_at", "updated_at"),
|
||||
"workspace_quota_rules": ("uid", "owner_kind", "owner_id", "deleted_at"),
|
||||
"workspace_editor_prefs": ("uid", "owner_kind", "owner_id", "deleted_at"),
|
||||
}
|
||||
|
||||
@@ -302,6 +302,91 @@ def test_apns_delivery_maps_statuses(monkeypatch):
|
||||
assert throttled.status == providers.REJECTED
|
||||
|
||||
|
||||
def test_apns_dead_reasons_excludes_topic_misconfiguration(monkeypatch):
|
||||
from devplacepy.push import providers
|
||||
|
||||
not_for_topic, _ = _apns_response_status(
|
||||
monkeypatch, 400, {"reason": "DeviceTokenNotForTopic"}
|
||||
)
|
||||
assert not_for_topic.status == providers.REJECTED
|
||||
|
||||
disallowed, _ = _apns_response_status(monkeypatch, 400, {"reason": "TopicDisallowed"})
|
||||
assert disallowed.status == providers.REJECTED
|
||||
|
||||
expired, _ = _apns_response_status(monkeypatch, 410, {"reason": "ExpiredToken"})
|
||||
assert expired.status == providers.DEAD
|
||||
|
||||
|
||||
def test_apns_dead_carries_apns_confirmation_timestamp(monkeypatch):
|
||||
from datetime import datetime, timezone
|
||||
from devplacepy.push import providers
|
||||
|
||||
gone, _ = _apns_response_status(
|
||||
monkeypatch, 410, {"reason": "Unregistered", "timestamp": 1700000000000}
|
||||
)
|
||||
assert gone.status == providers.DEAD
|
||||
assert gone.dead_before == datetime.fromtimestamp(
|
||||
1700000000000 / 1000, tz=timezone.utc
|
||||
).isoformat()
|
||||
|
||||
bad_token, _ = _apns_response_status(monkeypatch, 400, {"reason": "BadDeviceToken"})
|
||||
assert bad_token.status == providers.DEAD
|
||||
assert bad_token.dead_before is None
|
||||
|
||||
|
||||
def test_apns_auth_failure_invalidates_cached_token(monkeypatch, local_db):
|
||||
import httpx
|
||||
from tests.conftest import run_async
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(
|
||||
monkeypatch,
|
||||
**{
|
||||
apns.TEAM_ID_KEY: "TEAMID1234",
|
||||
apns.KEY_ID_KEY: "KEYID12345",
|
||||
apns.AUTH_KEY_KEY: _ec_private_key_pem(),
|
||||
apns.TOPIC_KEY: "nl.molodetz.devplace",
|
||||
},
|
||||
)
|
||||
provider = providers.PROVIDERS["apns"]
|
||||
pem = _ec_private_key_pem()
|
||||
first = apns.provider_token("TEAMID1234", "KEYID12345", pem)
|
||||
assert apns._token_state["current"]["token"] == first
|
||||
assert apns._read_shared_token()["token"] == first
|
||||
|
||||
def handler(request):
|
||||
return httpx.Response(403, json={"reason": "InvalidProviderToken"})
|
||||
|
||||
async def run():
|
||||
transport = httpx.MockTransport(handler)
|
||||
async with httpx.AsyncClient(transport=transport) as client:
|
||||
return await provider.deliver(
|
||||
client, {"token": "a" * 64}, provider.prepare({"message": "hi"})
|
||||
)
|
||||
|
||||
outcome = run_async(run())
|
||||
assert outcome.status == providers.REJECTED
|
||||
assert "current" not in apns._token_state
|
||||
assert apns._read_shared_token() is None
|
||||
|
||||
second = apns.provider_token("TEAMID1234", "KEYID12345", pem)
|
||||
assert second != first
|
||||
|
||||
|
||||
def test_apns_provider_token_is_shared_across_workers(monkeypatch, local_db):
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(monkeypatch)
|
||||
pem = _ec_private_key_pem()
|
||||
token = apns.provider_token("TEAMID1234", "KEYID12345", pem)
|
||||
|
||||
apns._token_state.clear()
|
||||
reused = apns.provider_token("TEAMID1234", "KEYID12345", pem)
|
||||
assert reused == token
|
||||
assert apns._token_state["current"]["token"] == token
|
||||
|
||||
|
||||
def test_apns_delivery_without_configuration_never_raises(monkeypatch):
|
||||
import httpx
|
||||
from tests.conftest import run_async
|
||||
@@ -345,3 +430,271 @@ def test_delivery_timeout_is_clamped(monkeypatch):
|
||||
|
||||
monkeypatch.setattr(delivery, "get_int_setting", lambda key, default: 100000)
|
||||
assert delivery.timeout_seconds() == float(delivery.MAX_TIMEOUT_SECONDS)
|
||||
|
||||
|
||||
def test_apns_parse_registration_accepts_client_id_and_normalizes_token():
|
||||
from devplacepy.push import providers
|
||||
|
||||
apns = providers.PROVIDERS["apns"]
|
||||
token = "A1B2C3D4" * 8
|
||||
assert apns.parse_registration(
|
||||
{"token": f"<{token[:8]} {token[8:]}>", "client_id": " device-1 "}
|
||||
) == {"token": token.lower(), "client_id": "device-1"}
|
||||
assert apns.parse_registration({"token": token, "client_id": ""}) == {
|
||||
"token": token.lower()
|
||||
}
|
||||
assert apns.parse_registration({"token": token, "client_id": {"uid": "x"}}) is None
|
||||
assert apns.parse_registration({"token": token, "client_id": "x" * 200}) is None
|
||||
assert apns.parse_registration({"client_id": "device-1"}) is None
|
||||
|
||||
|
||||
def test_apns_client_config_advertises_environment(monkeypatch):
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(monkeypatch, **{apns.ENVIRONMENT_KEY: "sandbox"})
|
||||
assert providers.PROVIDERS["apns"].client_config() == {"environment": "sandbox"}
|
||||
_apns_settings(monkeypatch, **{apns.ENVIRONMENT_KEY: "nonsense"})
|
||||
assert providers.PROVIDERS["apns"].client_config() == {"environment": "production"}
|
||||
|
||||
|
||||
def test_apns_stamp_registration_sets_environment(monkeypatch):
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(monkeypatch, **{apns.ENVIRONMENT_KEY: "sandbox"})
|
||||
stamped = providers.PROVIDERS["apns"].stamp_registration({"token": "a" * 64})
|
||||
assert stamped["environment"] == "sandbox"
|
||||
|
||||
|
||||
def test_apns_delivery_uses_registration_environment(monkeypatch):
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(
|
||||
monkeypatch,
|
||||
**{
|
||||
apns.TEAM_ID_KEY: "TEAMID1234",
|
||||
apns.KEY_ID_KEY: "KEYID12345",
|
||||
apns.AUTH_KEY_KEY: _ec_private_key_pem(),
|
||||
apns.TOPIC_KEY: "nl.molodetz.devplace",
|
||||
apns.ENVIRONMENT_KEY: "production",
|
||||
},
|
||||
)
|
||||
seen = {}
|
||||
|
||||
def handler(request):
|
||||
seen["url"] = str(request.url)
|
||||
return __import__("httpx").Response(200, json={})
|
||||
|
||||
async def run():
|
||||
import httpx
|
||||
|
||||
transport = httpx.MockTransport(handler)
|
||||
async with httpx.AsyncClient(transport=transport) as client:
|
||||
return await providers.PROVIDERS["apns"].deliver(
|
||||
client,
|
||||
{"token": "a" * 64, "environment": "sandbox"},
|
||||
"{}",
|
||||
)
|
||||
|
||||
from tests.conftest import run_async
|
||||
|
||||
outcome = run_async(run())
|
||||
assert outcome.status == providers.ACCEPTED
|
||||
assert seen["url"].startswith("https://api.sandbox.push.apple.com/3/device/")
|
||||
|
||||
|
||||
def test_apns_delivery_client_is_plain_http2():
|
||||
import httpx
|
||||
from devplacepy.curl_transport import CurlTransport
|
||||
from devplacepy.push.providers import apns
|
||||
from tests.conftest import run_async
|
||||
|
||||
client = apns.gateway_client(5.0)
|
||||
assert isinstance(client, httpx.AsyncClient)
|
||||
assert not isinstance(client._transport, CurlTransport)
|
||||
run_async(client.aclose())
|
||||
|
||||
|
||||
def test_apns_delivery_client_persists_across_calls():
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.providers import apns
|
||||
from tests.conftest import run_async
|
||||
|
||||
provider = providers.PROVIDERS["apns"]
|
||||
assert provider.closes_delivery_client() is False
|
||||
run_async(apns.close_client())
|
||||
try:
|
||||
first = provider.delivery_client(5.0)
|
||||
second = provider.delivery_client(5.0)
|
||||
assert first is second
|
||||
assert not first.is_closed
|
||||
run_async(apns.close_client())
|
||||
assert first.is_closed
|
||||
third = provider.delivery_client(5.0)
|
||||
assert third is not first
|
||||
finally:
|
||||
run_async(apns.close_client())
|
||||
|
||||
|
||||
def test_webpush_closes_delivery_client_by_default():
|
||||
from devplacepy.push import providers
|
||||
|
||||
provider = providers.PROVIDERS["webpush"]
|
||||
assert provider.closes_delivery_client() is True
|
||||
|
||||
|
||||
def test_store_upserts_by_client_id_and_revives_dead_tokens(local_db):
|
||||
from devplacepy.push import store
|
||||
|
||||
user_uid = "user-apns-1"
|
||||
token = "a" * 64
|
||||
write = store.register(user_uid, "apns", {"token": token, "client_id": "phone"})
|
||||
assert write.created is True
|
||||
assert write.revived is False
|
||||
assert write.probe is True
|
||||
|
||||
same = store.register(user_uid, "apns", {"token": token, "client_id": "phone"})
|
||||
assert same.created is False
|
||||
assert same.revived is False
|
||||
assert same.record["uid"] == write.record["uid"]
|
||||
|
||||
store.mark_dead(write.record["id"])
|
||||
revived = store.register(user_uid, "apns", {"token": token, "client_id": "phone"})
|
||||
assert revived.created is False
|
||||
assert revived.revived is True
|
||||
assert revived.record["deleted_at"] is None
|
||||
assert len(store.active_for_user(user_uid)) == 1
|
||||
|
||||
rotated = store.register(
|
||||
user_uid, "apns", {"token": "b" * 64, "client_id": "phone"}
|
||||
)
|
||||
assert rotated.created is False
|
||||
assert rotated.record["token"] == "b" * 64
|
||||
assert rotated.record["uid"] == write.record["uid"]
|
||||
assert len(store.active_for_user(user_uid)) == 1
|
||||
|
||||
|
||||
def test_store_revives_token_only_registration(local_db):
|
||||
from devplacepy.push import store
|
||||
|
||||
user_uid = "user-apns-2"
|
||||
token = "c" * 64
|
||||
write = store.register(user_uid, "apns", {"token": token})
|
||||
store.mark_dead(write.record["id"])
|
||||
revived = store.register(user_uid, "apns", {"token": token, "client_id": "phone-2"})
|
||||
assert revived.revived is True
|
||||
assert revived.record["client_id"] == "phone-2"
|
||||
assert len(store.active_for_user(user_uid)) == 1
|
||||
|
||||
|
||||
def test_store_attaches_client_id_to_existing_token_row(local_db):
|
||||
from devplacepy.push import store
|
||||
|
||||
user_uid = "user-apns-3"
|
||||
token = "d" * 64
|
||||
first = store.register(user_uid, "apns", {"token": token})
|
||||
second = store.register(user_uid, "apns", {"token": token, "client_id": "phone-3"})
|
||||
assert second.created is False
|
||||
assert second.record["uid"] == first.record["uid"]
|
||||
assert second.record["client_id"] == "phone-3"
|
||||
|
||||
|
||||
def test_store_mark_dead_skips_a_registration_revived_after_confirmation(local_db):
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from devplacepy.push import store
|
||||
|
||||
user_uid = "user-apns-4"
|
||||
token = "f" * 64
|
||||
write = store.register(user_uid, "apns", {"token": token})
|
||||
registered_at = write.record["registered_at"]
|
||||
assert registered_at
|
||||
|
||||
stale_confirmation = (
|
||||
datetime.fromisoformat(registered_at) - timedelta(minutes=5)
|
||||
).isoformat()
|
||||
store.mark_dead(write.record["id"], stale_confirmation)
|
||||
assert len(store.active_for_user(user_uid)) == 1
|
||||
|
||||
fresh_confirmation = (
|
||||
datetime.now(timezone.utc) + timedelta(minutes=5)
|
||||
).isoformat()
|
||||
store.mark_dead(write.record["id"], fresh_confirmation)
|
||||
assert store.active_for_user(user_uid) == []
|
||||
|
||||
|
||||
def test_dead_delivery_logs_the_reason(monkeypatch, caplog):
|
||||
import logging
|
||||
import httpx
|
||||
from tests.conftest import run_async
|
||||
from devplacepy.push import providers
|
||||
from devplacepy.push.delivery import _deliver_one
|
||||
|
||||
class _FakeStore:
|
||||
def __init__(self):
|
||||
self.dead = []
|
||||
|
||||
def mark_dead(self, registration_id, dead_before=None):
|
||||
self.dead.append(registration_id)
|
||||
|
||||
fake = _FakeStore()
|
||||
monkeypatch.setattr("devplacepy.push.delivery.store", fake)
|
||||
|
||||
async def dead_deliver(client, registration, prepared):
|
||||
return providers.Delivery(providers.DEAD, "400 BadDeviceToken")
|
||||
|
||||
provider = providers.PROVIDERS["apns"]
|
||||
monkeypatch.setattr(provider, "deliver", dead_deliver)
|
||||
|
||||
async def run():
|
||||
async with httpx.AsyncClient() as client:
|
||||
return await _deliver_one(
|
||||
provider, client, {"id": 9, "token": "a" * 64}, "{}", "user-x"
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
outcome = run_async(run())
|
||||
assert outcome.status == providers.DEAD
|
||||
assert fake.dead == [9]
|
||||
assert "400 BadDeviceToken" in caplog.text
|
||||
|
||||
|
||||
def test_notify_user_opens_apns_gateway_not_stealth(monkeypatch, local_db):
|
||||
import httpx
|
||||
from tests.conftest import run_async
|
||||
from devplacepy.push import store
|
||||
from devplacepy.push.delivery import notify_user
|
||||
from devplacepy.push.providers import apns
|
||||
|
||||
_apns_settings(
|
||||
monkeypatch,
|
||||
**{
|
||||
apns.TEAM_ID_KEY: "TEAMID1234",
|
||||
apns.KEY_ID_KEY: "KEYID12345",
|
||||
apns.AUTH_KEY_KEY: _ec_private_key_pem(),
|
||||
apns.TOPIC_KEY: "nl.molodetz.devplace",
|
||||
},
|
||||
)
|
||||
store.register("user-gw", "apns", {"token": "e" * 64, "environment": "production"})
|
||||
opened = []
|
||||
|
||||
def fake_gateway(timeout):
|
||||
opened.append(timeout)
|
||||
return httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(lambda request: httpx.Response(200, json={}))
|
||||
)
|
||||
|
||||
def fail_stealth(**kwargs):
|
||||
raise AssertionError("APNs must not use stealth")
|
||||
|
||||
monkeypatch.setattr(apns, "gateway_client", fake_gateway)
|
||||
monkeypatch.setattr(
|
||||
"devplacepy.stealth.stealth_async_client", fail_stealth
|
||||
)
|
||||
run_async(apns.close_client())
|
||||
try:
|
||||
run_async(notify_user("user-gw", {"title": "t", "message": "m"}))
|
||||
finally:
|
||||
run_async(apns.close_client())
|
||||
assert opened
|
||||
|
||||
@@ -150,3 +150,69 @@ def test_base_seo_context_consumes_ready_metadata(monkeypatch):
|
||||
assert "Generated Title" in ctx["page_title"]
|
||||
assert ctx["meta_description"] == "Generated clean description"
|
||||
assert ctx["meta_keywords"] == "generated, metadata"
|
||||
|
||||
|
||||
def _comment_item(uid, content, children=None, author="alice"):
|
||||
return {
|
||||
"comment": {"uid": uid, "content": content, "created_at": "2026-01-01T00:00:00+00:00"},
|
||||
"author": {"username": author},
|
||||
"children": children or [],
|
||||
}
|
||||
|
||||
|
||||
def test_comment_schema_shapes_a_comment_type():
|
||||
item = _comment_item("c1", "Nice post, thanks!")
|
||||
schema = seo.comment_schema(item, "https://x.test")
|
||||
assert schema["@type"] == "Comment"
|
||||
assert schema["text"] == "Nice post, thanks!"
|
||||
assert schema["author"] == {
|
||||
"@type": "Person",
|
||||
"name": "alice",
|
||||
"url": "https://x.test/profile/alice",
|
||||
}
|
||||
assert schema["datePublished"] == "2026-01-01T00:00:00+00:00"
|
||||
|
||||
|
||||
def test_comment_schema_handles_a_missing_author():
|
||||
item = _comment_item("c1", "text", author=None)
|
||||
item["author"] = None
|
||||
schema = seo.comment_schema(item, "https://x.test")
|
||||
assert schema["author"] == {"@type": "Person", "name": "Unknown", "url": ""}
|
||||
|
||||
|
||||
def test_comment_schema_list_flattens_replies_depth_first():
|
||||
reply = _comment_item("c2", "a reply")
|
||||
top = _comment_item("c1", "a top-level comment", children=[reply])
|
||||
flat = seo.comment_schema_list([top], "https://x.test")
|
||||
assert [c["text"] for c in flat] == ["a top-level comment", "a reply"]
|
||||
|
||||
|
||||
def test_comment_schema_list_respects_the_limit():
|
||||
tree = [_comment_item(f"c{i}", f"comment {i}") for i in range(30)]
|
||||
flat = seo.comment_schema_list(tree, "https://x.test", limit=5)
|
||||
assert len(flat) == 5
|
||||
|
||||
|
||||
def test_discussion_forum_posting_omits_comment_key_without_comments():
|
||||
schema = seo.discussion_forum_posting(
|
||||
{"uid": "p1", "title": "T", "content": "body", "created_at": "2026-01-01"},
|
||||
{"username": "alice"},
|
||||
0,
|
||||
0,
|
||||
"https://x.test",
|
||||
)
|
||||
assert "comment" not in schema
|
||||
|
||||
|
||||
def test_discussion_forum_posting_embeds_nested_comments():
|
||||
top = _comment_item("c1", "a top-level comment")
|
||||
schema = seo.discussion_forum_posting(
|
||||
{"uid": "p1", "title": "T", "content": "body", "created_at": "2026-01-01"},
|
||||
{"username": "alice"},
|
||||
1,
|
||||
0,
|
||||
"https://x.test",
|
||||
comments=seo.comment_schema_list([top], "https://x.test"),
|
||||
)
|
||||
assert schema["comment"][0]["@type"] == "Comment"
|
||||
assert schema["comment"][0]["text"] == "a top-level comment"
|
||||
|
||||
@@ -64,6 +64,22 @@ def test_dims_reflects_embedding_width():
|
||||
store.drop()
|
||||
|
||||
|
||||
def test_hybrid_search_results_carry_embedding_vectors():
|
||||
store = VectorStore("ds_store_test_embeddings")
|
||||
try:
|
||||
chunks = _chunks()
|
||||
vectors = local_embed([c.text for c in chunks]).vectors
|
||||
run_async(store.add(chunks, vectors))
|
||||
query = "where was the transistor invented"
|
||||
query_vector = local_embed([query]).vectors[0]
|
||||
results = run_async(store.hybrid_search(query, query_vector, top_k=3))
|
||||
assert results
|
||||
assert all(result.embedding for result in results)
|
||||
assert all(len(result.embedding) == len(vectors[0]) for result in results)
|
||||
finally:
|
||||
store.drop()
|
||||
|
||||
|
||||
def test_coverage_analytics_empty_collection():
|
||||
store = VectorStore("ds_store_test_cov_empty")
|
||||
try:
|
||||
|
||||
@@ -393,8 +393,27 @@ def test_unlocked_crops_gates_on_mastery_and_level():
|
||||
|
||||
def test_crop_payload_locked_by_mastery():
|
||||
distsys = economy.crop_for("distsys")
|
||||
assert economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=0)["locked"] is True
|
||||
assert economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=1)["locked"] is False
|
||||
locked = economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=0)
|
||||
assert locked["locked"] is True
|
||||
assert locked["locked_reason"] == "mastery"
|
||||
assert "Mastery" in locked["locked_text"]
|
||||
unlocked = economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=1)
|
||||
assert unlocked["locked"] is False
|
||||
assert unlocked["locked_reason"] == ""
|
||||
|
||||
|
||||
def test_crop_payload_locked_by_level_reason():
|
||||
rust = economy.crop_for("rust")
|
||||
locked = economy.crop_payload(rust, 1, 1)
|
||||
assert locked["locked_reason"] == "level"
|
||||
assert str(rust.min_level) in locked["locked_text"]
|
||||
|
||||
|
||||
def test_crop_lock_reason_matches_plant_enforcement():
|
||||
distsys = economy.crop_for("distsys")
|
||||
assert economy.crop_lock_reason(distsys, economy.MAX_LEVEL, 0) == ("mastery", "unlocks after reaching Mastery (Refactor to prestige 50)")
|
||||
assert economy.crop_lock_reason(distsys, economy.MAX_LEVEL, 1) is None
|
||||
assert economy.crop_lock_reason(distsys, 1, 1)[0] == "level"
|
||||
|
||||
|
||||
def test_secfort_crop_is_steal_immune():
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import json
|
||||
|
||||
from tests.conftest import run_async
|
||||
|
||||
from devplacepy.services.jobs.deepsearch import enhance as enhance_module
|
||||
from devplacepy.services.jobs.deepsearch.enhance import plan_followup_queries
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
def __init__(self, status_code, payload):
|
||||
self.status_code = status_code
|
||||
self._payload = payload
|
||||
|
||||
def json(self):
|
||||
return self._payload
|
||||
|
||||
|
||||
class _FakeClient:
|
||||
def __init__(self, response):
|
||||
self._response = response
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *exc):
|
||||
return False
|
||||
|
||||
async def post(self, *args, **kwargs):
|
||||
return self._response
|
||||
|
||||
|
||||
def _client_returning(response):
|
||||
def factory(**kwargs):
|
||||
return _FakeClient(response)
|
||||
|
||||
return factory
|
||||
|
||||
|
||||
def test_plan_followup_queries_empty_without_covered_titles():
|
||||
result = run_async(plan_followup_queries("q", [], "k"))
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_plan_followup_queries_parses_gateway_response(monkeypatch):
|
||||
payload = {"choices": [{"message": {"content": json.dumps({"queries": ["a", "b"]})}}]}
|
||||
monkeypatch.setattr(
|
||||
enhance_module.stealth,
|
||||
"stealth_async_client",
|
||||
_client_returning(_FakeResponse(200, payload)),
|
||||
)
|
||||
result = run_async(plan_followup_queries("q", ["Existing source"], "k"))
|
||||
assert result == ["a", "b"]
|
||||
|
||||
|
||||
def test_plan_followup_queries_empty_when_sources_already_cover_question(monkeypatch):
|
||||
payload = {"choices": [{"message": {"content": json.dumps({"queries": []})}}]}
|
||||
monkeypatch.setattr(
|
||||
enhance_module.stealth,
|
||||
"stealth_async_client",
|
||||
_client_returning(_FakeResponse(200, payload)),
|
||||
)
|
||||
result = run_async(plan_followup_queries("q", ["Existing source"], "k"))
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_plan_followup_queries_fails_soft_on_gateway_error(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
enhance_module.stealth,
|
||||
"stealth_async_client",
|
||||
_client_returning(_FakeResponse(500, {})),
|
||||
)
|
||||
result = run_async(plan_followup_queries("q", ["Existing source"], "k"))
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_plan_followup_queries_caps_at_max(monkeypatch):
|
||||
payload = {
|
||||
"choices": [
|
||||
{"message": {"content": json.dumps({"queries": ["a", "b", "c", "d", "e"]})}}
|
||||
]
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
enhance_module.stealth,
|
||||
"stealth_async_client",
|
||||
_client_returning(_FakeResponse(200, payload)),
|
||||
)
|
||||
result = run_async(plan_followup_queries("q", ["Existing source"], "k"))
|
||||
assert len(result) == enhance_module.MAX_FOLLOWUP_QUERIES
|
||||
@@ -4,9 +4,16 @@ import json
|
||||
|
||||
from tests.conftest import run_async
|
||||
|
||||
from devplacepy.services.deepsearch.store import Chunk
|
||||
from devplacepy.services.jobs.deepsearch import orchestrate as orchestrate_module
|
||||
from devplacepy.services.jobs.deepsearch.crawl import CrawledPage
|
||||
from devplacepy.services.jobs.deepsearch.orchestrate import Orchestration, orchestrate, source_diversity
|
||||
from devplacepy.services.jobs.deepsearch.orchestrate import (
|
||||
Orchestration,
|
||||
_cosine,
|
||||
_mmr_select,
|
||||
orchestrate,
|
||||
source_diversity,
|
||||
)
|
||||
|
||||
|
||||
def _page(url, text="content " * 30):
|
||||
@@ -149,6 +156,67 @@ def test_linker_receives_full_source_list(monkeypatch):
|
||||
assert f"[{n}]" in seen["linker"]
|
||||
|
||||
|
||||
def test_cosine_identical_vectors_is_one():
|
||||
assert round(_cosine([1.0, 0.0], [1.0, 0.0]), 6) == 1.0
|
||||
|
||||
|
||||
def test_cosine_orthogonal_vectors_is_zero():
|
||||
assert _cosine([1.0, 0.0], [0.0, 1.0]) == 0.0
|
||||
|
||||
|
||||
def test_cosine_mismatched_or_empty_is_zero():
|
||||
assert _cosine([], [1.0]) == 0.0
|
||||
assert _cosine([1.0], [1.0, 0.0]) == 0.0
|
||||
|
||||
|
||||
def test_mmr_select_prefers_diverse_over_redundant():
|
||||
query_vector = [1.0, 0.0, 0.0]
|
||||
most_relevant = Chunk(uid="a", text="a", url="https://a", title="A", embedding=[0.9, 0.436, 0.0])
|
||||
near_duplicate = Chunk(uid="b", text="b", url="https://a2", title="B", embedding=[0.85, 0.527, 0.0])
|
||||
diverse = Chunk(uid="c", text="c", url="https://c", title="C", embedding=[0.85, 0.0, 0.527])
|
||||
selected = _mmr_select([most_relevant, near_duplicate, diverse], query_vector, limit=2)
|
||||
assert {chunk.uid for chunk in selected} == {"a", "c"}
|
||||
|
||||
|
||||
def test_mmr_select_falls_back_when_embeddings_missing():
|
||||
chunks = [Chunk(uid="a", text="a", url="https://a", title="A")]
|
||||
assert _mmr_select(chunks, [1.0, 0.0], limit=1) == chunks
|
||||
|
||||
|
||||
def test_mmr_select_empty_input():
|
||||
assert _mmr_select([], [1.0, 0.0], limit=3) == []
|
||||
|
||||
|
||||
def test_orchestrate_grounded_run_includes_follow_up_questions(monkeypatch):
|
||||
replies = iter(
|
||||
[
|
||||
"## Answer\nA grounded answer [1].",
|
||||
json.dumps(
|
||||
{
|
||||
"findings": [
|
||||
{"title": "Finding", "detail": "Detail", "confidence": 0.7, "citations": [1]}
|
||||
]
|
||||
}
|
||||
),
|
||||
json.dumps({"confidence": 0.8}),
|
||||
json.dumps({"questions": ["What about X?", "How does Y compare?"]}),
|
||||
]
|
||||
)
|
||||
|
||||
async def fake_request_completion(messages, api_key, **kwargs):
|
||||
return ({"choices": [{"message": {"content": next(replies)}}]}, {}, 5)
|
||||
|
||||
monkeypatch.setattr(orchestrate_module, "request_completion", fake_request_completion)
|
||||
pages = [_page("https://a.example"), _page("https://b.example")]
|
||||
result = run_async(orchestrate("question", pages, "k", lambda frame: None))
|
||||
assert result.follow_up_questions == ["What about X?", "How does Y compare?"]
|
||||
|
||||
|
||||
def test_orchestrate_heuristic_path_has_no_follow_up_questions():
|
||||
result = run_async(orchestrate("q", [], "k", lambda frame: None))
|
||||
assert result.follow_up_questions == []
|
||||
|
||||
|
||||
def test_parse_json_tolerates_fences_and_trailing_garbage():
|
||||
from devplacepy.services.jobs.deepsearch.orchestrate import _parse_json
|
||||
|
||||
|
||||
@@ -20,13 +20,18 @@ def _patch_pipeline(monkeypatch, pages):
|
||||
async def fake_search(queries, emit=lambda frame: None):
|
||||
return [{"url": page.url, "title": page.title, "description": ""} for page in pages]
|
||||
|
||||
async def fake_crawl(candidates, max_pages, emit, is_cached, should_stop, query="", depth=1):
|
||||
outcome = CrawlOutcome()
|
||||
async def fake_crawl(
|
||||
candidates, max_pages, emit, is_cached, should_stop, query="", depth=1, seen_hashes=None
|
||||
):
|
||||
outcome = CrawlOutcome(seen_hashes=seen_hashes if seen_hashes is not None else set())
|
||||
for page in pages[:max_pages]:
|
||||
emit({"type": "page_loaded", "url": page.url, "done": 1, "total": len(pages)})
|
||||
outcome.pages.append(page)
|
||||
return outcome
|
||||
|
||||
async def fake_plan_followups(query, covered_titles, api_key, emit=lambda frame: None):
|
||||
return []
|
||||
|
||||
def fake_embed(texts, api_key):
|
||||
return local_embed(texts)
|
||||
|
||||
@@ -47,6 +52,7 @@ def _patch_pipeline(monkeypatch, pages):
|
||||
monkeypatch.setattr(worker_module, "plan_queries", fake_plan)
|
||||
monkeypatch.setattr(worker_module, "search_queries", fake_search)
|
||||
monkeypatch.setattr(worker_module, "crawl", fake_crawl)
|
||||
monkeypatch.setattr(worker_module, "plan_followup_queries", fake_plan_followups)
|
||||
monkeypatch.setattr(worker_module, "embed_texts", fake_embed_async)
|
||||
monkeypatch.setattr(worker_module, "orchestrate", fake_orchestrate)
|
||||
|
||||
@@ -141,6 +147,78 @@ def test_index_chunks_empty_pages_emits_done(monkeypatch):
|
||||
store.drop()
|
||||
|
||||
|
||||
def test_worker_run_performs_refinement_round_when_budget_remains(monkeypatch):
|
||||
initial_page = CrawledPage(
|
||||
url="https://example.com/a",
|
||||
title="Page A",
|
||||
text="The transistor was invented at Bell Labs. " * 20,
|
||||
source="httpx",
|
||||
status=200,
|
||||
)
|
||||
refined_page = CrawledPage(
|
||||
url="https://other.example/b",
|
||||
title="Page B",
|
||||
text="Semiconductors are made from silicon. " * 20,
|
||||
source="httpx",
|
||||
status=200,
|
||||
)
|
||||
_patch_pipeline(monkeypatch, [initial_page])
|
||||
|
||||
followup_calls = []
|
||||
|
||||
async def fake_plan_followups_once(query, covered_titles, api_key, emit=lambda frame: None):
|
||||
if followup_calls:
|
||||
return []
|
||||
followup_calls.append(covered_titles)
|
||||
return ["a more specific angle"]
|
||||
|
||||
async def fake_search_followup(queries, emit=lambda frame: None):
|
||||
return [{"url": refined_page.url, "title": refined_page.title, "description": ""}]
|
||||
|
||||
async def fake_crawl_refinement(
|
||||
candidates, max_pages, emit, is_cached, should_stop, query="", depth=1, seen_hashes=None
|
||||
):
|
||||
outcome = CrawlOutcome(seen_hashes=seen_hashes if seen_hashes is not None else set())
|
||||
outcome.pages.append(refined_page)
|
||||
return outcome
|
||||
|
||||
monkeypatch.setattr(worker_module, "plan_followup_queries", fake_plan_followups_once)
|
||||
|
||||
real_search_queries = worker_module.search_queries
|
||||
real_crawl = worker_module.crawl
|
||||
call_count = {"search": 0, "crawl": 0}
|
||||
|
||||
async def routed_search(queries, emit=lambda frame: None):
|
||||
call_count["search"] += 1
|
||||
if call_count["search"] == 1:
|
||||
return await real_search_queries(queries, emit)
|
||||
return await fake_search_followup(queries, emit)
|
||||
|
||||
async def routed_crawl(*args, **kwargs):
|
||||
call_count["crawl"] += 1
|
||||
if call_count["crawl"] == 1:
|
||||
return await real_crawl(*args, **kwargs)
|
||||
return await fake_crawl_refinement(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(worker_module, "search_queries", routed_search)
|
||||
monkeypatch.setattr(worker_module, "crawl", routed_crawl)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
output_dir = Path(tmp)
|
||||
payload = {
|
||||
"query": "history of the transistor",
|
||||
"max_pages": 5,
|
||||
"depth": 2,
|
||||
"api_key": "k",
|
||||
"collection": "ds_worker_refine_test",
|
||||
"cached_hashes": [],
|
||||
}
|
||||
report = run_async(worker_module._run(payload, output_dir))
|
||||
assert report["page_count"] == 2
|
||||
assert followup_calls and followup_calls[0] == ["Page A"]
|
||||
VectorStore("ds_worker_refine_test").drop()
|
||||
|
||||
|
||||
def test_worker_control_cancel_stops(monkeypatch):
|
||||
pages = [
|
||||
CrawledPage(url="https://x.example", title="X", text="content " * 40, source="httpx", status=200)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from devplacepy.database import get_table
|
||||
from devplacepy.utils import generate_uid
|
||||
from devplacepy.services.messaging.persist import stamp_content_revision
|
||||
|
||||
|
||||
def test_stamp_content_revision_sets_updated_at(local_db):
|
||||
uid = generate_uid()
|
||||
get_table("messages").insert(
|
||||
{
|
||||
"uid": uid,
|
||||
"sender_uid": "s1",
|
||||
"receiver_uid": "r1",
|
||||
"content": "hello",
|
||||
"read": False,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"updated_at": None,
|
||||
}
|
||||
)
|
||||
row = stamp_content_revision(uid)
|
||||
assert row is not None
|
||||
assert row["updated_at"]
|
||||
stored = get_table("messages").find_one(uid=uid)
|
||||
assert stored["updated_at"] == row["updated_at"]
|
||||
assert stored["content"] == "hello"
|
||||
Reference in New Issue
Block a user