# 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.
