# (c) cavaliba.com - test_dashboard_config_validation

from django.test import TestCase

from app_home.configuration_default import DASHBOARD_CONTENT_DEFAULT
from app_home.configuration_form import AppHomeConfigurationForm
from app_home.dashboard import validate_dashboard_yaml


class DashboardConfigValidationTest(TestCase):
    def _form_data(self, dashboard_content):
        # AppHomeConfigurationForm has other required-looking fields with
        # ChoiceField defaults; only DASHBOARD_CONTENT is under test here, so
        # supply the minimal set needed for the other fields to clean OK.
        return {
            "LOG_DEBUG": "yes",
            "DASHBOARD_CONTENT": dashboard_content,
        }

    def test_valid_yaml_passes(self):
        errors = validate_dashboard_yaml("- section: X\n  entries:\n    - app: import\n")
        self.assertEqual(errors, [])

    def test_empty_value_is_valid_at_validator_level(self):
        errors = validate_dashboard_yaml("")
        self.assertEqual(errors, [])

    def test_invalid_yaml_syntax_rejected(self):
        errors = validate_dashboard_yaml("not: [valid, yaml, structure")
        self.assertTrue(errors)
        self.assertIn("invalid YAML", errors[0])

    def test_non_list_top_level_rejected(self):
        errors = validate_dashboard_yaml("just_a_string")
        self.assertEqual(errors, ["dashboard_content must be a YAML list"])

    def test_section_missing_key_rejected(self):
        errors = validate_dashboard_yaml("- entries: []\n")
        self.assertTrue(any("missing 'section' key" in e for e in errors))

    def test_unknown_app_key_rejected(self):
        errors = validate_dashboard_yaml("- section: X\n  entries:\n    - app: totally_bogus_app\n")
        self.assertTrue(any("unknown app" in e for e in errors))

    def test_missing_link_value_rejected(self):
        errors = validate_dashboard_yaml("- section: X\n  entries:\n    - link: \n")
        self.assertTrue(any("'link' value required" in e for e in errors))

    def test_item_with_no_recognized_type_rejected(self):
        errors = validate_dashboard_yaml("- section: X\n  entries:\n    - foo: bar\n")
        self.assertTrue(any("exactly one of" in e for e in errors))

    def test_form_rejects_invalid_dashboard_content(self):
        form = AppHomeConfigurationForm(
            data=self._form_data("- section: X\n  entries:\n    - app: totally_bogus_app\n")
        )
        self.assertFalse(form.is_valid())
        self.assertIn("DASHBOARD_CONTENT", form.errors)

    def test_form_accepts_valid_dashboard_content(self):
        form = AppHomeConfigurationForm(
            data=self._form_data("- section: X\n  entries:\n    - app: import\n")
        )
        form.is_valid()
        self.assertNotIn("DASHBOARD_CONTENT", form.errors)

    def test_form_resets_cleared_dashboard_content_to_default(self):
        form = AppHomeConfigurationForm(data=self._form_data(""))
        form.is_valid()
        self.assertNotIn("DASHBOARD_CONTENT", form.errors)
        self.assertEqual(form.cleaned_data["DASHBOARD_CONTENT"], DASHBOARD_CONTENT_DEFAULT)
