90 行
3.6 KiB
Python
90 行
3.6 KiB
Python
|
|
from types import SimpleNamespace
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
|
|||
|
|
from backend.dto.common import ApiError, ErrorCode
|
|||
|
|
from backend.services.profile_document_import_service import ProfileDocumentImportService
|
|||
|
|
from backend.services.profile_service import ProfileService
|
|||
|
|
from core.identity import RequestContext
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_document_import_rejects_unsupported_file() -> None:
|
|||
|
|
with pytest.raises(ApiError) as error:
|
|||
|
|
ProfileDocumentImportService().import_document({"filename": "character.pdf", "content": "x"})
|
|||
|
|
assert error.value.error_code == ErrorCode.UNSUPPORTED_FILE_TYPE
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_document_import_falls_back_without_model(monkeypatch) -> None:
|
|||
|
|
service = ProfileDocumentImportService()
|
|||
|
|
monkeypatch.setattr(service, "_resolve_route", lambda _requested: "")
|
|||
|
|
result = service.import_document({"filename": "character.md", "content": "# Mika\nA quiet archivist."})
|
|||
|
|
assert result["llm_used"] is False
|
|||
|
|
assert result["questionnaire"]["role_core"].startswith("# Mika")
|
|||
|
|
assert result["questionnaire"]["extra_notes"].startswith("# Mika")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_document_import_normalizes_structured_model_result(monkeypatch) -> None:
|
|||
|
|
service = ProfileDocumentImportService()
|
|||
|
|
monkeypatch.setattr(service, "_resolve_route", lambda _requested: "test-route")
|
|||
|
|
monkeypatch.setattr(service, "_resolve_callable", lambda _route: lambda _request: {
|
|||
|
|
"summary": "A careful archivist",
|
|||
|
|
"questionnaire": {
|
|||
|
|
"role_core": "archivist",
|
|||
|
|
"personality": "quiet",
|
|||
|
|
"unknown_field": "ignored",
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
result = service.import_document({"filename": "character.txt", "content": "Mika"})
|
|||
|
|
assert result["llm_used"] is True
|
|||
|
|
assert result["questionnaire"] == {"role_core": "archivist", "personality": "quiet"}
|
|||
|
|
assert "unknown_field" not in result["questionnaire"]
|
|||
|
|
assert "appearance" in result["unresolved_fields"]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_sealed_profile_rejects_visual_updates() -> None:
|
|||
|
|
profile = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
|
|||
|
|
|
|||
|
|
class Repository:
|
|||
|
|
def get(self, _profile_id):
|
|||
|
|
return profile
|
|||
|
|
|
|||
|
|
def update(self, _profile_id, _payload):
|
|||
|
|
raise AssertionError("sealed visual update must not reach repository")
|
|||
|
|
|
|||
|
|
class Audit:
|
|||
|
|
def record(self, **_kwargs):
|
|||
|
|
raise AssertionError("sealed visual update must not be audited")
|
|||
|
|
|
|||
|
|
service = ProfileService(Repository(), Audit())
|
|||
|
|
with pytest.raises(ApiError) as error:
|
|||
|
|
service.update_profile("profile-1", {"appearance_prompt": "new face"}, RequestContext.system("test"))
|
|||
|
|
assert error.value.error_code == ErrorCode.FORBIDDEN
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_sealed_profile_allows_non_visual_update() -> None:
|
|||
|
|
profile = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
|
|||
|
|
updated = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
|
|||
|
|
profile.to_dict = lambda: vars(profile)
|
|||
|
|
updated.to_dict = lambda: vars(updated)
|
|||
|
|
|
|||
|
|
class Repository:
|
|||
|
|
def get(self, _profile_id):
|
|||
|
|
return profile
|
|||
|
|
|
|||
|
|
def update(self, _profile_id, payload):
|
|||
|
|
assert payload == {"description": "new backstory"}
|
|||
|
|
return updated
|
|||
|
|
|
|||
|
|
class Audit:
|
|||
|
|
def record(self, **_kwargs):
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
result = ProfileService(Repository(), Audit()).update_profile(
|
|||
|
|
"profile-1", {"description": "new backstory"}, RequestContext.system("test")
|
|||
|
|
)
|
|||
|
|
assert result is updated
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|