Files
Battle-for-Tismo/tests/completions_test.js
T

98 lines
3.5 KiB
JavaScript

import { TestCase } from "./framework/test_case.js";
import {
CompletionWatcher,
buildingLevelGained,
cargoUnloaded,
} from "../client/js/game_screen/completions.js";
const city = (id, civ, buildings = {}) => ({ id, civ, buildings });
const unit = (id, civ, cargo = []) => ({ id, civ, cargo });
const snapshot = ({ cities = [], units = [] } = {}) => ({ cities, units });
export class CompletionsTest extends TestCase {
test_first_snapshot_only_primes_the_baseline() {
const watcher = new CompletionWatcher();
const done = watcher.update(
snapshot({ cities: [city(1, 0, { 0: 2 })], units: [unit(7, 0)] }),
0
);
this.assertTrue(done.first);
this.assertFalse(done.building);
this.assertFalse(done.unit);
}
test_detects_a_building_gaining_a_level() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ cities: [city(1, 0, { 0: 1 })] }), 0);
const done = watcher.update(snapshot({ cities: [city(1, 0, { 0: 2 })] }), 0);
this.assertTrue(done.building);
this.assertFalse(done.unit);
}
test_detects_a_freshly_trained_unit() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ units: [unit(7, 0)] }), 0);
const done = watcher.update(snapshot({ units: [unit(7, 0), unit(8, 0)] }), 0);
this.assertTrue(done.unit);
this.assertFalse(done.building);
}
test_a_second_snapshot_with_no_change_is_silent() {
const watcher = new CompletionWatcher();
const state = snapshot({ cities: [city(1, 0, { 0: 1 })], units: [unit(7, 0)] });
watcher.update(state, 0);
const done = watcher.update(
snapshot({ cities: [city(1, 0, { 0: 1 })], units: [unit(7, 0)] }),
0
);
this.assertFalse(done.building);
this.assertFalse(done.unit);
}
test_a_captured_city_does_not_count_as_a_completion() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ cities: [city(1, 0, {})] }), 0);
const done = watcher.update(
snapshot({ cities: [city(1, 0, {}), city(2, 0, { 0: 3 })] }),
0
);
this.assertFalse(done.building);
}
test_only_the_local_player_is_watched() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ cities: [city(1, 1, {}), city(2, 0, {})] }), 0);
const done = watcher.update(
snapshot({ cities: [city(1, 1, { 0: 4 }), city(2, 0, {})] }),
0
);
this.assertFalse(done.building);
}
test_landing_troops_is_not_a_trained_unit() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ units: [unit(5, 0, [{}]), unit(6, 1)] }), 0);
// The transport empties and a fresh infantry id appears on the shore.
const done = watcher.update(snapshot({ units: [unit(5, 0, []), unit(9, 0)] }), 0);
this.assertFalse(done.unit, "disembarking does not chime");
}
test_reset_primes_the_baseline_again() {
const watcher = new CompletionWatcher();
watcher.update(snapshot({ cities: [city(1, 0, {})] }), 0);
watcher.update(snapshot({ cities: [city(1, 0, { 0: 1 })] }), 0);
watcher.reset();
const done = watcher.update(snapshot({ cities: [city(1, 0, { 0: 1 })] }), 0);
this.assertTrue(done.first);
}
test_helpers_compare_levels_and_cargo() {
this.assertTrue(buildingLevelGained(new Map([[1, { 0: 1 }]]), new Map([[1, { 0: 2 }]])));
this.assertFalse(
buildingLevelGained(new Map([[1, { 0: 1 }]]), new Map([[1, { 0: 1 }], [2, { 0: 5 }]]))
);
this.assertTrue(cargoUnloaded(new Map([[5, 2]]), new Map([[5, 0]])));
this.assertFalse(cargoUnloaded(new Map([[5, 2]]), new Map([[5, 2]])));
}
}