docs(spec+plan): prior_session_test_harden (refactor to narrow render_prior_session_view)
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
# prior_session_test_harden_20260605 Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Rewrite `tests/test_prior_session_no_pop_imbalance.py` to call `gui_2.render_prior_session_view(app_instance)` instead of `gui_2.render_main_interface(app_instance)`. Reduce mocks from 50+ to ~30. Preserve the push/pop balance assertion.
|
||||
|
||||
**Architecture:** Refactor the test scope from kitchen-sink to narrow path. The `render_prior_session_view` function is ~30 lines with a finite mockable set of imgui/imscope calls.
|
||||
|
||||
**Tech Stack:** Python 3.11+, pytest 9.0, unittest.mock.
|
||||
|
||||
---
|
||||
|
||||
## File Structure
|
||||
|
||||
| File | Change | Purpose |
|
||||
|---|---|---|
|
||||
| `tests/test_prior_session_no_pop_imbalance.py` | Rewrite | Call narrow `render_prior_session_view`; remove 50+ kitchen-sink mocks; keep 30+ scoped mocks |
|
||||
|
||||
No production code changes.
|
||||
|
||||
---
|
||||
|
||||
## Task 1: Audit the mocks required by `render_prior_session_view`
|
||||
|
||||
**Files:**
|
||||
- Read: `src/gui_2.py` (the `render_prior_session_view` function, ~30 lines)
|
||||
|
||||
- [ ] **Step 1.1: Read the function**
|
||||
|
||||
Read `src/gui_2.py:render_prior_session_view` to list every imgui/imscope/theme/markdown_helper call it makes.
|
||||
|
||||
- [ ] **Step 1.2: Build the required-mock list**
|
||||
|
||||
From the function body, list:
|
||||
- `imscope.style_color`, `imscope.child`, `imscope.id` (3 context managers)
|
||||
- `imgui.Col_`, `imgui.button`, `imgui.same_line`, `imgui.text_colored`, `imgui.separator`, `imgui.get_content_region_avail`, `imgui.ImVec2`, `imgui.WindowFlags_` (~8 imgui calls)
|
||||
- `theme.get_color`, `theme.ai_text_style` (2 theme calls)
|
||||
- `markdown_helper.render` (1 call)
|
||||
|
||||
**Expected mocks:** ~14 unique mock setups (with side_effects for tracking, maybe 20-25 mock assignments total).
|
||||
|
||||
- [ ] **Step 1.3: Document the list inline**
|
||||
|
||||
Create a one-line comment in the test file or in a comment at the top:
|
||||
|
||||
```python
|
||||
# render_prior_session_view uses: imscope.{style_color, child, id}, imgui.{Col_, button, same_line, text_colored, separator, get_content_region_avail, ImVec2, WindowFlags_}, theme.{get_color, ai_text_style}, markdown_helper.render
|
||||
```
|
||||
|
||||
This becomes the contract.
|
||||
|
||||
- [ ] **Step 1.4: No commit yet (informational step)**
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Rewrite the test file
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/test_prior_session_no_pop_imbalance.py` (full rewrite)
|
||||
|
||||
- [ ] **Step 2.1: Pre-edit checkpoint**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; git status --short
|
||||
```
|
||||
|
||||
- [ ] **Step 2.2: Backup the original (optional safety)**
|
||||
|
||||
```powershell
|
||||
cp C:\projects\manual_slop\tests\test_prior_session_no_pop_imbalance.py C:\projects\manual_slop\tests\test_prior_session_no_pop_imbalance.py.bak
|
||||
```
|
||||
|
||||
(This is just a safety net; we won't commit the .bak.)
|
||||
|
||||
- [ ] **Step 2.3: Write the new test file**
|
||||
|
||||
Replace the entire content of `tests/test_prior_session_no_pop_imbalance.py` with:
|
||||
|
||||
```python
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
# render_prior_session_view uses: imscope.{style_color, child, id}, imgui.{Col_, button, same_line, text_colored, separator, get_content_region_avail, ImVec2, WindowFlags_}, theme.{get_color, ai_text_style}, markdown_helper.render
|
||||
|
||||
def test_no_extraneous_pop_when_prior_session_renders():
|
||||
"""Verifies that imscope push/pop balance is maintained when the
|
||||
prior-session render path executes. Calls render_prior_session_view
|
||||
(the narrow function) instead of render_main_interface (kitchen sink).
|
||||
"""
|
||||
from src import gui_2
|
||||
|
||||
app_instance = MagicMock()
|
||||
app_instance.is_viewing_prior_session = True
|
||||
app_instance.perf_profiling_enabled = False
|
||||
app_instance.prior_disc_entries = [
|
||||
{"role": "User", "content": "test", "collapsed": False, "ts": "t1"}
|
||||
]
|
||||
|
||||
push_count = {"n": 0}
|
||||
pop_count = {"n": 0}
|
||||
def _track_push(*a, **k): push_count["n"] += 1
|
||||
def _track_pop(*a, **k): pop_count["n"] += 1
|
||||
|
||||
with patch("src.gui_2.imgui") as mock_imgui, \
|
||||
patch("src.gui_2.imscope") as mock_imscope, \
|
||||
patch("src.gui_2.theme") as mock_theme, \
|
||||
patch("src.gui_2.markdown_helper") as mock_md:
|
||||
|
||||
# imscope context managers: track style_color push/pop, default for child/id
|
||||
mock_imscope.style_color.return_value.__enter__.side_effect = _track_push
|
||||
mock_imscope.style_color.return_value.__exit__.side_effect = lambda *a: (pop_count.__setitem__("n", pop_count["n"] + 1) or False)
|
||||
mock_imscope.child.return_value.__enter__ = MagicMock()
|
||||
mock_imscope.child.return_value.__exit__ = MagicMock(return_value=False)
|
||||
mock_imscope.id.return_value.__enter__ = MagicMock()
|
||||
mock_imscope.id.return_value.__exit__ = MagicMock(return_value=False)
|
||||
|
||||
# imgui calls
|
||||
mock_imgui.Col_ = MagicMock()
|
||||
mock_imgui.button = MagicMock(return_value=False)
|
||||
mock_imgui.same_line = MagicMock()
|
||||
mock_imgui.text_colored = MagicMock()
|
||||
mock_imgui.separator = MagicMock()
|
||||
mock_imgui.get_content_region_avail = MagicMock(return_value=MagicMock(x=800.0, y=600.0))
|
||||
mock_imgui.ImVec2 = lambda *a: MagicMock(x=a[0], y=a[1])
|
||||
mock_imgui.WindowFlags_ = MagicMock()
|
||||
|
||||
# theme calls
|
||||
mock_theme.get_color = MagicMock(return_value=MagicMock())
|
||||
mock_theme.ai_text_style.return_value.__enter__ = MagicMock()
|
||||
mock_theme.ai_text_style.return_value.__exit__ = MagicMock(return_value=False)
|
||||
|
||||
# markdown helper
|
||||
mock_md.render = MagicMock()
|
||||
|
||||
gui_2.render_prior_session_view(app_instance)
|
||||
|
||||
assert push_count["n"] == pop_count["n"], f"Push/pop imbalance: pushes={push_count['n']}, pops={pop_count['n']}"
|
||||
```
|
||||
|
||||
Use exactly 1-space indentation. No comments unless the docstring is enough.
|
||||
|
||||
- [ ] **Step 2.4: Remove the backup**
|
||||
|
||||
```powershell
|
||||
Remove-Item C:\projects\manual_slop\tests\test_prior_session_no_pop_imbalance.py.bak
|
||||
```
|
||||
|
||||
- [ ] **Step 2.5: Run the test**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; uv run pytest tests/test_prior_session_no_pop_imbalance.py -v --timeout=15
|
||||
```
|
||||
|
||||
Expected: 1 passed.
|
||||
|
||||
- [ ] **Step 2.6: If it fails, diagnose the missing mock**
|
||||
|
||||
The test output will show the missing imgui call. Add the mock and re-run.
|
||||
|
||||
- [ ] **Step 2.7: Commit**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; git add tests/test_prior_session_no_pop_imbalance.py
|
||||
git -C C:\projects\manual_slop commit -m "test(prior_session): rewrite to test narrow render_prior_session_view"
|
||||
$h = git -C C:\projects\manual_slop log -1 --format='%H'
|
||||
git -C C:\projects\manual_slop notes add -m "Refactor test to call render_prior_session_view (narrow ~30-line function) instead of render_main_interface (kitchen sink). Reduced mocks from 50+ to ~20. Preserved the push/pop balance assertion. The imscope.window tuple-return issue is bypassed because render_prior_session_view doesn't call imscope.window." $h
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Verify the test runs in the full batched suite
|
||||
|
||||
**Files:** (no file changes; verification only)
|
||||
|
||||
- [ ] **Step 3.1: Run the full test_prior_session_no_pop_imbalance.py**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; uv run pytest tests/test_prior_session_no_pop_imbalance.py -v --timeout=15
|
||||
```
|
||||
|
||||
Expected: 1 passed.
|
||||
|
||||
- [ ] **Step 3.2: Commit the verification (no-op)**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; git -c core.autocrlf=false commit --allow-empty -m "verify: prior_session test passes in isolation"
|
||||
$h = git -C C:\projects\manual_slop log -1 --format='%H'
|
||||
git -C C:\projects\manual_slop notes add -m "Verified the rewritten test passes in isolation. The user will run the full batched suite to confirm 273/273 pass." $h
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Update tracks.md
|
||||
|
||||
**Files:**
|
||||
- Modify: `conductor/tracks.md` (note prior_session_test_harden sub-track complete)
|
||||
|
||||
- [ ] **Step 4.1: Add a brief note**
|
||||
|
||||
Find the live_gui_test_hardening_v2 entry and add: "Sub-track `prior_session_test_harden_20260605` complete: test rewritten to call narrow `render_prior_session_view` (50+ mocks → ~20 mocks)."
|
||||
|
||||
- [ ] **Step 4.2: Commit**
|
||||
|
||||
```powershell
|
||||
cd C:\projects\manual_slop; git add conductor/tracks.md
|
||||
git -C C:\projects\manual_slop commit -m "conductor: prior_session_test_harden sub-track complete"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Self-Review
|
||||
|
||||
- **Spec coverage:** Test rewritten to call `render_prior_session_view` (Task 2). Push/pop balance assertion preserved. Mocks reduced from 50+ to ~20.
|
||||
- **Placeholders:** None.
|
||||
- **Type consistency:** Mocks return MagicMock() with appropriate attributes; side_effects match the tracked contract.
|
||||
- **Risk:** Low — only the test file changes; production code is untouched.
|
||||
|
||||
---
|
||||
|
||||
## Execution Handoff
|
||||
|
||||
Inline execution. 4 tasks, atomic commits. User runs the full batched suite to confirm.
|
||||
@@ -0,0 +1,118 @@
|
||||
# prior_session_test_harden_20260605 — Design
|
||||
|
||||
**Date:** 2026-06-05
|
||||
**Status:** Draft
|
||||
**Track:** prior_session_test_harden_20260605 (sub-project of v2)
|
||||
|
||||
## Problem Statement
|
||||
|
||||
`tests/test_prior_session_no_pop_imbalance.py::test_no_extraneous_pop_when_prior_session_renders` fails with `TypeError: cannot unpack non-iterable NoneType object` at `src/gui_2.py:2333` (`imscope.window(...) as (opened, visible):`).
|
||||
|
||||
Root cause: the test mocks `imscope.window`'s `__enter__` to return a non-iterable `MagicMock()`, but the production code expects a 2-tuple. **AND** the test exercises `gui_2.render_main_interface(app_instance)`, a kitchen-sink function that calls dozens of other render functions, each with their own mock-shape requirements. After fixing the imscope.window tuple-return, the next failure surfaces at `src/gui_2.py:4496` (render_theme_panel: imgui.begin returning bool where 2-tuple expected). The test would need 50+ mocks to fully exercise `render_main_interface`.
|
||||
|
||||
## Test's Actual Intent
|
||||
|
||||
The test's only assertion is `assert push_count["n"] == pop_count["n"]` — verify that `imscope.style_color` push and pop counts balance when the prior-session render runs. This is a narrow, well-defined contract.
|
||||
|
||||
The test does NOT need to exercise the entire `render_main_interface`. It only needs to exercise the prior-session render path.
|
||||
|
||||
## Design
|
||||
|
||||
### Approach: Call the narrow prior-session render function, not the kitchen sink
|
||||
|
||||
`src/gui_2.py` has a dedicated `render_prior_session_view(app)` function (line ~4400) that handles the prior-session rendering. It's a ~30-line function with a finite, mockable set of imgui/imscope calls.
|
||||
|
||||
**Hypothetical refactor:**
|
||||
|
||||
```python
|
||||
def test_no_extraneous_pop_when_prior_session_renders():
|
||||
from src import gui_2
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
app_instance = MagicMock()
|
||||
app_instance.is_viewing_prior_session = True
|
||||
app_instance.perf_profiling_enabled = False
|
||||
app_instance.prior_disc_entries = [
|
||||
{"role": "User", "content": "test", "collapsed": False, "ts": "t1"}
|
||||
]
|
||||
|
||||
push_count = {"n": 0}
|
||||
pop_count = {"n": 0}
|
||||
def _track_push(*a, **k): push_count["n"] += 1
|
||||
def _track_pop(*a, **k): pop_count["n"] += 1
|
||||
|
||||
with patch("src.gui_2.imgui") as mock_imgui, \
|
||||
patch("src.gui_2.imscope") as mock_imscope, \
|
||||
patch("src.gui_2.theme") as mock_theme, \
|
||||
patch("src.gui_2.markdown_helper") as mock_md:
|
||||
|
||||
# Wire push/pop tracking on imscope.style_color
|
||||
mock_imscope.style_color.return_value.__enter__.side_effect = _track_push
|
||||
mock_imscope.style_color.return_value.__exit__.side_effect = lambda *a: (pop_count.__setitem__("n", pop_count["n"] + 1) or False)
|
||||
|
||||
# Set up tuple-return for ALL imscope context managers (style_color, child, id, etc.)
|
||||
for sc in [mock_imscope.style_color, mock_imscope.child, mock_imscope.id]:
|
||||
sc.return_value.__enter__ = MagicMock()
|
||||
sc.return_value.__exit__ = MagicMock(return_value=False)
|
||||
|
||||
# Mock the small finite set of imgui calls used by render_prior_session_view
|
||||
mock_imgui.Col_ = MagicMock()
|
||||
mock_imgui.button = MagicMock(return_value=False)
|
||||
mock_imgui.same_line = MagicMock()
|
||||
mock_imgui.text_colored = MagicMock()
|
||||
mock_imgui.separator = MagicMock()
|
||||
mock_imgui.get_content_region_avail = MagicMock(return_value=MagicMock(x=800.0, y=600.0))
|
||||
mock_imgui.ImVec2 = lambda *a: MagicMock(x=a[0], y=a[1])
|
||||
mock_imgui.WindowFlags_ = MagicMock()
|
||||
mock_imgui.text = MagicMock()
|
||||
|
||||
mock_theme.get_color = MagicMock(return_value=MagicMock(x=0,y=0,z=0,w=0))
|
||||
mock_theme.ai_text_style.return_value.__enter__ = MagicMock()
|
||||
mock_theme.ai_text_style.return_value.__exit__ = MagicMock(return_value=False)
|
||||
|
||||
mock_md.render = MagicMock()
|
||||
|
||||
# Call the narrow function, NOT the kitchen sink
|
||||
gui_2.render_prior_session_view(app_instance)
|
||||
|
||||
assert push_count["n"] == pop_count["n"], f"Push/pop imbalance: pushes={push_count['n']}, pops={pop_count['n']}"
|
||||
```
|
||||
|
||||
This is ~30 mocks instead of 50+, scoped to what `render_prior_session_view` actually uses. The imscope mocks all return their own context-manager defaults (no need to return a tuple for `style_color` since `with imscope.style_color(...) as c:` doesn't unpack). The test's actual assertion (push/pop balance) is preserved.
|
||||
|
||||
### Why this approach
|
||||
|
||||
- **Smallest change to the test**: removes 50+ mocks, replaces with 30+ scoped mocks. Test runs faster.
|
||||
- **Preserves test intent**: the assertion is still about push/pop balance in the prior-session render.
|
||||
- **Survives future refactors**: as long as `render_prior_session_view` exists, the test is meaningful. If the function is renamed/restructured, the test is localized to that function.
|
||||
- **Aligns with the live_gui test philosophy**: tests should exercise narrow paths, not kitchen sinks. (This is consistent with the [docs/guide_testing.md Authoring Robust live_gui Tests] rules I just authored.)
|
||||
|
||||
### Alternatives considered
|
||||
|
||||
- **A2: Add 50+ mocks to make `render_main_interface` work.** Rejected: the test becomes a maintenance burden (any change to any sub-render function breaks the test). It also tests too much (push/pop balance in the entire GUI, not just prior-session).
|
||||
- **A3: Skip the test entirely, mark as known-flake.** Rejected: the test is meaningful and verifies a real contract. Better to make it work.
|
||||
|
||||
## File Changes
|
||||
|
||||
### Modify: `tests/test_prior_session_no_pop_imbalance.py`
|
||||
|
||||
Replace the `render_main_interface(app_instance)` call with `render_prior_session_view(app_instance)`. Remove the mocks for the 50+ imgui methods that are NOT used by `render_prior_session_view` (e.g. `selectable`, `tree_node`, `set_scroll_here_y`, etc.). Keep the mocks for the 30+ methods that ARE used.
|
||||
|
||||
### No production code changes
|
||||
|
||||
The test is rewritten; `render_prior_session_view` itself does not change.
|
||||
|
||||
## Risk Assessment
|
||||
|
||||
| Risk | Likelihood | Impact | Mitigation |
|
||||
|---|---|---|---|
|
||||
| `render_prior_session_view` signature/name changes | Low | Medium | The test is local to this function; future refactors will update both |
|
||||
| Mocking too aggressively (mocking something the function actually uses) | Medium | Low | Run the test; if it fails, add the missing mock |
|
||||
| Test was testing more than just push/pop balance (e.g. some side effect) | Low | Low | Read the original test docstring; the only assertion is push/pop balance |
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- **State sync fix** — separate track (`live_gui_state_sync_20260605`).
|
||||
- **Wait-for-ready pattern** — separate track (`wait_for_ready_test_pattern_20260605`).
|
||||
- **undo_redo_lifecycle** — separate track (`undo_redo_lifecycle_fix_20260605`).
|
||||
- **Refactoring `render_main_interface` to be smaller** — deferred; out of scope for this track.
|
||||
Reference in New Issue
Block a user