Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy

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
This commit is contained in:
jvanvik 2026-10-11 18:32:36 +02:00
parent b56b13880e
commit 249818a7aa
111 changed files with 17472 additions and 13 deletions

View file

@ -0,0 +1,310 @@
'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 });
async function found(t, u, name, tag) {
const r = await t.api('/alliances', post({ name, tag }), u.token);
assert.equal(r.status, 201, JSON.stringify(r.body));
return r.body.allianceId;
}
// invite + accept
async function join(t, leader, allianceId, u) {
const inv = await t.api('/alliances/' + allianceId + '/invite', post({ operatorId: u.operatorId }), leader.token);
assert.equal(inv.status, 201, JSON.stringify(inv.body));
const r = await t.api('/alliance-invites/' + inv.body.inviteId + '/accept', { method: 'POST' }, u.token);
assert.equal(r.status, 200, JSON.stringify(r.body));
}
test('alliances: founding needs a Peering office, names and tags are unique per world', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const noOffice = await t.signup('nooffice');
let r = await t.api('/alliances', post({ name: 'Nope', tag: 'NO' }), noOffice.token);
assert.equal(r.status, 409); assert.equal(r.body.error.code, 'peering_office_required');
const a = await t.signupPeering('ada', 3);
r = await t.api('/alliances', post({ name: 'Fibre Friends', tag: 'FF' }), a.token);
assert.equal(r.status, 201);
assert.equal(r.body.sizeCap, 16, '10 + 2 * 3');
assert.equal(r.body.role, 'leader');
r = await t.api('/alliances', post({ name: 'Other', tag: 'OT' }), a.token);
assert.equal(r.body.error.code, 'already_in_alliance');
const b = await t.signupPeering('bob');
r = await t.api('/alliances', post({ name: 'fibre friends', tag: 'XX' }), b.token);
assert.equal(r.body.error.code, 'alliance_name_taken', 'case-insensitive');
r = await t.api('/alliances', post({ name: 'Different', tag: 'ff' }), b.token);
assert.equal(r.body.error.code, 'alliance_tag_taken');
r = await t.api('/alliances', post({ name: 'Bad<>Name', tag: 'BN' }), b.token);
assert.equal(r.status, 422);
r = await t.api('/alliances', post({ name: 'Fine Name', tag: 'a b' }), b.token);
assert.equal(r.status, 422);
r = await t.api('/alliances', post({ name: 'Zero​width', tag: 'ZW' }), b.token);
assert.equal(r.status, 201, 'invisible characters are stripped, not rejected');
assert.equal(r.body.name, 'Zerowidth');
});
test('alliances: invite, accept, decline, cancel, idempotency and room checks', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const lead = await t.signupPeering('lead', 1);
const m1 = await t.signupPeering('mem1');
const m2 = await t.signupPeering('mem2');
const bare = await t.signup('bare'); // no Peering office
const id = await found(t, lead, 'Core Club', 'CC');
// invite twice: same invite, second call is a no-op (200)
let r = await t.api('/alliances/' + id + '/invite', post({ name: 'MEM1' }), lead.token);
assert.equal(r.status, 201); const inv1 = r.body.inviteId;
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m1.operatorId }), lead.token);
assert.equal(r.status, 200); assert.equal(r.body.inviteId, inv1); assert.equal(r.body.changed, false);
r = await t.api('/alliances/' + id + '/invite', post({}), lead.token);
assert.equal(r.status, 422, 'needs a target');
// the invitee sees it and got a notification
r = await t.api('/alliance-invites', {}, m1.token);
assert.equal(r.body.items.length, 1); assert.equal(r.body.items[0].alliance.tag, 'CC');
r = await t.api('/notifications', {}, m1.token);
assert.ok(r.body.items.some((n) => n.type === 'alliance_invite' && n.payload.inviteId === inv1));
// someone else cannot answer it
r = await t.api('/alliance-invites/' + inv1 + '/accept', { method: 'POST' }, m2.token);
assert.equal(r.status, 404);
r = await t.api('/alliance-invites/' + inv1 + '/accept', { method: 'POST' }, m1.token);
assert.equal(r.status, 200); assert.equal(r.body.changed, true);
r = await t.api('/alliance-invites/' + inv1 + '/accept', { method: 'POST' }, m1.token);
assert.equal(r.status, 200); assert.equal(r.body.changed, false, 'accepting twice is a no-op');
r = await t.api('/alliance-invites/' + inv1 + '/decline', { method: 'POST' }, m1.token);
assert.equal(r.status, 409, 'cannot decline what was accepted');
// a plain member may not invite
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m2.operatorId }), m1.token);
assert.equal(r.status, 403); assert.equal(r.body.error.details.need, 'invite');
// already in an alliance
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m1.operatorId }), lead.token);
assert.equal(r.body.error.code, 'already_in_alliance');
// decline, then re-invite, then cancel
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m2.operatorId }), lead.token);
const inv2 = r.body.inviteId;
r = await t.api('/alliance-invites/' + inv2 + '/decline', { method: 'POST' }, m2.token);
assert.equal(r.body.changed, true);
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m2.operatorId }), lead.token);
const inv3 = r.body.inviteId; assert.notEqual(inv3, inv2);
r = await t.api('/alliances/' + id + '/invites', {}, lead.token);
assert.equal(r.body.items.length, 1);
r = await t.api('/alliances/' + id + '/invites/' + inv3, { method: 'DELETE' }, lead.token);
assert.equal(r.body.changed, true);
r = await t.api('/alliances/' + id + '/invites/' + inv3, { method: 'DELETE' }, lead.token);
assert.equal(r.body.changed, false);
r = await t.api('/alliance-invites/' + inv3 + '/accept', { method: 'POST' }, m2.token);
assert.equal(r.status, 409);
// joining needs a Peering office
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: bare.operatorId }), lead.token);
r = await t.api('/alliance-invites/' + r.body.inviteId + '/accept', { method: 'POST' }, bare.token);
assert.equal(r.body.error.code, 'peering_office_required');
t.setBuilding(bare.townId, 'peering_office', 1);
r = await t.api('/alliances/' + id + '/invites', {}, lead.token);
r = await t.api('/alliance-invites/' + r.body.items[0].id + '/accept', { method: 'POST' }, bare.token);
assert.equal(r.status, 200, JSON.stringify(r.body));
// room: the cap is 10 + 2 * 1 = 12 members; fill it with quick operators
const filler = [];
for (let i = 0; filler.length < 12 - 3; i++) {
const f = t.quickSignup('fill' + i);
t.setBuilding(f.townId, 'peering_office', 1);
filler.push(f);
}
for (const f of filler) t.srv.commands.run('allianceInvite', { userId: lead.userId, allianceId: id, operatorId: f.operatorId });
for (const f of filler) {
const inv = t.srv.db.get("SELECT id FROM alliance_invites WHERE operator_id = ? AND status = 'pending'", f.operatorId);
t.srv.commands.run('allianceInviteAnswer', { userId: f.userId, inviteId: inv.id, accept: true });
}
assert.equal(A.memberCount(t.srv.db, id), 12);
r = await t.api('/alliances/' + id + '/invite', post({ operatorId: m2.operatorId }), lead.token);
assert.equal(r.body.error.code, 'alliance_full');
assert.equal(r.body.error.details.cap, 12);
});
test('alliances: the size cap is stored, only raised, and refreshed from the leader\'s best Peering office', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const lead = await t.signupPeering('lead', 1);
const m = await t.signupPeering('mmm');
const id = await found(t, lead, 'Growers', 'GR');
let r = await t.api('/alliances/' + id, {}, lead.token);
assert.equal(r.body.alliance.sizeCap, 12);
t.setBuilding(lead.townId, 'peering_office', 6);
r = await t.api('/alliances/' + id, {}, lead.token);
assert.equal(r.body.alliance.sizeCap, 22, 'reads show the effective cap');
assert.equal(t.srv.db.get('SELECT size_cap FROM alliances WHERE id = ?', id).size_cap, 12, 'reads do not write');
await t.api('/alliances/' + id + '/invite', post({ operatorId: m.operatorId }), lead.token);
assert.equal(t.srv.db.get('SELECT size_cap FROM alliances WHERE id = ?', id).size_cap, 22, 'a command stores the raise');
t.srv.db.run("UPDATE buildings SET level = 1 WHERE town_id = ? AND type = 'peering_office'", lead.townId);
r = await t.api('/alliances/' + id, {}, lead.token);
assert.equal(r.body.alliance.sizeCap, 22, 'never lowered');
});
test('alliances: roles, kick rules, leader transfer, leaving, notes, members list', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const lead = await t.signupPeering('lead');
const off = await t.signupPeering('off');
const dip = await t.signupPeering('dip');
const mem = await t.signupPeering('mem');
const id = await found(t, lead, 'Roles', 'RL');
for (const u of [off, dip, mem]) await join(t, lead, id, u);
const url = (p) => '/alliances/' + id + p;
// role changes: leader only, never to leader via /role
let r = await t.api(url('/role'), post({ operatorId: off.operatorId, role: 'officer' }), lead.token);
assert.equal(r.body.changed, true);
r = await t.api(url('/role'), post({ operatorId: off.operatorId, role: 'officer' }), lead.token);
assert.equal(r.body.changed, false);
await t.api(url('/role'), post({ operatorId: dip.operatorId, role: 'diplomat' }), lead.token);
r = await t.api(url('/role'), post({ operatorId: mem.operatorId, role: 'leader' }), lead.token);
assert.equal(r.status, 422);
r = await t.api(url('/role'), post({ operatorId: mem.operatorId, role: 'officer' }), off.token);
assert.equal(r.status, 403, 'officers cannot promote');
r = await t.api(url('/role'), post({ operatorId: lead.operatorId, role: 'member' }), lead.token);
assert.equal(r.status, 422, 'the leader cannot demote themselves');
// kick: officer may kick members but not diplomats or the leader
r = await t.api(url('/kick'), post({ operatorId: dip.operatorId }), off.token);
assert.equal(r.status, 403); assert.equal(r.body.error.details.need, 'outrank');
r = await t.api(url('/kick'), post({ operatorId: lead.operatorId }), off.token);
assert.equal(r.status, 403);
r = await t.api(url('/kick'), post({ operatorId: mem.operatorId }), dip.token);
assert.equal(r.status, 403, 'a diplomat has no kick permission');
r = await t.api(url('/kick'), post({ operatorId: mem.operatorId }), off.token);
assert.equal(r.body.changed, true);
r = await t.api(url('/kick'), post({ operatorId: mem.operatorId }), off.token);
assert.equal(r.body.changed, false, 'kicking twice is a no-op');
r = await t.api('/notifications', {}, mem.token);
assert.ok(r.body.items.some((n) => n.type === 'alliance_kicked'));
assert.equal(t.srv.db.get('SELECT alliance_id FROM operators WHERE id = ?', mem.operatorId).alliance_id, null);
// members list, with last online from the token use
t.clock.advance(10 * 60 * 1000);
await t.api('/me', {}, dip.token); // touches dip's token at the new time
r = await t.api(url('/members'), {}, lead.token);
assert.deepEqual(r.body.items.map((m) => m.role), ['leader', 'diplomat', 'officer']);
const d = r.body.items.find((m) => m.name === 'dip');
assert.equal(d.towns, 1); assert.equal(d.lastOnline, t.clock.now());
const o = r.body.items.find((m) => m.name === 'off');
assert.ok(o.lastOnline < t.clock.now(), 'off was not online since');
r = await t.api(url('/members'), {}, mem.token);
assert.equal(r.status, 403, 'non-members cannot list members');
// notes
r = await t.api(url('/notes'), post({ text: 'Rally at 20:00‮' }), mem.token);
assert.equal(r.status, 403);
r = await t.api(url('/notes'), post({ text: 'Rally at 20:00‮' }), off.token);
assert.equal(r.status, 201); assert.equal(r.body.note.text, 'Rally at 20:00');
const nid = r.body.note.id;
r = await t.api(url('/notes/' + nid), post({ text: 'Rally at 21:00' }), dip.token);
assert.equal(r.body.note.text, 'Rally at 21:00');
r = await t.api(url('/notes'), {}, dip.token);
assert.equal(r.body.items.length, 1);
r = await t.api(url('/notes/' + nid), { method: 'DELETE' }, off.token);
assert.equal(r.body.changed, true);
for (let i = 0; i < 10; i++) await t.api(url('/notes'), post({ text: 'n' + i }), lead.token);
r = await t.api(url('/notes'), post({ text: 'one too many' }), lead.token);
assert.equal(r.body.error.code, 'notes_full');
// leader cannot leave while others remain; transfer then leave
r = await t.api(url('/leave'), { method: 'POST' }, lead.token);
assert.equal(r.body.error.code, 'leader_must_transfer');
r = await t.api(url('/transfer'), post({ operatorId: lead.operatorId }), lead.token);
assert.equal(r.status, 422);
r = await t.api(url('/transfer'), post({ operatorId: mem.operatorId }), lead.token);
assert.equal(r.status, 404, 'target must be a member');
r = await t.api(url('/transfer'), post({ operatorId: dip.operatorId }), off.token);
assert.equal(r.status, 403);
r = await t.api(url('/transfer'), post({ operatorId: dip.operatorId }), lead.token);
assert.equal(r.body.changed, true);
assert.equal(A.membership(t.srv.db, dip.operatorId).role, 'leader');
assert.equal(A.membership(t.srv.db, lead.operatorId).role, 'officer');
r = await t.api(url('/leave'), { method: 'POST' }, lead.token);
assert.equal(r.body.changed, true);
r = await t.api(url('/leave'), { method: 'POST' }, lead.token);
assert.equal(r.body.changed, false, 'leaving twice is a no-op');
// disband: leader only; everything goes, names are free again
r = await t.api(url('/disband'), { method: 'POST' }, off.token);
assert.equal(r.status, 403);
r = await t.api(url('/disband'), { method: 'POST' }, dip.token);
assert.equal(r.status, 200);
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM alliances').n, 0);
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM alliance_members').n, 0);
assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM alliance_notes').n, 0);
assert.equal((await t.api(url(''), {}, dip.token)).status, 404);
r = await t.api('/alliances', post({ name: 'Roles', tag: 'RL' }), off.token);
assert.equal(r.status, 201, 'name and tag are reusable after a disband');
});
test('alliances: a lone leader leaving disbands; alliance shows in /me and /map; directory and profile', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const a = await t.signupPeering('aa1');
const b = await t.signupPeering('bb1');
const idA = await found(t, a, 'Alpha Net', 'ALP');
const idB = await found(t, b, 'Beta Net', 'BET');
let r = await t.api('/me', {}, a.token);
assert.deepEqual(r.body.operators[0].alliance, { id: idA, tag: 'ALP', name: 'Alpha Net' });
const out = await t.api('/map?x=0&y=0&r=40', {}, b.token);
const mine = out.body.towns.find((x) => x.owner.id === a.operatorId);
assert.deepEqual(mine.owner.alliance, { id: idA, tag: 'ALP', name: 'Alpha Net' });
assert.equal(mine.owner.allianceId, idA);
r = await t.api('/alliances', {}, a.token);
assert.deepEqual(r.body.items.map((x) => x.tag), ['ALP', 'BET']);
r = await t.api('/alliances?q=beta', {}, a.token);
assert.deepEqual(r.body.items.map((x) => x.tag), ['BET']);
r = await t.api('/alliances?limit=1', {}, a.token);
assert.equal(r.body.items.length, 1); assert.equal(r.body.next, idA);
r = await t.api('/alliances?limit=1&after=' + r.body.next, {}, a.token);
assert.equal(r.body.items[0].tag, 'BET'); assert.equal(r.body.next, null);
r = await t.api('/alliances/' + idB, {}, a.token);
assert.equal(r.body.alliance.memberCount, 1); assert.equal(r.body.alliance.myRole, null); assert.equal(r.body.alliance.relation, 'neutral');
assert.equal(r.body.alliance.leader.name, 'bb1'); assert.ok(r.body.alliance.footprint >= 0);
r = await t.api('/alliances/mine', {}, a.token);
assert.equal(r.body.role, 'leader'); assert.ok(r.body.permissions.includes('disband')); assert.equal(r.body.peeringLevel, 1);
assert.equal((await t.api('/alliances/abc', {}, a.token)).status, 404);
r = await t.api('/alliances/' + idA + '/leave', { method: 'POST' }, a.token);
assert.equal(r.body.disbanded, true);
r = await t.api('/alliances/mine', {}, a.token);
assert.equal(r.body.alliance, null); assert.equal(r.body.role, null);
r = await t.api('/me', {}, a.token);
assert.equal(r.body.operators[0].alliance, null);
});
test('alliances: lookup helpers for other modules', async (t0) => {
const t = await createTestServer({ contract: true });
t0.after(() => t.close());
const a = await t.signupPeering('hh1'), b = await t.signupPeering('hh2'), c = await t.signupPeering('hh3'), d = await t.signup('hh4');
const idA = await found(t, a, 'Hel A', 'HA');
const idB = await found(t, b, 'Hel B', 'HB');
await join(t, a, idA, c);
const db = t.srv.db;
assert.equal(A.operatorRelation(db, a.operatorId, a.operatorId), 'self');
assert.equal(A.operatorRelation(db, a.operatorId, c.operatorId), 'same_alliance');
assert.equal(A.operatorRelation(db, a.operatorId, b.operatorId), 'neutral');
assert.equal(A.operatorRelation(db, a.operatorId, d.operatorId), 'neutral');
await t.api('/alliances/' + idA + '/diplomacy', post({ targetId: idB, state: 'war' }), a.token);
assert.equal(A.operatorRelation(db, c.operatorId, b.operatorId), 'war');
assert.ok(A.atWar(db, b.operatorId, c.operatorId));
assert.ok(!A.pactPartners(db, a.operatorId, b.operatorId));
assert.ok(A.pactPartners(db, a.operatorId, c.operatorId) && A.confederated(db, a.operatorId, c.operatorId));
assert.equal(A.relationBetween(db, idA, idB), 'war');
assert.equal(A.relationBetween(db, idA, idA), 'neutral');
});