summaryrefslogtreecommitdiff
path: root/server/test/resource/line_builder/stat_applier_test.dart
blob: c87e81b329fb9e8e8f4814dfee2b6b3a511c8526 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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'));
    });
  });
}