Files
microfish/backend/tests/test_memory_extraction.py
Kunthawat Greethong 8b84378fe1 feat: SaaS foundation for CrowdSight
Elevate MiroFish/CrowdSight from single-container dev to a SaaS foundation:

- Local memory backend (Zep-compatible): memory services/models, local graph
  builder + updater, AgentActivity seam, import-boundary isolation; Zep stays
  default, local is opt-in behind MEMORY_BACKEND. Semantic parity not yet proven.
- Durable product persistence: projects/simulations/reports schema (migration
  0007) + tenant/owner-scoped ProductRepository + dual-write + scoped_project
  read-first + ArtifactStore abstraction; durable JobQueue + worker.py.
- SaaS hardening: durable RateLimiter (wired to login), UsageService (LLM
  accounting), redacted AuditService, idempotency, CORS allowlist, safe API
  errors, single-use PasswordResetService + endpoints (covers invite-pending).
- Exactly 3 roles (super_admin/admin/user) with tenant authz policy.
- Admin UI: GET/POST/PATCH /api/admin/users + GET/PUT /api/admin/settings
  (super-admin only, encrypted/masked); AdminView.vue + SettingsView.vue with
  admin/super-admin route guards, th/en i18n.
- Production deploy topology: multi-stage Dockerfile (frontend build + gunicorn
  wsgi + nginx SPA-proxy + supervisord worker), backend/wsgi.py, gunicorn dep.

Backend 197 passed; frontend 10 tests + build green. ruff unavailable (gap).
No commit of credentials; secrets handled via env/.env.example.
Deferred: Zep semantic A/B parity, object storage cutover, mobile QA, EasyPanel
container build of deploy topology.
2026-08-31 13:05:21 +07:00

93 lines
2.8 KiB
Python

import json
import pytest
from pydantic import ValidationError
from app.services.memory_extraction import (
MemoryExtractionResult,
build_extraction_prompt,
parse_extraction_response,
)
def test_parse_memory_extraction_result_with_entities_edges_and_evidence():
raw = json.dumps(
{
"entities": [
{
"mention": "Alice",
"canonical_name": "Alice",
"labels": ["Person"],
"aliases": ["A"],
"attributes": {"role": "founder"},
"summary": "A founder.",
"confidence": 0.92,
}
],
"edges": [
{
"source_entity_ref": "Alice",
"target_entity_ref": "Bob",
"relation": "KNOWS",
"fact": "Alice knows Bob.",
"attributes": {},
"valid_at": None,
"invalid_at": None,
"expired_at": None,
"confidence": 0.8,
"evidence": ["episode-1:0-15"],
}
],
"episode_summary": "A relationship statement.",
"unresolved_mentions": ["Bob"],
}
)
result = parse_extraction_response(raw)
assert isinstance(result, MemoryExtractionResult)
assert result.entities[0].canonical_name == "Alice"
assert result.edges[0].relation == "KNOWS"
assert result.edges[0].evidence == ["episode-1:0-15"]
def test_parse_memory_extraction_result_rejects_unknown_fields_and_bad_confidence():
payload = {
"entities": [],
"edges": [],
"episode_summary": "summary",
"unresolved_mentions": [],
"secret_prompt": "must not be accepted",
}
with pytest.raises(ValidationError):
parse_extraction_response(json.dumps(payload))
payload.pop("secret_prompt")
payload["entities"] = [
{
"mention": "Alice",
"canonical_name": "Alice",
"labels": [],
"aliases": [],
"attributes": {},
"summary": "",
"confidence": 1.5,
}
]
with pytest.raises(ValidationError):
parse_extraction_response(payload)
def test_extraction_prompt_puts_language_instruction_first_and_bounds_episode_size():
prompt = build_extraction_prompt(
language="th",
ontology={"entity_types": ["Person"], "edge_types": ["KNOWS"]},
episode_text="Alice knows Bob.",
)
assert prompt.startswith("IMPORTANT:")
assert "Return JSON only" in prompt
assert "entity_refs" in prompt
assert "Alice knows Bob." in prompt
with pytest.raises(ValueError, match="episode_too_large"):
build_extraction_prompt(language="en", ontology={}, episode_text="x" * 20001)