forked from retoor/devplacepy
Add personal notes, DeepSearch history, backup offload, and gateway auth throttling
Also streamline the top navigation: drop the Tools dropdown, make Quizzes and Battles icon-only entries, and remove the Workspace, Containers and Editor entry points from the project detail page.
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from devplacepy.database import set_setting
|
||||
from devplacepy.services import correction
|
||||
|
||||
|
||||
class _FakeResponse:
|
||||
def __init__(self, content: str) -> None:
|
||||
self._content = content
|
||||
self.headers: dict = {}
|
||||
|
||||
def raise_for_status(self) -> None:
|
||||
return None
|
||||
|
||||
def json(self) -> dict:
|
||||
return {"choices": [{"message": {"content": self._content}}]}
|
||||
|
||||
|
||||
class _FakeClient:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[dict] = []
|
||||
|
||||
def post(self, url, json=None, headers=None, timeout=None):
|
||||
self.calls.append({"url": url, "json": json, "headers": headers})
|
||||
return _FakeResponse("corrected text")
|
||||
|
||||
|
||||
def test_gateway_complete_sets_bypass_preamble_and_internal_header(local_db, monkeypatch):
|
||||
set_setting("gateway_internal_key", "test-internal-secret")
|
||||
fake = _FakeClient()
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
try:
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key",
|
||||
"system prompt",
|
||||
"hello world",
|
||||
5.0,
|
||||
bypass_preamble=True,
|
||||
)
|
||||
finally:
|
||||
set_setting("gateway_internal_key", "")
|
||||
assert content == "corrected text"
|
||||
sent = fake.calls[-1]
|
||||
assert sent["json"]["bypass_preamble"] is True
|
||||
assert sent["headers"]["Authorization"] == "Bearer user-api-key"
|
||||
assert sent["headers"]["X-Gateway-Internal-Key"] == "test-internal-secret"
|
||||
|
||||
|
||||
def test_gateway_complete_without_bypass_preamble_omits_it(local_db, monkeypatch):
|
||||
fake = _FakeClient()
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
correction.gateway_complete("user-api-key", "system prompt", "hello world", 5.0)
|
||||
sent = fake.calls[-1]
|
||||
assert "bypass_preamble" not in sent["json"]
|
||||
assert "X-Gateway-Internal-Key" not in sent["headers"]
|
||||
|
||||
|
||||
def test_correct_text_requests_the_preamble_bypass(local_db, monkeypatch):
|
||||
fake = _FakeClient()
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
correction.correct_text("user-api-key", "", "hello world")
|
||||
sent = fake.calls[-1]
|
||||
assert sent["json"]["bypass_preamble"] is True
|
||||
|
||||
|
||||
class _ScriptedClient:
|
||||
def __init__(self, content: str) -> None:
|
||||
self._content = content
|
||||
self.calls: list[dict] = []
|
||||
|
||||
def post(self, url, json=None, headers=None, timeout=None):
|
||||
self.calls.append({"url": url, "json": json, "headers": headers})
|
||||
return _FakeResponse(self._content)
|
||||
|
||||
|
||||
def test_structure_diverges_false_on_reworded_list_item():
|
||||
original = "- alpha\n- beta\n- gamma\n"
|
||||
corrected = "- Alpha\n- beta\n- gamma\n"
|
||||
assert correction.structure_diverges(original, corrected) is False
|
||||
|
||||
|
||||
def test_structure_diverges_true_on_flattened_list():
|
||||
original = "- alpha\n- beta\n- gamma\n"
|
||||
corrected = "alpha, beta and gamma."
|
||||
assert correction.structure_diverges(original, corrected) is True
|
||||
|
||||
|
||||
def test_gateway_complete_structure_check_disabled_by_default(local_db, monkeypatch):
|
||||
original = "- alpha\n- beta\n"
|
||||
corrected = "alpha and beta."
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key", "system prompt", original, 5.0
|
||||
)
|
||||
assert content == corrected
|
||||
|
||||
|
||||
def test_gateway_complete_structure_check_passes_typo_fix(local_db, monkeypatch):
|
||||
original = "- alpha\n- beta\n- gamma\n"
|
||||
corrected = "- Alpha\n- beta\n- gamma\n"
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key", "system prompt", original, 5.0, check_structure=True
|
||||
)
|
||||
assert content == corrected.strip()
|
||||
|
||||
|
||||
def test_gateway_complete_structure_check_rejects_flattened_list(local_db, monkeypatch):
|
||||
original = "- alpha\n- beta\n- gamma\n"
|
||||
corrected = "alpha, beta and gamma."
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key", "system prompt", original, 5.0, check_structure=True
|
||||
)
|
||||
assert content == original
|
||||
|
||||
|
||||
def test_gateway_complete_structure_check_rejects_dropped_code_fence(local_db, monkeypatch):
|
||||
original = "Here is the fix:\n\n```python\nprint(1)\n```\n"
|
||||
corrected = "Here is the fix:\n\nprint(1)\n"
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key", "system prompt", original, 5.0, check_structure=True
|
||||
)
|
||||
assert content == original
|
||||
|
||||
|
||||
def test_gateway_complete_structure_check_rejects_removed_link(local_db, monkeypatch):
|
||||
original = "Check out [DevPlace](https://example.com) for details."
|
||||
corrected = "Check out DevPlace for details."
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.gateway_complete(
|
||||
"user-api-key", "system prompt", original, 5.0, check_structure=True
|
||||
)
|
||||
assert content == original
|
||||
|
||||
|
||||
def test_correct_text_rejects_corrupted_structure(local_db, monkeypatch):
|
||||
original = "# Setup\n\n- step one\n- step two\n"
|
||||
corrected = "Setup: step one, step two."
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.correct_text("user-api-key", "", original)
|
||||
assert content == original
|
||||
|
||||
|
||||
def test_correct_text_keeps_grammar_only_correction(local_db, monkeypatch):
|
||||
original = "# Setup\n\n- step one\n- step two\n"
|
||||
corrected = "# Setup\n\n- Step one\n- Step two\n"
|
||||
fake = _ScriptedClient(corrected)
|
||||
monkeypatch.setattr(correction, "_client", lambda: fake)
|
||||
content, _usage = correction.correct_text("user-api-key", "", original)
|
||||
assert content == corrected.strip()
|
||||
Reference in New Issue
Block a user