from bs4 import BeautifulSoup
from importlib import resources

import ploneintranet
import re
import unittest


class TestCheckMarkup(unittest.TestCase):
    def test_check_application_body_class(self):
        """Check that all templates have an element with an id like
        `application-body` or `application-body-*`
        also have the class `application-body`.

        This is needed for the diazo transform to work properly.
        """
        id_pattern = re.compile(r"application-body(-[a-zA-Z0-9-_]+)?$")
        with resources.path(ploneintranet, "") as ploneintranet_path:
            for f in ploneintranet_path.glob("**/*.pt"):
                soup = BeautifulSoup(f.read_text(), "lxml")
                if el := soup.find("div", id=id_pattern):
                    msg = f"Please add the application-body class in {f} "
                    self.assertIn("class", el.attrs, msg)
                    self.assertIn("application-body", el.attrs["class"], msg)
