# (c) cavaliba.com - test / field templates / call site -> display_mode mapping

"""
A call *site* is not a display_mode: instance_new.html renders with
display_mode="edit". This file pins the mapping itself, and - the part that
actually catches bugs - that no fields/*.html branches on a display_mode no call
site ever passes, since such a branch is dead and the mode renders blank.

See project/claude_doc/2026-10-03-FIELD-TEMPLATE-UNIFICATION-DONE.md. Replaces
test_field_render_tech.py, whose tech call site went away with the detail tech
view (2026-10-03-REMOVE-DETAIL-TECH-VIEW-DONE.md).
"""

import pathlib
import re

from django.conf import settings
from django.test import TestCase

from tests import helper


class TestFieldRenderCallSites(TestCase):
    def test_new_call_site_renders_with_edit_display_mode(self):
        self.assertEqual(helper.display_mode_for_call_site("new"), "edit")

    def test_unknown_call_site_is_rejected(self):
        with self.assertRaises(ValueError):
            helper.template_for_mode("nosuchmode")

    def test_every_call_site_resolves_to_the_single_dispatcher(self):
        for call_site in helper.DISPLAY_MODE_FOR_CALL_SITE:
            with self.subTest(call_site=call_site):
                self.assertEqual(helper.template_for_mode(call_site), "app_data/fields/_field.html")

    def test_no_field_template_branches_on_an_unpassed_display_mode(self):
        folder = pathlib.Path(settings.BASE_DIR) / "app_data/templates/app_data/fields"
        passed = set(helper.DISPLAY_MODE_FOR_CALL_SITE.values())
        pattern = re.compile(r'display_mode\s*==\s*"([a-z_]+)"')

        for path in sorted(folder.glob("_*.html")):
            for mode in pattern.findall(path.read_text()):
                with self.subTest(template=path.name, mode=mode):
                    self.assertIn(mode, passed)
