nomadro/apps/api/tests/test_uiux_config.py
Eric 499f2d3e26 Ship coffeechat scheduling, notify PB access, and social/admin UI polish for .com.
Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 18:19:37 +08:00

80 lines
4.1 KiB
Python

"""Config edit regression tests: no business DB or network calls."""
import importlib.util
import json
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
from fastapi import HTTPException
from pydantic import ValidationError
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'services'))
def load(relative, name):
spec = importlib.util.spec_from_file_location(name, ROOT / 'services/ebook_svc' / relative)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
class ConfigEditingTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.addCleanup(self.temporary.cleanup)
self.path = Path(self.temporary.name) / 'config.json'
self.original = {'version': 5, 'modules': {'social': {'enabled': True}}, 'audience': {'cn': {'homeLead': '中文原稿'}, 'intl': {'homeLead': 'English'}}, 'flags': {'payEnabled': True}}
self.path.write_text(json.dumps(self.original), encoding='utf-8')
self.audience = load('services/audience_config.py', 'uiux_audience')
self.blog = load('services/social_blog_ops.py', 'uiux_blog')
for module in [self.audience, self.blog]:
handle = patch.object(module, 'resolve_config_file', return_value=self.path)
handle.start(); self.addCleanup(handle.stop)
def test_stale_audience_write_preserves_file(self):
with self.assertRaises(HTTPException) as error:
self.audience.write_audience_config({'expectedVersion': 4, 'cn': {'homeLead': 'overwrite'}})
self.assertEqual(error.exception.status_code, 409)
self.assertEqual(json.loads(self.path.read_text()), self.original)
def test_stale_blog_write_preserves_file(self):
with self.assertRaises(HTTPException) as error:
self.blog.patch_ops({'expectedVersion': 4, 'adsenseEnabled': False})
self.assertEqual(error.exception.status_code, 409)
self.assertEqual(json.loads(self.path.read_text()), self.original)
def test_audience_uses_atomic_writer_and_preserves_other_lane(self):
written = []
def atomic_writer(data):
data['version'] += 1
written.append(data)
self.path.write_text(json.dumps(data), encoding='utf-8')
with patch.dict(sys.modules, {'ebook_svc.services.weapp_switches': SimpleNamespace(_write_raw=atomic_writer)}):
result = self.audience.write_audience_config({'expectedVersion': 5, 'cn': {'homeLead': '新文案'}})
self.assertEqual(result['version'], 6)
self.assertEqual(result['cn']['homeLead'], '新文案')
self.assertEqual(result['intl']['homeLead'], 'English')
self.assertEqual(written[0]['modules'], self.original['modules'])
def test_blog_order_is_featured_then_visible_then_newest(self):
posts = [{'slug': 'old', 'date': '2026-01-01'}, {'slug': 'new', 'date': '2026-11-05'}, {'slug': 'pinned', 'date': '2025-01-01'}]
with patch.object(self.blog, '_fetch_posts', return_value=posts), patch.object(self.blog, '_ops_from_raw', return_value={'featuredSlug': 'pinned', 'hiddenSlugs': [], 'note': '', 'heroLead': ''}):
result = self.blog.list_posts()
self.assertEqual([item['slug'] for item in result['items']], ['pinned', 'new', 'old'])
def test_blog_schema_validates_ads_and_keeps_optional_legacy_version(self):
with patch.dict(sys.modules, {'ebook_svc.services.social_blog_ops': self.blog}):
router = load('routers/social_blog_admin.py', 'uiux_blog_router')
self.assertIsNone(router.BlogOpsPatch.model_validate({'moduleEnabled': True}).expectedVersion)
self.assertEqual(router.BlogOpsPatch.model_validate({'expectedVersion': 5, 'adsenseClientId': 'ca-pub-123'}).expectedVersion, 5)
for body in [{'adsenseClientId': 'invalid'}, {'adsenseSlotBlogMid': 'x'}, {'expectedVersion': -1}, {'note': 'x' * 501}]:
with self.assertRaises(ValidationError): router.BlogOpsPatch.model_validate(body)
if __name__ == '__main__': unittest.main()