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/resource/line_builder/stat_applier_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/resource/line_builder/stat_applier_test.dart')
| -rw-r--r-- | server/test/resource/line_builder/stat_applier_test.dart | 284 |
1 files changed, 284 insertions, 0 deletions
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<GameState>().clearList(); + SetLevelCommand(1, null).execute(); + SetCampaignCommand('Jaws of the Lion').execute(); + AddMonsterCommand( + 'Zealot', + 1, + false, + gameState: getIt<GameState>(), + ).execute(); + }); + + // Zealot level 1: normal attack=2 move=2 range=0, elite attack=3 move=2 range=0 + + Monster zealot() => + getIt<GameState>().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<GameState>(), + ).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<GameState>().undo(); + }); + + test('elite standee present → shows both normal and elite stat', () { + AddStandeeCommand( + 1, + null, + 'Zealot', + MonsterType.elite, + false, + gameState: getIt<GameState>(), + ).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<GameState>().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<GameState>().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<GameState>(), + ).execute(); + }); + + Monster blackSludge() => + getIt<GameState>().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')); + }); + }); +} |
