-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_hud_helpers.py
More file actions
410 lines (321 loc) · 15.1 KB
/
Copy pathtest_hud_helpers.py
File metadata and controls
410 lines (321 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""Tests for hud_helpers module (#1324).
Validates HUD state transitions driven by each hook lifecycle event:
SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop.
"""
import json
import os
import sys
import tempfile
import pytest
# Ensure hooks/lib is importable
_hooks_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_lib_dir = os.path.join(_hooks_dir, "lib")
if _lib_dir not in sys.path:
sys.path.insert(0, _lib_dir)
from hud_state import init_hud_state, read_hud_state
from hud_helpers import (
init_baseline,
on_mode_entry,
on_tool_start,
on_tool_end,
on_session_stop,
_detect_focus,
_detect_strategy,
_extract_mode_from_parse_mode,
)
@pytest.fixture()
def state_file(tmp_path):
"""Create a temp HUD state file, initialized with baseline state."""
sf = str(tmp_path / "hud-state.json")
init_hud_state("test-session-123", "5.0.0", state_file=sf)
return sf
def _read(sf: str) -> dict:
"""Read and return state from file."""
return read_hud_state(sf, fill_defaults=True)
# ---- init_baseline ----
class TestInitBaseline:
"""SessionStart: init_baseline enriches freshly-initialized state."""
def test_sets_mode_and_phase_from_pending_context(self, state_file):
init_baseline({"mode": "ACT"}, state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "ACT"
assert state["phase"] == "executing"
def test_noop_when_no_pending_context(self, state_file):
init_baseline(None, state_file=state_file)
state = _read(state_file)
assert state["currentMode"] is None # unchanged from init
assert state["phase"] == "ready"
def test_noop_when_pending_context_has_no_mode(self, state_file):
init_baseline({"status": "in_progress"}, state_file=state_file)
state = _read(state_file)
assert state["currentMode"] is None
def test_plan_mode_from_context(self, state_file):
init_baseline({"mode": "PLAN"}, state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "PLAN"
assert state["phase"] == "planning"
# ---- on_mode_entry ----
class TestOnModeEntry:
"""UserPromptSubmit: on_mode_entry resets workflow fields."""
def test_plan_mode_sets_phase_planning(self, state_file):
on_mode_entry("PLAN", state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "PLAN"
assert state["phase"] == "planning"
assert state["focus"] is None
assert state["blockerCount"] == 0
def test_act_mode_sets_phase_executing(self, state_file):
on_mode_entry("ACT", state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "ACT"
assert state["phase"] == "executing"
def test_eval_mode_sets_phase_evaluating(self, state_file):
on_mode_entry("EVAL", state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "EVAL"
assert state["phase"] == "evaluating"
def test_auto_mode_sets_phase_cycling(self, state_file):
on_mode_entry("AUTO", state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "AUTO"
assert state["phase"] == "cycling"
def test_resets_focus_and_blockers(self, state_file):
# Set some values first
from hud_state import update_hud_state
update_hud_state(state_file=state_file, focus="old-file.py", blockerCount=3)
on_mode_entry("PLAN", state_file=state_file)
state = _read(state_file)
assert state["focus"] is None
assert state["blockerCount"] == 0
@pytest.mark.parametrize("mode,expected_phase", [
("PLAN", "planning"),
("ACT", "executing"),
("EVAL", "evaluating"),
("AUTO", "cycling"),
])
def test_resets_all_stale_workflow_fields(self, state_file, mode, expected_phase):
"""Seed ALL workflow fields with non-default values then verify full reset."""
from hud_state import update_hud_state
update_hud_state(
state_file=state_file,
currentMode="EVAL",
phase="evaluating",
focus="old-file.py",
blockerCount=5,
activeAgent="Security Specialist",
executionStrategy="subagent",
councilStatus="voting",
lastHandoff="Frontend Developer",
)
on_mode_entry(mode, state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == mode
assert state["phase"] == expected_phase
assert state["focus"] is None
assert state["blockerCount"] == 0
assert state["activeAgent"] is None
assert state["executionStrategy"] is None
assert state["councilStatus"] is None
assert state["lastHandoff"] is None
def test_unknown_mode_defaults_to_ready(self, state_file):
on_mode_entry("UNKNOWN", state_file=state_file)
state = _read(state_file)
assert state["currentMode"] == "UNKNOWN"
assert state["phase"] == "ready"
# ---- on_tool_start ----
class TestOnToolStart:
"""PreToolUse: on_tool_start updates active agent, focus, strategy."""
def test_sets_active_agent_from_env(self, state_file, monkeypatch):
monkeypatch.setenv("CODINGBUDDY_ACTIVE_AGENT", "Frontend Developer")
on_tool_start("Edit", {"file_path": "/src/app.tsx"}, state_file=state_file)
state = _read(state_file)
assert state["activeAgent"] == "Frontend Developer"
def test_sets_focus_for_edit_tool(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Edit", {"file_path": "/src/components/Button.tsx"}, state_file=state_file)
state = _read(state_file)
assert state["focus"] == "Button.tsx"
def test_sets_focus_for_write_tool(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Write", {"file_path": "/src/new-file.py"}, state_file=state_file)
state = _read(state_file)
assert state["focus"] == "new-file.py"
def test_sets_focus_testing_for_pytest_command(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Bash", {"command": "python -m pytest tests/ -v"}, state_file=state_file)
state = _read(state_file)
assert state["focus"] == "testing"
def test_sets_focus_building_for_build_command(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Bash", {"command": "yarn build"}, state_file=state_file)
state = _read(state_file)
assert state["focus"] == "building"
def test_sets_strategy_subagent_for_agent_tool(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Agent", {"prompt": "review code"}, state_file=state_file)
state = _read(state_file)
assert state["executionStrategy"] == "subagent"
def test_sets_strategy_taskmaestro_for_tmux(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_start("Bash", {"command": "tmux split-window"}, state_file=state_file)
state = _read(state_file)
assert state["executionStrategy"] == "taskmaestro"
def test_noop_when_no_meaningful_info(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
# Read tool with no matching patterns
old_state = _read(state_file)
on_tool_start("Read", {"file_path": "/src/app.py"}, state_file=state_file)
new_state = _read(state_file)
# updatedAt may change, compare meaningful fields
assert new_state["activeAgent"] == old_state["activeAgent"]
assert new_state["focus"] == old_state["focus"]
# ---- on_tool_end ----
class TestOnToolEnd:
"""PostToolUse: on_tool_end records post-action state."""
def test_updates_agent_and_handoff(self, state_file, monkeypatch):
monkeypatch.setenv("CODINGBUDDY_ACTIVE_AGENT", "Security Specialist")
on_tool_end("Bash", {}, "", state_file=state_file)
state = _read(state_file)
assert state["activeAgent"] == "Security Specialist"
assert state["lastHandoff"] == "Security Specialist"
def test_updates_mode_from_parse_mode(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
on_tool_end(
"mcp__codingbuddy__parse_mode",
{"prompt": "EVAL: review code quality"},
"{}",
state_file=state_file,
)
state = _read(state_file)
assert state["currentMode"] == "EVAL"
assert state["phase"] == "evaluating"
def test_noop_when_no_agent_and_no_parse_mode(self, state_file, monkeypatch):
monkeypatch.delenv("CODINGBUDDY_ACTIVE_AGENT", raising=False)
old_state = _read(state_file)
on_tool_end("Read", {}, "", state_file=state_file)
new_state = _read(state_file)
assert new_state["activeAgent"] == old_state["activeAgent"]
# ---- on_session_stop ----
class TestOnSessionStop:
"""Stop: on_session_stop clears active state."""
def test_clears_agent_and_sets_completed(self, state_file):
# Set up active state first
from hud_state import update_hud_state
update_hud_state(
state_file=state_file,
activeAgent="Frontend Developer",
phase="executing",
focus="app.tsx",
executionStrategy="subagent",
blockerCount=2,
)
on_session_stop(state_file=state_file)
state = _read(state_file)
assert state["activeAgent"] is None
assert state["phase"] == "completed"
assert state["focus"] is None
assert state["executionStrategy"] is None
assert state["councilStatus"] is None
assert state["blockerCount"] == 0
def test_preserves_session_metadata(self, state_file):
on_session_stop(state_file=state_file)
state = _read(state_file)
# Session metadata should survive
assert state["sessionId"] == "test-session-123"
assert state["version"] == "5.0.0"
assert "sessionStartTimestamp" in state
# ---- private helpers ----
class TestDetectFocus:
def test_returns_filename_for_edit(self):
assert _detect_focus("Edit", {"file_path": "/a/b/c.py"}) == "c.py"
def test_returns_filename_for_write(self):
assert _detect_focus("Write", {"file_path": "/x/y.ts"}) == "y.ts"
def test_returns_testing_for_pytest(self):
assert _detect_focus("Bash", {"command": "python -m pytest"}) == "testing"
def test_returns_testing_for_vitest(self):
assert _detect_focus("Bash", {"command": "npx vitest run"}) == "testing"
def test_returns_building_for_yarn_build(self):
assert _detect_focus("Bash", {"command": "yarn build"}) == "building"
def test_returns_committing_for_git_commit(self):
assert _detect_focus("Bash", {"command": "git commit -m 'fix'"}) == "committing"
def test_returns_pushing_for_git_push(self):
assert _detect_focus("Bash", {"command": "git push origin main"}) == "pushing"
def test_returns_none_for_unrecognized_tool(self):
assert _detect_focus("Glob", {"pattern": "*.py"}) is None
def test_returns_none_for_generic_bash(self):
assert _detect_focus("Bash", {"command": "ls -la"}) is None
def test_truncates_parse_mode_prompt(self):
result = _detect_focus(
"mcp__codingbuddy__parse_mode",
{"prompt": "PLAN: implement a very long feature description here that exceeds limit"},
)
assert len(result) <= 40
class TestDetectStrategy:
def test_subagent_for_agent_tool(self):
assert _detect_strategy("Agent", {}) == "subagent"
def test_taskmaestro_for_tmux_command(self):
assert _detect_strategy("Bash", {"command": "tmux new-session"}) == "taskmaestro"
def test_none_for_regular_tool(self):
assert _detect_strategy("Edit", {}) is None
def test_none_for_non_tmux_bash(self):
assert _detect_strategy("Bash", {"command": "git status"}) is None
class TestExtractModeFromParseMode:
def test_extracts_plan(self):
assert _extract_mode_from_parse_mode({"prompt": "PLAN: test"}) == "PLAN"
def test_extracts_act(self):
assert _extract_mode_from_parse_mode({"prompt": "ACT: do it"}) == "ACT"
def test_extracts_eval(self):
assert _extract_mode_from_parse_mode({"prompt": "EVAL: review"}) == "EVAL"
def test_extracts_auto(self):
assert _extract_mode_from_parse_mode({"prompt": "AUTO: build"}) == "AUTO"
def test_returns_none_for_no_mode(self):
assert _extract_mode_from_parse_mode({"prompt": "hello world"}) is None
def test_returns_none_for_empty_prompt(self):
assert _extract_mode_from_parse_mode({"prompt": ""}) is None
def test_returns_none_for_missing_prompt(self):
assert _extract_mode_from_parse_mode({}) is None
# ---- Full lifecycle transition test ----
class TestFullLifecycle:
"""End-to-end test of HUD state through a complete session lifecycle."""
def test_session_lifecycle(self, tmp_path, monkeypatch):
sf = str(tmp_path / "hud-state.json")
# 1. SessionStart: init
init_hud_state("lifecycle-test", "5.0.0", state_file=sf)
state = _read(sf)
assert state["phase"] == "ready"
assert state["currentMode"] is None
# 2. SessionStart: baseline from pending context
init_baseline({"mode": "PLAN"}, state_file=sf)
state = _read(sf)
assert state["currentMode"] == "PLAN"
assert state["phase"] == "planning"
# 3. UserPromptSubmit: new mode entry
on_mode_entry("ACT", state_file=sf)
state = _read(sf)
assert state["currentMode"] == "ACT"
assert state["phase"] == "executing"
assert state["focus"] is None
assert state["blockerCount"] == 0
# 4. PreToolUse: editing a file with agent active
monkeypatch.setenv("CODINGBUDDY_ACTIVE_AGENT", "Frontend Developer")
on_tool_start("Edit", {"file_path": "/src/App.tsx"}, state_file=sf)
state = _read(sf)
assert state["activeAgent"] == "Frontend Developer"
assert state["focus"] == "App.tsx"
# 5. PostToolUse: agent handoff recorded
on_tool_end("Edit", {}, "", state_file=sf)
state = _read(sf)
assert state["lastHandoff"] == "Frontend Developer"
# 6. PreToolUse: running tests
on_tool_start("Bash", {"command": "python -m pytest tests/"}, state_file=sf)
state = _read(sf)
assert state["focus"] == "testing"
# 7. Stop: clear active state
on_session_stop(state_file=sf)
state = _read(sf)
assert state["activeAgent"] is None
assert state["phase"] == "completed"
assert state["focus"] is None
assert state["executionStrategy"] is None
# Session metadata survives
assert state["sessionId"] == "lifecycle-test"