Files
microfish/backend/tests/test_worker.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

87 lines
2.4 KiB
Python

"""TDD gate: durable worker loop run_once lifecycle.
Proves a worker claims a job, invokes the registered handler, and records
success/failure on the durable job — the portable core of the production worker.
"""
import pytest
from sqlalchemy import create_engine
from app.db import Base, create_session_factory
from app.models.operations import Job, JobStatus
from worker import run_once
@pytest.fixture()
def session_factory():
engine = create_engine("sqlite+pysqlite:///:memory:")
Base.metadata.create_all(engine)
factory = create_session_factory(engine)
try:
yield factory
finally:
engine.dispose()
def _seed(session_factory, *, organization_id="org-a", operation="graph.build", result=None):
session = session_factory()
job = Job(
organization_id=organization_id,
owner_user_id="user-1",
operation=operation,
status=JobStatus.QUEUED,
result=result,
)
session.add(job)
session.commit()
job_id = job.id
session.close()
return job_id
def test_run_once_processes_and_completes_job(session_factory):
job_id = _seed(session_factory, result={"graph_id": "g1"})
def handler(payload, job):
assert payload == {"graph_id": "g1"}
return {"ok": True}
session = session_factory()
try:
handled = run_once(
session,
worker_id="w-1",
organization_id="org-a",
**{"graph.build": handler},
)
assert handled is True
session.expire_all()
refreshed = session.get(Job, job_id)
assert refreshed.status == JobStatus.SUCCEEDED.value
assert refreshed.result == {"ok": True}
finally:
session.close()
def test_run_once_returns_false_when_no_job(session_factory):
session = session_factory()
try:
assert run_once(session, worker_id="w-1") is False
finally:
session.close()
def test_run_once_marks_unhandled_job_failed(session_factory):
job_id = _seed(session_factory, operation="no.handler")
session = session_factory()
try:
handled = run_once(session, worker_id="w-1", organization_id="org-a")
assert handled is True
session.expire_all()
refreshed = session.get(Job, job_id)
assert refreshed.status == JobStatus.FAILED.value
assert refreshed.error_code
finally:
session.close()