feat: add active_view to StatePatch model and patch_state handler

- Add optional active_view field to StatePatch Pydantic model
- Add active_view handling in patch_state to persist the field
- Add test_patch_state_sets_active_view: verifies PATCH persists active_view
- Add test_patch_state_active_view_defaults_to_all: verifies GET returns 'all' by default

Closes task-1: Add active_view to StatePatch Model
This commit is contained in:
Brian Krabach
2026-04-15 16:56:43 -07:00
parent ab5560a623
commit b69a1f8c15
2 changed files with 36 additions and 0 deletions
+3
View File
@@ -423,6 +423,7 @@ class StatePatch(BaseModel):
session_order: list[str] | None = None session_order: list[str] | None = None
active_session: str | None = None active_session: str | None = None
active_remote_id: str | None = None active_remote_id: str | None = None
active_view: str | None = None
class HeartbeatPayload(BaseModel): class HeartbeatPayload(BaseModel):
@@ -495,6 +496,8 @@ async def patch_state(patch: StatePatch) -> dict:
state["active_session"] = patch.active_session state["active_session"] = patch.active_session
if "active_remote_id" in changed: if "active_remote_id" in changed:
state["active_remote_id"] = patch.active_remote_id state["active_remote_id"] = patch.active_remote_id
if "active_view" in changed:
state["active_view"] = patch.active_view
save_state(state) save_state(state)
return state return state
+33
View File
@@ -227,6 +227,39 @@ def test_patch_state_ignores_unknown_fields(client):
assert data["session_order"] == ["a", "b"] assert data["session_order"] == ["a", "b"]
def test_patch_state_sets_active_view(client):
"""PATCH /api/state with active_view persists the value.
Verifies response contains active_view and subsequent GET returns the value.
"""
from muxplex.state import load_state
# PATCH with a specific active_view value
response = client.patch("/api/state", json={"active_view": "my-view"})
assert response.status_code == 200
data = response.json()
assert data["active_view"] == "my-view"
# Verify the value persists via GET
get_response = client.get("/api/state")
assert get_response.status_code == 200
get_data = get_response.json()
assert get_data["active_view"] == "my-view"
# Also verify it was persisted to disk
persisted = load_state()
assert persisted["active_view"] == "my-view"
def test_patch_state_active_view_defaults_to_all(client):
"""GET /api/state returns active_view='all' by default."""
response = client.get("/api/state")
assert response.status_code == 200
data = response.json()
assert "active_view" in data
assert data["active_view"] == "all"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# GET /api/sessions # GET /api/sessions
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------