Back to Repositories

Testing Command Output Event Serialization in OpenHands

This test suite validates the event serialization functionality in OpenHands, specifically focusing on command output observation serialization and proper mapping of exit codes to success states.

Test Coverage Overview

The test coverage focuses on command output serialization with both successful and failed command scenarios:

  • Validates successful command execution with exit code 0
  • Verifies failed command handling with exit code 1
  • Tests proper mapping of command metadata including ID, content, and status

Implementation Analysis

The testing approach uses direct instantiation of CmdOutputObservation objects to verify the event_to_dict serialization function. The implementation employs straightforward assertion-based validation to ensure correct boolean success flag mapping based on exit codes.

Technical Details

Key technical components include:

  • CmdOutputObservation class from openhands.events.observation
  • event_to_dict serialization function
  • Python’s built-in assertion mechanism
  • Exit code validation (0 for success, non-zero for failure)

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Clear test case separation for success and failure scenarios
  • Explicit assertion checks for expected outcomes
  • Comprehensive command metadata validation
  • Focused scope testing single responsibility principle

all-hands-ai/openhands

tests/unit/test_event_serialization.py

            
from openhands.events.observation import CmdOutputObservation
from openhands.events.serialization import event_to_dict


def test_command_output_success_serialization():
    # Test successful command
    obs = CmdOutputObservation(
        command_id=1, command='ls', content='file1.txt
file2.txt', exit_code=0
    )
    serialized = event_to_dict(obs)
    assert serialized['success'] is True

    # Test failed command
    obs = CmdOutputObservation(
        command_id=2, command='ls', content='No such file or directory', exit_code=1
    )
    serialized = event_to_dict(obs)
    assert serialized['success'] is False