|
1 | | -"""Tests for HUD state management module (#1087).""" |
| 1 | +"""Tests for HUD state management module (#1087, #1326).""" |
2 | 2 | import json |
3 | 3 | import os |
4 | 4 | import sys |
|
12 | 12 | if _lib_dir not in sys.path: |
13 | 13 | sys.path.insert(0, _lib_dir) |
14 | 14 |
|
15 | | -from hud_state import init_hud_state, read_hud_state, update_hud_state |
| 15 | +from hud_state import _EXTENDED_DEFAULTS, init_hud_state, read_hud_state, update_hud_state |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class TestReadHudState: |
@@ -86,6 +86,144 @@ def test_noop_when_file_missing(self, tmp_path): |
86 | 86 | update_hud_state(state_file=path, currentMode="PLAN") |
87 | 87 |
|
88 | 88 |
|
| 89 | +class TestExtendedSchemaDefaults: |
| 90 | + """Tests for extended HUD schema fields (#1326).""" |
| 91 | + |
| 92 | + def test_init_includes_extended_fields(self, tmp_path): |
| 93 | + path = str(tmp_path / "hud-state.json") |
| 94 | + init_hud_state("s1", "5.3.0", state_file=path) |
| 95 | + data = read_hud_state(path) |
| 96 | + |
| 97 | + assert data["phase"] == "ready" |
| 98 | + assert data["focus"] is None |
| 99 | + assert data["executionStrategy"] is None |
| 100 | + assert data["councilStatus"] is None |
| 101 | + assert data["blockerCount"] == 0 |
| 102 | + assert data["lastHandoff"] is None |
| 103 | + |
| 104 | + def test_update_extended_fields(self, tmp_path): |
| 105 | + path = str(tmp_path / "hud-state.json") |
| 106 | + init_hud_state("s1", "5.3.0", state_file=path) |
| 107 | + |
| 108 | + update_hud_state( |
| 109 | + state_file=path, |
| 110 | + phase="planning", |
| 111 | + focus="auth-feature", |
| 112 | + executionStrategy="subagent", |
| 113 | + blockerCount=2, |
| 114 | + ) |
| 115 | + data = read_hud_state(path) |
| 116 | + |
| 117 | + assert data["phase"] == "planning" |
| 118 | + assert data["focus"] == "auth-feature" |
| 119 | + assert data["executionStrategy"] == "subagent" |
| 120 | + assert data["blockerCount"] == 2 |
| 121 | + # Unchanged fields preserved |
| 122 | + assert data["councilStatus"] is None |
| 123 | + assert data["lastHandoff"] is None |
| 124 | + |
| 125 | + def test_partial_update_preserves_other_extended_fields(self, tmp_path): |
| 126 | + path = str(tmp_path / "hud-state.json") |
| 127 | + init_hud_state("s1", "5.3.0", state_file=path) |
| 128 | + |
| 129 | + update_hud_state(state_file=path, phase="acting", councilStatus="quorum") |
| 130 | + update_hud_state(state_file=path, blockerCount=1) |
| 131 | + |
| 132 | + data = read_hud_state(path) |
| 133 | + assert data["phase"] == "acting" |
| 134 | + assert data["councilStatus"] == "quorum" |
| 135 | + assert data["blockerCount"] == 1 |
| 136 | + |
| 137 | + |
| 138 | +class TestBackwardCompat: |
| 139 | + """Backward compatibility with older state files missing extended keys (#1326).""" |
| 140 | + |
| 141 | + def test_read_old_state_without_fill(self, tmp_path): |
| 142 | + """Reading an old-format file without fill_defaults returns raw data.""" |
| 143 | + path = str(tmp_path / "old.json") |
| 144 | + old_data = { |
| 145 | + "sessionId": "old-1", |
| 146 | + "version": "5.1.0", |
| 147 | + "currentMode": "PLAN", |
| 148 | + "activeAgent": None, |
| 149 | + "updatedAt": "2026-01-01T00:00:00+00:00", |
| 150 | + "sessionStartTimestamp": "2026-01-01T00:00:00+00:00", |
| 151 | + } |
| 152 | + with open(path, "w") as f: |
| 153 | + json.dump(old_data, f) |
| 154 | + |
| 155 | + result = read_hud_state(path) |
| 156 | + assert "phase" not in result |
| 157 | + assert "blockerCount" not in result |
| 158 | + |
| 159 | + def test_read_old_state_with_fill_defaults(self, tmp_path): |
| 160 | + """fill_defaults=True back-fills missing extended keys.""" |
| 161 | + path = str(tmp_path / "old.json") |
| 162 | + old_data = { |
| 163 | + "sessionId": "old-1", |
| 164 | + "version": "5.1.0", |
| 165 | + "currentMode": "PLAN", |
| 166 | + "activeAgent": None, |
| 167 | + "updatedAt": "2026-01-01T00:00:00+00:00", |
| 168 | + "sessionStartTimestamp": "2026-01-01T00:00:00+00:00", |
| 169 | + } |
| 170 | + with open(path, "w") as f: |
| 171 | + json.dump(old_data, f) |
| 172 | + |
| 173 | + result = read_hud_state(path, fill_defaults=True) |
| 174 | + assert result["phase"] == "ready" |
| 175 | + assert result["blockerCount"] == 0 |
| 176 | + assert result["focus"] is None |
| 177 | + assert result["executionStrategy"] is None |
| 178 | + assert result["councilStatus"] is None |
| 179 | + assert result["lastHandoff"] is None |
| 180 | + # Original fields untouched |
| 181 | + assert result["sessionId"] == "old-1" |
| 182 | + assert result["currentMode"] == "PLAN" |
| 183 | + |
| 184 | + def test_fill_defaults_does_not_overwrite_existing(self, tmp_path): |
| 185 | + """fill_defaults must not overwrite keys already present.""" |
| 186 | + path = str(tmp_path / "partial.json") |
| 187 | + partial = { |
| 188 | + "sessionId": "p-1", |
| 189 | + "phase": "acting", |
| 190 | + "blockerCount": 3, |
| 191 | + } |
| 192 | + with open(path, "w") as f: |
| 193 | + json.dump(partial, f) |
| 194 | + |
| 195 | + result = read_hud_state(path, fill_defaults=True) |
| 196 | + assert result["phase"] == "acting" |
| 197 | + assert result["blockerCount"] == 3 |
| 198 | + assert result["focus"] is None # filled |
| 199 | + |
| 200 | + def test_fill_defaults_on_empty_returns_empty(self, tmp_path): |
| 201 | + """fill_defaults on missing file still returns empty dict.""" |
| 202 | + path = str(tmp_path / "nope.json") |
| 203 | + result = read_hud_state(path, fill_defaults=True) |
| 204 | + assert result == {} |
| 205 | + |
| 206 | + def test_update_old_state_adds_new_field(self, tmp_path): |
| 207 | + """Updating an old-format state file can add new fields.""" |
| 208 | + path = str(tmp_path / "old.json") |
| 209 | + old_data = { |
| 210 | + "sessionId": "old-1", |
| 211 | + "version": "5.1.0", |
| 212 | + "currentMode": None, |
| 213 | + "activeAgent": None, |
| 214 | + "updatedAt": "2026-01-01T00:00:00+00:00", |
| 215 | + "sessionStartTimestamp": "2026-01-01T00:00:00+00:00", |
| 216 | + } |
| 217 | + with open(path, "w") as f: |
| 218 | + json.dump(old_data, f) |
| 219 | + |
| 220 | + update_hud_state(state_file=path, phase="evaluating", blockerCount=1) |
| 221 | + result = read_hud_state(path) |
| 222 | + assert result["phase"] == "evaluating" |
| 223 | + assert result["blockerCount"] == 1 |
| 224 | + assert result["sessionId"] == "old-1" |
| 225 | + |
| 226 | + |
89 | 227 | class TestRoundtrip: |
90 | 228 | def test_init_read_update_read(self, tmp_path): |
91 | 229 | path = str(tmp_path / "hud-state.json") |
|
0 commit comments