Files

91 lines
2.7 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { computeEnergy, ENERGY_MAX, ENERGY_REGEN_SECONDS } = require('../../src/game/energy');
// No time passed
test('computeEnergy no time passed', () => {
const now = Date.now();
const result = computeEnergy(50, now, now);
assert.equal(result.energy, 50);
assert.equal(result.updatedAtMs, now);
});
// Clock going backwards
test('computeEnergy clock going backwards', () => {
const result = computeEnergy(50, Date.now(), Date.now() - 100000);
assert.equal(result.energy, 50);
});
// Less than one regen interval
test('computeEnergy partial tick (less than 1 regen interval)', () => {
const now = Date.now();
const result = computeEnergy(50, now - 10000, now);
// 10s < 180s → no regen
assert.equal(result.energy, 50);
});
// One full tick
test('computeEnergy one full tick', () => {
const now = Date.now();
const result = computeEnergy(50, now - 180000, now);
// 180s / 180s = 1 tick
assert.equal(result.energy, 51);
});
// Multiple full ticks
test('computeEnergy multiple full ticks', () => {
const now = Date.now();
const result = computeEnergy(50, now - 360000, now);
// 360s / 180s = 2 ticks
assert.equal(result.energy, 52);
});
// Caps at max
test('computeEnergy caps at max', () => {
const now = Date.now();
const result = computeEnergy(100, now - 3600000, now);
assert.equal(result.energy, ENERGY_MAX);
});
// Large gap (hours ago)
test('computeEnergy large gap caps at max', () => {
const now = Date.now();
const result = computeEnergy(30, now - 36000000, now); // 10 hours ago
assert.equal(result.energy, ENERGY_MAX);
});
// Partial interval preserved (timestamp advances by whole ticks only)
test('computeEnergy preserves partial interval', () => {
const updatedAt = 1000000;
const now = 1000000 + 180000 + 50000; // 1 tick + 50s partial
const result = computeEnergy(50, updatedAt, now);
assert.equal(result.energy, 51);
// Timestamp should advance by exactly 1 tick, not the full elapsed
assert.equal(result.updatedAtMs, updatedAt + 180000);
});
// Starting from zero
test('computeEnergy from zero', () => {
const now = Date.now();
const result = computeEnergy(0, now - 540000, now);
// 540s / 180s = 3 ticks → 0 + 3 = 3
assert.equal(result.energy, 3);
});
// At max, large gap → resets timestamp
test('computeEnergy at max resets timestamp', () => {
const now = Date.now();
const result = computeEnergy(100, now - 10000000, now);
assert.equal(result.energy, ENERGY_MAX);
assert.equal(result.updatedAtMs, now);
});
// Constants are correct
test('ENERGY_MAX is 100', () => {
assert.equal(ENERGY_MAX, 100);
});
test('ENERGY_REGEN_SECONDS is 180', () => {
assert.equal(ENERGY_REGEN_SECONDS, 180);
});