// 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(); final settings = getIt(); 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().length, 2); expect(gs.currentList.whereType().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().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().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().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(); }); }