test(tui/persona/registry): PersonaRegistry.get() returns None for corrupted YAML and invalid schema #10372

Open
opened 2026-04-18 09:15:07 +00:00 by HAL9000 · 0 comments
Owner

Metadata

  • Commit message: test(tui/persona/registry): add tests for PersonaRegistry.get() error handling
  • Branch: test/persona-registry-get-error-handling

Background and Context

PersonaRegistry.get() in src/cleveragents/tui/persona/registry.py does not catch yaml.YAMLError or pydantic.ValidationError, unlike list_personas() which handles these gracefully. A corrupted persona YAML file causes an unhandled exception that crashes the TUI on every input submission. These tests are written first (TDD) to define the expected behavior before the fix is implemented.

Expected Behavior

PersonaRegistry.get() should return None for corrupted or schema-invalid persona files, consistent with list_personas() which logs a warning and skips invalid files.

Acceptance Criteria

  • test_get_returns_none_for_corrupted_yaml passes
  • test_get_returns_none_for_invalid_schema passes
  • test_get_consistent_with_list_personas_error_handling passes
  • PersonaRegistry.get() catches (yaml.YAMLError, ValidationError) and returns None
  • A warning is logged when a corrupted file is encountered (consistent with list_personas())

Subtasks

  • Write test_get_returns_none_for_corrupted_yaml
  • Write test_get_returns_none_for_invalid_schema
  • Write test_get_consistent_with_list_personas_error_handling
  • Confirm all three tests fail before the fix (expected-fail state)
  • Confirm all three tests pass after the fix is applied

Test Specification

Verify that PersonaRegistry.get() handles corrupted YAML and invalid persona schema gracefully, consistent with list_personas().

Test Cases

# @tdd_issue
# @tdd_issue_1
# @tdd_expected_fail

def test_get_returns_none_for_corrupted_yaml(tmp_path):
    """PersonaRegistry.get() must return None (not raise) for corrupted YAML."""
    registry = PersonaRegistry(config_dir=tmp_path)
    registry.ensure_dirs()
    # Write a corrupted YAML file
    persona_file = registry.personas_dir / "bad.yaml"
    persona_file.write_text("name: bad\nactor: [invalid yaml: {", encoding="utf-8")
    
    # Must not raise yaml.YAMLError
    result = registry.get("bad")
    assert result is None

def test_get_returns_none_for_invalid_schema(tmp_path):
    """PersonaRegistry.get() must return None (not raise) for schema-invalid YAML."""
    registry = PersonaRegistry(config_dir=tmp_path)
    registry.ensure_dirs()
    # Write a YAML file that fails Persona validation (missing required 'actor' field)
    persona_file = registry.personas_dir / "invalid.yaml"
    persona_file.write_text("name: invalid\n# actor field missing\n", encoding="utf-8")
    
    # Must not raise pydantic.ValidationError
    result = registry.get("invalid")
    assert result is None

def test_get_consistent_with_list_personas_error_handling(tmp_path):
    """get() and list_personas() must handle the same error cases consistently."""
    registry = PersonaRegistry(config_dir=tmp_path)
    registry.ensure_dirs()
    # Write a corrupted YAML file
    persona_file = registry.personas_dir / "corrupted.yaml"
    persona_file.write_text(": invalid: yaml: content:", encoding="utf-8")
    
    # list_personas() skips invalid files (no exception)
    personas = registry.list_personas()
    assert len(personas) == 0
    
    # get() must also not raise (consistent behavior)
    result = registry.get("corrupted")
    assert result is None

Definition of Done

This issue should be closed when:

  • All three test cases are implemented and passing
  • PersonaRegistry.get() catches (yaml.YAMLError, ValidationError) and returns None
  • A warning is logged when a corrupted file is encountered (consistent with list_personas())
  • The corresponding bug issue is resolved

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit message:** `test(tui/persona/registry): add tests for PersonaRegistry.get() error handling` - **Branch:** `test/persona-registry-get-error-handling` ## Background and Context `PersonaRegistry.get()` in `src/cleveragents/tui/persona/registry.py` does not catch `yaml.YAMLError` or `pydantic.ValidationError`, unlike `list_personas()` which handles these gracefully. A corrupted persona YAML file causes an unhandled exception that crashes the TUI on every input submission. These tests are written first (TDD) to define the expected behavior before the fix is implemented. ## Expected Behavior `PersonaRegistry.get()` should return `None` for corrupted or schema-invalid persona files, consistent with `list_personas()` which logs a warning and skips invalid files. ## Acceptance Criteria - [ ] `test_get_returns_none_for_corrupted_yaml` passes - [ ] `test_get_returns_none_for_invalid_schema` passes - [ ] `test_get_consistent_with_list_personas_error_handling` passes - [ ] `PersonaRegistry.get()` catches `(yaml.YAMLError, ValidationError)` and returns `None` - [ ] A warning is logged when a corrupted file is encountered (consistent with `list_personas()`) ## Subtasks - [ ] Write `test_get_returns_none_for_corrupted_yaml` - [ ] Write `test_get_returns_none_for_invalid_schema` - [ ] Write `test_get_consistent_with_list_personas_error_handling` - [ ] Confirm all three tests fail before the fix (expected-fail state) - [ ] Confirm all three tests pass after the fix is applied ## Test Specification Verify that `PersonaRegistry.get()` handles corrupted YAML and invalid persona schema gracefully, consistent with `list_personas()`. ### Test Cases ```python # @tdd_issue # @tdd_issue_1 # @tdd_expected_fail def test_get_returns_none_for_corrupted_yaml(tmp_path): """PersonaRegistry.get() must return None (not raise) for corrupted YAML.""" registry = PersonaRegistry(config_dir=tmp_path) registry.ensure_dirs() # Write a corrupted YAML file persona_file = registry.personas_dir / "bad.yaml" persona_file.write_text("name: bad\nactor: [invalid yaml: {", encoding="utf-8") # Must not raise yaml.YAMLError result = registry.get("bad") assert result is None def test_get_returns_none_for_invalid_schema(tmp_path): """PersonaRegistry.get() must return None (not raise) for schema-invalid YAML.""" registry = PersonaRegistry(config_dir=tmp_path) registry.ensure_dirs() # Write a YAML file that fails Persona validation (missing required 'actor' field) persona_file = registry.personas_dir / "invalid.yaml" persona_file.write_text("name: invalid\n# actor field missing\n", encoding="utf-8") # Must not raise pydantic.ValidationError result = registry.get("invalid") assert result is None def test_get_consistent_with_list_personas_error_handling(tmp_path): """get() and list_personas() must handle the same error cases consistently.""" registry = PersonaRegistry(config_dir=tmp_path) registry.ensure_dirs() # Write a corrupted YAML file persona_file = registry.personas_dir / "corrupted.yaml" persona_file.write_text(": invalid: yaml: content:", encoding="utf-8") # list_personas() skips invalid files (no exception) personas = registry.list_personas() assert len(personas) == 0 # get() must also not raise (consistent behavior) result = registry.get("corrupted") assert result is None ``` ## Definition of Done This issue should be closed when: - All three test cases are implemented and passing - `PersonaRegistry.get()` catches `(yaml.YAMLError, ValidationError)` and returns `None` - A warning is logged when a corrupted file is encountered (consistent with `list_personas()`) - The corresponding bug issue is resolved --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10372
No description provided.