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
121 lines
7.5 KiB
JavaScript
121 lines
7.5 KiB
JavaScript
'use strict';
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const { createServer } = require('../server');
|
|
const { FakeClock } = require('../server/clock');
|
|
const T = require('./military-helpers');
|
|
const { D, H } = T;
|
|
|
|
// A real server on port 0 with a fake clock: signup two players over HTTP, then drive the military routes.
|
|
async function boot() {
|
|
const clock = new FakeClock();
|
|
const srv = await createServer({ port: 0, host: '127.0.0.1', dbPath: ':memory:', clock, autoArm: false, log: () => {} });
|
|
const base = 'http://127.0.0.1:' + srv.port + '/api/v1';
|
|
const call = async (p, o, token) => {
|
|
o = o || {};
|
|
const headers = Object.assign({ 'content-type': 'application/json' }, o.headers || {});
|
|
if (token) headers.authorization = 'Bearer ' + token;
|
|
const r = await fetch(base + p, Object.assign({}, o, { headers, body: o.body && JSON.stringify(o.body) }));
|
|
return { status: r.status, body: await r.json() };
|
|
};
|
|
const signup = async (username) => {
|
|
const r = await call('/auth/signup', { method: 'POST', body: { username, password: 'correct horse battery' } });
|
|
assert.equal(r.status, 201, JSON.stringify(r.body));
|
|
const t = r.body.operators[0].towns[0];
|
|
return { token: r.body.token, townId: t.id, x: t.x, y: t.y, opId: r.body.operators[0].id, userId: r.body.user.id };
|
|
};
|
|
return { srv, clock, call, signup };
|
|
}
|
|
|
|
test('API: train, send a raid, warning for the target, reports, recall, error codes', async () => {
|
|
const { srv, clock, call, signup } = await boot();
|
|
try {
|
|
const a = await signup('alice'), b = await signup('bobby');
|
|
const db = srv.db;
|
|
// both start protected (72 h): move the clock past it, give alice infrastructure to train
|
|
clock.advance(73 * H);
|
|
for (const [t, lv] of [['ops_lab', 3], ['research_lab', 3], ['noc', 2]]) db.run('INSERT OR REPLACE INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', a.townId, t, t, lv);
|
|
db.run('UPDATE towns SET fiber = 5000, hardware = 5000, capital = 5000 WHERE id = ?', a.townId);
|
|
|
|
let r = await call('/towns/' + a.townId + '/train', { method: 'POST', body: { unit: 'script_crew', count: 20 } }, a.token);
|
|
assert.equal(r.status, 201, JSON.stringify(r.body));
|
|
assert.equal(typeof r.body.serverTime, 'number');
|
|
r = await call('/towns/' + a.townId, {}, a.token);
|
|
assert.equal(r.body.town.military.training.length, 1);
|
|
assert.equal(r.body.town.military.trainable.find((u) => u.unit === 'script_crew').locked, false);
|
|
assert.equal(r.body.town.military.trainable.find((u) => u.unit === 'botnet').locked, true);
|
|
clock.advance(3 * H);
|
|
r = await call('/towns/' + a.townId, {}, a.token);
|
|
assert.deepEqual(r.body.town.military.units.home, { script_crew: 20 });
|
|
|
|
// errors are enveloped with stable codes
|
|
r = await call('/movements', { method: 'POST', body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 99 } } }, a.token);
|
|
assert.equal(r.status, 409); assert.equal(r.body.error.code, 'not_enough_units');
|
|
r = await call('/movements', { method: 'POST', body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 5 } } }, b.token);
|
|
assert.equal(r.status, 404, 'cannot send from somebody else\'s POP');
|
|
r = await call('/movements', { method: 'POST', body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 5 }, extra: 1 } }, a.token);
|
|
assert.equal(r.status, 422);
|
|
r = await call('/movements', { method: 'POST', body: { fromTownId: a.townId, type: 'teleport', toTownId: b.townId, units: {} } }, a.token);
|
|
assert.equal(r.status, 422);
|
|
|
|
// alice is protected no more (73 h passed) and bob neither: the raid goes out
|
|
r = await call('/movements', { method: 'POST', headers: { 'idempotency-key': 'raid-key-0001' }, body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 10 } } }, a.token);
|
|
assert.equal(r.status, 201, JSON.stringify(r.body));
|
|
const mid = r.body.movementId;
|
|
const replay = await call('/movements', { method: 'POST', headers: { 'idempotency-key': 'raid-key-0001' }, body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 10 } } }, a.token);
|
|
assert.equal(replay.body.movementId, mid, 'idempotent replay: no second movement');
|
|
assert.equal(db.get('SELECT COUNT(*) AS n FROM movements').n, 1);
|
|
|
|
r = await call('/movements', {}, b.token);
|
|
assert.equal(r.body.incoming.length, 1); assert.equal(r.body.incoming[0].type, 'unknown');
|
|
r = await call('/movements', {}, a.token);
|
|
assert.equal(r.body.outgoing[0].type, 'raid');
|
|
|
|
// recall inside 90 s, then send again and let it land
|
|
r = await call('/movements/' + mid + '/recall', { method: 'POST' }, a.token);
|
|
assert.equal(r.status, 200, JSON.stringify(r.body));
|
|
clock.advance(H);
|
|
r = await call('/movements', { method: 'POST', body: { fromTownId: a.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 20 } } }, a.token);
|
|
assert.equal(r.status, 201, JSON.stringify(r.body));
|
|
clock.advance(100 * 1000);
|
|
r = await call('/movements/' + r.body.movementId + '/recall', { method: 'POST' }, a.token);
|
|
assert.equal(r.status, 409); assert.equal(r.body.error.code, 'recall_expired');
|
|
clock.advance(5 * H);
|
|
|
|
r = await call('/reports', {}, a.token);
|
|
assert.equal(r.status, 200); assert.equal(r.body.items.length, 1); assert.equal(r.body.unread, 1);
|
|
const id = r.body.items[0].id;
|
|
const rb = await call('/reports', {}, b.token);
|
|
assert.equal(rb.body.items[0].id, id, 'defender sees the same report');
|
|
r = await call('/reports/' + id, {}, a.token);
|
|
assert.equal(r.body.report.type, 'raid'); assert.equal(r.body.report.data.outcome, 'attacker_won');
|
|
r = await call('/reports/' + id + '/read', { method: 'POST' }, a.token);
|
|
assert.equal(r.status, 200);
|
|
r = await call('/reports?unread=true', {}, a.token);
|
|
assert.equal(r.body.items.length, 0);
|
|
r = await call('/reports/999999', {}, a.token);
|
|
assert.equal(r.status, 404);
|
|
|
|
// tile info and research over the API
|
|
r = await call('/map/tile?x=' + b.x + '&y=' + b.y, {}, a.token);
|
|
assert.equal(r.body.type, 'pop'); assert.equal(r.body.town.owner.name, 'bobby');
|
|
r = await call('/towns/' + a.townId + '/research', { method: 'POST', body: { unit: 'script_crew', stat: 'attack' } }, a.token);
|
|
assert.equal(r.status, 201, JSON.stringify(r.body));
|
|
r = await call('/towns/' + a.townId + '/research', { method: 'POST', body: { unit: 'script_crew', stat: 'attack' } }, a.token);
|
|
assert.equal(r.status, 409); assert.equal(r.body.error.code, 'queue_full');
|
|
r = await call('/notifications', {}, b.token);
|
|
assert.ok(r.body.items.some((n) => n.type === 'incoming'));
|
|
assert.ok(r.body.items.some((n) => n.type === 'report'));
|
|
} finally { await srv.close(); }
|
|
});
|
|
|
|
test('API: openapi describes every military route', () => {
|
|
const { build } = require('../tools/gen-openapi');
|
|
const spec = build();
|
|
for (const [p, m] of [['/towns/{id}/train', 'post'], ['/towns/{id}/research', 'post'], ['/movements', 'post'], ['/movements', 'get'], ['/movements/{id}/recall', 'post'],
|
|
['/towns/{id}/withdraw', 'post'], ['/reports', 'get'], ['/reports/{id}', 'get'], ['/reports/{id}/read', 'post'], ['/reports/read-all', 'post'], ['/map/tile', 'get']]) {
|
|
assert.ok(spec.paths[p] && spec.paths[p][m], m + ' ' + p + ' is in openapi.json');
|
|
}
|
|
for (const c of ['not_enough_units', 'units_offline', 'target_protected', 'protection_active', 'footprint_too_low', 'cannot_takeover', 'recall_expired']) assert.ok(spec['x-error-codes'][c], c);
|
|
assert.ok(D.UNIT_ORDER.length === 10);
|
|
});
|