from __future__ import annotations

import re
import unittest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
NAV = ROOT / "frontend/m/config/nav.config.js"
PAGES = ROOT / "frontend/m/js/pages.js"
ROUTER = ROOT / "frontend/m/js/router.js"


class MobileSystemPagesRegressionTests(unittest.TestCase):
    """Prevent mobile system-management entries from falling back to placeholders."""

    def test_nav_system_entries_are_registered_as_real_pages(self) -> None:
        nav = NAV.read_text(encoding="utf-8")
        pages = PAGES.read_text(encoding="utf-8")
        keys = re.findall(r'key:\s*"(system/[^"]+)"', nav)
        self.assertEqual(
            keys,
            [
                "system/profile",
                "system/password",
                "system/membership",
                "system/admin",
                "system/members",
            ],
        )
        for key in keys:
            self.assertIn(f'"{key}": setupSystemPage', pages)
            self.assertIn(f'"{key}": loadSystem', pages)

    def test_placeholder_copy_is_only_a_router_fallback(self) -> None:
        router = ROUTER.read_text(encoding="utf-8")
        pages = PAGES.read_text(encoding="utf-8")
        self.assertIn("该功能页将在后续批次实现", router)
        self.assertNotIn("该功能页将在后续批次实现", pages)
        self.assertIn("function setupSystemPage", pages)
        self.assertIn("function loadSystem", pages)

    def test_system_pages_render_real_controls_not_stubs(self) -> None:
        pages = PAGES.read_text(encoding="utf-8")
        for marker in (
            'data-system-page="home"',
            'data-system-page="profile"',
            'data-system-page="password"',
            'data-system-page="membership"',
            'data-system-page="members"',
            'data-system-page="forbidden"',
            'data-system-admin-panel="market"',
            "m-sys-birth-date",
            "m-sys-password-current",
            "m-sys-token",
            "m-sys-member-limit",
            "data-system-switch",
            "data-system-edit-model",
            "data-system-open-member",
            "管理员专区",
            "保存密钥",
            "保存分工",
            'location.assign("/login/")',
        ):
            self.assertIn(marker, pages)

    def test_system_boolean_attrs_do_not_have_stray_quotes(self) -> None:
        pages = PAGES.read_text(encoding="utf-8")
        stray = re.findall(r'data-system-[a-z-]*"(?=[>\s])', pages)
        self.assertEqual(
            stray,
            [],
            "boolean data-system attributes must not have a trailing quote before > or space",
        )
        for name in (
            "data-system-save-birth",
            "data-system-save-password",
            "data-system-add-model",
            "data-system-save-models",
            "data-system-save-market",
            "data-system-refresh",
            "data-system-toggle-refresh",
            "data-system-save-model",
        ):
            self.assertIn(name, pages)
            self.assertNotIn(name + '">', pages)
