from __future__ import annotations

import re
import unittest

from backend.bootstrap.config import APP_DIR, STATIC_DIR
from tests.preservation_helpers import (
    FRONTEND_ROOT,
    ORIGINAL_STATIC,
    assert_frontend_runtime_matches_audited_baseline,
    assert_moved_asset_matches,
    assert_page_prefix_matches,
)


class FrontendPreservationSliceTests(unittest.TestCase):
    def test_split_runtime_matches_original_except_audited_retirements(self) -> None:
        assert_frontend_runtime_matches_audited_baseline(self)

    def test_index_diff_is_limited_to_asset_relocation_and_split_loading(self) -> None:
        migrated = (FRONTEND_ROOT / "index.html").read_text(encoding="utf-8")
        restored = migrated
        for current, original in (
            ("/styles/styles.css", "/styles.css"),
            ("/styles/renovation.css", "/renovation.css"),
            ("/styles/redesign-v2.css", "/redesign-v2.css"),
            ("/styles/design-system.css", "/design-system.css"),
            ("/styles/theme.css", "/theme.css"),
            ("/pages/heaven/page.css", "/wentian-v2.css"),
            ("/shared/ui-core.js", "/ui-core.js"),
            ("/pages/heaven/loading-v2.js", "/heaven-loading-v2.js"),
        ):
            restored = restored.replace(current, original)
        restored = restored.replace(
            '  <script src="/pages/market/runtime.js?v=20260731-1" defer></script>\n',
            "",
        )
        restored = restored.replace(
            '  <script src="/shared/export.js?v=20260731-1" defer></script>\n',
            "",
        )
        self.assertEqual(
            restored,
            (ORIGINAL_STATIC / "index.html").read_text(encoding="utf-8"),
        )

    def test_stylesheet_stack_matches_baseline_after_audited_retirements(self) -> None:
        for original, migrated in (
            ("shared/tokens.css", "shared/tokens.css"),
            ("styles.css", "styles/styles.css"),
            ("renovation.css", "styles/renovation.css"),
            ("redesign-v2.css", "styles/redesign-v2.css"),
            ("design-system.css", "styles/design-system.css"),
            ("theme.css", "styles/theme.css"),
            ("wentian-v2.css", "pages/heaven/page.css"),
        ):
            with self.subTest(asset=original):
                assert_moved_asset_matches(self, original, migrated)

    def test_shared_vendor_and_animation_assets_are_byte_identical(self) -> None:
        for original, migrated in (
            ("ui-core.js", "shared/ui-core.js"),
            ("heaven-loading-v2.js", "pages/heaven/loading-v2.js"),
            ("vendor/lucide.min.js", "vendor/lucide.min.js"),
            ("pages.config.js", "pages.config.js"),
            ("pages/runtime.js", "pages/runtime.js"),
            ("shared/api.js", "shared/api.js"),
            ("shared/components.js", "shared/components.js"),
            ("shared/shell.js", "shared/shell.js"),
            ("shared/state.js", "shared/state.js"),
        ):
            with self.subTest(asset=original):
                assert_moved_asset_matches(self, original, migrated)

    def test_original_page_registration_prefixes_are_preserved(self) -> None:
        for path in sorted((ORIGINAL_STATIC / "pages").glob("*/page.js")):
            relative = path.relative_to(ORIGINAL_STATIC).as_posix()
            with self.subTest(page=relative):
                assert_page_prefix_matches(self, relative)

    def test_frontend_is_the_only_served_static_root(self) -> None:
        self.assertEqual(STATIC_DIR, APP_DIR / "frontend")
        self.assertFalse((APP_DIR / "static").exists())

    def test_shared_api_remains_the_only_fetch_exit(self) -> None:
        consumers = []
        for path in FRONTEND_ROOT.rglob("*.js"):
            if "vendor" in path.parts:
                continue
            if re.search(r"\bfetch\s*\(", path.read_text(encoding="utf-8")):
                consumers.append(path.relative_to(FRONTEND_ROOT).as_posix())
        self.assertEqual(consumers, ["shared/api.js"])


if __name__ == "__main__":
    unittest.main()
