Files
Battle-for-Tismo/tests/game_state_technology_test.js
T
2026-09-16 19:59:18 +02:00

103 lines
4.3 KiB
JavaScript

import { TestCase } from "./framework/test_case.js";
import { smallState } from "./framework/helpers.js";
import { TECHNOLOGIES } from "../shared/data.js";
const BIG_RESEARCH = 100000.0;
export class GameStateTechnologyTest extends TestCase {
index(state, id) {
return state.technologies.findIndex((t) => t.id === id);
}
test_catalogue_links_prerequisites() {
const method = TECHNOLOGIES.find((t) => t.id === "scientific_method");
this.assertEmpty(method.prerequisites);
this.assertGreater(method.cost, 0);
const automation = TECHNOLOGIES.find((t) => t.id === "industrial_automation");
this.assertEqual(automation.prerequisites, ["scientific_method"]);
}
test_every_civilisation_starts_with_nothing_researched() {
const state = smallState();
this.assertSize(state.technologies, TECHNOLOGIES.length);
this.assertFalse(state.isResearched(0, 0));
this.assertEmpty(state.getResearchedIndices(0));
}
test_request_research_spends_points_and_marks_it_known() {
const state = smallState();
const index = this.index(state, "scientific_method");
state.research.set(0, BIG_RESEARCH);
const cost = state.technologies[index].cost;
this.assertTrue(state.requestResearchTechnology(0, index));
this.assertTrue(state.isResearched(0, index));
this.assertApprox(state.getResearch(0), BIG_RESEARCH - cost);
this.assertFalse(state.requestResearchTechnology(0, index), "already known");
}
test_request_research_rejects_unknown_and_unaffordable() {
const state = smallState();
state.research.set(0, BIG_RESEARCH);
this.assertFalse(state.requestResearchTechnology(0, 999), "unknown technology");
this.assertFalse(state.requestResearchTechnology(999, 0), "unknown civilisation");
state.research.set(0, 0);
this.assertFalse(state.requestResearchTechnology(0, 0), "cannot afford it");
}
test_request_research_rejects_when_prerequisites_are_missing() {
const state = smallState();
state.research.set(0, BIG_RESEARCH);
const space = this.index(state, "space_program");
this.assertFalse(state.canResearchTechnology(0, space));
this.assertFalse(state.requestResearchTechnology(0, space));
}
test_prerequisites_unlock_the_tree() {
const state = smallState();
state.research.set(0, BIG_RESEARCH);
const method = this.index(state, "scientific_method");
const automation = this.index(state, "industrial_automation");
const quantum = this.index(state, "quantum_computing");
const space = this.index(state, "space_program");
this.assertFalse(state.requestResearchTechnology(0, automation), "locked before its prerequisite");
this.assertTrue(state.requestResearchTechnology(0, method));
this.assertTrue(state.requestResearchTechnology(0, automation));
this.assertTrue(state.requestResearchTechnology(0, quantum));
this.assertTrue(state.requestResearchTechnology(0, space));
}
test_technology_effects_are_folded_into_gdp() {
const state = smallState();
state.research.set(0, BIG_RESEARCH);
const before = state.getPlayerGdp(0);
const method = this.index(state, "scientific_method");
this.assertTrue(state.requestResearchTechnology(0, method));
this.assertApprox(state.getPlayerGdp(0), (before * 1.07) / 1.05, 0.01);
}
test_researched_technology_adds_its_research_rate() {
const withTech = smallState();
const without = smallState();
withTech.research.set(0, BIG_RESEARCH);
const method = this.index(withTech, "scientific_method");
this.assertTrue(withTech.requestResearchTechnology(0, method));
const base = withTech.getResearch(0);
withTech.advanceHour();
without.advanceHour();
const delta = withTech.getResearch(0) - base - without.getResearch(0);
this.assertApprox(delta, 1.0);
}
test_snapshot_exposes_technologies_and_progress() {
const state = smallState();
state.research.set(0, BIG_RESEARCH);
const index = this.index(state, "scientific_method");
this.assertTrue(state.requestResearchTechnology(0, index));
const snapshot = state.snapshot(0);
this.assertSize(snapshot.technologies, state.technologies.length);
this.assertSize(snapshot.researched, state.civilisations.length);
this.assertTrue(snapshot.researched[0].includes(index));
this.assertEmpty(snapshot.researched[1]);
}
}