Upstream/test/social-rules.test.js
jvanvik 249818a7aa 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
2026-10-11 18:32:36 +02:00

54 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const test = require('node:test');
const assert = require('node:assert');
const R = require('../public/js/rules.js');
const D = require('../public/js/data.js');
const { cleanText } = require('../server/text');
test('rules: alliance size cap is 10 + 2 per Peering office level', () => {
assert.equal(R.allianceCap(0), 10);
assert.equal(R.allianceCap(1), 12);
assert.equal(R.allianceCap(10), 30);
assert.equal(R.allianceCap(-3), 10);
});
test('rules: permission table and seniority', () => {
assert.ok(R.allianceCan('leader', 'disband') && !R.allianceCan('officer', 'disband'));
assert.ok(R.allianceCan('officer', 'invite') && !R.allianceCan('member', 'invite'));
assert.ok(R.allianceCan('diplomat', 'diplomacy') && !R.allianceCan('officer', 'diplomacy'));
assert.ok(!R.allianceCan('leader', 'no_such_action'));
assert.ok(R.outranks('leader', 'diplomat') && R.outranks('diplomat', 'officer') && R.outranks('officer', 'member'));
assert.ok(!R.outranks('officer', 'officer') && !R.outranks('member', 'leader'));
// every role listed in the table exists
for (const roles of Object.values(D.SOCIAL.permissions)) for (const r of roles) assert.ok(D.SOCIAL.roles.includes(r), r);
});
test('rules: diplomacy moves', () => {
assert.equal(R.diplomacyMove('neutral', 'war'), 'immediate');
assert.equal(R.diplomacyMove('neutral', 'nap'), 'proposal');
assert.equal(R.diplomacyMove('war', 'neutral'), 'proposal');
assert.equal(R.diplomacyMove('nap', 'neutral'), 'delayed');
assert.equal(R.diplomacyMove('confed', 'war'), 'delayed');
assert.equal(R.diplomacyMove('war', 'confed'), null);
assert.equal(R.diplomacyMove('war', 'war'), null);
assert.equal(R.diplomacyMove('bogus', 'war'), null);
assert.equal(R.pactCancelMs(1), 24 * 3600 * 1000);
assert.equal(R.pactCancelMs(3), 8 * 3600 * 1000);
});
test('text: control, bidi and invisible characters are stripped', () => {
assert.equal(cleanText('a‮b​c\u0007d'), 'abcd');
assert.equal(cleanText('x\u{E0041}y؜z'), 'xyz');
assert.equal(cleanText(' a \t\n b '), 'a b');
assert.equal(cleanText('l1\r\nl2\n\n\n\n\nl3', { multiline: true }), 'l1\nl2\n\n\nl3');
assert.equal(cleanText('é'), 'é'); // NFC
assert.equal(cleanText('\u{1F468}‍\u{1F469}'), '\u{1F468}‍\u{1F469}'); // ZWJ emoji sequences survive
assert.equal(cleanText(undefined), '');
});
test('helpers: the response contract check really fails on a body that breaks its schema', () => {
const { checkContract } = require('./helpers');
assert.throws(() => checkContract('GET', '/alliances/mine', 200, { serverTime: 1 }), /breaks its response schema/);
assert.throws(() => checkContract('GET', '/rankings/alliances', 200, { serverTime: 1, total: 'x', you: null, items: [], next: null }), /total/);
checkContract('GET', '/alliances/mine', 200, { serverTime: 1, operatorId: 1, worldId: 1, alliance: null, role: null, permissions: [], peeringLevel: 0 });
});