129 lines
6.2 KiB
JavaScript
129 lines
6.2 KiB
JavaScript
|
|
'use strict';
|
||
|
|
const { test } = require('node:test');
|
||
|
|
const assert = require('node:assert/strict');
|
||
|
|
const path = require('path');
|
||
|
|
for (const f of ['parts.js', 'robot.js', 'eco.js']) require(path.join(__dirname, '..', 'game', f));
|
||
|
|
const RC = globalThis.RC;
|
||
|
|
const E = RC.Eco;
|
||
|
|
|
||
|
|
const fresh = () => E.newOnlineSave();
|
||
|
|
const anyPart = (slot, notIn = []) => Object.values(RC.PARTS).find(p => p.slot === slot && !notIn.includes(p.id) && !Object.values(RC.STARTER).includes(p.id));
|
||
|
|
|
||
|
|
test('fresh online save is a valid starter robot with 800 credits', () => {
|
||
|
|
const s = fresh();
|
||
|
|
assert.equal(s.money, 800);
|
||
|
|
assert.ok(RC.validate(s.build).ok);
|
||
|
|
assert.equal(s.build, s.robots[0].build, 'mirror points at the active robot');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('buy: deducts, owns, equips; refuses without money or twice', () => {
|
||
|
|
const s = fresh(), p = anyPart('armour');
|
||
|
|
s.money = p.cost;
|
||
|
|
assert.ok(E.apply(s, { type: 'buy', part: p.id }).ok);
|
||
|
|
assert.equal(s.money, 0);
|
||
|
|
assert.ok(s.owned.includes(p.id));
|
||
|
|
assert.equal(s.build.armour, p.id);
|
||
|
|
assert.equal(E.apply(s, { type: 'buy', part: p.id }).error, 'OWNED');
|
||
|
|
const q = anyPart('armour', [p.id]);
|
||
|
|
assert.equal(E.apply(s, { type: 'buy', part: q.id }).error, 'NO_MONEY');
|
||
|
|
assert.equal(E.apply(s, { type: 'buy', part: 'nope' }).error, 'BAD_PART');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('sell: 50 %, never while fitted on any robot, starter kit sells for 0', () => {
|
||
|
|
const s = fresh(), p = anyPart('armour');
|
||
|
|
s.money = 10000;
|
||
|
|
E.apply(s, { type: 'buy', part: p.id });
|
||
|
|
assert.equal(E.apply(s, { type: 'sell', part: p.id }).error, 'IN_USE');
|
||
|
|
E.apply(s, { type: 'equip', slot: 'armour', part: null });
|
||
|
|
const before = s.money;
|
||
|
|
assert.ok(E.apply(s, { type: 'sell', part: p.id }).ok);
|
||
|
|
assert.equal(s.money - before, Math.round(p.cost * 0.5));
|
||
|
|
// fitted on a second robot only -> still in use
|
||
|
|
E.apply(s, { type: 'buy', part: p.id });
|
||
|
|
E.apply(s, { type: 'robotNew' });
|
||
|
|
assert.equal(E.apply(s, { type: 'sell', part: p.id }).error, 'IN_USE', 'used by robot 0');
|
||
|
|
assert.equal(E.sellValue(RC.STARTER.chassis), 0);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('exploit closed: new robot -> sell its starter weapon -> delete robot gains nothing', () => {
|
||
|
|
const s = fresh();
|
||
|
|
const start = s.money;
|
||
|
|
for (let i = 0; i < 3; i++) {
|
||
|
|
E.apply(s, { type: 'robotNew' });
|
||
|
|
while (s.build.weapons.length) E.apply(s, { type: 'weaponRemove', index: 0 });
|
||
|
|
E.apply(s, { type: 'robotDelete', index: s.active });
|
||
|
|
}
|
||
|
|
assert.equal(s.money, start);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('equip: only owned parts of the right slot; required slots cannot be emptied', () => {
|
||
|
|
const s = fresh(), p = anyPart('motor');
|
||
|
|
assert.equal(E.apply(s, { type: 'equip', slot: 'motor', part: p.id }).error, 'NOT_OWNED');
|
||
|
|
assert.equal(E.apply(s, { type: 'equip', slot: 'motor', part: null }).error, 'REQUIRED');
|
||
|
|
assert.equal(E.apply(s, { type: 'equip', slot: 'armour', part: RC.STARTER.motor }).error, 'BAD_PART');
|
||
|
|
assert.equal(E.apply(s, { type: 'equip', slot: 'weapon', part: null }).error, 'BAD_SLOT');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('weapons: add costs money, max 3, remove refunds, placement is clamped', () => {
|
||
|
|
const s = fresh(), w = anyPart('weapon');
|
||
|
|
s.money = 1e6;
|
||
|
|
while (s.build.weapons.length < RC.MAX_WEAPONS) assert.ok(E.apply(s, { type: 'weaponAdd', part: w.id }).ok);
|
||
|
|
assert.equal(E.apply(s, { type: 'weaponAdd', part: w.id }).error, 'MAX_WEAPONS');
|
||
|
|
assert.ok(E.apply(s, { type: 'weaponPlace', index: 1, side: 'left', up: 5 }).ok);
|
||
|
|
assert.equal(s.build.weapons[1].side, 'left');
|
||
|
|
assert.equal(s.build.weapons[1].up, 0.6);
|
||
|
|
assert.equal(E.apply(s, { type: 'weaponPlace', index: 1, side: 'top' }).error, 'BAD_SIDE');
|
||
|
|
const m = s.money;
|
||
|
|
assert.ok(E.apply(s, { type: 'weaponRemove', index: 1 }).ok);
|
||
|
|
assert.equal(s.money - m, Math.round(w.cost * 0.5));
|
||
|
|
assert.equal(E.apply(s, { type: 'weaponRemove', index: 9 }).error, 'BAD_INDEX');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('hangar: max 6 robots, keep at least one, active index follows deletes', () => {
|
||
|
|
const s = fresh();
|
||
|
|
for (let i = 1; i < E.MAX_ROBOTS; i++) assert.ok(E.apply(s, { type: 'robotNew', name: 'R' + i }).ok);
|
||
|
|
assert.equal(E.apply(s, { type: 'robotNew' }).error, 'HANGAR_FULL');
|
||
|
|
E.apply(s, { type: 'robotSwitch', index: 4 });
|
||
|
|
E.apply(s, { type: 'robotDelete', index: 1 });
|
||
|
|
assert.equal(s.active, 3);
|
||
|
|
assert.equal(s.robotName, 'R4');
|
||
|
|
assert.equal(s.build, s.robots[s.active].build);
|
||
|
|
while (s.robots.length > 1) E.apply(s, { type: 'robotDelete', index: 0 });
|
||
|
|
assert.equal(E.apply(s, { type: 'robotDelete', index: 0 }).error, 'LAST_ROBOT');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('unknown / malformed actions are refused without changes', () => {
|
||
|
|
const s = fresh(), snap = JSON.stringify(s);
|
||
|
|
assert.equal(E.apply(s, { type: 'toString' }).error, 'BAD_ACTION');
|
||
|
|
assert.equal(E.apply(s, { type: '__proto__' }).error, 'BAD_ACTION');
|
||
|
|
assert.equal(E.apply(s, null).error, 'BAD_ACTION');
|
||
|
|
assert.equal(E.apply(s, { type: 'body', hx: 'x' }).error, 'BAD_BODY');
|
||
|
|
assert.equal(JSON.stringify(s), snap);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('pvpReward: win/draw/loss, bonus, repair never below zero', () => {
|
||
|
|
const win = E.pvpReward({ won: true, winner: 0, reason: 'ko', dealt: 100, hp: 50, maxHp: 100, value: 1000, money: 0 });
|
||
|
|
assert.equal(win.fame, 190);
|
||
|
|
assert.equal(win.money, 600 + 120 + 60 - 30);
|
||
|
|
const loss = E.pvpReward({ won: false, winner: 1, reason: 'ko', dealt: 0, hp: 0, maxHp: 100, value: 1e6, money: 0 });
|
||
|
|
assert.equal(loss.money, 0, 'repair capped at what you have');
|
||
|
|
const draw = E.pvpReward({ won: false, winner: -1, reason: 'time', dealt: 0, hp: 100, maxHp: 100, value: 0 });
|
||
|
|
assert.equal(draw.money, 300);
|
||
|
|
assert.ok(draw.draw);
|
||
|
|
const s = fresh(); E.applyReward(s, win);
|
||
|
|
assert.equal(s.wins, 1); assert.equal(s.games, 1); assert.equal(s.best, 190);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('mergeCosmetics takes names/paint/flags but never economy or robot count', () => {
|
||
|
|
const s = fresh();
|
||
|
|
E.mergeCosmetics(s, { money: 1e9, owned: ['x'], flag: 'DE', robots: [{ name: 'Zed', paint: { body: 0x123456, accent: -5, decal: 'javascript:1' }, build: { chassis: 'x' } }, { name: 'ghost' }] });
|
||
|
|
assert.equal(s.money, 800);
|
||
|
|
assert.equal(s.flag, 'DE');
|
||
|
|
assert.equal(s.robots.length, 1);
|
||
|
|
assert.equal(s.robotName, 'Zed');
|
||
|
|
assert.equal(s.paint.body, 0x123456);
|
||
|
|
assert.equal(s.paint.accent, 0x15171b, 'bad colour ignored');
|
||
|
|
assert.equal(s.paint.decal, null, 'non-PNG decal ignored');
|
||
|
|
assert.equal(s.build.chassis, RC.STARTER.chassis);
|
||
|
|
});
|