import os.path
import pkgutil
import unittest

import streamlink.plugins
from streamlink.plugin.plugin import Matcher, Plugin
from streamlink.utils import load_module


class PluginTestMeta(type):
    def __new__(mcs, name, bases, dict):
        plugin_path = os.path.dirname(streamlink.plugins.__file__)
        plugins = []
        for loader, pname, ispkg in pkgutil.iter_modules([plugin_path]):
            module = load_module(f"streamlink.plugins.{pname}", plugin_path)
            if hasattr(module, "__plugin__"):
                plugins.append((loader, pname))

        def gentest(loader, pname):
            def load_plugin_test(self):
                plugin = loader.find_module(pname).load_module(pname)
                assert hasattr(plugin, "__plugin__"), "It exports __plugin__"

                pluginclass = plugin.__plugin__
                assert issubclass(plugin.__plugin__, Plugin), "__plugin__ is an instance of the Plugin class"

                classname = pluginclass.__name__
                assert classname == classname[0].upper() + classname[1:], "__plugin__ class name starts with uppercase letter"
                assert "_" not in classname, "__plugin__ class name does not contain underscores"

                assert isinstance(pluginclass.matchers, list) and len(pluginclass.matchers) > 0, "Has at least one matcher"
                assert all(isinstance(matcher, Matcher) for matcher in pluginclass.matchers), "Only has valid matchers"

                assert not hasattr(pluginclass, "can_handle_url"), "Does not implement deprecated can_handle_url(url)"
                assert not hasattr(pluginclass, "priority"), "Does not implement deprecated priority(url)"
                assert callable(pluginclass._get_streams), "Implements _get_streams()"

            return load_plugin_test

        for loader, pname in plugins:
            dict[f"test_{pname}_load"] = gentest(loader, pname)

        return type.__new__(mcs, name, bases, dict)


class TestPlugins(unittest.TestCase, metaclass=PluginTestMeta):
    """
    Test that each plugin can be loaded and does not fail when calling can_handle_url.
    """

@@ -302,419 +302,77 @@
 __)%0A
-        plugins = %5B%5D%0A        for loader, pname, ispkg in pkgutil.iter_modules(%5Bplugin_path%5D):%0A            module = load_module(f%22streamlink.plugins.%7Bpname%7D%22, plugin_path)%0A            if hasattr(module, %22__plugin__%22):%0A                plugins.append((loader, pname))%0A%0A        def gentest(loader, pname):%0A            def load_plugin_test(self):%0A                plugin = loader.find_module(pname).load_module(pname)
+%0A        def gentest(plugin):%0A            def load_plugin_test(self):
 %0A   
@@ -1477,16 +1477,35 @@
 n_test%0A%0A
+        pname: str%0A
         
@@ -1508,19 +1508,19 @@
     for 
-loa
+fin
 der, pna
@@ -1525,20 +1525,208 @@
 name
- in plugins:
+, ispkg in pkgutil.iter_modules(%5Bplugin_path%5D):%0A            if pname.startswith(%22common_%22):%0A                continue%0A            plugin_module = load_module(f%22streamlink.plugins.%7Bpname%7D%22, plugin_path)
 %0A   
@@ -1775,20 +1775,20 @@
 est(
-loader, pnam
+plugin_modul
 e)%0A%0A

fix deprecated module imports on py310