-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_hud_state.py
More file actions
355 lines (291 loc) · 12.5 KB
/
Copy pathtest_hud_state.py
File metadata and controls
355 lines (291 loc) · 12.5 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
"""Tests for HUD state management module (#1087, #1326)."""
import json
import os
import sys
import time
import pytest
# Ensure hooks/lib is on path
_tests_dir = os.path.dirname(os.path.abspath(__file__))
_lib_dir = os.path.join(os.path.dirname(_tests_dir), "hooks", "lib")
if _lib_dir not in sys.path:
sys.path.insert(0, _lib_dir)
from hud_state import _EXTENDED_DEFAULTS, init_hud_state, read_hud_state, update_hud_state
class TestReadHudState:
def test_returns_empty_dict_when_file_missing(self, tmp_path):
path = str(tmp_path / "nonexistent.json")
result = read_hud_state(path)
assert result == {}
def test_returns_empty_dict_when_json_corrupted(self, tmp_path):
path = str(tmp_path / "bad.json")
with open(path, "w") as f:
f.write("{invalid json")
result = read_hud_state(path)
assert result == {}
def test_reads_valid_state(self, tmp_path):
path = str(tmp_path / "state.json")
data = {"sessionId": "abc", "currentMode": "PLAN"}
with open(path, "w") as f:
json.dump(data, f)
result = read_hud_state(path)
assert result == data
class TestInitHudState:
def test_creates_file_with_correct_schema(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("session-123", "5.1.1", state_file=path)
with open(path, "r") as f:
data = json.load(f)
assert data["sessionId"] == "session-123"
assert data["version"] == "5.1.1"
assert data["currentMode"] is None
assert data["activeAgent"] is None
assert "sessionStartTimestamp" in data
assert "updatedAt" in data
def test_creates_parent_directory(self, tmp_path):
path = str(tmp_path / "nested" / "deep" / "hud-state.json")
init_hud_state("s1", "5.1.1", state_file=path)
assert os.path.isfile(path)
class TestUpdateHudState:
def test_merges_kwargs_into_existing_state(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.1.1", state_file=path)
update_hud_state(state_file=path, currentMode="ACT")
result = read_hud_state(path)
assert result["currentMode"] == "ACT"
assert result["sessionId"] == "s1"
assert result["version"] == "5.1.1"
def test_updates_timestamp(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.1.1", state_file=path)
before = read_hud_state(path)["updatedAt"]
time.sleep(0.01)
update_hud_state(state_file=path, currentMode="EVAL")
after = read_hud_state(path)["updatedAt"]
assert after > before
def test_noop_when_file_missing(self, tmp_path):
path = str(tmp_path / "nonexistent.json")
# Should not raise
update_hud_state(state_file=path, currentMode="PLAN")
class TestExtendedSchemaDefaults:
"""Tests for extended HUD schema fields (#1326)."""
def test_init_includes_extended_fields(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.3.0", state_file=path)
data = read_hud_state(path)
assert data["phase"] == "ready"
assert data["focus"] is None
assert data["executionStrategy"] is None
assert data["councilStatus"] is None
assert data["blockerCount"] == 0
assert data["lastHandoff"] is None
# Council fields (#1364)
assert data["councilActive"] is False
assert data["councilStage"] == ""
assert data["councilCast"] == []
def test_update_extended_fields(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.3.0", state_file=path)
update_hud_state(
state_file=path,
phase="planning",
focus="auth-feature",
executionStrategy="subagent",
blockerCount=2,
)
data = read_hud_state(path)
assert data["phase"] == "planning"
assert data["focus"] == "auth-feature"
assert data["executionStrategy"] == "subagent"
assert data["blockerCount"] == 2
# Unchanged fields preserved
assert data["councilStatus"] is None
assert data["lastHandoff"] is None
def test_partial_update_preserves_other_extended_fields(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.3.0", state_file=path)
update_hud_state(state_file=path, phase="acting", councilStatus="quorum")
update_hud_state(state_file=path, blockerCount=1)
data = read_hud_state(path)
assert data["phase"] == "acting"
assert data["councilStatus"] == "quorum"
assert data["blockerCount"] == 1
class TestCouncilFields:
"""Tests for council UX fields (#1364)."""
def test_init_includes_council_defaults(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.4.0", state_file=path)
data = read_hud_state(path)
assert data["councilActive"] is False
assert data["councilStage"] == ""
assert data["councilCast"] == []
def test_update_council_active(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.4.0", state_file=path)
update_hud_state(state_file=path, councilActive=True)
data = read_hud_state(path)
assert data["councilActive"] is True
def test_update_council_stage(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.4.0", state_file=path)
update_hud_state(state_file=path, councilStage="reviewing")
data = read_hud_state(path)
assert data["councilStage"] == "reviewing"
def test_update_council_cast(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.4.0", state_file=path)
cast = ["security-specialist", "frontend-developer"]
update_hud_state(state_file=path, councilCast=cast)
data = read_hud_state(path)
assert data["councilCast"] == cast
def test_council_full_lifecycle(self, tmp_path):
"""Council start -> review -> consensus -> done."""
path = str(tmp_path / "hud-state.json")
init_hud_state("s1", "5.4.0", state_file=path)
# Start council
update_hud_state(
state_file=path,
councilActive=True,
councilStage="opening",
councilCast=["arch", "security"],
)
data = read_hud_state(path)
assert data["councilActive"] is True
assert data["councilStage"] == "opening"
assert data["councilCast"] == ["arch", "security"]
# Advance to reviewing
update_hud_state(state_file=path, councilStage="reviewing")
data = read_hud_state(path)
assert data["councilStage"] == "reviewing"
assert data["councilActive"] is True # preserved
# Consensus
update_hud_state(state_file=path, councilStage="consensus")
data = read_hud_state(path)
assert data["councilStage"] == "consensus"
# Done
update_hud_state(
state_file=path,
councilActive=False,
councilStage="done",
councilCast=[],
)
data = read_hud_state(path)
assert data["councilActive"] is False
assert data["councilStage"] == "done"
assert data["councilCast"] == []
def test_fill_defaults_includes_council_fields(self, tmp_path):
"""Old state files get council defaults via fill_defaults."""
path = str(tmp_path / "old.json")
old_data = {"sessionId": "old-1", "version": "5.1.0"}
with open(path, "w") as f:
json.dump(old_data, f)
result = read_hud_state(path, fill_defaults=True)
assert result["councilActive"] is False
assert result["councilStage"] == ""
assert result["councilCast"] == []
def test_fill_defaults_does_not_overwrite_council(self, tmp_path):
"""fill_defaults must not overwrite existing council fields."""
path = str(tmp_path / "partial.json")
partial = {
"sessionId": "p-1",
"councilActive": True,
"councilStage": "reviewing",
"councilCast": ["agent-a"],
}
with open(path, "w") as f:
json.dump(partial, f)
result = read_hud_state(path, fill_defaults=True)
assert result["councilActive"] is True
assert result["councilStage"] == "reviewing"
assert result["councilCast"] == ["agent-a"]
class TestBackwardCompat:
"""Backward compatibility with older state files missing extended keys (#1326)."""
def test_read_old_state_without_fill(self, tmp_path):
"""Reading an old-format file without fill_defaults returns raw data."""
path = str(tmp_path / "old.json")
old_data = {
"sessionId": "old-1",
"version": "5.1.0",
"currentMode": "PLAN",
"activeAgent": None,
"updatedAt": "2026-01-01T00:00:00+00:00",
"sessionStartTimestamp": "2026-01-01T00:00:00+00:00",
}
with open(path, "w") as f:
json.dump(old_data, f)
result = read_hud_state(path)
assert "phase" not in result
assert "blockerCount" not in result
def test_read_old_state_with_fill_defaults(self, tmp_path):
"""fill_defaults=True back-fills missing extended keys."""
path = str(tmp_path / "old.json")
old_data = {
"sessionId": "old-1",
"version": "5.1.0",
"currentMode": "PLAN",
"activeAgent": None,
"updatedAt": "2026-01-01T00:00:00+00:00",
"sessionStartTimestamp": "2026-01-01T00:00:00+00:00",
}
with open(path, "w") as f:
json.dump(old_data, f)
result = read_hud_state(path, fill_defaults=True)
assert result["phase"] == "ready"
assert result["blockerCount"] == 0
assert result["focus"] is None
assert result["executionStrategy"] is None
assert result["councilStatus"] is None
assert result["lastHandoff"] is None
# Original fields untouched
assert result["sessionId"] == "old-1"
assert result["currentMode"] == "PLAN"
def test_fill_defaults_does_not_overwrite_existing(self, tmp_path):
"""fill_defaults must not overwrite keys already present."""
path = str(tmp_path / "partial.json")
partial = {
"sessionId": "p-1",
"phase": "acting",
"blockerCount": 3,
}
with open(path, "w") as f:
json.dump(partial, f)
result = read_hud_state(path, fill_defaults=True)
assert result["phase"] == "acting"
assert result["blockerCount"] == 3
assert result["focus"] is None # filled
def test_fill_defaults_on_empty_returns_empty(self, tmp_path):
"""fill_defaults on missing file still returns empty dict."""
path = str(tmp_path / "nope.json")
result = read_hud_state(path, fill_defaults=True)
assert result == {}
def test_update_old_state_adds_new_field(self, tmp_path):
"""Updating an old-format state file can add new fields."""
path = str(tmp_path / "old.json")
old_data = {
"sessionId": "old-1",
"version": "5.1.0",
"currentMode": None,
"activeAgent": None,
"updatedAt": "2026-01-01T00:00:00+00:00",
"sessionStartTimestamp": "2026-01-01T00:00:00+00:00",
}
with open(path, "w") as f:
json.dump(old_data, f)
update_hud_state(state_file=path, phase="evaluating", blockerCount=1)
result = read_hud_state(path)
assert result["phase"] == "evaluating"
assert result["blockerCount"] == 1
assert result["sessionId"] == "old-1"
class TestRoundtrip:
def test_init_read_update_read(self, tmp_path):
path = str(tmp_path / "hud-state.json")
init_hud_state("rt-1", "5.1.1", state_file=path)
state1 = read_hud_state(path)
assert state1["currentMode"] is None
update_hud_state(state_file=path, currentMode="PLAN", activeAgent="architect")
state2 = read_hud_state(path)
assert state2["currentMode"] == "PLAN"
assert state2["activeAgent"] == "architect"
assert state2["sessionId"] == "rt-1"
update_hud_state(state_file=path, currentMode="ACT")
state3 = read_hud_state(path)
assert state3["currentMode"] == "ACT"
assert state3["activeAgent"] == "architect" # preserved