Upstream/test/bot-server-fixes.test.js
jvanvik 931f59e11e Phase 3: trade/end-game client, bot simulator, first balance pass
Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

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

47 lines
3.1 KiB
JavaScript

'use strict';
/* Small server fixes found by the bot harness (tools/bot.js), each with the test that failed before the fix. */
const test = require('node:test');
const assert = require('node:assert');
const T = require('./military-helpers');
const M = require('../server/military/movements');
const { R, D } = T;
// The conservation ledger of the harness found it: a shipment that comes home (a landing site changed hands, a POP vanished) rides
// back on a `link` return leg. Cargo above the Rack hall cap is dropped on arrival, but the return handler stayed silent for link
// legs, so the loss was invisible to the player and to every audit that sums the `cargoLost` notifications.
test('a bounced shipment that does not fit the Rack hall reports the overflow (link return legs with cargo are no longer silent)', () => {
const h = T.setup();
const a = T.addOperator(h, 'alice', { infra: { rack_hall: 1 } });
const cap = R.storageCap(require('../server/state').loadTown(h.db, a.townId));
h.db.run('UPDATE towns SET fiber = ?, hardware = 0, capital = 0 WHERE id = ?', cap - 100, a.townId);
const row = T.town(h, a.townId);
const ctx = h.ctx();
M.launchReturn(ctx, { worldId: h.worldId, ownerId: a.opId, fromTownId: null, at: { x: row.x + 5, y: row.y }, to: row, units: {}, cargo: { fiber: 400 }, arriveAt: h.clock.now() + 60 * 1000, parentId: null, extra: { link: 1 } });
h.advance(2 * 60 * 1000);
const n = T.notes(h, a.userId, 'movement_returned');
assert.equal(n.length, 1, 'the bounced shipment is announced');
assert.ok(Math.abs(n[0].cargoLost.fiber - 300) <= 2, 'and says how much did not fit (400 sent, 100 of room, a minute of production on top): ' + n[0].cargoLost.fiber);
assert.ok(Math.abs(T.town(h, a.townId).fiber - cap) < 1e-6, 'the store is full, not above the cap');
});
test('empty carriers coming home on a link leg stay silent (no notification spam)', () => {
const h = T.setup();
const a = T.addOperator(h, 'alice');
const row = T.town(h, a.townId);
M.launchReturn(h.ctx(), { worldId: h.worldId, ownerId: a.opId, fromTownId: null, at: { x: row.x + 5, y: row.y }, to: row, units: {}, cargo: {}, arriveAt: h.clock.now() + 60 * 1000, parentId: null, extra: { link: 1 } });
h.advance(2 * 60 * 1000);
assert.equal(T.notes(h, a.userId, 'movement_returned').length, 0);
});
// Balance pass B1 (docs/BALANCE.md): the second build slot opens at Core router 3; FORMULAS.extraQueueCores (default none) can open more.
test('build slots follow the Core router: one, a second from FORMULAS.secondQueueCoreLevel, more from FORMULAS.extraQueueCores', () => {
const F = D.FORMULAS;
const at = (core) => R.queueSlots({ buildings: { core_router: { type: 'core_router', level: core } } });
assert.equal(F.secondQueueCoreLevel, 3, 'the tuned value (B1)');
assert.deepEqual([0, 1, 2, 3, 10, 20].map(at), [1, 1, 1, 2, 2, 2], 'default: two slots from the second-slot level, no extra ones');
const saved = F.extraQueueCores;
try {
F.extraQueueCores = [6, 10];
assert.deepEqual([2, 3, 5, 6, 9, 10, 20].map(at), [1, 2, 2, 3, 3, 4, 4]);
} finally { F.extraQueueCores = saved; }
});