import tempfile
import unittest
from pathlib import Path

from app.explorer import render_explorer


class SunburstContractTest(unittest.TestCase):
    def test_label_engine_contract_in_html(self) -> None:
        rows = [
            {
                "item_id": "1",
                "name": "A very very very long folder name for overlap testing",
                "path": "My Drive/Team buildings & Team events/Very Long Folder Name Here",
                "root_path": "My Drive",
                "item_kind": "FOLDER",
                "file_category": "FOLDER",
                "original_owner_email": "owner@example.com",
                "shared_by_email": "owner@example.com",
                "access_scope": "SHARED_USERS",
                "shared_with_count": 3,
                "access_targets": "a@example.com|b@example.com|c@example.com",
                "permission_entries": "",
                "risk_level": "LOW",
                "risk_reason": "",
            },
            {
                "item_id": "2",
                "name": "another-really-long-file-name-to-force-truncation.yaml",
                "path": "My Drive/Team buildings & Team events/Very Long Folder Name Here/another-really-long-file-name-to-force-truncation.yaml",
                "root_path": "My Drive",
                "item_kind": "FILE",
                "file_category": "YAML",
                "original_owner_email": "owner@example.com",
                "shared_by_email": "owner@example.com",
                "access_scope": "PRIVATE",
                "shared_with_count": 0,
                "access_targets": "",
                "permission_entries": "",
                "risk_level": "LOW",
                "risk_reason": "",
            },
        ]

        with tempfile.TemporaryDirectory() as td:
            out = Path(td) / "explorer.html"
            render_explorer(rows, out, build_id="TESTBUILD")
            html = out.read_text(encoding="utf-8")

        self.assertIn("const SUN_LABEL_MIN_ANGLE", html)
        self.assertIn("const SUN_LABEL_MIN_RING_PX", html)
        self.assertIn("function applySunLabelRules", html)
        self.assertIn("labelText", html)
        self.assertIn("rotate: 0", html)
        self.assertIn("formatter: (p) => (p?.data?.labelText || '')", html)
        self.assertIn("hideOverlap: true", html)
        self.assertIn("Build: ${BUILD_ID || '-'}", html)


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