Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions tests/test_daemon/test_service_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
from __future__ import annotations

import json
import socket
import subprocess
import sys
import threading
import time
from dataclasses import replace
from http.server import BaseHTTPRequestHandler, HTTPServer
from typing import TYPE_CHECKING

import pytest
from typer.testing import CliRunner

from biff import run_dir
from biff.__main__ import app
from biff.daemon import lifecycle
from biff.plugins.claude_code.claude_md import ClaudeMdImport
from biff.run_dir import RunDir
from biff.run_dir import DEFAULT_PORT, RunDir
from biff.supervision.biffd import BIFFD_SPEC
from biff.supervision.health import HealthProbe
from biff.supervision.holder import Holder
from biff.supervision.port import PortProbe
Expand Down Expand Up @@ -43,6 +48,13 @@
_REAL_WAIT = PortProbe.wait_until_answering # captured before any test patches it


def _unused_port() -> int:
"""Return a loopback port nothing listens on."""
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
return int(sock.getsockname()[1])


@pytest.fixture
def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""A throwaway home that holds the ``biffd`` executable."""
Expand All @@ -56,6 +68,12 @@ def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
monkeypatch.setattr(sys, "executable", str(tmp_path / "py" / "python"))
port_is(monkeypatch, "free")
port_answers(monkeypatch, answers=True)
# status probes the recorded port (the default when none is recorded) and
# then the unit's port; point both off a real biffd's 8424.
unused = _unused_port()
monkeypatch.setattr(run_dir, "DEFAULT_PORT", unused)
unit = replace(BIFFD_SPEC, health_port=unused)
monkeypatch.setattr("biff.daemon.lifecycle.BIFFD_SPEC", unit)
return tmp_path


Expand Down Expand Up @@ -86,6 +104,17 @@ def log_message(self, format: str, *args: object) -> None:
return


@pytest.fixture
def unit_port(home: Path) -> Iterator[int]:
"""A loopback listener that answers ``/health`` on the unit's own port."""
port = run_dir.DEFAULT_PORT
server = HTTPServer(("127.0.0.1", port), _Health)
threading.Thread(target=server.serve_forever, daemon=True).start()
yield port
server.shutdown()
server.server_close()


@pytest.fixture
def live_biffd(home: Path) -> Iterator[int]:
"""A loopback listener that answers ``/health``, recorded as biffd's port."""
Expand Down Expand Up @@ -228,7 +257,7 @@ def test_one_warning_line_names_port_holder_and_command(
part
for part in (
"[WARNING]",
"8424",
str(run_dir.DEFAULT_PORT),
"python3 (pid 4930)",
f"biff daemon {verb}",
)
Expand Down Expand Up @@ -377,6 +406,20 @@ def test_installed_and_running_reports_version_and_port(
assert "version: 9.9.9" in result.output
assert "pid: 4321" in result.output

def test_the_default_suite_cannot_reach_a_real_biffd(self, home: Path) -> None:
# Both probes status makes: the recorded-port default and the unit's port.
assert RunDir.default().read_port() != DEFAULT_PORT
patched = vars(lifecycle)["BIFFD_SPEC"] # the name status reads at call time
assert patched.health_port != BIFFD_SPEC.health_port

def test_a_stale_record_falls_back_to_the_units_port(
self, backend: FakeBackend, unit_port: int
) -> None:
runner.invoke(app, ["daemon", "install"])
RunDir.default().write_port(1) # a crashed biffd's leftover record
result = runner.invoke(app, ["daemon", "status"])
assert f"port: {unit_port}" in result.output

def test_installed_but_not_answering_exits_1(
self, backend: FakeBackend, home: Path
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_legacy_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import json
import platform
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -154,7 +155,7 @@ def test_a_tracked_file_in_another_case_is_a_different_file_here(
def test_linux_runs_the_distinct_file_test_instead_of_skipping_it(
self, tmp_path: Path
) -> None:
if not sys.platform.startswith("linux"):
if platform.system() != "Linux":
pytest.skip("only Linux is required to run the distinct-file test")
assert _distinct_file_skip_reason(tmp_path) is None

Expand Down
Loading