"""Tracker control-plane boundaries."""

import subprocess
import sys
import textwrap


def test_tracker_startup_does_not_import_or_load_model_backends():
    "The public tracker is a router/API endpoint, not an inference worker.\n\nTags: routing, tracker"

    code = textwrap.dedent(
        """
        import builtins
        import sys

        blocked_roots = {"meshnet_node", "torch", "transformers"}
        real_import = builtins.__import__

        def guarded_import(name, globals=None, locals=None, fromlist=(), level=0):
            root = name.split(".", 1)[0]
            if root in blocked_roots:
                raise AssertionError(f"tracker imported model backend dependency: {name}")
            return real_import(name, globals, locals, fromlist, level)

        builtins.__import__ = guarded_import

        from meshnet_tracker.server import TrackerServer

        tracker = TrackerServer()
        port = tracker.start()
        assert port > 0
        tracker.stop()

        imported = blocked_roots & set(sys.modules)
        assert not imported, f"unexpected model modules imported: {sorted(imported)}"
        """
    )

    result = subprocess.run(
        [sys.executable, "-c", code],
        check=False,
        capture_output=True,
        text=True,
    )

    assert result.returncode == 0, result.stderr
