Files
cleveragents-core/features/materializers_coverage_boost.feature
T
hurui200320 2434253c1a
CI / build (push) Successful in 16s
CI / lint (push) Successful in 18s
CI / quality (push) Successful in 28s
CI / typecheck (push) Successful in 1m0s
CI / security (push) Successful in 1m0s
CI / benchmark-regression (push) Has been skipped
CI / unit_tests (push) Successful in 3m14s
CI / integration_tests (push) Successful in 3m38s
CI / docker (push) Successful in 55s
CI / e2e_tests (push) Successful in 5m15s
CI / coverage (push) Successful in 7m8s
CI / benchmark-publish (push) Successful in 20m34s
feat(cli): implement full output rendering framework (#812)
## Summary

Implement the 6 missing element handle types (Tree, Text, Code, Diff, Separator, ActionHint) and ensure all 6 materialization strategies (rich, color, table, plain, json, yaml) support all 10 element types. This completes the output rendering framework per the specification (§25417-27276).

Closes #550

## Changes

### Source (restructured into smaller modules)

#### `handles/` package (was `handles.py` — 1062 lines → 5 files, all ≤500 lines)
- **`_models.py`**: All Pydantic data models, constants (MAX_TREE_DEPTH, MAX_ELEMENTS_PER_SESSION, MAX_TABLE_ROWS), exceptions (ElementClosedError), event classes, ElementSnapshot union. P2-1: `DiffLine.type` renamed to `line_type` with backward-compatible alias.
- **`_base.py`**: Generic `ElementHandle[E]` base class with thread-safe `element_copy()` (lock-protected).
- **`_panel_table.py`**: PanelHandle, TableHandle, StatusHandle.
- **`_concrete.py`**: ProgressHandle (P1-1: validates `total` non-negative), TreeHandle, TextHandle (P3-2: close() delegates to super()), CodeHandle, DiffHandle, SeparatorHandle, ActionHintHandle. P3-8: `increment(delta=0)` now rejected.
- **`__init__.py`**: Re-exports all public symbols.

#### `_renderers.py` — plain renderers, sanitization, and shared helpers
- Plain render functions for all 10 element types.
- Terminal escape sanitization (`strip_terminal_escapes`).
- P2-3: `_sort_table_rows` now consults `ColumnDef.col_type` for numeric sorting.
- P2-6: `compute_column_widths()` shared helper extracted (DRY fix).

#### `_color_renderers.py` — ANSI colour renderers
- Colour-coded render functions for all 10 element types.
- P2-2: TextBlock now gets color treatment (`_render_text_color`) instead of falling through to plain.

#### `_boxdraw.py` — box-drawing renderers
- P2-5: Upgraded from ASCII `+-|` to Unicode box-drawing `╭─╮│╰╯` with rounded corners per spec §26821.

#### `_ids.py` — ID generation helpers
- P3-1: Session and handle IDs now use separate counters, making IDs monotonic within their namespace.

#### `materializers.py` — strategy protocol and 6 concrete strategies
- P1-2: `_snapshot_to_dict` now includes `timing` field in JSON/YAML output per spec §27022.
- P2-4: `_column_def_to_dict` always includes all fields unconditionally for stable JSON schemas.

#### `selection.py` — materializer selection with fallback
- P1-4: `NO_COLOR` environment variable now respected (https://no-color.org/). When set, all visual formats fall back to plain. Precedence: explicit flag > NO_COLOR > terminal capability fallback.

#### `session.py`
- P1-1: `session.progress()` factory validates `total >= 0`.
- P1-2: `snapshot()` includes `timing` when available.

#### `__init__.py` — package docstring
- P1-3: SD-29 corrected to reflect actual Table → Color → Plain fallback chain.
- SD-14 marked as implemented (NO_COLOR support added).

### Spec Deviations (Documented)
28 deliberate deviations documented in `__init__.py` module docstring (SD-1 through SD-29, with SD-14 now implemented). SD-29 corrected.

### Tests (updated)
- **+23 new BDD scenarios** covering: P1-1 total validation, P1-4 NO_COLOR, P2-1 DiffLine.line_type alias, P2-2 text color, P2-3 numeric sorting, P2-4 ColumnDef serialization, P2-5 Unicode box-drawing, P3-3 add_rows limit, P3-5 10-thread stress test, P3-6 summary truncation, P3-7 explicit format for color/table, P3-8 zero delta.
- **Robot tests**: Updated box-drawing assertion for Unicode chars.

## Verification

| Check | Result |
|-------|--------|
| Pyright | 0 errors, 1 pre-existing warning |
| Ruff lint | All passed |
| Unit tests | 393 features, 11,344 scenarios, 0 failures |
| Integration tests | All passed |
| E2E tests | All passed |
| Coverage | 97% overall (threshold: 97%) |

## Review Fixes Applied (Luis Review #2412)

| ID | Severity | Fix |
|----|----------|-----|
| P1-1 | High | `set_progress()` and `session.progress()` validate `total >= 0` |
| P1-2 | High | `_snapshot_to_dict` includes `timing` field |
| P1-3 | High | SD-29 documentation corrected |
| P1-4 | High | `NO_COLOR` env var respected |
| P2-1 | Medium | `DiffLine.type` → `line_type` with alias |
| P2-2 | Medium | TextBlock gets color treatment |
| P2-3 | Medium | Numeric column sorting |
| P2-4 | Medium | ColumnDef always serializes all fields |
| P2-5 | Medium | Unicode box-drawing characters |
| P2-6 | Medium | Shared `compute_column_widths()` helper |
| P3-1 | Low | Separate ID counters |
| P3-2 | Low | TextHandle.close() delegates to super() |
| P3-3 | Low | add_rows batch limit test |
| P3-5 | Low | 10-thread stress test |
| P3-6 | Low | Summary truncation test |
| P3-7 | Low | Explicit format tests for color/table |
| P3-8 | Low | Zero delta rejected |

### Deferred Items
| ID | Reason |
|----|--------|
| P3-9 | snapshot() lock scope — acceptable correctness trade-off |
| P3-10 | CLEVERAGENTS_FORMAT env var — documented as SD-15, requires CLI framework changes |

Reviewed-on: #812
Reviewed-by: Jeffrey Phillips Freeman <jeffrey.freeman@cleverthis.com>
Co-authored-by: Rui Hu <rui.hu@cleverthis.com>
Co-committed-by: Rui Hu <rui.hu@cleverthis.com>
2026-03-19 08:46:46 +00:00

146 lines
7.2 KiB
Gherkin

Feature: Materializers coverage boost
As a developer maintaining the output rendering framework
I want thorough test coverage on materializer strategies
So that all code paths including edge cases are verified
# ---------------------------------------------------------------
# Scenario 1: Force-flush remaining buffers on session end
# Targets lines 466-469 in materializers.py (_BaseBufferStrategy.on_session_end)
# This exercises the path where elements remain in self._buffers
# because they were closed out of declaration order and the session
# ends before sequential flushing catches up.
# ---------------------------------------------------------------
Scenario: Session end force-flushes out-of-order buffered elements
Given a PlainMaterializer buffer strategy
And an OutputSession wired to that buffer strategy
When I create three panels in declaration order
And I close only the third and second panels
And I end the session without closing the first panel
Then the strategy output includes content from all three panels
# ---------------------------------------------------------------
# Scenario 2: Session end with non-empty buffers from skipped indices
# Also targets lines 466-469 but with a different pattern: element
# index 0 never closes, so _next_render_index stays at 0 and
# elements 1 and 2 accumulate in self._buffers.
# ---------------------------------------------------------------
Scenario: Session end flushes buffered content when leading element never closes manually
Given a ColorMaterializer buffer strategy
And an OutputSession wired to that color buffer strategy
When I create two status elements in order
And I close only the second status element
And the session is closed via context exit
Then the color strategy output includes both status messages
# ---------------------------------------------------------------
# Scenario 3: _render_element_plain fallback for unknown type
# Targets line 169 (_render_element_plain fallback to str())
# ---------------------------------------------------------------
Scenario: Plain renderer falls back to str for unknown element types
Given an unknown element type object
When I render it with the plain element dispatcher
Then the result is the string representation of the object
# ---------------------------------------------------------------
# Scenario 4: _render_element_color fallback for unknown type
# Targets line 269 (_render_element_color fallback to str())
# ---------------------------------------------------------------
Scenario: Color renderer falls back to str for unknown element types
Given an unknown element type object
When I render it with the color element dispatcher
Then the color result is the string representation of the object
# ---------------------------------------------------------------
# Scenario 5: _element_to_dict fallback for unknown type
# Targets line 372 (_element_to_dict returns {"type": "unknown"})
# ---------------------------------------------------------------
Scenario: Element to dict returns unknown type for unrecognized elements
Given an unknown element type object
When I convert it to a dict using element_to_dict
Then the result dict has type equal to unknown
# ---------------------------------------------------------------
# Scenario 6: _BaseBufferStrategy._render_element default
# Targets line 440 (base class fallback)
# ---------------------------------------------------------------
Scenario: Base buffer strategy render_element delegates to plain renderer
Given a raw BaseBufferStrategy instance
And a simple Panel element for rendering
When I call the base strategy render_element with the panel
Then the rendered output contains the panel title
# ---------------------------------------------------------------
# Scenario 7: _AccumulateStrategy._serialise default
# Targets line 551 (base class returns empty string)
# ---------------------------------------------------------------
Scenario: Base accumulate strategy serialise returns empty string
Given a raw AccumulateStrategy instance
And a mock StructuredOutput snapshot
When I call the base strategy serialise method
Then the result is an empty string
# ---------------------------------------------------------------
# Scenario 8: _AccumulateStrategy._serialise_dict default
# Targets line 576 (base class returns empty string)
# ---------------------------------------------------------------
Scenario: Base accumulate strategy serialise_dict returns empty string
Given a raw AccumulateStrategy instance
When I call the base strategy serialise_dict with a sample dict
Then the serialise_dict result is an empty string
# ---------------------------------------------------------------
# Scenario 9: on_element_closed with unknown handle_id
# Targets line 445 (early return when idx is None)
# ---------------------------------------------------------------
Scenario: Buffer strategy ignores element closed events for unknown handles
Given a PlainMaterializer buffer strategy
And an OutputSession wired to that buffer strategy
When I dispatch an ElementClosed event with an unknown handle id
Then the strategy output is empty
# ---------------------------------------------------------------
# Scenario 10: TableMaterializer renders non-table element via color
# Targets _render_element_table delegates to render_element_color
# (R2-C2 fix #12: table format now uses color for non-table elements)
# ---------------------------------------------------------------
Scenario: Table materializer renders status message via color fallback
Given a TableMaterializer buffer strategy
And an OutputSession wired to that table buffer strategy
When I create and close a status element with message "deploy complete" and level "ok"
Then the table strategy output contains "deploy complete"
# ---------------------------------------------------------------
# Scenario 11: Session end with empty buffers (no remaining)
# Ensures line 465 is hit but loop body (466-469) is skipped
# when all elements were already flushed
# ---------------------------------------------------------------
Scenario: Session end with all elements already flushed does not double-write
Given a PlainMaterializer buffer strategy
And an OutputSession wired to that buffer strategy
When I create and close a panel with title "Already Flushed" and entry "k" "v"
And the session is ended cleanly
Then the strategy output contains "Already Flushed" exactly once
# ---------------------------------------------------------------
# Scenario 12: RichMaterializer delegates to color renderer
# Ensures RichMaterializer._render_element hits line 526
# ---------------------------------------------------------------
Scenario: Rich materializer renders panel with ANSI color codes
Given a RichMaterializer buffer strategy
And an OutputSession wired to that rich buffer strategy
When I create and close a rich panel with title "Rich Panel" and entry "host" "localhost"
Then the rich strategy output contains ANSI escape codes
And the rich strategy output contains "Rich Panel"