summaryrefslogtreecommitdiff
path: root/server/test/command/action_log_rollback_test.dart
blob: a3bf806e18a16d3a1485e2377df12e89ccaed19a (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
// ignore_for_file: no-magic-number, avoid-late-keyword

import 'package:test/test.dart';
import 'package:x_haven_server/core/commands/add_monster_command.dart';
import 'package:x_haven_server/core/state/game_state.dart';
import 'package:x_haven_server/core/service_locator.dart';

import 'test_helpers.dart';

void main() {
  setUpAll(() async {
    await setUpGame();
  });

  GameState gs() => getIt<GameState>();

  // The tap-to-rollback in ActionLogMenu rolls back to a chosen entry by calling
  // undo (currentIndex - targetIndex) times. This verifies that semantics: the
  // chosen action stays, later ones are undone, and everything stays redoable.
  test('rolling back to an action undoes only the later ones (redoable)', () {
    gs().clearList();
    final startMonsters = gs().currentList.whereType<Monster>().length;

    gs().action(AddMonsterCommand("Zealot", 1, false, gameState: gs()));
    final afterZealotIndex = gs().commandIndex; // the entry we roll back to
    gs().action(
      AddMonsterCommand("Ancient Artillery (FH)", 1, false, gameState: gs()),
    );

    expect(gs().currentList.whereType<Monster>().length, startMonsters + 2);

    // Roll back to "after Zealot": undo (current - target) times.
    final count = gs().commandIndex - afterZealotIndex;
    expect(count, 1);
    for (int k = 0; k < count; k++) {
      gs().undo();
    }

    // Zealot remains; the later add is undone.
    expect(gs().currentList.any((e) => e.id == "Zealot"), true);
    expect(
      gs().currentList.any((e) => e.id == "Ancient Artillery (FH)"),
      false,
    );

    // Still redoable until a new action is taken.
    for (int k = 0; k < count; k++) {
      gs().redo();
    }
    expect(gs().currentList.any((e) => e.id == "Ancient Artillery (FH)"), true);
  });
}