from django.test import TestCase

from jobs.content import build_job_display_content
from jobs.models import Job


class JobContentDedupTests(TestCase):
    def _job(self, **kwargs):
        defaults = {
            'title': 'Sales & Marketing Executive',
            'slug': 'sales-marketing-executive',
            'company_name': 'VIZTRANIX',
            'company_initials': 'VZ',
            'location': 'Lusaka',
        }
        defaults.update(kwargs)
        return Job.objects.create(**defaults)

    def test_hides_sections_that_repeat_description(self):
        body = (
            'VIZTRANIX is Hiring!!\n\n'
            '*Sales & Marketing Executive:*\n\n'
            'https://example.com/sales.pdf'
        )
        job = self._job(description=body, responsibilities=body, requirements=body)
        content = build_job_display_content(job)
        self.assertEqual(content['description'], body)
        self.assertEqual(content['list_items']['responsibilities'], [])
        self.assertEqual(content['list_items']['requirements'], [])

    def test_dedupes_repeated_lines_across_sections(self):
        job = self._job(
            description='Company overview.',
            responsibilities='Manage clients\nAttend meetings',
            requirements='Attend meetings\nValid driving license',
        )
        content = build_job_display_content(job)
        self.assertEqual(content['list_items']['responsibilities'], ['Manage clients', 'Attend meetings'])
        self.assertEqual(content['list_items']['requirements'], ['Valid driving license'])

    def test_hides_how_to_apply_when_it_matches_description(self):
        apply_text = 'Send CV to careers@example.com'
        job = self._job(description=apply_text, how_to_apply=apply_text)
        content = build_job_display_content(job)
        self.assertFalse(content['show_how_to_apply'])

    def test_list_items_property_uses_deduped_values(self):
        body = 'Same text everywhere'
        job = self._job(description=body, responsibilities=body, requirements=body)
        self.assertEqual(job.list_items['responsibilities'], [])
        self.assertEqual(job.list_items['requirements'], [])
from types import SimpleNamespace

from django.test import SimpleTestCase

from jobs.content import build_job_display_content
from jobs.formatting import format_job_text


class JobBodyFormattingTests(SimpleTestCase):
    def test_lists_headings_and_paragraphs_are_structured(self):
        html, heads = format_job_text('Intro line\r\n\r\n1. Chef\r\n2. Cook\r\n\r\nKey Responsibilities:\r\n\r\n- Prepare food\r\n- Keep it clean')
        self.assertIn('<ol class="jd-list"><li>Chef</li><li>Cook</li></ol>', html)
        self.assertIn('<h4 class="jd-heading" id="jd-1">Key Responsibilities</h4>', html)
        self.assertIn('<ul class="jd-list"><li>Prepare food</li><li>Keep it clean</li></ul>', html)
        self.assertEqual(heads, [('jd-1', 'Key Responsibilities')])

    def test_markup_is_escaped_and_links_are_external(self):
        html, _ = format_job_text('<script>alert(1)</script> apply at https://example.com/x')
        self.assertNotIn('<script>', html)
        self.assertIn('&lt;script&gt;', html)
        self.assertIn('target="_blank"', html)

    def test_blank_line_clutter_is_removed(self):
        html, _ = format_job_text('One\n\n\n\n\nTwo')
        self.assertEqual(html.count('<p>'), 2)

    def test_truncated_copy_of_description_is_hidden(self):
        description = 'Great school hiring.\n1. Teacher\n2. Chef\n3. Security guard for the boarding section'
        job = SimpleNamespace(description=description, requirements=description[:-12], responsibilities='', nice_to_have='',
                              benefits='', how_to_apply='')
        content = build_job_display_content(job)
        self.assertEqual(content['list_items']['requirements'], [])

    def test_real_requirements_are_kept(self):
        job = SimpleNamespace(description='A busy shop needs staff.', requirements='Grade 12\nCashier experience',
                              responsibilities='', nice_to_have='', benefits='', how_to_apply='')
        content = build_job_display_content(job)
        self.assertEqual(content['list_items']['requirements'], ['Grade 12', 'Cashier experience'])
