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
This commit is contained in:
parent
249818a7aa
commit
b18645dca1
87 changed files with 10201 additions and 71 deletions
177
test/endgame-gaps.test.js
Normal file
177
test/endgame-gaps.test.js
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
'use strict';
|
||||
/* Gaps closed next to the end game: buildDone after exploit-kit damage, confederation sees incoming, shared reports. */
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const T = require('./military-helpers');
|
||||
const { R, D, H } = T;
|
||||
|
||||
const level = (h, townId, key) => h.db.get('SELECT level FROM buildings WHERE town_id = ? AND key = ?', townId, key).level;
|
||||
|
||||
test('buildDone: an upgrade in flight lands ONE level above whatever the building is now (exploit kits cannot be undone by it)', () => {
|
||||
const h = T.setup();
|
||||
const a = T.addOperator(h, 'alice', { infra: { firewall: 5, core_router: 10 } });
|
||||
const q = h.run('build', { userId: a.userId, townId: a.townId, key: 'firewall' });
|
||||
assert.equal(q.toLevel, 6);
|
||||
// exploit kits hit the firewall while the upgrade is being built (siege writes the level directly)
|
||||
h.db.run("UPDATE buildings SET level = 2 WHERE town_id = ? AND key = 'firewall'", a.townId);
|
||||
h.advanceTo(q.dueAt);
|
||||
assert.equal(level(h, a.townId, 'firewall'), 3, 'the finished level adds one to the damaged building, it does not restore the queued level');
|
||||
const done = T.notes(h, a.userId, 'build_done');
|
||||
assert.equal(done[done.length - 1].level, 3, 'the notification reports the real level');
|
||||
});
|
||||
|
||||
test('buildDone: a building knocked down to 0 while its upgrade is queued comes back at level 1; an undamaged one still gets +1', () => {
|
||||
const h = T.setup();
|
||||
const a = T.addOperator(h, 'alice', { infra: { firewall: 4, noc: 5, core_router: 10 } });
|
||||
const q1 = h.run('build', { userId: a.userId, townId: a.townId, key: 'firewall' }); // 4 -> 5
|
||||
const q2 = h.run('build', { userId: a.userId, townId: a.townId, key: 'noc' }); // 5 -> 6 (second slot: core router 10)
|
||||
h.db.run("UPDATE buildings SET level = 0 WHERE town_id = ? AND key = 'firewall'", a.townId);
|
||||
h.advanceTo(Math.max(q1.dueAt, q2.dueAt));
|
||||
assert.equal(level(h, a.townId, 'firewall'), 1);
|
||||
assert.equal(level(h, a.townId, 'noc'), 6);
|
||||
});
|
||||
|
||||
test('buildDone: a brand-new infrastructure building appears at level 1 (no row yet)', () => {
|
||||
const h = T.setup();
|
||||
const a = T.addOperator(h, 'alice', { infra: { core_router: 10 } });
|
||||
h.db.run("DELETE FROM buildings WHERE town_id = ? AND key = 'firewall'", a.townId);
|
||||
const q = h.run('build', { userId: a.userId, townId: a.townId, key: 'firewall' });
|
||||
h.advanceTo(q.dueAt);
|
||||
assert.equal(level(h, a.townId, 'firewall'), 1);
|
||||
});
|
||||
|
||||
// ---- helpers: alliances without the HTTP layer
|
||||
const A = require('../server/alliances');
|
||||
function alliance(h, name, leader, members) {
|
||||
const id = Number(h.db.run('INSERT INTO alliances (world_id, name, tag, size_cap, created_by, created_at) VALUES (?, ?, ?, 10, ?, ?)', h.worldId, name, name.slice(0, 3).toUpperCase(), leader.opId, h.clock.now()).lastInsertRowid);
|
||||
A.addMember(h.db, id, leader.opId, 'leader', h.clock.now());
|
||||
for (const m of members || []) A.addMember(h.db, id, m.opId, 'member', h.clock.now());
|
||||
return id;
|
||||
}
|
||||
|
||||
test('confederation: members see each other\'s incoming movements (origin and ETA, type hidden), strangers and neutrals do not', () => {
|
||||
const h = T.setup();
|
||||
const a = T.addOperator(h, 'alice', { x: 5, y: 0 });
|
||||
const a2 = T.addOperator(h, 'alice2', { x: 5, y: 6 });
|
||||
const b = T.addOperator(h, 'bob', { x: 9, y: 0 });
|
||||
const c = T.addOperator(h, 'cat', { x: 14, y: 0, units: { script_crew: 20 } });
|
||||
const d = T.addOperator(h, 'dan', { x: 20, y: 0 });
|
||||
const X = alliance(h, 'Xray', a, [a2]), Y = alliance(h, 'Yankee', b);
|
||||
const sent = h.run('sendMovement', { userId: c.userId, fromTownId: c.townId, type: 'attack', toTownId: b.townId, units: { script_crew: 20 } });
|
||||
|
||||
// neutral: nobody but the target is told
|
||||
assert.equal(h.mq.movements(a.userId, {}).alliedIncoming.length, 0);
|
||||
assert.equal(h.mq.movements(b.userId, {}).incoming.length, 1);
|
||||
|
||||
A.setRelation(h.db, h.worldId, X, Y, 'confed', h.clock.now());
|
||||
for (const who of [a, a2]) {
|
||||
const l = h.mq.movements(who.userId, {});
|
||||
assert.equal(l.alliedIncoming.length, 1, who.name + ' sees the attack on the confederate');
|
||||
const m = l.alliedIncoming[0];
|
||||
assert.equal(m.id, sent.movementId);
|
||||
assert.equal(m.type, 'unknown');
|
||||
assert.equal(m.units, undefined);
|
||||
assert.equal(m.arriveAt, sent.arriveAt);
|
||||
assert.equal(m.from.townId, c.townId);
|
||||
assert.equal(m.to.townId, b.townId);
|
||||
assert.equal(m.toOperator.id, b.opId);
|
||||
assert.equal(l.incoming.length, 0, 'the personal incoming list is only about your own POPs');
|
||||
}
|
||||
const bl = h.mq.movements(b.userId, {});
|
||||
assert.equal(bl.incoming.length, 1, 'bob is the target himself');
|
||||
assert.equal(bl.alliedIncoming.length, 0, 'nothing is heading for his confederates');
|
||||
assert.equal(h.mq.movements(d.userId, {}).alliedIncoming.length, 0, 'a stranger sees nothing');
|
||||
assert.equal(h.mq.movements(c.userId, {}).alliedIncoming.length, 0);
|
||||
|
||||
// an attack on alice shows up for bob, but alice2's own reinforcement of alice is friendly traffic and does not
|
||||
h.db.run("UPDATE movements SET status = 'done'");
|
||||
T.giveUnits(h, a2.townId, { soc_analyst: 5 });
|
||||
T.giveUnits(h, c.townId, { script_crew: 5 });
|
||||
h.run('sendMovement', { userId: a2.userId, fromTownId: a2.townId, type: 'reinforce', toTownId: a.townId, units: { soc_analyst: 5 } });
|
||||
const atk = h.run('sendMovement', { userId: c.userId, fromTownId: c.townId, type: 'raid', toTownId: a.townId, units: { script_crew: 1 } });
|
||||
const bl2 = h.mq.movements(b.userId, {});
|
||||
assert.deepEqual(bl2.alliedIncoming.map((m) => m.id), [atk.movementId]);
|
||||
assert.equal(h.mq.movements(a.userId, {}).incoming.length, 2, 'alice herself sees both, types hidden');
|
||||
|
||||
// the confederation ends: the window closes
|
||||
A.setRelation(h.db, h.worldId, X, Y, 'neutral', h.clock.now());
|
||||
assert.equal(h.mq.movements(b.userId, {}).alliedIncoming.length, 0);
|
||||
});
|
||||
|
||||
// ---- shared report folder (HTTP, so routes, permissions and schemas are covered)
|
||||
const { createTestServer } = require('./helpers');
|
||||
const { createReport } = require('../server/military/reports');
|
||||
|
||||
test('shared reports: share, list, read, permissions, retention, unshare', async () => {
|
||||
const t = await createTestServer({ contract: true });
|
||||
try {
|
||||
const lead = await t.signupPeering('lead'), mem = await t.signupPeering('mem'), out = await t.signupPeering('outsider');
|
||||
const mk = await t.api('/alliances', { method: 'POST', body: { name: 'Folder', tag: 'FLD' } }, lead.token);
|
||||
const aid = mk.body.allianceId;
|
||||
const inv = await t.api('/alliances/' + aid + '/invite', { method: 'POST', body: { operatorId: mem.operatorId } }, lead.token);
|
||||
await t.api('/alliance-invites/' + inv.body.inviteId + '/accept', { method: 'POST' }, mem.token);
|
||||
|
||||
// two reports: one for lead, one for outsider
|
||||
const ctx = { db: t.srv.db, enqueue: (e) => t.srv.scheduler.enqueue(e), notify: () => {} };
|
||||
const now = t.clock.now();
|
||||
const rid = createReport(ctx, { worldId: 1, type: 'raid', now, summary: { s: 1 }, data: { secret: 'x' }, recipients: [{ userId: lead.userId, role: 'attacker' }] });
|
||||
const rid2 = createReport(ctx, { worldId: 1, type: 'scan', now, summary: {}, data: {}, recipients: [{ userId: out.userId, role: 'attacker' }] });
|
||||
|
||||
// sharing needs to be a recipient and an alliance member
|
||||
assert.equal((await t.api('/reports/' + rid2 + '/share', { method: 'POST' }, lead.token)).status, 404, 'not your report');
|
||||
assert.equal((await t.api('/reports/' + rid + '/share', { method: 'POST' }, out.token)).status, 404, 'a stranger cannot even see it exists');
|
||||
const outRep = createReport(ctx, { worldId: 1, type: 'raid', now, summary: {}, data: {}, recipients: [{ userId: out.userId, role: 'attacker' }] });
|
||||
const noAll = await t.api('/reports/' + outRep + '/share', { method: 'POST' }, out.token);
|
||||
assert.equal(noAll.body.error.code, 'not_in_alliance');
|
||||
|
||||
// the folder is empty, only members read it
|
||||
assert.deepEqual((await t.api('/alliances/' + aid + '/reports', {}, mem.token)).body.items, []);
|
||||
assert.equal((await t.api('/alliances/' + aid + '/reports', {}, out.token)).status, 403);
|
||||
|
||||
// share; retention is pushed out; repeating is a no-op
|
||||
t.clock.advance(10 * 24 * 3600e3);
|
||||
const sh = await t.api('/reports/' + rid + '/share', { method: 'POST' }, lead.token);
|
||||
assert.equal(sh.status, 200);
|
||||
assert.equal(sh.body.changed, true);
|
||||
assert.equal((await t.api('/reports/' + rid + '/share', { method: 'POST' }, lead.token)).body.changed, false);
|
||||
const exp = t.srv.db.get('SELECT expires_at FROM reports WHERE id = ?', rid).expires_at;
|
||||
assert.equal(exp, t.clock.now() + D.MILITARY.reportRetentionMs, 'shared report is kept 14 days from the share');
|
||||
|
||||
// the member reads it although they were never a participant; the stranger still cannot
|
||||
const list = await t.api('/alliances/' + aid + '/reports', {}, mem.token);
|
||||
assert.equal(list.body.items.length, 1);
|
||||
assert.equal(list.body.items[0].id, rid);
|
||||
assert.equal(list.body.items[0].sharedBy.name, 'lead');
|
||||
const one = await t.api('/alliances/' + aid + '/reports/' + rid, {}, mem.token);
|
||||
assert.equal(one.body.report.data.secret, 'x');
|
||||
assert.equal((await t.api('/alliances/' + aid + '/reports/' + rid, {}, out.token)).status, 403);
|
||||
assert.equal((await t.api('/reports/' + rid, {}, mem.token)).status, 404, 'the personal report list is unchanged: sharing is not handing out the participant role');
|
||||
|
||||
// the member cannot remove the leader's share; the leader can; the report row survives it
|
||||
assert.equal((await t.api('/reports/' + rid + '/share', { method: 'DELETE' }, mem.token)).status, 403);
|
||||
assert.equal((await t.api('/reports/' + rid + '/share', { method: 'DELETE' }, lead.token)).body.changed, true);
|
||||
assert.equal((await t.api('/alliances/' + aid + '/reports', {}, mem.token)).body.items.length, 0);
|
||||
assert.ok(t.srv.db.get('SELECT 1 AS x FROM reports WHERE id = ?', rid));
|
||||
|
||||
// a member shares their own; deleting the report (cleanup) takes the share with it
|
||||
const mrep = createReport(ctx, { worldId: 1, type: 'raid', now: t.clock.now(), summary: {}, data: {}, recipients: [{ userId: mem.userId, role: 'defender' }] });
|
||||
assert.equal((await t.api('/reports/' + mrep + '/share', { method: 'POST' }, mem.token)).status, 200);
|
||||
assert.equal((await t.api('/reports/' + mrep + '/share', { method: 'DELETE' }, mem.token)).status, 200, 'the sharer may remove their own');
|
||||
await t.api('/reports/' + mrep + '/share', { method: 'POST' }, mem.token);
|
||||
t.srv.db.run('DELETE FROM reports WHERE id = ?', mrep);
|
||||
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM report_shares WHERE report_id = ?', mrep).n, 0);
|
||||
|
||||
// cursor: newest first, next page holds older ones
|
||||
const ids = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const r = createReport(ctx, { worldId: 1, type: 'raid', now: t.clock.now(), summary: {}, data: {}, recipients: [{ userId: lead.userId, role: 'attacker' }] });
|
||||
await t.api('/reports/' + r + '/share', { method: 'POST' }, lead.token);
|
||||
ids.push(r);
|
||||
}
|
||||
const p1 = await t.api('/alliances/' + aid + '/reports?limit=2', {}, lead.token);
|
||||
assert.deepEqual(p1.body.items.map((i) => i.id), [ids[2], ids[1]]);
|
||||
const p2 = await t.api('/alliances/' + aid + '/reports?limit=2&after=' + p1.body.next, {}, lead.token);
|
||||
assert.deepEqual(p2.body.items.map((i) => i.id), [ids[0]]);
|
||||
assert.equal(p2.body.next, null);
|
||||
} finally { await t.close(); }
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue