from __future__ import annotations

import socket
from gal_fingerprint_i.config import PACKAGE_ROOT, registered_paths
from gal_fingerprint_i.web import _available_port


def test_complete_gate_c_assets_are_present():
    paths = registered_paths()
    required = [
        PACKAGE_ROOT / "config/gal_fingerprint_i_registered_specification.json",
        PACKAGE_ROOT / "web/static/struc_i_v1_0_4_engine.js",
        PACKAGE_ROOT / "reference/struc_i_v1_0_4_galaxy_profiles.html",
        PACKAGE_ROOT / "gal_fingerprint_i/gate_c.py",
        PACKAGE_ROOT / "data/registered/gate_b/GAL_FINGERPRINT_I_GATE_B_RESULT_RECORD.json",
        paths["primary"], paths["ladder_manifest"], paths["profiles"], paths["r1_profiles"],
    ]
    assert all(path.exists() for path in required)


def test_port_fallback_skips_occupied_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(("127.0.0.1", 0))
        occupied = sock.getsockname()[1]
        selected = _available_port("127.0.0.1", occupied)
        assert selected != occupied
        assert occupied < selected <= occupied + 19
