from __future__ import annotations import importlib.util from pathlib import Path import sys import types import unittest from tests._host_stub import install install() ROOT = Path(__file__).resolve().parents[1] def _user_api_mixin() -> type: module_name = "companion_intimacy_test.page_api_users_groups" if module_name in sys.modules: return sys.modules[module_name].PrivateCompanionPageApiUsersGroupsMixin quart = types.ModuleType("quart") quart.request = object() sys.modules.setdefault("quart", quart) package = types.ModuleType("companion_intimacy_test") package.__path__ = [str(ROOT)] sys.modules[package.__name__] = package spec = importlib.util.spec_from_file_location(module_name, ROOT / "page_api_users_groups.py") if spec is None or spec.loader is None: raise AssertionError("could not load user page API test module") module = importlib.util.module_from_spec(spec) sys.modules[module_name] = module spec.loader.exec_module(module) return module.PrivateCompanionPageApiUsersGroupsMixin class CompanionIntimacyInputTests(unittest.TestCase): def test_manual_value_accepts_only_bounded_integers(self) -> None: parser = _user_api_mixin()._relationship_score_input self.assertEqual(-1200, parser(-1200)) self.assertEqual(0, parser(" 0 ")) self.assertEqual(1200, parser("+1200")) for value in (True, False, "", "2.5", 2.0, -1201, 1201, object()): with self.subTest(value=repr(value)): with self.assertRaises(ValueError): parser(value) def test_update_path_has_a_dedicated_public_field_and_locked_write(self) -> None: source = (ROOT / "page_api_users_groups.py").read_text(encoding="utf-8") start = source.index("async def update_user") end = source.index("async def delete_user", start) update_path = source[start:end] self.assertIn('"companion_intimacy"', update_path) self.assertIn("async with self.plugin._data_lock", update_path) self.assertIn('user["relationship_score"] = relationship_score', update_path) self.assertIn("self.plugin._save_data_sync()", update_path) class CompanionIntimacyPageWiringTests(unittest.TestCase): def test_safe_dto_and_editor_are_wired_without_p4_actions(self) -> None: page_api = (ROOT / "page_api.py").read_text(encoding="utf-8") app_js = (ROOT / "pages" / "陪伴面板" / "app.js").read_text(encoding="utf-8") self.assertGreaterEqual(page_api.count('"relationship_intimacy"'), 2) self.assertIn('"min": -1200', page_api) self.assertIn('"max": 1200', page_api) self.assertIn('"phase": {"key": phase_key, "label": phase_label}', page_api) self.assertIn("function renderCompanionIntimacyEditor", app_js) self.assertNotIn('id="relationshipScoreRange"', app_js) self.assertIn("当前亲密阶段", app_js) self.assertIn("companion_intimacy: value", app_js) self.assertIn("不影响 P4 好感、黑屋、审核或安全权限", app_js) editor_start = app_js.index("function renderCompanionIntimacyEditor") editor_end = app_js.index("function renderRelationshipPanel", editor_start) editor = app_js[editor_start:editor_end] for forbidden in ("ledger", "attestation", "candidate_id", "person_id"): self.assertNotIn(forbidden, editor) if __name__ == "__main__": unittest.main()