from conftest import LIBEXEC


OVERLAY_HELPER = LIBEXEC / "left4me-overlay"


def test_overlay_helper_is_python_with_strict_validation():
    text = OVERLAY_HELPER.read_text()
    assert text.startswith("#!/usr/bin/python3")
    # Validation surface
    assert "NAME_RE = re.compile" in text
    assert "LOWERDIR_ALLOWLIST" in text
    assert "user.fuseoverlayfs." in text
    assert "MAX_LOWERDIRS = 500" in text
    # Mounts via PID 1's mount namespace
    assert "/proc/1/ns/mnt" in text
    assert "nsenter" in text
    # Verbs are mount and umount (not unmount)
    assert '"mount"' in text and '"umount"' in text
    assert '"unmount"' not in text


def test_overlay_helper_mount_is_idempotent_when_already_mounted():
    """ExecStartPre runs on every Restart=on-failure cycle. If a previous
    start mounted successfully but ExecStart failed afterwards, the next
    ExecStartPre would re-mount on top -- which fails. The helper must
    short-circuit when merged is already a mount point.
    """
    text = OVERLAY_HELPER.read_text()
    # Two ismount checks now: one in cmd_mount (skip if mounted),
    # one in cmd_umount (skip if not mounted).
    assert text.count("os.path.ismount") >= 2
