Upstream/test/endgame-rules.test.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:05:03 +02:00

128 lines
7.7 KiB
JavaScript

'use strict';
/* End game and trade rules as pure functions (public/js/rules.js, data.js). */
const test = require('node:test');
const assert = require('node:assert');
const R = require('../public/js/rules.js');
const D = require('../public/js/data.js');
test('backbone sites: 7 fixed positions per (seed, radius), on plain land, spread out, never in the free centre, deterministic', () => {
for (const [seed, radius] of [[4242, 50], [1, 50], [99, 20], [7, 150], [123456, 30]]) {
const sites = R.backboneSites(seed, radius);
assert.equal(sites.length, D.ENDGAME.sites, 'seed ' + seed + ' radius ' + radius);
assert.deepEqual(sites.map((s) => s.index), [0, 1, 2, 3, 4, 5, 6]);
for (const s of sites) {
assert.equal(R.terrainAt(seed, radius, s.x, s.y).type, 'land');
assert.ok(Math.abs(s.x) > 2 || Math.abs(s.y) > 2, 'not in the free centre');
assert.ok(Math.abs(s.x) <= radius && Math.abs(s.y) <= radius);
for (const o of sites) if (o !== s) assert.ok(R.distance(s.x, s.y, o.x, o.y) >= D.ENDGAME.siteMinSpacing, 'spacing');
}
assert.deepEqual(R.backboneSites(seed, radius), sites, 'same input, same answer');
}
assert.notDeepEqual(R.backboneSites(1, 50), R.backboneSites(2, 50), 'the seed decides');
});
test('tileAt: a landing site is plain land until world day openDay, then reads backbone; old 4-argument callers see no change', () => {
const seed = 4242, radius = 50;
const s = R.backboneSites(seed, radius)[3];
assert.deepEqual(R.tileAt(seed, radius, s.x, s.y), { type: 'land' });
assert.deepEqual(R.tileAt(seed, radius, s.x, s.y, 0), { type: 'land' });
assert.deepEqual(R.tileAt(seed, radius, s.x, s.y, D.ENDGAME.openDay - 0.001), { type: 'land' });
assert.deepEqual(R.tileAt(seed, radius, s.x, s.y, D.ENDGAME.openDay), { type: 'backbone', site: 3 });
assert.deepEqual(R.tileAt(seed, radius, s.x, s.y, 500), { type: 'backbone', site: 3 });
// every other tile is identical with and without age
let seen = 0;
for (let x = -radius; x <= radius; x += 1) for (let y = -radius; y <= radius; y += 3) {
const a = R.tileAt(seed, radius, x, y), b = R.tileAt(seed, radius, x, y, 1000);
if (b.type === 'backbone') { seen++; assert.ok(R.isBackboneSite(seed, radius, x, y)); } else assert.deepEqual(a, b);
}
assert.ok(seen >= 1);
assert.equal(R.tileAt(seed, radius, radius + 1, 0, 1000).type, 'void');
});
test('spawning and settling never use a landing site, not even before it shows', () => {
const seed = 4242, radius = 50;
const sites = R.backboneSites(seed, radius);
const towns = [];
for (let n = 0; n < 150; n++) {
const p = R.pickSpawn(seed, radius, n, towns);
if (!p) break;
assert.ok(!sites.some((s) => s.x === p.x && s.y === p.y), 'spawn ' + n + ' on a landing site');
towns.push(p);
}
assert.ok(towns.length > 100);
const s = sites[0];
const ok = { tileType: 'land', occupied: false, nearestOwnDist: 3, ownedCount: 1, footprint: 1e9 };
assert.equal(R.settleCheck(ok).ok, true);
const refused = R.settleCheck(Object.assign({ backbone: R.isBackboneSite(seed, radius, s.x, s.y) }, ok));
assert.equal(refused.ok, false);
assert.equal(refused.reason, 'backbone_site');
assert.equal(R.settleCheck({ tileType: 'backbone', occupied: false, nearestOwnDist: 3, ownedCount: 1, footprint: 1e9 }).ok, false);
});
test('world phase and age: running, backbone_open from day 100 / speed, ended wins over everything', () => {
const DAY = 24 * 3600e3;
assert.equal(R.worldAgeDays(1000, 1000, 1), 0);
assert.equal(R.worldAgeDays(1000, 1000 - 5, 1), 0, 'never negative');
assert.equal(R.worldAgeDays(0, 10 * DAY, 3), 30);
assert.equal(R.worldPhase('running', 99.9), 'running');
assert.equal(R.worldPhase('running', 100), 'backbone_open');
assert.equal(R.worldPhase('ended', 5), 'ended');
assert.equal(R.worldPhase('ended', 500), 'ended');
// speed 4: day 100 of world time arrives after 25 real days
assert.ok(R.worldAgeDays(0, 25 * DAY, 4) >= D.ENDGAME.openDay);
});
test('backbone defence grows with world age and node level; cost and time curves are monotonic and far above any other building', () => {
const seed = 4242;
const d0 = R.backboneDefence(seed, 10, 10, 100, 0), d50 = R.backboneDefence(seed, 10, 10, 100, 50), dOld = R.backboneDefence(seed, 10, 10, 200, 0);
assert.ok(d50 > d0 && dOld > d0);
assert.equal(R.backboneDefence(seed, 10, 10, 100, 0), Math.round(R.incumbentDefence(seed, 10, 10, 100) * D.ENDGAME.defenceMult));
assert.ok(d0 > 10 * R.incumbentDefence(seed, 10, 10, 100), 'heavy: well above an ordinary incumbent site');
let prev = 0, prevT = 0, sumFiber = 0;
for (let l = 1; l <= D.ENDGAME.maxLevel; l++) {
const c = R.backboneCost(l), t = R.backboneTimeMs(l, 1);
assert.ok(c.fiber > prev && t > prevT, 'level ' + l);
prev = c.fiber; prevT = t; sumFiber += c.fiber;
}
assert.equal(R.backboneCost(1).fiber, D.BUILDINGS.backbone_node.cost.fiber);
assert.ok(R.backboneCost(1).fiber > R.buildingCost('rollout_depot', 1).fiber * 10, 'level 1 already dwarfs the dearest ordinary building level 1');
assert.ok(R.backboneCost(D.ENDGAME.maxLevel).fiber > R.buildingCost('rack_hall', 20).fiber, 'level 100 > the best ordinary building');
assert.equal(R.backboneTimeMs(1, 4), R.backboneTimeMs(1, 1) / 4, 'world speed divides');
assert.ok(sumFiber > 1e6, 'millions in total (a first guess, not balanced)');
});
test('backbonePlan: one level at a time, paid from the pool', () => {
const pool = (n) => ({ fiber: n, hardware: n, capital: n });
assert.equal(R.backbonePlan({ level: 0, build: null, pool: pool(100) }, 1).code, 'not_enough_resources');
assert.equal(R.backbonePlan({ level: 0, build: null, pool: { fiber: 4000, hardware: 4000, capital: 3999 } }, 1).code, 'not_enough_resources');
const ok = R.backbonePlan({ level: 0, build: null, pool: pool(4000) }, 1);
assert.equal(ok.ok, true); assert.equal(ok.toLevel, 1); assert.deepEqual(ok.cost, R.backboneCost(1));
assert.equal(R.backbonePlan({ level: 3, build: { to: 4 }, pool: pool(1e9) }, 1).code, 'already_queued');
assert.equal(R.backbonePlan({ level: 100, build: null, pool: pool(1e12) }, 1).code, 'max_level');
});
test('trade terms: ratio band 1:2 both ways, one transport link per side, distinct resources, whole numbers', () => {
const t = (o) => R.tradeTermsCheck(Object.assign({ giveRes: 'fiber', giveAmount: 100, wantRes: 'hardware', wantAmount: 100, maxDistance: 10 }, o));
assert.equal(t().ok, true);
assert.equal(t({ giveAmount: 200, wantAmount: 100 }).ok, true);
assert.equal(t({ giveAmount: 100, wantAmount: 200 }).ok, true);
assert.equal(t({ giveAmount: 201, wantAmount: 100 }).code, 'trade_ratio');
assert.equal(t({ giveAmount: 100, wantAmount: 201 }).code, 'trade_ratio');
assert.equal(t({ giveAmount: D.TRANSPORT.capacityPerLink, wantAmount: D.TRANSPORT.capacityPerLink }).ok, true);
assert.equal(t({ giveAmount: D.TRANSPORT.capacityPerLink + 1, wantAmount: D.TRANSPORT.capacityPerLink }).code, 'capacity_exceeded');
assert.equal(t({ wantRes: 'fiber' }).code, 'validation_failed');
assert.equal(t({ giveRes: 'power' }).code, 'validation_failed');
assert.equal(t({ giveAmount: 0.5 }).code, 'validation_failed');
assert.equal(t({ maxDistance: 0 }).code, 'validation_failed');
assert.equal(R.tradeReach(0), 0);
assert.equal(R.tradeReach(3), 3 * D.TRADE.reachPerLevel);
assert.equal(R.tradeInRange(10, 10, 1), true);
assert.equal(R.tradeInRange(10.01, 20, 1), false, "the taker's reach limits");
assert.equal(R.tradeInRange(5, 4, 5), false, "the offer's max distance limits");
assert.equal(R.tradeInRange(1, 50, 0), false, 'no exchange');
});
test('data: the end-game level cap matches the Backbone node building', () => {
assert.equal(D.ENDGAME.maxLevel, D.BUILDINGS.backbone_node.maxLevel);
assert.equal(D.BUILDINGS.backbone_node.special, 'backbone');
});