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
|
// ignore_for_file: no-magic-number
/// Headless full-round integration test (Phase 1 / Milestone M1).
///
/// Drives the ported engine through a complete game round using only
/// command invocations — no UI, no network:
/// scenario setup → characters → initiative → draw → turns → next round →
/// round 2 (and a first turn of round 2 advanced to round 3).
library;
import 'package:test/test.dart';
import 'package:x_haven_server/core/commands/add_character_command.dart';
import 'package:x_haven_server/core/commands/add_monster_command.dart';
import 'package:x_haven_server/core/commands/add_standee_command.dart';
import 'package:x_haven_server/core/commands/set_campaign_command.dart';
import 'package:x_haven_server/core/commands/set_init_command.dart';
import 'package:x_haven_server/core/commands/set_scenario_command.dart';
import 'package:x_haven_server/core/commands/turn_done_command.dart';
import 'package:x_haven_server/core/enums.dart';
import 'package:x_haven_server/core/game_actions.dart';
import 'package:x_haven_server/core/game_methods.dart';
import 'package:x_haven_server/core/settings.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();
});
test('full round: setup, initiative, draw, turns, next round, round 2', () {
final gs = getIt<GameState>();
final settings = getIt<Settings>();
gs.clearList();
settings.noInit = false; // force the initiative requirement
// --- Scenario setup ----------------------------------------------------
// 'custom' is the no-data scenario used throughout the reference test
// suite (named scenarios in the test data reference monsters that are
// not in the test monster table).
gs.action(SetCampaignCommand('Frosthaven'));
gs.action(SetScenarioCommand('custom', false, gameState: gs));
expect(gs.currentCampaign, 'Frosthaven');
expect(gs.scenario, 'custom');
expect(gs.roundState, RoundState.chooseInitiative);
// --- Figures -----------------------------------------------------------
gs.action(AddCharacterCommand('Blinkblade', 'Frosthaven', 'Blinky', 1));
gs.action(AddCharacterCommand('Banner Spear', 'Frosthaven', 'Spear', 1));
gs.action(
AddMonsterCommand('Ancient Artillery (FH)', 2, false, gameState: gs),
);
expect(gs.currentList.whereType<Character>().length, 2);
expect(gs.currentList.whereType<Monster>().length, 1);
// A monster only joins combat (takes turns) once a standee is placed on
// it — the reference app's auto-add dialog is UI, so we do it explicitly.
gs.action(
AddStandeeCommand(
1,
null,
'Ancient Artillery (FH)',
MonsterType.normal,
false,
gameState: gs,
),
);
final addedMonster =
gs.currentList.firstWhere((e) => e is Monster) as Monster;
expect(addedMonster.isActive, isTrue);
// --- Initiative (secret until every player has entered) ----------------
gs.action(SetInitCommand('Blinkblade', 4, gameState: gs));
expect(
GameMethods.canDraw(),
isFalse,
reason: 'Banner Spear has not entered initiative yet',
);
final blocked = runDrawOrNextRoundAction(gs);
expect(blocked.isBlocked, isTrue, reason: 'draw must wait for all inits');
gs.action(SetInitCommand('Banner Spear', 2, gameState: gs));
expect(GameMethods.canDraw(), isTrue);
// --- Draw ---------------------------------------------------------------
final indexBeforeDraw = gs.commandIndex;
expect(runDrawOrNextRoundAction(gs), same(DrawOrNextRoundResult.success));
expect(gs.commandIndex, indexBeforeDraw + 1);
expect(gs.roundState, RoundState.playTurns);
// Lower initiative acts first: Spear (2) before Blinky (4).
expect(gs.currentList.whereType<Character>().map((c) => c.id), [
'Banner Spear',
'Blinkblade',
]);
expect(
gs.currentList.any((e) => e.turnState == TurnsState.current),
isTrue,
reason: 'the first figure starts its turn',
);
// --- Turns: each active figure takes exactly one turn ------------------
String? lastTurnedId;
var turns = 0;
while (gs.currentList.any((e) => e.turnState == TurnsState.current) &&
turns < 10) {
final currentId = gs.currentList
.firstWhere((e) => e.turnState == TurnsState.current)
.id;
final indexBeforeTurn = gs.commandIndex;
gs.action(TurnDoneCommand(currentId, gameState: gs));
expect(
gs.commandIndex,
indexBeforeTurn + 1,
reason: 'command index advances per action',
);
lastTurnedId = currentId;
turns++;
}
expect(
turns,
gs.currentList.length,
reason: 'every figure (2 characters + 1 monster) took a turn',
);
expect(
gs.currentList.every((e) => e.turnState == TurnsState.done),
isTrue,
reason: 'no figure left with a turn pending',
);
// --- Undo/redo of the last turn -----------------------------------------
gs.undo();
expect(
gs.currentList.firstWhere((e) => e.id == lastTurnedId).turnState,
TurnsState.current,
reason: 'undo restores the finished turn',
);
gs.redo();
expect(
gs.currentList.firstWhere((e) => e.id == lastTurnedId).turnState,
TurnsState.done,
);
// --- Next round ---------------------------------------------------------
expect(runDrawOrNextRoundAction(gs), same(DrawOrNextRoundResult.success));
expect(gs.round, 2);
expect(gs.roundState, RoundState.chooseInitiative);
expect(
gs.currentList.every((e) => e.turnState == TurnsState.notDone),
isTrue,
reason: 'turn states reset for the new round',
);
expect(
gs.currentList.whereType<Character>().every(
(c) => c.characterState.initiative == 0,
),
isTrue,
reason: 'regular characters must re-enter initiative each round',
);
// --- Round 2: initiative, draw, one turn, then round 3 -----------------
gs.action(SetInitCommand('Blinkblade', 3, gameState: gs));
gs.action(SetInitCommand('Banner Spear', 1, gameState: gs));
expect(runDrawOrNextRoundAction(gs), same(DrawOrNextRoundResult.success));
expect(gs.roundState, RoundState.playTurns);
expect(gs.currentList.whereType<Character>().map((c) => c.id), [
'Banner Spear',
'Blinkblade',
], reason: 'round 2 sorts by the new initiative values (1 before 3)');
final round2CurrentId = gs.currentList
.firstWhere((e) => e.turnState == TurnsState.current)
.id;
gs.action(TurnDoneCommand(round2CurrentId, gameState: gs));
expect(runDrawOrNextRoundAction(gs), same(DrawOrNextRoundResult.success));
expect(gs.round, 3);
expect(gs.roundState, RoundState.chooseInitiative);
checkSaveState();
});
}
|