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
|
// ignore_for_file: no-magic-number
import 'package:test/test.dart';
import 'package:x_haven_server/core/commands/add_monster_command.dart';
import 'package:x_haven_server/core/commands/set_level_command.dart';
import 'package:x_haven_server/core/state/game_state.dart';
import 'package:x_haven_server/core/service_locator.dart';
import '../command/test_helpers.dart';
void main() {
setUpAll(() async {
await setUpGame();
});
setUp(() {
getIt<GameState>().clearList();
});
group('ActionHandler', () {
group('getCurrent', () {
test('getCurrent returns the last executed command', () {
final gs = getIt<GameState>();
final cmd = SetLevelCommand(3, null);
gs.action(cmd);
expect(gs.getCurrent(), same(cmd));
gs.undo();
});
});
group('redo', () {
test('redo restores state after undo', () {
final gs = getIt<GameState>();
// Two actions so gameSaveStates has enough entries for redo
gs.action(SetLevelCommand(2, null));
gs.action(SetLevelCommand(5, null));
expect(gs.level, 5);
gs.undo(); // back to level 2
expect(gs.level, 2);
gs.redo(); // forward to level 5
expect(gs.level, 5);
gs.undo();
gs.undo();
});
test('redo is a no-op when there is nothing to redo', () {
final gs = getIt<GameState>();
gs.action(SetLevelCommand(4, null));
// No undo performed, so redo should not crash and not change state
final levelBefore = gs.level;
gs.redo();
expect(gs.level, levelBefore);
gs.undo();
});
test('redo increments commandIndex', () {
final gs = getIt<GameState>();
gs.action(SetLevelCommand(2, null));
gs.action(SetLevelCommand(3, null));
final indexAfterActions = gs.commandIndex;
gs.undo();
expect(gs.commandIndex, indexAfterActions - 1);
gs.redo();
expect(gs.commandIndex, indexAfterActions);
gs.undo();
gs.undo();
});
});
group('redo list cleanup', () {
test('new action after undo clears the redo list', () {
final gs = getIt<GameState>();
gs.action(SetLevelCommand(3, null));
gs.action(SetLevelCommand(4, null));
//final indexBefore = gs.commandIndex;
gs.undo(); // undo SetLevel(4), now commandIndex = indexBefore - 1
// Do a new action — should clear SetLevel(4) from the redo list
gs.action(SetLevelCommand(5, null));
// commands length should equal commandIndex + 1 (no redo entries)
expect(gs.commands.length - 1, gs.commandIndex);
expect(gs.commandDescriptions.length - 1, gs.commandIndex);
// Undo back to baseline
gs.undo();
gs.undo();
});
});
group('maxUndo eviction', () {
test('commands and gameSaveStates beyond maxUndo are nulled', () {
final gs = getIt<GameState>();
final maxUndo = gs.maxUndo;
final startIndex = gs.commandIndex;
// Execute maxUndo + 1 commands to trigger eviction
for (int i = 0; i <= maxUndo; i++) {
gs.action(SetLevelCommand((i % 7) + 1, null));
}
// After maxUndo+1 actions, the oldest command entry should be nulled
expect(gs.commands[startIndex + 1], isNull);
// gameSaveStates at oldest entry should also be nulled
expect(gs.gameSaveStates[startIndex + 1], isNull);
// Undo back to near the start (only valid undo states)
for (int i = 0; i < maxUndo; i++) {
gs.undo();
}
});
});
group('getCurrent edge cases', () {
test(
'getCurrent throws when there is no valid command at current index',
() {
final gs = getIt<GameState>();
// Reset so commandIndex is -1 (no commands executed yet).
gs.commandIndex = -1;
gs.resetCommandHistory();
// getCurrent accesses _commands[commandIndex] — either a RangeError
// (negative index) or TypeError (null-check on a null entry). Either
// way it must throw an Error so callers know to guard the call site.
expect(() => gs.getCurrent(), throwsA(isA<Error>()));
},
);
});
group('redo after maxUndo eviction', () {
test('redo from the oldest valid position does not crash', () {
final gs = getIt<GameState>();
final maxUndo = gs.maxUndo;
// Execute maxUndo + 1 commands so the oldest save state is evicted.
for (int i = 0; i <= maxUndo; i++) {
gs.action(SetLevelCommand((i % 7) + 1, null));
}
// Undo all the way to the eviction boundary:
// only the last maxUndo states are guaranteed non-null.
for (int i = 0; i < maxUndo; i++) {
gs.undo();
}
// commandIndex is now at the boundary; gameSaveStates[commandIndex + 1]
// may be null (evicted). redo() must return early without crashing.
expect(() => gs.redo(), returnsNormally);
// Restore state for other tests.
gs.undo();
});
});
group('add monster then undo', () {
test('undo after adding a monster removes it from the list', () {
final gs = getIt<GameState>();
gs.clearList();
gs.action(
AddMonsterCommand('Zealot', 1, false, gameState: getIt<GameState>()),
);
expect(gs.currentList.any((e) => e.id == 'Zealot'), isTrue);
gs.undo();
expect(gs.currentList.any((e) => e.id == 'Zealot'), isFalse);
});
});
group('undo with commandIndex beyond gameSaveStates (Sentry regression)', () {
// Regression test for:
// ArgumentError: RangeError (length): Invalid value: Not in inclusive range 0..1079: 1080
// ActionHandler.undo — crash when commandIndex >= gameSaveStates.length.
//
// Cause: in the server multiplayer path, Server.updateStateFromMessage sets
// commandIndex directly, then calls save(). After a
// resetState() (history cleared) a client that was ahead can send a message
// whose index ends up beyond the current gameSaveStates length after save().
// A subsequent "undo" message from any client then crashes at
// _gameSaveStates[commandIndex] with no upper-bound guard.
test('does not crash when commandIndex >= gameSaveStates.length', () {
final gs = getIt<GameState>();
// Build a small amount of history.
gs.action(SetLevelCommand(3, null));
gs.action(SetLevelCommand(4, null));
// Advance commandIndex past the end of gameSaveStates, simulating the
// server receiving a state message whose index was not matched by a save().
gs.commandIndex = gs.gameSaveStates.length; // one beyond valid range
// A client sends "undo" — the server calls undoState() → undo().
// This must not throw a RangeError.
expect(() => gs.undo(), returnsNormally);
// Restore a clean state for subsequent tests.
gs.commandIndex = -1;
gs.resetCommandHistory();
});
});
group('redo after connection reset in server mode', () {
// Regression test for: ArgumentError: RangeError (length): Invalid value:
// Valid value range is empty: -1 in ActionHandler.redo when a stale client
// sends "redo" after the server resets its command history on reconnect.
test('does not crash when commandIndex is -1 after connection reset', () {
final gs = getIt<GameState>();
// Build some history so gameSaveStates has an entry to retain.
gs.action(SetLevelCommand(3, null));
// Simulate the server-side connection-reset sequence:
// commandIndex → -1, command/description lists cleared, last save state kept.
gs.commandIndex = -1;
gs.resetCommandHistory();
// Stale client sends "redo" before it has re-synced with the server.
expect(() => gs.redo(), returnsNormally);
});
});
});
}
|