# -*- coding: utf-8 -*- """ Test fixtures for account-manager tests. All tests use an isolated temporary database under a fake JIANGCHANG_DATA_ROOT so they never touch real user data. """ import os import sys import tempfile import shutil # Ensure scripts/ is on the path _scripts = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "scripts") if _scripts not in sys.path: sys.path.insert(0, _scripts) import pytest @pytest.fixture(scope="session") def fake_env(): """Set up a fake JIANGCHANG_DATA_ROOT / JIANGCHANG_USER_ID for the session.""" tmp = tempfile.mkdtemp(prefix="account_manager_test_") data_root = os.path.join(tmp, "claw-data") os.makedirs(data_root) fake_uid = "99999" os.environ["JIANGCHANG_DATA_ROOT"] = data_root os.environ["JIANGCHANG_USER_ID"] = fake_uid yield {"data_root": data_root, "user_id": fake_uid, "tmp_dir": tmp} @pytest.fixture(scope="function") def clean_db(fake_env): """Each test gets a fresh in-memory DB (init_db uses the env vars set by fake_env).""" # Import after env vars are set from db.connection import get_conn, init_db from util.constants import SKILL_SLUG from util.logging_config import setup_skill_logging setup_skill_logging(SKILL_SLUG, "openclaw.skill.account_manager_test") init_db() conn = get_conn() conn.execute("DELETE FROM account_leases") conn.execute("DELETE FROM credentials") conn.execute("DELETE FROM accounts") conn.commit() yield conn conn.close()