from __future__ import annotations import importlib.util from pathlib import Path import unittest MODULE_PATH = Path(__file__).resolve().parents[1] / "p6_readonly_projection.py" SPEC = importlib.util.spec_from_file_location("p6_readonly_projection_under_test", MODULE_PATH) assert SPEC and SPEC.loader projection = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(projection) class P6ReadOnlyProjectionTests(unittest.TestCase): def test_only_bounded_counts_and_static_contract_are_returned(self) -> None: result = projection.build_p6_readonly_status({ "profiles": 3, "identity_links": 4, "audit_events": 5, "operations": 6, "person_id": "must-not-leak", "p4_event": {"body": "must-not-leak"}, "capability": "must-not-leak", }) self.assertEqual("private_companion", result["source_plugin"]) self.assertEqual({"profiles": 3, "identity_links": 4, "audit_events": 5, "operations": 6}, result["counts"]) self.assertEqual({"schema_version", "source_plugin", "contract_fingerprint", "health", "reason_code", "counts"}, set(result)) self.assertNotIn("must-not-leak", repr(result)) def test_invalid_status_fails_closed_without_reading_object(self) -> None: class Poison: def __getattribute__(self, name): raise AssertionError(name) result = projection.build_p6_readonly_status(Poison()) self.assertEqual("unverifiable", result["health"]) self.assertEqual("registry_status_unavailable", result["reason_code"]) self.assertEqual({"profiles": 0, "identity_links": 0, "audit_events": 0, "operations": 0}, result["counts"]) def test_memory_consumer_uses_the_same_frozen_contract_fingerprint(self) -> None: memory_path = Path(__file__).resolve().parents[2] / "astrbot_plugin_memory_companion-main" / "core" / "p6_readonly_projection.py" spec = importlib.util.spec_from_file_location("p6_readonly_projection_memory_under_test", memory_path) self.assertIsNotNone(spec) self.assertIsNotNone(spec.loader) memory = importlib.util.module_from_spec(spec) spec.loader.exec_module(memory) self.assertEqual(projection.P6_READONLY_STATUS_SCHEMA, memory.P6_READONLY_STATUS_SCHEMA) self.assertEqual(projection.P6_READONLY_STATUS_FINGERPRINT, memory.P6_READONLY_STATUS_FINGERPRINT) if __name__ == "__main__": unittest.main()