summaryrefslogtreecommitdiff
path: root/server/test/command/test_helpers.dart
blob: 3480d585cf8df3074c476998e7108d4a80baaf4a (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
// ignore_for_file: no-magic-number, avoid-late-keyword, avoid-top-level-members-in-tests

import 'dart:convert';

import 'package:x_haven_server/core/game_data.dart';
import 'package:x_haven_server/core/save_store.dart';
import 'package:x_haven_server/core/service_locator.dart';
import 'package:x_haven_server/core/state/game_state.dart';
import 'package:json_diff/json_diff.dart';

late final GameState gameState;

/// Bootstraps the headless engine with the test data set.
///
/// Mirror of the reference app's `setUpGame()`: registers the core
/// singletons with an in-memory save store, initializes the game state, loads
/// the test data from `assets/testData/`, and restores the (empty) save.
Future<void> setUpGame() async {
  setupCore(saveStore: InMemorySaveStore());
  gameState = getIt<GameState>();
  //initialize game
  gameState.init();
  await getIt<GameData>().loadData('assets/testData/');
  gameState.load();
}

void checkSaveState() {
  final String state = gameState.toString();
  final int nrStates = gameState.gameSaveStates.length;
  gameState.save();
  gameState.load();
  final String newState = gameState.toString();
  assert(
    gameState.gameSaveStates.length == nrStates + 2,
  ); //for some reason a null state is added on load.
  assert(newState == state);
}

void checkNoSideEffects(List<String> changedFields, String oldState) {
  final differ = JsonDiffer.fromJson(
    json.decode(oldState),
    json.decode(gameState.toString()),
  );
  final DiffNode diff = differ.diff();

  diff.forEach((s, dn) {
    print("for each");
    print(s);
    assert(changedFields.contains(s));
    print(dn.added);
    print(dn.moved);
    print(dn.removed);
    print(dn.changed);
    print(dn.node);
  });
}