173 lines
4.4 KiB
JavaScript
173 lines
4.4 KiB
JavaScript
import {
|
|
getInvasionStatus,
|
|
isAllInvasionsResisted,
|
|
checkBreakthrough,
|
|
} from '../src/engine/invasionEngine.js';
|
|
|
|
function assertEqual(actual, expected, message) {
|
|
if (actual !== expected) {
|
|
throw new Error(`${message}: expected ${expected}, got ${actual}`);
|
|
}
|
|
}
|
|
|
|
function assertTrue(value, message) {
|
|
if (!value) {
|
|
throw new Error(`${message}: expected true, got ${value}`);
|
|
}
|
|
}
|
|
|
|
function assertArrayEqual(actual, expected, message) {
|
|
if (actual.length !== expected.length) {
|
|
throw new Error(`${message}: length mismatch, expected ${expected.length}, got ${actual.length}`);
|
|
}
|
|
for (let i = 0; i < actual.length; i++) {
|
|
if (actual[i].key !== expected[i].key || actual[i].triggered !== expected[i].triggered) {
|
|
throw new Error(`${message}: mismatch at index ${i}, expected ${JSON.stringify(expected[i])}, got ${JSON.stringify(actual[i])}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
function testGetInvasionStatus() {
|
|
let state = {
|
|
invasionsTriggered: {
|
|
military_40: false,
|
|
spiritual_45: false,
|
|
political_50: false,
|
|
final_60: false,
|
|
},
|
|
};
|
|
let status = getInvasionStatus(state);
|
|
assertArrayEqual(status, [
|
|
{ key: 'military_40', triggered: false },
|
|
{ key: 'spiritual_45', triggered: false },
|
|
{ key: 'political_50', triggered: false },
|
|
{ key: 'final_60', triggered: false },
|
|
], 'all false');
|
|
|
|
state = {
|
|
invasionsTriggered: {
|
|
military_40: true,
|
|
spiritual_45: true,
|
|
political_50: true,
|
|
final_60: true,
|
|
},
|
|
};
|
|
status = getInvasionStatus(state);
|
|
assertArrayEqual(status, [
|
|
{ key: 'military_40', triggered: true },
|
|
{ key: 'spiritual_45', triggered: true },
|
|
{ key: 'political_50', triggered: true },
|
|
{ key: 'final_60', triggered: true },
|
|
], 'all true');
|
|
|
|
state = {
|
|
invasionsTriggered: {
|
|
military_40: true,
|
|
spiritual_45: false,
|
|
political_50: true,
|
|
final_60: false,
|
|
},
|
|
};
|
|
status = getInvasionStatus(state);
|
|
assertArrayEqual(status, [
|
|
{ key: 'military_40', triggered: true },
|
|
{ key: 'spiritual_45', triggered: false },
|
|
{ key: 'political_50', triggered: true },
|
|
{ key: 'final_60', triggered: false },
|
|
], 'mixed values');
|
|
}
|
|
|
|
function testIsAllInvasionsResisted() {
|
|
let state = {
|
|
worldFlags: {
|
|
invasion_military_resisted: true,
|
|
invasion_spiritual_resisted: true,
|
|
invasion_political_resisted: true,
|
|
},
|
|
};
|
|
assertTrue(isAllInvasionsResisted(state), 'all resisted should be true');
|
|
|
|
state = {
|
|
worldFlags: {
|
|
invasion_military_resisted: true,
|
|
invasion_spiritual_resisted: true,
|
|
invasion_political_resisted: false,
|
|
},
|
|
};
|
|
assertEqual(isAllInvasionsResisted(state), false, 'one not resisted should be false');
|
|
|
|
state = {
|
|
worldFlags: {
|
|
invasion_military_resisted: false,
|
|
invasion_spiritual_resisted: false,
|
|
invasion_political_resisted: false,
|
|
},
|
|
};
|
|
assertEqual(isAllInvasionsResisted(state), false, 'none resisted should be false');
|
|
}
|
|
|
|
function testCheckBreakthrough() {
|
|
let state = {
|
|
worldFlags: {
|
|
breakthrough_general: true,
|
|
breakthrough_minister: false,
|
|
breakthrough_immortal: false,
|
|
},
|
|
};
|
|
assertEqual(checkBreakthrough(state), 'general', 'general breakthrough');
|
|
|
|
state = {
|
|
worldFlags: {
|
|
breakthrough_general: false,
|
|
breakthrough_minister: true,
|
|
breakthrough_immortal: false,
|
|
},
|
|
};
|
|
assertEqual(checkBreakthrough(state), 'minister', 'minister breakthrough');
|
|
|
|
state = {
|
|
worldFlags: {
|
|
breakthrough_general: false,
|
|
breakthrough_minister: false,
|
|
breakthrough_immortal: true,
|
|
},
|
|
};
|
|
assertEqual(checkBreakthrough(state), 'immortal', 'immortal breakthrough');
|
|
|
|
state = {
|
|
worldFlags: {},
|
|
};
|
|
assertEqual(checkBreakthrough(state), null, 'no breakthrough');
|
|
}
|
|
|
|
function runTests() {
|
|
let passed = 0;
|
|
let failed = 0;
|
|
|
|
const tests = [
|
|
{ name: 'testGetInvasionStatus', fn: testGetInvasionStatus },
|
|
{ name: 'testIsAllInvasionsResisted', fn: testIsAllInvasionsResisted },
|
|
{ name: 'testCheckBreakthrough', fn: testCheckBreakthrough },
|
|
];
|
|
|
|
for (const test of tests) {
|
|
try {
|
|
test.fn();
|
|
console.log(` PASS: ${test.name}`);
|
|
passed++;
|
|
} catch (err) {
|
|
console.log(` FAIL: ${test.name} - ${err.message}`);
|
|
failed++;
|
|
}
|
|
}
|
|
|
|
console.log(`\n${passed} passed, ${failed} failed`);
|
|
if (failed === 0) {
|
|
console.log('All invasionEngine tests PASS');
|
|
} else {
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
runTests();
|