Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances, roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map, alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js. Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push, bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
186 lines
12 KiB
JavaScript
186 lines
12 KiB
JavaScript
'use strict';
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const { createTestServer } = require('./helpers');
|
|
const A = require('../server/alliances');
|
|
|
|
const post = (body) => ({ method: 'POST', body });
|
|
const H = 3600 * 1000;
|
|
|
|
// Two alliances (leaders lA, lB), each with a diplomat (dA, dB) and a plain member (mA).
|
|
async function setup() {
|
|
const t = await createTestServer({ contract: true });
|
|
const lA = await t.signupPeering('lead_a'), lB = await t.signupPeering('lead_b');
|
|
const dA = await t.signupPeering('dip_a'), dB = await t.signupPeering('dip_b'), mA = await t.signupPeering('mem_a');
|
|
const mk = async (lead, name, tag) => (await t.api('/alliances', post({ name, tag }), lead.token)).body.allianceId;
|
|
const A1 = await mk(lA, 'Alpha', 'AAA'), B1 = await mk(lB, 'Bravo', 'BBB');
|
|
const join = async (lead, id, u, role) => {
|
|
const inv = await t.api('/alliances/' + id + '/invite', post({ operatorId: u.operatorId }), lead.token);
|
|
await t.api('/alliance-invites/' + inv.body.inviteId + '/accept', { method: 'POST' }, u.token);
|
|
if (role) await t.api('/alliances/' + id + '/role', post({ operatorId: u.operatorId, role }), lead.token);
|
|
};
|
|
await join(lA, A1, dA, 'diplomat'); await join(lB, B1, dB, 'diplomat'); await join(lA, A1, mA);
|
|
return { t, lA, lB, dA, dB, mA, A1, B1 };
|
|
}
|
|
const rel = (t, id, tok) => t.api('/alliances/' + id + '/diplomacy', {}, tok);
|
|
const types = async (t, u) => (await t.api('/notifications', {}, u.token)).body.items.map((n) => n.type);
|
|
|
|
test('diplomacy: war is declared at once, peace and pacts need the other side to accept', async (t0) => {
|
|
const { t, lA, lB, dA, dB, mA, A1, B1 } = await setup();
|
|
t0.after(() => t.close());
|
|
|
|
// permission: members cannot, diplomats can
|
|
let r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'war' }), mA.token);
|
|
assert.equal(r.status, 403);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: A1, state: 'war' }), dA.token);
|
|
assert.equal(r.status, 422);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'neutral' }), dA.token);
|
|
assert.equal(r.body.error.code, 'diplomacy_invalid', 'already neutral');
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'bogus' }), dA.token);
|
|
assert.equal(r.status, 422);
|
|
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'war' }), dA.token);
|
|
assert.equal(r.status, 200); assert.equal(r.body.result, 'applied');
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'war');
|
|
r = await rel(t, B1, lB.token);
|
|
assert.equal(r.body.relations[0].state, 'war'); assert.equal(r.body.relations[0].other.tag, 'AAA');
|
|
assert.ok((await types(t, dB)).includes('diplomacy_changed'), 'the other side is told');
|
|
assert.ok((await types(t, mA)).includes('diplomacy_changed'), 'so are plain members of the declaring side');
|
|
|
|
// war -> confed is not allowed; war -> neutral is a proposal
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'confed' }), dB.token);
|
|
assert.equal(r.body.error.code, 'diplomacy_invalid');
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'neutral' }), dB.token);
|
|
assert.equal(r.body.result, 'proposed'); const pid = r.body.proposalId;
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'war', 'nothing changes before acceptance');
|
|
assert.ok((await types(t, dA)).includes('diplomacy_proposal'));
|
|
assert.ok(!(await types(t, mA)).includes('diplomacy_proposal'), 'only leader/diplomats are pinged');
|
|
|
|
r = await rel(t, A1, dA.token);
|
|
assert.equal(r.body.proposalsIn.length, 1); assert.equal(r.body.proposalsIn[0].state, 'neutral');
|
|
r = await rel(t, B1, dB.token);
|
|
assert.equal(r.body.proposalsOut.length, 1);
|
|
|
|
// wrong alliance / wrong role cannot answer
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/accept', { method: 'POST' }, dB.token);
|
|
assert.equal(r.status, 403, 'the proposer cannot accept their own proposal');
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/accept', { method: 'POST' }, mA.token);
|
|
assert.equal(r.status, 403);
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/accept', { method: 'POST' }, dA.token);
|
|
assert.equal(r.body.changed, true);
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'neutral');
|
|
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM alliance_relations').n, 0, 'neutral keeps no row');
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/accept', { method: 'POST' }, dA.token);
|
|
assert.equal(r.body.changed, false, 'idempotent');
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/decline', { method: 'POST' }, dA.token);
|
|
assert.equal(r.status, 409);
|
|
|
|
// a pact: propose, replace, withdraw, decline, accept
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'nap' }), dA.token);
|
|
const p1 = r.body.proposalId;
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'confed' }), dA.token);
|
|
const p2 = r.body.proposalId; assert.notEqual(p1, p2);
|
|
assert.equal(t.srv.db.get('SELECT status FROM alliance_proposals WHERE id = ?', p1).status, 'withdrawn', 'a new proposal replaces the old one');
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'confed' }), dB.token);
|
|
assert.equal(r.body.error.code, 'proposal_exists');
|
|
r = await t.api('/diplomacy/proposals/' + p2, { method: 'DELETE' }, dB.token);
|
|
assert.equal(r.status, 403, 'only the proposer may withdraw');
|
|
r = await t.api('/diplomacy/proposals/' + p2, { method: 'DELETE' }, dA.token);
|
|
assert.equal(r.body.changed, true);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'nap' }), dA.token);
|
|
r = await t.api('/diplomacy/proposals/' + r.body.proposalId + '/decline', { method: 'POST' }, dB.token);
|
|
assert.equal(r.body.changed, true);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'nap' }), dA.token);
|
|
r = await t.api('/diplomacy/proposals/' + r.body.proposalId + '/accept', { method: 'POST' }, dB.token);
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'nap');
|
|
assert.ok(A.pactPartners(t.srv.db, lA.operatorId, lB.operatorId));
|
|
assert.ok(!A.confederated(t.srv.db, lA.operatorId, lB.operatorId));
|
|
});
|
|
|
|
test('diplomacy: cancelling a pact takes 24 h / world speed, can be revoked, and runs through the event queue', async (t0) => {
|
|
const { t, lA, lB, dA, dB, A1, B1 } = await setup();
|
|
t0.after(() => t.close());
|
|
const accept = async (from, fromTok, to, toTok, state) => {
|
|
const r = await t.api('/alliances/' + from + '/diplomacy', post({ targetId: to, state }), fromTok);
|
|
return t.api('/diplomacy/proposals/' + r.body.proposalId + '/accept', { method: 'POST' }, toTok);
|
|
};
|
|
await accept(A1, dA.token, B1, dB.token, 'confed');
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'confed');
|
|
assert.ok(A.confederated(t.srv.db, lA.operatorId, lB.operatorId));
|
|
|
|
const t0ms = t.clock.now();
|
|
let r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'neutral' }), dA.token);
|
|
assert.equal(r.body.result, 'scheduled'); assert.equal(r.body.effectiveAt, t0ms + 24 * H);
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'confed', 'the pact still holds while the cancel is pending');
|
|
r = await rel(t, B1, dB.token);
|
|
assert.equal(r.body.relations[0].pending.to, 'neutral'); assert.equal(r.body.relations[0].pending.byUs, false);
|
|
assert.ok((await types(t, lB)).includes('diplomacy_cancel_pending'));
|
|
// no further changes while one is pending
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'war' }), dB.token);
|
|
assert.equal(r.body.error.code, 'diplomacy_pending');
|
|
|
|
// revoke: only the side that started it
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy/' + A1 + '/pending', { method: 'DELETE' }, dB.token);
|
|
assert.equal(r.status, 403);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy/' + B1 + '/pending', { method: 'DELETE' }, dA.token);
|
|
assert.equal(r.body.changed, true);
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy/' + B1 + '/pending', { method: 'DELETE' }, dA.token);
|
|
assert.equal(r.body.changed, false);
|
|
t.clock.advance(25 * H); t.srv.scheduler.runDue();
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'confed', 'a revoked cancel never fires');
|
|
assert.equal(t.srv.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'diploChange' AND status = 'cancelled'").n, 1);
|
|
|
|
// cancel again and let it run: just before the deadline nothing, after it the change applies
|
|
r = await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'war' }), dB.token);
|
|
assert.equal(r.body.result, 'scheduled', 'confed -> war is also a delayed move');
|
|
t.clock.advance(24 * H - 1000); t.srv.scheduler.runDue();
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'confed');
|
|
t.clock.advance(2000); t.srv.scheduler.runDue();
|
|
assert.equal(A.relationBetween(t.srv.db, A1, B1), 'war');
|
|
r = await rel(t, A1, dA.token);
|
|
assert.equal(r.body.relations[0].pending, null);
|
|
assert.ok((await types(t, lA)).filter((x) => x === 'diplomacy_changed').length >= 2);
|
|
assert.equal(t.srv.db.get("SELECT status FROM events WHERE type = 'diploChange' ORDER BY id DESC LIMIT 1").status, 'done');
|
|
});
|
|
|
|
test('diplomacy: faster worlds shorten the cancel, disbanding clears relations and pending events', async (t0) => {
|
|
const t = await createTestServer({ contract: true, server: { world: { speed: 3 } } });
|
|
t0.after(() => t.close());
|
|
const l1 = await t.signupPeering('lead_one'), l2 = await t.signupPeering('lead_two');
|
|
const a = (await t.api('/alliances', post({ name: 'One', tag: 'ONE' }), l1.token)).body.allianceId;
|
|
const b = (await t.api('/alliances', post({ name: 'Two', tag: 'TWO' }), l2.token)).body.allianceId;
|
|
let r = await t.api('/alliances/' + a + '/diplomacy', post({ targetId: b, state: 'nap' }), l1.token);
|
|
await t.api('/diplomacy/proposals/' + r.body.proposalId + '/accept', { method: 'POST' }, l2.token);
|
|
const now = t.clock.now();
|
|
r = await t.api('/alliances/' + a + '/diplomacy', post({ targetId: b, state: 'neutral' }), l1.token);
|
|
assert.equal(r.body.effectiveAt, now + 8 * H, '24 h at 3x');
|
|
|
|
// disband the other side while the cancel is pending
|
|
await t.api('/alliances/' + b + '/disband', { method: 'POST' }, l2.token);
|
|
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM alliance_relations').n, 0);
|
|
assert.equal(t.srv.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'diploChange' AND status = 'pending'").n, 0);
|
|
t.clock.advance(9 * H); t.srv.scheduler.runDue();
|
|
assert.equal(t.srv.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0);
|
|
r = await t.api('/notifications', {}, l1.token);
|
|
assert.ok(r.body.items.some((n) => n.type === 'diplomacy_changed' && n.payload.reason === 'disbanded' && n.payload.to === 'neutral'));
|
|
});
|
|
|
|
test('diplomacy: proposals expire, and a stale proposal is re-validated on accept', async (t0) => {
|
|
const { t, dA, dB, A1, B1 } = await setup();
|
|
t0.after(() => t.close());
|
|
let r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'nap' }), dA.token);
|
|
const pid = r.body.proposalId;
|
|
t.clock.advance(73 * H);
|
|
r = await rel(t, B1, dB.token);
|
|
assert.equal(r.body.proposalsIn.length, 0, 'expired proposals are not listed');
|
|
r = await t.api('/diplomacy/proposals/' + pid + '/accept', { method: 'POST' }, dB.token);
|
|
assert.equal(r.body.error.code, 'proposal_expired');
|
|
|
|
// pending proposal, then war is declared: the proposal is withdrawn by the state change
|
|
r = await t.api('/alliances/' + A1 + '/diplomacy', post({ targetId: B1, state: 'nap' }), dA.token);
|
|
const p2 = r.body.proposalId;
|
|
await t.api('/alliances/' + B1 + '/diplomacy', post({ targetId: A1, state: 'war' }), dB.token);
|
|
assert.equal(t.srv.db.get('SELECT status FROM alliance_proposals WHERE id = ?', p2).status, 'withdrawn');
|
|
r = await t.api('/diplomacy/proposals/' + p2 + '/accept', { method: 'POST' }, dB.token);
|
|
assert.equal(r.status, 409);
|
|
});
|