diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-12 00:28:50 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-12 00:28:50 +0100 |
| commit | 808294adad3f378ad8d66b698acf8df7f49c2302 (patch) | |
| tree | cfd11a1395a8ad482311969f86abbbad97e7e0b4 /server/test/integration/headless_round_test.dart | |
| parent | f6955e34489c79f6d5b764cb6a39b1387366b433 (diff) | |
Phase 1: headless game engine on the server (M1)
Complete mechanical port of the reference app's game engine to
server/lib/core/ so later phases are UI-only:
- models/ (7), state/ (GameState + 18 part files), methods/ (7),
game_methods, 79 command files, enums, card_stack, stat_calculator,
action_handler (index, 250-deep history, save-per-action, undo/redo),
disk-based game_data, l10n, translation_service, service_locator
- ValueNotifier/ChangeNotifier removed in favor of plain fields with a
minimal GameState listener hook for the Phase 2 broadcast layer;
shared_preferences replaced by the SaveStore abstraction
(InMemory for tests, File-backed for bin/main.dart)
- command registry: 82 entries mapping wire names to constructors with
arg validation; live-object args (loot/ability cards, figure lists)
travel as id/name/nr and resolve against live state, enums by name,
injected singletons never serialize
- line_builder ported to Phase 1 scope: stat_applier + minimal
LineBuilder (isElement, tokens); widget rendering deferred to Phase 4
- ported test suite: 81 test files, 562 tests green (command tests 1:1
with the reference, resource tests, registry smoke tests)
- new headless integration test drives a full round: scenario setup,
characters + standee, secret-initiative gate, draw, every figure's
turn (incl. undo/redo), next round, round 2, round 3
- PORTING.md: the conventions contract used for the port
- bin/main.dart boots the engine (file save store, assets/data) before
serving; pubspec gains built_collection, collection, get_it, intl
- dart format drift on pre-existing net/test files from the current
formatter (whitespace only)
dart analyze warning-free; dart test 562/562.
Diffstat (limited to 'server/test/integration/headless_round_test.dart')
| -rw-r--r-- | server/test/integration/headless_round_test.dart | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/server/test/integration/headless_round_test.dart b/server/test/integration/headless_round_test.dart new file mode 100644 index 0000000..4b1f034 --- /dev/null +++ b/server/test/integration/headless_round_test.dart @@ -0,0 +1,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(); + }); +} |
