Render markdown in gist and project sidebar titles; add content visibility and GIF picker

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 17:59:21 +01:00
co-authored by Claude Sonnet 5.5
parent bc11475f29
commit 05d6633204
154 changed files with 6394 additions and 289 deletions
+247
View File
@@ -0,0 +1,247 @@
# retoor <retoor@molodetz.nl>
import pytest
import dataset
from pydantic import ValidationError
from devplacepy.database import visibility as visibility_module
from devplacepy.database import (
DEFAULT_VISIBILITY,
LISTED_VALUES,
OPEN_VALUES,
SOFT_DELETE_TABLES,
TARGET_TABLES,
VISIBILITY_CHOICES,
VISIBILITY_COLUMNS,
VISIBILITY_HINTS,
VISIBILITY_LABELS,
VISIBILITY_VALUES,
is_listed,
is_open,
item_visibility,
listed_clause,
listed_sql,
normalize_visibility,
visibility_label,
visibility_options,
)
from devplacepy.models import (
GistEditForm,
GistForm,
PostEditForm,
PostForm,
ProjectEditForm,
ProjectForm,
VisibilityForm,
)
from devplacepy.schemas.content import GistOut, PostOut, ProjectOut
@pytest.fixture
def scratch(monkeypatch):
memory = dataset.connect("sqlite:///:memory:")
monkeypatch.setattr(visibility_module, "db", memory)
monkeypatch.setattr(visibility_module, "get_table", lambda name: memory[name])
yield memory
memory.close()
def test_the_four_statuses_and_the_default():
assert VISIBILITY_VALUES == ("published", "hidden", "draft", "offline")
assert DEFAULT_VISIBILITY == "published"
assert VISIBILITY_CHOICES == ("published", "hidden", "offline")
assert set(VISIBILITY_CHOICES) <= set(VISIBILITY_VALUES)
def test_listed_is_a_subset_of_open():
assert set(LISTED_VALUES) <= set(OPEN_VALUES)
assert LISTED_VALUES == ("published",)
assert OPEN_VALUES == ("published", "hidden")
def test_labels_and_hints_cover_every_value():
for value in VISIBILITY_VALUES:
assert VISIBILITY_LABELS[value]
assert VISIBILITY_HINTS[value]
assert visibility_label(value) == VISIBILITY_LABELS[value]
def test_options_expose_only_user_choices_by_default():
assert [o["value"] for o in visibility_options()] == list(VISIBILITY_CHOICES)
assert [o["value"] for o in visibility_options(VISIBILITY_VALUES)] == list(
VISIBILITY_VALUES
)
@pytest.mark.parametrize("raw", [None, "", " ", "nonsense", 0, "PUBLISHED "])
def test_normalize_falls_back_to_published_or_lowercases(raw):
expected = "published"
assert normalize_visibility(raw) == expected
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_normalize_is_idempotent(value):
assert normalize_visibility(normalize_visibility(value)) == value
assert normalize_visibility(value.upper()) == value
def test_every_visibility_table_is_a_soft_delete_table_with_a_target():
for table in VISIBILITY_COLUMNS:
assert table in SOFT_DELETE_TABLES
assert set(TARGET_TABLES.values()) == set(VISIBILITY_COLUMNS)
def test_news_uses_its_existing_status_column():
assert VISIBILITY_COLUMNS["news"] == "status"
for table in ("posts", "gists", "projects"):
assert VISIBILITY_COLUMNS[table] == "visibility"
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
def test_null_and_unknown_rows_read_as_published(table):
column = VISIBILITY_COLUMNS[table]
assert item_visibility(table, {}) == "published"
assert item_visibility(table, {column: None}) == "published"
assert item_visibility(table, {column: "weird"}) == "published"
assert is_listed(table, {column: None})
assert is_open(table, {column: None})
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
@pytest.mark.parametrize(
"value,listed,open_",
[
("published", True, True),
("hidden", False, True),
("offline", False, False),
("draft", False, False),
],
)
def test_listed_and_open_truth_table(table, value, listed, open_):
row = {VISIBILITY_COLUMNS[table]: value}
assert is_listed(table, row) is listed
assert is_open(table, row) is open_
def test_untracked_tables_are_always_open_and_listed():
assert item_visibility("quizzes", {"visibility": "offline"}) == "published"
assert is_listed("quizzes", {"visibility": "offline"})
assert listed_sql("quizzes") == "1 = 1"
def test_listed_sql_uses_coalesce_so_null_rows_stay_visible(scratch):
scratch["posts"].insert({"uid": "x", "visibility": "published"})
scratch["news"].insert({"uid": "y", "status": "published"})
assert "COALESCE(p.visibility, 'published') = 'published'" == listed_sql(
"posts", "p"
)
assert listed_sql("news") == "COALESCE(status, 'published') = 'published'"
def test_listed_clause_is_none_for_tables_without_a_visibility_column(scratch):
scratch["sessions"].insert({"uid": "x"})
assert listed_clause(scratch["sessions"]) is None
def test_listed_clause_filters_rows(scratch):
table = scratch["posts"]
table.insert(
{
"uid": "vis-unit-published",
"user_uid": "owner-1",
"visibility": "published",
"deleted_at": None,
}
)
table.insert(
{
"uid": "vis-unit-legacy",
"user_uid": "owner-1",
"visibility": None,
"deleted_at": None,
}
)
for value in ("hidden", "offline", "draft"):
table.insert(
{
"uid": f"vis-unit-{value}",
"user_uid": "owner-1",
"visibility": value,
"deleted_at": None,
}
)
wanted = {"vis-unit-published", "vis-unit-legacy"}
clause = listed_clause(table)
found = {r["uid"] for r in table.find(clause) if r["uid"].startswith("vis-unit-")}
assert found == wanted
own = {
r["uid"]
for r in table.find(listed_clause(table, own_uid="owner-1"))
if r["uid"].startswith("vis-unit-")
}
assert own == {f"vis-unit-{v}" for v in VISIBILITY_VALUES} | {"vis-unit-legacy"}
stranger = {
r["uid"]
for r in table.find(listed_clause(table, own_uid="someone-else"))
if r["uid"].startswith("vis-unit-")
}
assert stranger == wanted
CREATE_FORMS = (
(PostForm, {"content": "x" * 12}),
(GistForm, {"title": "t", "source_code": "c"}),
(ProjectForm, {"title": "t", "description": "d"}),
)
EDIT_FORMS = (
(PostEditForm, {"content": "x" * 12}),
(GistEditForm, {"title": "t", "source_code": "c"}),
(ProjectEditForm, {"title": "t", "description": "d"}),
)
@pytest.mark.parametrize("form,base", CREATE_FORMS)
def test_create_forms_default_to_published(form, base):
assert form(**base).visibility == "published"
assert form(**base, visibility="").visibility == "published"
@pytest.mark.parametrize("form,base", CREATE_FORMS)
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_create_forms_accept_every_status(form, base, value):
assert form(**base, visibility=value.upper()).visibility == value
@pytest.mark.parametrize("form,base", EDIT_FORMS)
def test_edit_forms_leave_visibility_unset_by_default(form, base):
assert form(**base).visibility is None
assert form(**base, visibility="").visibility is None
@pytest.mark.parametrize("form,base", EDIT_FORMS)
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_edit_forms_accept_every_status(form, base, value):
assert form(**base, visibility=value).visibility == value
@pytest.mark.parametrize("form,base", CREATE_FORMS + EDIT_FORMS)
def test_forms_reject_unknown_status(form, base):
with pytest.raises(ValidationError):
form(**base, visibility="secret")
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
def test_visibility_form_accepts_every_status(value):
assert VisibilityForm(value=value).value == value
def test_visibility_form_rejects_unknown_status():
with pytest.raises(ValidationError):
VisibilityForm(value="secret")
@pytest.mark.parametrize("schema", [PostOut, GistOut, ProjectOut])
def test_output_schemas_always_report_a_status(schema):
assert schema(uid="1").visibility == "published"
assert schema(uid="1", visibility=None).visibility == "published"
assert schema(uid="1", visibility="offline").visibility == "offline"
+37
View File
@@ -0,0 +1,37 @@
# retoor <retoor@molodetz.nl>
import pytest
from devplacepy.docs_api import API_GROUPS
ENDPOINTS = {e["id"]: e for g in API_GROUPS for e in g["endpoints"]}
WRITES = ("posts-create", "posts-edit", "gists-create", "gists-edit", "projects-create", "projects-edit")
def _field(endpoint_id):
return next(p for p in ENDPOINTS[endpoint_id]["params"] if p["name"] == "visibility")
@pytest.mark.parametrize("endpoint_id", WRITES)
def test_visibility_is_documented_optional_and_enumerated(endpoint_id):
field = _field(endpoint_id)
assert field["required"] is False
assert field["location"] == "form"
assert field["options"] == ["published", "hidden", "offline", "draft"]
@pytest.mark.parametrize("endpoint_id", ("posts-create", "gists-create", "projects-create"))
def test_create_docs_state_the_default(endpoint_id):
assert "published" in _field(endpoint_id)["description"]
assert "when omitted the item is published" in _field(endpoint_id)["description"]
@pytest.mark.parametrize("endpoint_id", ("posts-edit", "gists-edit", "projects-edit"))
def test_edit_docs_state_that_omitting_keeps_the_value(endpoint_id):
assert "when omitted the current value is kept" in _field(endpoint_id)["description"]
def test_admin_news_visibility_endpoint_is_documented_as_admin_only():
endpoint = ENDPOINTS["admin-news-visibility"]
assert endpoint["auth"] == "admin"
assert any(p["name"] == "value" and p["required"] for p in endpoint["params"])
+15
View File
@@ -56,3 +56,18 @@ def test_is_auth_error_detects_rclone_html_401():
)
assert offload._is_auth_error("didn't find section in config file")
assert not offload._is_auth_error("connection timed out")
def test_run_cycle_skips_clearly_when_rclone_binary_missing(monkeypatch):
monkeypatch.setattr(offload.config, "RCLONE_BIN", "/nonexistent/rclone-test-binary")
async def explode(*args, **kwargs):
raise AssertionError("rclone must not be spawned when the binary is missing")
monkeypatch.setattr(offload, "_run_rclone", explode)
logs = []
result = run_async(offload.run_cycle(keep_local=5, keep_remote=5, log=logs.append))
assert result == {"uploaded": 0, "purged_local": 0, "removed_remote": 0}
assert len(logs) == 1
assert "rclone binary not found" in logs[0]
assert "backup_offload_enabled" in logs[0]
+23
View File
@@ -1,7 +1,10 @@
# retoor <retoor@molodetz.nl>
import asyncio
from tests.conftest import run_async
from devplacepy.services.dbapi.progress import hub
from devplacepy.services.dbapi.service import DbApiJobService
@@ -30,3 +33,23 @@ def test_process_rejects_non_select(local_db):
finally:
svc.cleanup(job)
assert raised
def test_stream_publishes_progress_to_subscriber(local_db):
svc = DbApiJobService()
uid = "unit-dbquery-progress"
queue = hub.subscribe(uid)
try:
rows, truncated = run_async(svc._stream(uid, "SELECT 1 AS n", 5000))
assert rows == [{"n": 1}]
assert truncated is False
frames = []
while True:
try:
frames.append(queue.get_nowait())
except asyncio.QueueEmpty:
break
assert any(frame.get("type") == "progress" for frame in frames)
finally:
hub.unsubscribe(uid, queue)
hub.clear(uid)
+1
View File
@@ -0,0 +1 @@
# retoor <retoor@molodetz.nl>
+190
View File
@@ -0,0 +1,190 @@
# retoor <retoor@molodetz.nl>
import httpx
import pytest
from tests.conftest import run_async
from devplacepy.services.giphy import client as client_module
from devplacepy.services.giphy.client import (
MAX_LIMIT,
GiphyClient,
GiphyError,
clear_cache,
normalize_gif,
)
from devplacepy.services.giphy.config import GiphyConfig
CONFIG = GiphyConfig(api_key="secret-key", base_url="https://api.giphy.test/v1/gifs", rating="pg")
def _item(gif_id="abc", title=" Happy dance "):
return {
"id": gif_id,
"title": title,
"url": f"https://giphy.test/gifs/{gif_id}",
"images": {
"fixed_width": {"url": "https://m.giphy.test/200.gif", "width": "200", "height": "150", "size": "9"},
"original": {"url": "https://m.giphy.test/o.gif", "width": "480", "height": "360", "size": "99"},
},
}
class _Recorder:
def __init__(self, handler):
self.calls = []
self.handler = handler
def factory(self, **kwargs):
def respond(request):
self.calls.append(request)
return self.handler(request)
return httpx.AsyncClient(transport=httpx.MockTransport(respond))
@pytest.fixture(autouse=True)
def _fresh_cache():
clear_cache()
yield
clear_cache()
def _patch(monkeypatch, handler):
recorder = _Recorder(handler)
monkeypatch.setattr(client_module.stealth, "stealth_async_client", recorder.factory)
return recorder
def test_normalize_gif_picks_preview_and_original():
gif = normalize_gif(_item())
assert gif["id"] == "abc"
assert gif["title"] == "Happy dance"
assert gif["preview"] == {"url": "https://m.giphy.test/200.gif", "width": 200, "height": 150, "size": 9}
assert gif["original"]["url"] == "https://m.giphy.test/o.gif"
def test_normalize_gif_survives_missing_images():
gif = normalize_gif({"id": 7})
assert gif == {"id": "7", "title": "", "url": "", "preview": {}, "original": {}}
def test_normalize_gif_falls_back_when_a_rendition_is_missing():
item = _item()
del item["images"]["fixed_width"]
assert normalize_gif(item)["preview"]["url"] == "https://m.giphy.test/o.gif"
def test_search_sends_key_rating_and_clamped_limit(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": [_item()]}))
gifs = await GiphyClient(CONFIG).search("cat", limit=999)
assert [gif["id"] for gif in gifs] == ["abc"]
request = recorder.calls[0]
assert request.url.path == "/v1/gifs/search"
params = dict(request.url.params)
assert params["api_key"] == "secret-key"
assert params["rating"] == "pg"
assert params["q"] == "cat"
assert params["limit"] == str(MAX_LIMIT)
run_async(body())
def test_limit_has_a_floor_of_one(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": []}))
await GiphyClient(CONFIG).trending(limit=-5)
assert dict(recorder.calls[0].url.params)["limit"] == "1"
run_async(body())
def test_identical_requests_hit_the_upstream_once(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": [_item()]}))
client = GiphyClient(CONFIG)
for _ in range(5):
await client.search("cat")
assert len(recorder.calls) == 1
await client.search("dog")
assert len(recorder.calls) == 2
run_async(body())
def test_cache_is_shared_across_api_keys_but_not_across_ratings(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": []}))
await GiphyClient(CONFIG).search("cat")
await GiphyClient(GiphyConfig("other-key", CONFIG.base_url, "pg")).search("cat")
assert len(recorder.calls) == 1
await GiphyClient(GiphyConfig("secret-key", CONFIG.base_url, "r")).search("cat")
assert len(recorder.calls) == 2
run_async(body())
def test_errors_are_not_cached(monkeypatch):
async def body():
answers = iter([httpx.Response(500, text="boom"), httpx.Response(200, json={"data": []})])
recorder = _patch(monkeypatch, lambda request: next(answers))
client = GiphyClient(CONFIG)
with pytest.raises(GiphyError) as caught:
await client.trending()
assert caught.value.status == 500
assert await client.trending() == []
assert len(recorder.calls) == 2
run_async(body())
def test_transport_failure_becomes_a_giphy_error(monkeypatch):
async def body():
def refuse(request):
raise httpx.ConnectError("no route", request=request)
_patch(monkeypatch, refuse)
with pytest.raises(GiphyError) as caught:
await GiphyClient(CONFIG).trending()
assert caught.value.status == 0
run_async(body())
def test_unconfigured_client_refuses_without_a_request(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={}))
client = GiphyClient(GiphyConfig("", CONFIG.base_url, "pg"))
for call in (client.trending(), client.search("cat"), client.get_gif("abc")):
with pytest.raises(GiphyError) as caught:
await call
assert caught.value.status == 503
assert recorder.calls == []
run_async(body())
def test_get_gif_not_found_and_empty_id(monkeypatch):
async def body():
_patch(monkeypatch, lambda request: httpx.Response(200, json={"data": {}}))
client = GiphyClient(CONFIG)
with pytest.raises(GiphyError) as missing:
await client.get_gif("nope")
assert missing.value.status == 404
with pytest.raises(GiphyError) as empty:
await client.get_gif("")
assert empty.value.status == 400
run_async(body())
def test_get_gif_returns_the_normalized_item(monkeypatch):
async def body():
recorder = _patch(monkeypatch, lambda request: httpx.Response(200, json={"data": _item("xyz")}))
gif = await GiphyClient(CONFIG).get_gif("xyz")
assert gif["id"] == "xyz"
assert recorder.calls[0].url.path == "/v1/gifs/xyz"
run_async(body())
+84
View File
@@ -0,0 +1,84 @@
# retoor <retoor@molodetz.nl>
from devplacepy.services.giphy import limits
from devplacepy.services.giphy.limits import TtlCache, UserRateLimiter
def test_cache_returns_what_was_stored():
cache = TtlCache()
cache.put("a", {"x": 1})
assert cache.get("a") == {"x": 1}
assert cache.get("missing") is None
def test_cache_expires_entries(monkeypatch):
clock = [1000.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
cache = TtlCache(ttl_seconds=10)
cache.put("a", 1)
clock[0] += 9
assert cache.get("a") == 1
clock[0] += 2
assert cache.get("a") is None
assert len(cache) == 0
def test_cache_never_exceeds_its_entry_cap():
cache = TtlCache(max_entries=8)
for index in range(500):
cache.put(index, index)
assert len(cache) <= 8
assert cache.get(499) == 499
assert cache.get(0) is None
def test_cache_evicts_the_least_recently_used():
cache = TtlCache(max_entries=2)
cache.put("a", 1)
cache.put("b", 2)
assert cache.get("a") == 1
cache.put("c", 3)
assert cache.get("b") is None
assert cache.get("a") == 1
def test_limiter_allows_exactly_the_cap_then_refuses():
limiter = UserRateLimiter(max_calls=5, window_seconds=60)
results = [limiter.allow("u1") for _ in range(8)]
assert results == [True] * 5 + [False] * 3
def test_limiter_is_per_user():
limiter = UserRateLimiter(max_calls=1, window_seconds=60)
assert limiter.allow("u1") is True
assert limiter.allow("u1") is False
assert limiter.allow("u2") is True
def test_limiter_window_slides(monkeypatch):
clock = [0.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
limiter = UserRateLimiter(max_calls=2, window_seconds=60)
assert limiter.allow("u") and limiter.allow("u")
assert limiter.allow("u") is False
clock[0] += 61
assert limiter.allow("u") is True
def test_limiter_bounds_the_number_of_tracked_users():
limiter = UserRateLimiter(max_calls=1, window_seconds=60, max_tracked=16)
for index in range(1000):
limiter.allow(f"user-{index}")
assert len(limiter._calls) <= 16
def test_refused_calls_do_not_extend_the_window(monkeypatch):
clock = [0.0]
monkeypatch.setattr(limits.time, "monotonic", lambda: clock[0])
limiter = UserRateLimiter(max_calls=1, window_seconds=60)
assert limiter.allow("u") is True
for _ in range(50):
clock[0] += 1
assert limiter.allow("u") is False
clock[0] += 11
assert limiter.allow("u") is True
+80
View File
@@ -0,0 +1,80 @@
# retoor <retoor@molodetz.nl>
import pytest
from tests.conftest import run_async
from devplacepy.database import set_setting
from devplacepy.services.giphy import runtime
from devplacepy.services.giphy.client import GiphyError
from devplacepy.services.giphy.config import CONFIG_FIELDS, giphy_config
from devplacepy.services.giphy.fake import FakeGiphyClient
from devplacepy.services.giphy.service import GiphyService
@pytest.fixture
def settings(local_db):
yield
for key in ("giphy_api_key", "giphy_base_url", "giphy_rating"):
set_setting(key, "")
runtime.set_client(None)
def test_config_fields_declare_the_secret_key_and_rating_choices():
fields = {field.key: field for field in CONFIG_FIELDS}
assert fields["giphy_api_key"].secret is True
assert fields["giphy_api_key"].default == ""
assert fields["giphy_rating"].options == ["g", "pg", "pg-13", "r"]
assert GiphyService.config_fields is CONFIG_FIELDS
assert GiphyService.default_enabled is False
def test_config_reads_settings_and_applies_defaults(settings):
assert giphy_config().is_configured is False
set_setting("giphy_api_key", "k")
set_setting("giphy_rating", "")
config = giphy_config()
assert config.is_configured is True
assert config.rating == "pg"
assert config.base_url.startswith("https://api.giphy.com")
def test_run_once_is_quiet_without_a_key(settings):
async def body():
service = GiphyService()
runtime.set_client(FakeGiphyClient())
await service.run_once()
run_async(body())
def test_run_once_probes_trending_when_configured(settings):
async def body():
set_setting("giphy_api_key", "k")
calls = []
class Probe(FakeGiphyClient):
async def trending(self, limit=24):
calls.append(limit)
return await super().trending(limit)
runtime.set_client(Probe())
await GiphyService().run_once()
assert calls == [1]
run_async(body())
def test_run_once_survives_an_upstream_failure(settings):
async def body():
set_setting("giphy_api_key", "k")
class Broken(FakeGiphyClient):
async def trending(self, limit=24):
raise GiphyError("down", status=502)
runtime.set_client(Broken())
await GiphyService().run_once()
run_async(body())
@@ -3006,3 +3006,207 @@ def test_responses_protocol_default_provider(local_db, monkeypatch):
assert resp.status_code == 200
payload = json.loads(bytes(resp.body).decode())
assert payload["object"] == "chat.completion"
EMULATED_CALL_TEXT_openai_gateway = (
"Sure.\n<tool_call>\n"
'{"name": "bash", "arguments": {"command": "ls"}}\n'
"</tool_call>"
)
BASH_TOOL_openai_gateway = {
"type": "function",
"function": {
"name": "bash",
"description": "Run a shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
}
def _emulated_obj_openai_gateway(model, text):
obj = _resp_obj_openai_gateway(model)
obj["output"][0]["content"][0]["text"] = text
return obj
class FakeEmulatedClient_openai_gateway(FakeResponsesClient_openai_gateway):
text = EMULATED_CALL_TEXT_openai_gateway
async def send(self, request, stream=False):
self.calls.append((request.url, request.json_body))
model = (request.json_body or {}).get("model", "")
obj = _emulated_obj_openai_gateway(model, self.text)
events = [{"type": "response.created", "response": {"id": "resp_1"}}]
for i in range(0, len(self.text), 4):
events.append(
{"type": "response.output_text.delta", "delta": self.text[i : i + 4]}
)
events.append({"type": "response.completed", "response": obj})
return FakeResponsesResp_openai_gateway(payload=obj, events=events)
def _setup_emulated_route_openai_gateway():
from devplacepy.services.openai_gateway import routing
routing.provider_store.set(
routing.ProviderIn(
name="gwmeta",
base_url="https://api.meta.ai/v1/responses",
api_key="sk-test",
protocol="responses",
)
)
routing.model_store.set(
routing.ModelRouteIn(
source_model="meta-chat",
provider="gwmeta",
target_model="muse-spark-1.3-contributor",
kind="chat",
tools_mode="emulated",
)
)
def _emulated_chat_openai_gateway(monkeypatch, body, **tools):
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeEmulatedClient_openai_gateway)
_setup_emulated_route_openai_gateway()
svc = GatewayService()
cfg = svc.effective_config()
cfg["gateway_vision_enabled"] = False
rt = svc.runtime()
resp = run_async(rt.handle_chat(body, cfg, ("guest", "test"), "test", "default"))
return rt, resp
def test_emulated_tools_meta_responses_nonstream(local_db, monkeypatch):
body = _chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway], tool_choice="auto")
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
url, sent = rt._client.calls[-1]
assert str(url) == "https://api.meta.ai/v1/responses"
assert "tools" not in sent and "tool_choice" not in sent
assert "<tools>" in sent["instructions"] and '"bash"' in sent["instructions"]
assert resp.status_code == 200
message = json.loads(bytes(resp.body).decode())["choices"][0]
assert message["finish_reason"] == "tool_calls"
call = message["message"]["tool_calls"][0]
assert call["function"]["name"] == "bash"
assert json.loads(call["function"]["arguments"]) == {"command": "ls"}
assert message["message"]["content"] == "Sure."
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_meta_responses_stream(local_db, monkeypatch):
body = _chat_body_openai_gateway(
stream=True, tools=[BASH_TOOL_openai_gateway]
)
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
async def drain():
out = []
async for chunk in resp.body_iterator:
out.append(chunk if isinstance(chunk, str) else chunk.decode())
return "".join(out)
raw = run_async(drain())
chunks = [
json.loads(line[5:])
for line in raw.splitlines()
if line.startswith("data:") and "[DONE]" not in line
]
text = "".join(
c["choices"][0]["delta"].get("content", "")
for c in chunks
if c.get("choices")
)
calls = [
tc
for c in chunks
for choice in c.get("choices") or []
for tc in choice["delta"].get("tool_calls") or []
]
assert "<tool_call>" not in text and text.strip() == "Sure."
assert len(calls) == 1 and calls[0]["function"]["name"] == "bash"
finishes = [
c["choices"][0]["finish_reason"]
for c in chunks
if c.get("choices") and c["choices"][0].get("finish_reason")
]
assert finishes[-1] == "tool_calls"
assert raw.rstrip().endswith("[DONE]")
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_convert_history_for_the_meta_transpiler(local_db, monkeypatch):
history = [
{"role": "user", "content": "go"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
}
],
},
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
]
body = _chat_body_openai_gateway(messages=history, tools=[BASH_TOOL_openai_gateway])
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
_, sent = rt._client.calls[-1]
assert resp.status_code == 200
assert all(item.get("type") == "message" for item in sent["input"])
joined = json.dumps(sent["input"])
assert "<tool_call>" in joined and "<tool_response>" in joined and "a.txt" in joined
assert "function_call" not in joined
finally:
_teardown_responses_route_openai_gateway()
def test_emulated_tools_over_the_cap_is_a_400_with_no_upstream_call(local_db, monkeypatch):
from devplacepy.services.openai_gateway import tool_emulation
body = _chat_body_openai_gateway(
tools=[BASH_TOOL_openai_gateway] * (tool_emulation.MAX_TOOLS + 1)
)
try:
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
assert resp.status_code == 400
assert rt._client.calls == []
finally:
_teardown_responses_route_openai_gateway()
def test_native_route_keeps_forwarding_tools(local_db, monkeypatch):
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway)
FakeResponsesClient_openai_gateway.behavior = "ok"
_setup_responses_route_openai_gateway()
try:
svc = GatewayService()
cfg = svc.effective_config()
cfg["gateway_vision_enabled"] = False
rt = svc.runtime()
run_async(
rt.handle_chat(
_chat_body_openai_gateway(tools=[BASH_TOOL_openai_gateway]),
cfg,
("guest", "test"),
"test",
"default",
)
)
_, sent = rt._client.calls[-1]
assert "tools" in sent
assert "<tools>" not in sent["instructions"]
finally:
_teardown_responses_route_openai_gateway()
@@ -0,0 +1,286 @@
# retoor <retoor@molodetz.nl>
import json
import pytest
from devplacepy.services.openai_gateway import tool_emulation as te
BASH = {
"type": "function",
"function": {
"name": "bash",
"description": "Run a shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
}
def _payload(**over):
body = {
"model": "m",
"messages": [
{"role": "system", "content": "Be terse."},
{"role": "user", "content": "list files"},
],
"tools": [BASH],
}
body.update(over)
return body
def _block(name="bash", arguments=None):
arguments = {"command": "ls"} if arguments is None else arguments
return f'{te.CALL_OPEN}\n{json.dumps({"name": name, "arguments": arguments})}\n{te.CALL_CLOSE}'
def test_request_strips_native_fields_and_injects_catalog():
out = te.emulate_request(_payload(tool_choice="auto", parallel_tool_calls=False))
for key in te.REMOVED_PAYLOAD_KEYS:
assert key not in out
system = out["messages"][0]["content"]
assert system.startswith("Be terse.")
assert '"name": "bash"' in system
assert te.CALL_OPEN in system
assert "at most one tool call" in system
assert out["stop"] == [te.RESPONSE_OPEN]
def test_request_does_not_mutate_the_input():
original = _payload()
snapshot = json.dumps(original, sort_keys=True)
te.emulate_request(original)
assert json.dumps(original, sort_keys=True) == snapshot
def test_system_message_is_created_when_absent():
out = te.emulate_request(_payload(messages=[{"role": "user", "content": "hi"}]))
assert out["messages"][0]["role"] == "system"
assert "<tools>" in out["messages"][0]["content"]
def test_tool_choice_none_leaves_the_catalog_out():
out = te.emulate_request(_payload(tool_choice="none"))
assert "<tools>" not in out["messages"][0]["content"]
def test_tool_choice_required_and_named_add_a_directive():
required = te.emulate_request(_payload(tool_choice="required"))
assert "MUST call at least one tool" in required["messages"][0]["content"]
named = te.emulate_request(
_payload(tool_choice={"type": "function", "function": {"name": "bash"}})
)
assert 'MUST call the tool "bash"' in named["messages"][0]["content"]
def test_history_roundtrips_into_the_same_format_the_model_emits():
history = [
{"role": "user", "content": "go"},
{
"role": "assistant",
"content": "ok",
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "ls"}'},
},
{
"id": "call_2",
"type": "function",
"function": {"name": "bash", "arguments": '{"command": "pwd"}'},
},
],
},
{"role": "tool", "tool_call_id": "call_1", "content": "a.txt"},
{"role": "tool", "tool_call_id": "call_2", "content": "/tmp"},
]
out = te.emulate_request(_payload(messages=history))
roles = [m["role"] for m in out["messages"]]
assert roles == ["system", "user", "assistant", "user"]
assistant = out["messages"][2]
assert "tool_calls" not in assistant
assert assistant["content"].count(te.CALL_OPEN) == 2
results = out["messages"][3]["content"]
assert results.count(te.RESPONSE_OPEN) == 2
assert '"name": "bash"' in results and "a.txt" in results
def test_caps_reject_loudly():
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[BASH] * (te.MAX_TOOLS + 1)))
huge = json.loads(json.dumps(BASH))
huge["function"]["description"] = "x" * (te.MAX_CATALOG_CHARS + 1)
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[huge]))
with pytest.raises(te.ToolEmulationError):
te.emulate_request(_payload(tools=[{"type": "retrieval"}]))
def test_existing_stop_sequences_are_kept_and_capped():
out = te.emulate_request(_payload(stop="END"))
assert out["stop"] == ["END", te.RESPONSE_OPEN]
full = te.emulate_request(_payload(stop=["a", "b", "c", "d"]))
assert full["stop"] == ["a", "b", "c", "d"]
def test_request_without_tools_or_tool_history_is_untouched():
plain = {"model": "m", "messages": [{"role": "user", "content": "hi"}]}
assert te.emulate_request(plain) is plain
def test_parse_extracts_calls_and_remaining_text():
declared = te.declared_tools(_payload())
text = "Sure.\n" + _block() + "\n" + _block(arguments={"command": "pwd"})
remaining, calls, warnings = te.parse_tool_calls(text, declared)
assert remaining == "Sure."
assert [json.loads(c["function"]["arguments"])["command"] for c in calls] == ["ls", "pwd"]
assert all(c["id"].startswith("call_") and c["type"] == "function" for c in calls)
assert warnings == []
def test_parse_is_tolerant_of_fences_arrays_and_unterminated_blocks():
declared = te.declared_tools(_payload())
fenced = f'{te.CALL_OPEN}\n```json\n{{"name": "bash", "arguments": {{"command": "ls"}}}}\n```\n{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(fenced, declared)[1]) == 1
array = f'{te.CALL_OPEN}[{{"name":"bash","arguments":{{"command":"a"}}}},{{"name":"bash","arguments":{{"command":"b"}}}}]{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(array, declared)[1]) == 2
unterminated = f'{te.CALL_OPEN}{{"name": "bash", "arguments": {{"command": "ls"}}}}'
assert len(te.parse_tool_calls(unterminated, declared)[1]) == 1
string_args = f'{te.CALL_OPEN}{{"name":"bash","arguments":"{{\\"command\\": \\"ls\\"}}"}}{te.CALL_CLOSE}'
assert len(te.parse_tool_calls(string_args, declared)[1]) == 1
def test_parse_rejects_invalid_calls_loudly_and_keeps_the_text():
declared = te.declared_tools(_payload())
for bad in (
_block(name="nope"),
_block(arguments={}),
f"{te.CALL_OPEN}not json{te.CALL_CLOSE}",
):
remaining, calls, warnings = te.parse_tool_calls(bad, declared)
assert calls == []
assert warnings
assert te.CALL_OPEN in remaining
def test_parallel_and_per_turn_caps():
declared = te.declared_tools(_payload())
text = "".join(_block() for _ in range(3))
_, calls, warnings = te.parse_tool_calls(text, declared, allow_parallel=False)
assert len(calls) == 1 and warnings
many = "".join(_block() for _ in range(te.MAX_CALLS_PER_TURN + 3))
assert len(te.parse_tool_calls(many, declared)[1]) == te.MAX_CALLS_PER_TURN
def test_convert_completion_builds_openai_tool_calls():
declared = te.declared_tools(_payload())
completion = {"choices": [{"message": {"role": "assistant", "content": "x\n" + _block()}, "finish_reason": "stop"}]}
out, warnings = te.convert_completion(completion, declared)
choice = out["choices"][0]
assert choice["finish_reason"] == "tool_calls"
assert choice["message"]["content"] == "x"
assert choice["message"]["tool_calls"][0]["function"]["name"] == "bash"
assert warnings == []
def test_convert_completion_with_only_a_call_has_null_content():
declared = te.declared_tools(_payload())
completion = {"choices": [{"message": {"content": _block()}, "finish_reason": "stop"}]}
out, _ = te.convert_completion(completion, declared)
assert out["choices"][0]["message"]["content"] is None
def _chunk(content=None, finish=None, **extra):
delta = {} if content is None else {"content": content}
return {
"id": "c",
"object": "chat.completion.chunk",
"model": "m",
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
**extra,
}
def _run_stream(pieces, declared=None, finish="stop", allow_parallel=True):
stream = te.ToolCallStream(declared or te.declared_tools(_payload()), allow_parallel)
out = []
for piece in pieces:
out.extend(stream.feed(_chunk(piece)))
out.extend(stream.feed(_chunk(None, finish)))
return out
def _texts(chunks):
return "".join(
c["choices"][0]["delta"].get("content", "") for c in chunks if c.get("choices")
)
def _calls(chunks):
found = []
for c in chunks:
for choice in c.get("choices") or []:
found.extend(choice["delta"].get("tool_calls") or [])
return found
def test_stream_passes_plain_text_through_unchanged():
out = _run_stream(["hel", "lo ", "world"])
assert _texts(out) == "hello world"
assert _calls(out) == []
assert out[-1]["choices"][0]["finish_reason"] == "stop"
def test_stream_converts_a_call_split_across_arbitrary_chunk_boundaries():
full = "Sure.\n" + _block()
for size in (1, 2, 3, 5, 7, 50):
pieces = [full[i : i + size] for i in range(0, len(full), size)]
out = _run_stream(pieces)
assert _texts(out).strip() == "Sure."
calls = _calls(out)
assert len(calls) == 1
assert calls[0]["function"]["name"] == "bash"
assert json.loads(calls[0]["function"]["arguments"]) == {"command": "ls"}
assert calls[0]["index"] == 0
assert out[-1]["choices"][0]["finish_reason"] == "tool_calls"
def test_stream_never_leaks_call_markup_into_content():
out = _run_stream(["a <tool", "_call>", '{"name":"bash","arguments":{"command":"ls"}}', "</tool_call>"])
assert te.CALL_OPEN not in _texts(out)
def test_stream_emits_a_lookalike_prefix_that_never_became_a_call():
out = _run_stream(["use <tool", " carefully"])
assert _texts(out) == "use <tool carefully"
def test_stream_invalid_call_is_flushed_as_text_with_a_warning():
stream = te.ToolCallStream(te.declared_tools(_payload()))
out = []
for piece in [_block(name="nope")]:
out.extend(stream.feed(_chunk(piece)))
out.extend(stream.feed(_chunk(None, "stop")))
assert _calls(out) == []
assert te.CALL_OPEN in _texts(out)
assert stream.warnings
def test_stream_without_a_finish_chunk_flushes_on_finish():
stream = te.ToolCallStream(te.declared_tools(_payload()))
out = stream.feed(_chunk(_block()))
out.extend(stream.finish())
assert len(_calls(out)) == 1
def test_stream_keeps_usage_chunks_and_other_deltas():
stream = te.ToolCallStream(te.declared_tools(_payload()))
usage = {"id": "c", "choices": [], "usage": {"total_tokens": 3}}
assert stream.feed(usage) == [usage]
out = stream.feed({"id": "c", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}]})
assert out[0]["choices"][0]["delta"] == {"role": "assistant"}