from __future__ import annotations import asyncio from pathlib import Path import sys import types import unittest ROOT = Path(__file__).resolve().parents[1] PACKAGE_NAME = "c7_companion_test_package" package = types.ModuleType(PACKAGE_NAME) package.__path__ = [str(ROOT)] sys.modules.setdefault(PACKAGE_NAME, package) from c7_companion_test_package.bot_personal_outbox import BotPersonalOutbox class OutboxLifecycleTests(unittest.TestCase): def test_async_save_uses_lifecycle_callback_when_provided(self): labels: list[str] = [] def create_task(operation, label): labels.append(label) operation.close() return None async def save(): return None outbox = BotPersonalOutbox({}, save=save, background_task=create_task) asyncio.run(asyncio.to_thread(outbox._persist)) self.assertEqual(["bot_personal_outbox_save"], labels) class CompanionConcurrencyStaticTests(unittest.TestCase): def test_data_lock_external_awaits_use_temporary_release_context(self): source = (ROOT / "main.py").read_text(encoding="utf-8") self.assertIn("async def _temporarily_release_data_lock", source) self.assertGreaterEqual(source.count("async with self._temporarily_release_data_lock()"), 12) self.assertIn('label="refresh_group_episode"', source) self.assertIn('label="refresh_persona_relationship_inbound"', source) def test_untracked_main_background_tasks_are_not_left_in_message_paths(self): source = (ROOT / "main.py").read_text(encoding="utf-8") for marker in ( "asyncio.create_task(self._refine_inbound_emotion_with_model", "asyncio.create_task(self._maybe_refresh_companion_memory", "asyncio.create_task(self._maybe_refresh_group_episode", "asyncio.create_task(self._kick_proactive_loop_once", ): self.assertNotIn(marker, source) if __name__ == "__main__": unittest.main()