c61296a24e
- Add TestUserContext: tests for UserContext dataclass (creation, to_dict, from_dict) - Add TestUserContextManager: tests for manager initialization and load - Add TestAtomicWrites: tests for atomic save with backup rotation - Add TestMultiUser: tests for multi-user isolation and convenience methods - Add tmp_users_root and user_context_manager fixtures to conftest.py - Export UserContext and UserContextManager from __init__.py Note: Implementation was already done in commit 9bd29a2 as part of 04-02 plan. This commit adds the tests that should have been part of 04-01 TDD workflow. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
"""Pytest fixtures for gsd-openclaw tests."""
|
|
import shutil
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
from gsd_openclaw.workspace import Workspace, WorkspaceManager
|
|
from gsd_openclaw.state_manager import SessionState
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_workspaces_root(tmp_path: Path) -> Path:
|
|
"""Temporary root for workspace testing.
|
|
|
|
This ensures tests don't pollute the real ~/gsd-workspaces directory.
|
|
|
|
Args:
|
|
tmp_path: Pytest's built-in temporary directory fixture.
|
|
|
|
Returns:
|
|
Path to a temporary gsd-workspaces directory.
|
|
"""
|
|
return tmp_path / "gsd-workspaces"
|
|
|
|
|
|
@pytest.fixture
|
|
def workspace_manager(tmp_workspaces_root: Path) -> WorkspaceManager:
|
|
"""WorkspaceManager with temporary root for isolation.
|
|
|
|
Creates a WorkspaceManager that uses a temporary directory instead
|
|
of the user's home directory. Automatically cleans up after the test.
|
|
|
|
Args:
|
|
tmp_workspaces_root: Fixture providing temporary workspace root.
|
|
|
|
Yields:
|
|
WorkspaceManager instance configured for testing.
|
|
"""
|
|
manager = WorkspaceManager("test-project", tmp_workspaces_root)
|
|
yield manager
|
|
# Cleanup
|
|
if manager.workspace.root.exists():
|
|
shutil.rmtree(manager.workspace.root)
|
|
|
|
|
|
@pytest.fixture
|
|
def created_workspace(workspace_manager: WorkspaceManager) -> Workspace:
|
|
"""A workspace that has been created (directories exist).
|
|
|
|
Args:
|
|
workspace_manager: Fixture providing WorkspaceManager.
|
|
|
|
Returns:
|
|
Workspace object with all directories created.
|
|
"""
|
|
return workspace_manager.create()
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_state_dir(tmp_path: Path) -> Path:
|
|
"""Temporary directory for state file testing.
|
|
|
|
Args:
|
|
tmp_path: Pytest's built-in temporary directory fixture.
|
|
|
|
Returns:
|
|
Path to a temporary directory for state files.
|
|
"""
|
|
state_dir = tmp_path / ".gsd"
|
|
state_dir.mkdir(parents=True, exist_ok=True)
|
|
return state_dir
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_state() -> SessionState:
|
|
"""A valid SessionState instance for testing.
|
|
|
|
Returns:
|
|
SessionState with all fields populated.
|
|
"""
|
|
return SessionState(
|
|
schema_version="1.0",
|
|
session_id="test-session-fixture",
|
|
project_name="test-project",
|
|
tmux_session_name="gsd-test-project",
|
|
tmux_pane_id="%100",
|
|
created_at="2026-03-10T10:00:00Z",
|
|
last_active_at="2026-03-10T12:00:00Z",
|
|
command_count=10,
|
|
last_output_snapshot="Test output snapshot",
|
|
display_name="Test Project Display Name"
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_users_root(tmp_path: Path) -> Path:
|
|
"""Temporary root for user context testing.
|
|
|
|
This ensures tests don't pollute the real ~/gsd-workspaces/.users directory.
|
|
|
|
Args:
|
|
tmp_path: Pytest's built-in temporary directory fixture.
|
|
|
|
Returns:
|
|
Path to a temporary .users directory.
|
|
"""
|
|
return tmp_path / "gsd-workspaces" / ".users"
|
|
|
|
|
|
@pytest.fixture
|
|
def user_context_manager(tmp_users_root: Path) -> "UserContextManager":
|
|
"""UserContextManager with temporary root for isolation.
|
|
|
|
Args:
|
|
tmp_users_root: Fixture providing temporary user context root.
|
|
|
|
Returns:
|
|
UserContextManager instance configured for testing.
|
|
"""
|
|
from gsd_openclaw.user_context import UserContextManager
|
|
return UserContextManager("test-user", tmp_users_root)
|