from __future__ import annotations

import sys
import types
import unittest
from types import SimpleNamespace


class MalwareScanFilterTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        if 'pymysql' not in sys.modules:
            sys.modules['pymysql'] = types.ModuleType('pymysql')
        from wordfence.cli.malwarescan.malwarescan import MalwareScanSubcommand
        from wordfence.scanning.filtering import FileFilter

        cls._subcommand_cls = MalwareScanSubcommand
        cls._file_filter_cls = FileFilter

    def _build_filter(self, **overrides):
        defaults = {
            'include_all_files': False,
            'include_files': None,
            'include_files_pattern': None,
            'exclude_files': None,
            'exclude_files_pattern': None,
            'images': False,
        }
        defaults.update(overrides)
        subcommand = object.__new__(self._subcommand_cls)
        subcommand.config = SimpleNamespace(**defaults)
        return subcommand._initialize_file_filter()

    def test_initialize_file_filter_with_include_all_files(self):
        filter_instance = self._build_filter(include_all_files=True)
        self.assertIsInstance(filter_instance, self._file_filter_cls)
        self.assertTrue(filter_instance.filter(b'/tmp/allowed.php'))
        self.assertTrue(filter_instance.filter(b'/tmp/image.jpg'))

    def test_initialize_file_filter_images_flag(self):
        filter_instance = self._build_filter(
            include_all_files=True,
            images=True,
        )
        self.assertTrue(filter_instance.filter(b'/tmp/image.jpg'))


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