From 808294adad3f378ad8d66b698acf8df7f49c2302 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 12 Nov 2026 00:28:50 +0100 Subject: 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. --- .../resource/line_builder/line_builder_test.dart | 67 +++++ .../resource/line_builder/stat_applier_test.dart | 284 +++++++++++++++++++++ 2 files changed, 351 insertions(+) create mode 100644 server/test/resource/line_builder/line_builder_test.dart create mode 100644 server/test/resource/line_builder/stat_applier_test.dart (limited to 'server/test/resource/line_builder') diff --git a/server/test/resource/line_builder/line_builder_test.dart b/server/test/resource/line_builder/line_builder_test.dart new file mode 100644 index 0000000..c6aece7 --- /dev/null +++ b/server/test/resource/line_builder/line_builder_test.dart @@ -0,0 +1,67 @@ +import 'package:test/test.dart'; +import 'package:x_haven_server/core/line_builder/line_builder.dart'; + +// Only the pure-logic parts of the reference `line_builder_test.dart` are +// ported: the widget/metrics tests (`createLines`, `getTopPaddingForStyle`) +// depend on Flutter classes and the unported rendering code, so they are +// skipped until the Phase 4 web client re-implements the rendering. + +void main() { + // ── isElement ───────────────────────────────────────────────────────────── + + group('LineBuilder.isElement', () { + test('"fire" is an element', () { + expect(LineBuilder.isElement('fire'), isTrue); + }); + + test('"air" is an element', () { + expect(LineBuilder.isElement('air'), isTrue); + }); + + test('"earth" is an element', () { + expect(LineBuilder.isElement('earth'), isTrue); + }); + + test('"ice" is an element', () { + expect(LineBuilder.isElement('ice'), isTrue); + }); + + test('"dark" is an element', () { + expect(LineBuilder.isElement('dark'), isTrue); + }); + + test('"light" is an element', () { + expect(LineBuilder.isElement('light'), isTrue); + }); + + test('"any" is an element', () { + expect(LineBuilder.isElement('any'), isTrue); + }); + + test('string containing element name returns true', () { + // e.g. "fire_fh" contains "fire" + expect(LineBuilder.isElement('fire_fh'), isTrue); + expect(LineBuilder.isElement('dark_half'), isTrue); + }); + + test('"attack" is not an element', () { + expect(LineBuilder.isElement('attack'), isFalse); + }); + + test('"move" is not an element', () { + expect(LineBuilder.isElement('move'), isFalse); + }); + + test('"poison" is not an element', () { + expect(LineBuilder.isElement('poison'), isFalse); + }); + + test('empty string is not an element', () { + expect(LineBuilder.isElement(''), isFalse); + }); + + test('"heal" is not an element', () { + expect(LineBuilder.isElement('heal'), isFalse); + }); + }); +} diff --git a/server/test/resource/line_builder/stat_applier_test.dart b/server/test/resource/line_builder/stat_applier_test.dart new file mode 100644 index 0000000..c87e81b --- /dev/null +++ b/server/test/resource/line_builder/stat_applier_test.dart @@ -0,0 +1,284 @@ +import 'package:test/test.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_level_command.dart'; +import 'package:x_haven_server/core/enums.dart'; +import 'package:x_haven_server/core/line_builder/stat_applier.dart'; +import 'package:x_haven_server/core/service_locator.dart'; +import 'package:x_haven_server/core/state/game_state.dart'; + +import '../../command/test_helpers.dart'; + +void main() { + setUpAll(() async { + await setUpGame(); + }); + + setUp(() { + getIt().clearList(); + SetLevelCommand(1, null).execute(); + SetCampaignCommand('Jaws of the Lion').execute(); + AddMonsterCommand( + 'Zealot', + 1, + false, + gameState: getIt(), + ).execute(); + }); + + // Zealot level 1: normal attack=2 move=2 range=0, elite attack=3 move=2 range=0 + + Monster zealot() => + getIt().currentList.firstWhere((e) => e.id == 'Zealot') + as Monster; + + // ── plain text – no formulas ────────────────────────────────────────────── + + group('StatApplier.applyMonsterStats – plain text', () { + test('line with no formula is returned unchanged', () { + final result = StatApplier.applyMonsterStats( + 'Move 3', + '', + zealot(), + false, + ); + expect(result, equals(['Move 3'])); + }); + + test('line with pure text and token name unchanged', () { + final result = StatApplier.applyMonsterStats( + '%attack%', + '', + zealot(), + false, + ); + expect(result, equals(['%attack%'])); + }); + + test('single digit with no preceding token is treated as plain text', () { + // "3" alone at position 0 is a plain number; no token → calculateFormula("3")=3 + // but length must be >2 or formula must have lastToken; "3" alone is length 1 → not applied + final result = StatApplier.applyMonsterStats('3', '', zealot(), false); + expect(result, equals(['3'])); + }); + }); + + // ── forceShowAll=false with no standees ────────────────────────────────── + + group('StatApplier.applyMonsterStats – no standees (forceShowAll=false)', () { + test('%attack% +0 unchanged when no standees and forceShowAll=false', () { + // showNormal=false, showElite=false → line returned as-is + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + false, + ); + expect(result, equals(['%attack% +0'])); + }); + + test('%move% +1 unchanged when no standees and forceShowAll=false', () { + final result = StatApplier.applyMonsterStats( + '%move% +1', + '', + zealot(), + false, + ); + expect(result, equals(['%move% +1'])); + }); + }); + + // ── forceShowAll=true: attack formula ───────────────────────────────────── + + group('StatApplier.applyMonsterStats – forceShowAll attack', () { + test('%attack% +0 with forceShowAll splits into multiple results', () { + // normal attack=2, elite attack=3; formula "+0" → normal=2, elite=3 + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + true, + ); + expect(result.length, greaterThan(1)); + }); + + test('normal result contains the computed normal attack value', () { + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + true, + ); + // First string contains "%attack% 2/" (the normal result) + expect(result.first, contains('2')); + }); + + test('elite result contains the computed elite attack value', () { + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + true, + ); + // Second string is the elite part starting with "!" + final elite = result.firstWhere((s) => s.startsWith('!')); + expect(elite, contains('3')); + }); + + test('%attack% +1 adds 1 to both normal and elite values', () { + // normal=2+1=3, elite=3+1=4 + final result = StatApplier.applyMonsterStats( + '%attack% +1', + '', + zealot(), + true, + ); + expect(result.first, contains('3')); + final elite = result.firstWhere((s) => s.startsWith('!')); + expect(elite, contains('4')); + }); + + test('%attack% -1 subtracts 1 (clamps to 0 if negative)', () { + // normal=2-1=1, elite=3-1=2 + final result = StatApplier.applyMonsterStats( + '%attack% -1', + '', + zealot(), + true, + ); + expect(result.first, contains('1')); + final elite = result.firstWhere((s) => s.startsWith('!')); + expect(elite, contains('2')); + }); + }); + + // ── forceShowAll=true: move formula ────────────────────────────────────── + + group('StatApplier.applyMonsterStats – forceShowAll move', () { + test('%move% +0 yields Zealot move value (2)', () { + final result = StatApplier.applyMonsterStats( + '%move% +0', + '', + zealot(), + true, + ); + expect(result.first, contains('2')); + }); + + test('%move% +2 yields Zealot move+2 (4) for both', () { + // normal move=2+2=4, elite move=2+2=4 + final result = StatApplier.applyMonsterStats( + '%move% +2', + '', + zealot(), + true, + ); + expect(result.first, contains('4')); + }); + }); + + // ── with standees present (forceShowAll=false) ─────────────────────────── + + group('StatApplier.applyMonsterStats – with actual standees', () { + test('normal standee present → shows normal stat', () { + AddStandeeCommand( + 1, + null, + 'Zealot', + MonsterType.normal, + false, + gameState: getIt(), + ).execute(); + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + false, + ); + // showNormal=true, showElite=false + expect(result.first, contains('2')); + // No elite part + expect(result.every((s) => !s.contains('3')), isTrue); + getIt().undo(); + }); + + test('elite standee present → shows both normal and elite stat', () { + AddStandeeCommand( + 1, + null, + 'Zealot', + MonsterType.elite, + false, + gameState: getIt(), + ).execute(); + final result = StatApplier.applyMonsterStats( + '%attack% +0', + '', + zealot(), + false, + ); + // showElite=true → split output with both values + expect(result.length, greaterThan(1)); + expect(result.any((s) => s.contains('3')), isTrue); + getIt().undo(); + }); + }); + + // ── formula without preceding token ────────────────────────────────────── + + group('StatApplier.applyMonsterStats – standalone formula', () { + test('C+L formula (≥3 chars, no token) is evaluated', () { + // With level=1 and 0 characters (C clamped to 2): C+L = 2+1 = 3 + final result = StatApplier.applyMonsterStats( + 'C+L health', + '', + zealot(), + false, + ); + expect(result.first, contains('3')); + }); + + test('plain number ≥3 digits is evaluated as formula', () { + // "100" is 3 chars and contains only digits; calculateFormula("100")=100 + final result = StatApplier.applyMonsterStats( + '100 damage', + '', + zealot(), + false, + ); + expect(result.first, contains('100')); + }); + }); + + // ── shield from attribute ───────────────────────────────────────────────── + + group('StatApplier.applyMonsterStats – shield token', () { + setUp(() { + getIt().clearList(); + SetLevelCommand(1, null).execute(); + SetCampaignCommand('Jaws of the Lion').execute(); + // Black Sludge has %shield% 1 in both normal and elite at level 1 + AddMonsterCommand( + 'Black Sludge', + 1, + false, + gameState: getIt(), + ).execute(); + }); + + Monster blackSludge() => + getIt().currentList.firstWhere((e) => e.id == 'Black Sludge') + as Monster; + + test('%shield% +0 with Black Sludge yields shield value 1', () { + final result = StatApplier.applyMonsterStats( + '%shield% +0', + '', + blackSludge(), + true, + ); + expect(result.first, contains('1')); + }); + }); +} -- cgit v1.3.1