oclaw/tests/test_sa_tenant_bind_code.py
oliver d14e9d3596 feat(persistence): PostgreSQL assistant store, chat persist fixes, gateway scripts
- Add SQLAlchemy Core repos, pg adapter/compat, assistant_store factory, Alembic bootstrap and migration/cutover scripts.

- Harden chat_message writes (NUL scrub for PG), turn_uuid on attempt failure, WS turn_runner fallbacks and gateway executed_turn_uuid init.

- start_gateway: log paths, PS7 stderr handling via cmd, background stdout/stderr redirect; runtime assistant_runtime_log_dir export.

- Ops: clear_all_chat_sessions with PG-only --postgresql and env-gated wipe; clear_postgres_chat_sessions.ps1.

- Tests: SA repos, pg compat, persist fallback, smoke env isolation; CI and docs touch-ups.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-14 09:33:50 +08:00

48 lines
1.8 KiB
Python

"""SA migration: tenant + bind_code."""
from __future__ import annotations
import pytest
from svc.persistence.db.engine import clear_assistant_engine_cache
from svc.persistence.sqlite_store import SqliteStore
@pytest.fixture
def fresh_sqlite_store(monkeypatch: pytest.MonkeyPatch, tmp_path):
monkeypatch.delenv("AIA_ASSISTANT_DB_BACKEND", raising=False)
monkeypatch.setenv("OPS_WORKSPACE_ROOT", str(tmp_path))
dbfile = tmp_path / "sa_tenant_bc.sqlite"
monkeypatch.setenv("AIA_ASSISTANT_DB_PATH", str(dbfile))
clear_assistant_engine_cache()
s = SqliteStore(str(dbfile))
try:
yield s
finally:
clear_assistant_engine_cache()
def test_sa_tenant_crud(fresh_sqlite_store: SqliteStore) -> None:
s = fresh_sqlite_store
t = s.create_tenant("Acme")
listed = s.list_tenants(limit=50)
assert any(x["id"] == t["id"] for x in listed)
assert s.delete_tenant(tenant_id=t["id"]) == 1
assert s.delete_tenant(tenant_id=t["id"]) == 0
def test_sa_bind_code_list_and_consume(fresh_sqlite_store: SqliteStore) -> None:
s = fresh_sqlite_store
t = s.create_tenant("BindCo")
s.create_bind_code(tenant_id=t["id"], role="member", code="CODE12345")
rows = s.list_bind_codes(tenant_id=t["id"], limit=10)
assert len(rows) == 1
assert rows[0]["code"] == "CODE12345"
assert rows[0]["used_at"] is None
out = s.consume_bind_code(code="CODE12345", channel="wecom", external_user_id="wx-1", display_name="Ext")
assert out is not None
assert out["tenant_id"] == t["id"]
rows2 = s.list_bind_codes(tenant_id=t["id"], limit=10)
assert rows2[0]["used_at"] is not None
assert rows2[0]["used_by_external_user_id"] == "wx-1"
assert s.consume_bind_code(code="CODE12345", channel="wecom", external_user_id="wx-2") is None