generated from coulomb/repo-seed
Full TDD implementation of the `bridge` CLI tool covering all phases from BRIDGE-WP-0001: project scaffolding, config loading, state management, audit logging, health checks, tunnel lifecycle manager, and all CLI commands (up/down/restart/status/logs). 77 tests, all green. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
"""Tests for state management."""
|
|
import os
|
|
import signal
|
|
|
|
import pytest
|
|
|
|
from bridge.models import BridgeState
|
|
from bridge.state import StateManager
|
|
|
|
|
|
@pytest.fixture
|
|
def state_dir(tmp_path):
|
|
return tmp_path / "bridge"
|
|
|
|
|
|
@pytest.fixture
|
|
def mgr(state_dir):
|
|
return StateManager(state_dir=state_dir)
|
|
|
|
|
|
class TestStateManager:
|
|
def test_read_state_no_file_returns_stopped(self, mgr):
|
|
assert mgr.read_state("my-tunnel") == BridgeState.STOPPED
|
|
|
|
def test_write_and_read_state(self, mgr):
|
|
mgr.write_state("my-tunnel", BridgeState.CONNECTED)
|
|
assert mgr.read_state("my-tunnel") == BridgeState.CONNECTED
|
|
|
|
def test_state_roundtrip_all_values(self, mgr):
|
|
for state in BridgeState:
|
|
mgr.write_state("t", state)
|
|
assert mgr.read_state("t") == state
|
|
|
|
def test_write_pid(self, mgr):
|
|
# Write a live PID (our own process) so read_pid can confirm it's alive
|
|
pid = os.getpid()
|
|
mgr.write_pid("my-tunnel", pid)
|
|
assert mgr.read_pid("my-tunnel") == pid
|
|
|
|
def test_read_pid_no_file_returns_none(self, mgr):
|
|
assert mgr.read_pid("nonexistent") is None
|
|
|
|
def test_stale_pid_returns_none(self, mgr):
|
|
# PID 999999 almost certainly does not exist
|
|
mgr.write_pid("my-tunnel", 999999)
|
|
assert mgr.read_pid("my-tunnel") is None
|
|
|
|
def test_current_pid_is_alive(self, mgr):
|
|
mgr.write_pid("my-tunnel", os.getpid())
|
|
assert mgr.read_pid("my-tunnel") == os.getpid()
|
|
|
|
def test_clear_pid(self, mgr):
|
|
mgr.write_pid("my-tunnel", os.getpid())
|
|
mgr.clear_pid("my-tunnel")
|
|
assert mgr.read_pid("my-tunnel") is None
|
|
|
|
def test_state_dir_created_on_write(self, state_dir):
|
|
assert not state_dir.exists()
|
|
mgr = StateManager(state_dir=state_dir)
|
|
mgr.write_state("t", BridgeState.STOPPED)
|
|
assert state_dir.exists()
|
|
|
|
def test_is_running_false_when_stopped(self, mgr):
|
|
assert not mgr.is_running("my-tunnel")
|
|
|
|
def test_is_running_true_when_pid_alive(self, mgr):
|
|
mgr.write_pid("my-tunnel", os.getpid())
|
|
mgr.write_state("my-tunnel", BridgeState.CONNECTED)
|
|
assert mgr.is_running("my-tunnel")
|