Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e
Built: - Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds). - End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x). - Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in. - e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English. Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented). NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
parent
9104480463
commit
3c23c51902
92 changed files with 4887 additions and 300 deletions
118
test/deploy-offsite.test.js
Normal file
118
test/deploy-offsite.test.js
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
'use strict';
|
||||
/* deploy/upstream-offsite: newest snapshot, verified first, copied (rsync to a local directory standing in for the far side),
|
||||
* never overwritten, content compared. ssh itself is not exercised (no remote host here). */
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const { spawnSync } = require('child_process');
|
||||
const admin = require('../tools/admin.js');
|
||||
const { openDb, migrate } = require('../server/db');
|
||||
|
||||
const SCRIPT = path.join(__dirname, '..', 'deploy', 'upstream-offsite');
|
||||
const haveRsync = spawnSync('rsync', ['--version']).status === 0;
|
||||
const opts = { skip: !haveRsync && 'rsync not installed' };
|
||||
|
||||
// the real tools/admin.js does the verification, exactly like on the server (upstream-admin wrapper replaced by a tiny sh)
|
||||
function fixture() {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-offsite-'));
|
||||
const p = (...n) => path.join(root, ...n);
|
||||
fs.mkdirSync(p('backups')); fs.mkdirSync(p('far'));
|
||||
fs.writeFileSync(p('admin'), '#!/bin/sh\nexec node "' + path.join(__dirname, '..', 'tools', 'admin.js') + '" "$@"\n', { mode: 0o755 });
|
||||
const dbFile = p('game.db');
|
||||
const db = openDb(dbFile); migrate(db);
|
||||
db.run("INSERT INTO worlds (name, seed, speed, map_radius, status, start_at, created_at) VALUES ('w', 1, 1, 10, 'running', 0, 0)");
|
||||
db.close();
|
||||
const snap = (name, ageSec) => {
|
||||
const code = admin.main(['backup', p('backups'), '--db', dbFile], { out() {}, err() {}, env: {}, now: () => Date.UTC(2026, 9, 11, 4, 15, 0) + (snap.n = (snap.n || 0) + 1) * 1000 });
|
||||
assert.equal(code, 0);
|
||||
const made = fs.readdirSync(p('backups')).filter((f) => f.startsWith('upstream-')).sort().pop();
|
||||
if (name) fs.renameSync(p('backups', made), p('backups', name));
|
||||
const t = (Date.now() - ageSec * 1000) / 1000;
|
||||
fs.utimesSync(p('backups', name || made), t, t);
|
||||
return name || made;
|
||||
};
|
||||
const run = (args, env) => {
|
||||
const r = spawnSync('sh', [SCRIPT].concat(args || []), { encoding: 'utf8', env: Object.assign({}, process.env, { BACKUP_DIR: p('backups'), UPSTREAM_ADMIN: p('admin'), OFFSITE_DEST: p('far'), OFFSITE_KNOWN_HOSTS: p('none') }, env || {}) });
|
||||
return { code: r.status, out: r.stdout, err: r.stderr, all: r.stdout + r.stderr };
|
||||
};
|
||||
return { p, snap, run, far: () => fs.readdirSync(p('far')).sort() };
|
||||
}
|
||||
|
||||
test('--check is the ExecCondition: unset destination = exit 1, set = 0', opts, () => {
|
||||
const f = fixture();
|
||||
assert.equal(f.run(['--check'], { OFFSITE_DEST: '' }).code, 1);
|
||||
assert.equal(f.run(['--check']).code, 0);
|
||||
const r = f.run([], { OFFSITE_DEST: '' });
|
||||
assert.equal(r.code, 2);
|
||||
assert.match(r.err, /not configured/);
|
||||
});
|
||||
|
||||
test('copies the newest snapshot (by mtime, also against a same-second -1 name), verified, mode 0600, content identical', opts, () => {
|
||||
const f = fixture();
|
||||
f.snap('upstream-20261010T041500Z.db', 7200);
|
||||
const newest = f.snap('upstream-20261011T041500Z-1.db', 60); // sorts BEFORE a plain ...Z.db by name, but is the newer one
|
||||
f.snap('upstream-20261011T041500Z.db', 3600);
|
||||
const r = f.run();
|
||||
assert.equal(r.code, 0, r.all);
|
||||
assert.deepEqual(f.far(), [newest]);
|
||||
assert.deepEqual(fs.readFileSync(f.p('far', newest)), fs.readFileSync(f.p('backups', newest)));
|
||||
assert.equal(fs.statSync(f.p('far', newest)).mode & 0o777, 0o600);
|
||||
assert.match(r.out, /checksum compared/);
|
||||
// the copy is a real snapshot
|
||||
assert.equal(admin.verifySnapshot(f.p('far', newest)).integrity, 'ok');
|
||||
// again: already there, still exit 0
|
||||
assert.equal(f.run().code, 0);
|
||||
assert.deepEqual(f.far(), [newest]);
|
||||
});
|
||||
|
||||
test('a snapshot that does not verify is never copied', opts, () => {
|
||||
const f = fixture();
|
||||
const name = f.snap('upstream-20261011T041500Z.db', 60);
|
||||
fs.writeFileSync(f.p('backups', name), 'not a database');
|
||||
const r = f.run();
|
||||
assert.equal(r.code, 1);
|
||||
assert.match(r.err, /does not verify/);
|
||||
assert.deepEqual(f.far(), []);
|
||||
});
|
||||
|
||||
test('nothing to copy, and a different file of the same name on the far side, are failures (never overwritten)', opts, () => {
|
||||
const f = fixture();
|
||||
assert.equal(f.run().code, 1);
|
||||
const name = f.snap('upstream-20261011T041500Z.db', 60);
|
||||
fs.writeFileSync(f.p('far', name), 'someone else was here');
|
||||
const r = f.run();
|
||||
assert.equal(r.code, 1);
|
||||
assert.match(r.err, /differs/);
|
||||
assert.equal(fs.readFileSync(f.p('far', name), 'utf8'), 'someone else was here');
|
||||
});
|
||||
|
||||
test('OFFSITE_METHOD=scp is refused (scp would overwrite), nothing is copied', () => {
|
||||
const f = fixture();
|
||||
f.snap('upstream-20261011T041500Z.db', 60);
|
||||
const r = f.run([], { OFFSITE_METHOD: 'scp' });
|
||||
assert.equal(r.code, 2);
|
||||
assert.match(r.err, /scp is not supported|not supported any more/);
|
||||
assert.deepEqual(f.far(), []);
|
||||
});
|
||||
|
||||
test('an unwritable far side fails loudly', opts, () => {
|
||||
const f = fixture();
|
||||
f.snap('upstream-20261011T041500Z.db', 60);
|
||||
const r = f.run([], { OFFSITE_DEST: f.p('nope', 'deeper', 'dir') });
|
||||
assert.equal(r.code, 1);
|
||||
assert.match(r.err, /failed/);
|
||||
});
|
||||
|
||||
test('the unit files point at the script and gate on OFFSITE_DEST', () => {
|
||||
const dir = path.join(__dirname, '..', 'deploy');
|
||||
const svc = fs.readFileSync(path.join(dir, 'upstream-offsite.service'), 'utf8');
|
||||
assert.match(svc, /^ExecCondition=\/opt\/upstream\/deploy\/upstream-offsite --check$/m);
|
||||
assert.match(svc, /^ExecStart=\/opt\/upstream\/deploy\/upstream-offsite$/m);
|
||||
assert.match(svc, /^Type=oneshot$/m);
|
||||
const timer = fs.readFileSync(path.join(dir, 'upstream-offsite.timer'), 'utf8');
|
||||
assert.match(timer, /^Unit=upstream-offsite\.service$/m);
|
||||
assert.ok((fs.statSync(path.join(dir, 'upstream-offsite')).mode & 0o111) !== 0, 'executable bit (ExecStart needs it)');
|
||||
assert.equal(spawnSync('sh', ['-n', SCRIPT]).status, 0);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue