Files
wibosim/server/src/eqhist.test.js
T

43 lines
1.7 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
process.env.NODE_ENV = 'test';
process.env.TIME_MODE = 'turbo';
process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-test-'));
const { settings } = await import('./settings.js');
const { Store } = await import('./store.js');
const { Game } = await import('./game.js');
Object.assign(settings.game, { dayMs: 60_000, tickMs: 1000, recallsEnabled: false });
settings.news.enabled = false;
function setup() {
const clock = { t: Date.now() };
const g = new Game({ store: new Store(fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-eq-'))), now: () => clock.t });
return { g, clock };
}
test('Depot: Depotwert-Verlauf geht gekürzt und gerundet an den Client', async () => {
const { g } = setup();
const a = (await g.register({ name: 'Verlauf', password: 'geheim123' })).player;
assert.deepEqual(g.portfolio(a).eqHist, [], 'neues Depot: kein Verlauf');
a.eqHist = Array.from({ length: 300 }, (_, i) => 100_000 + i + 0.37);
const h = g.portfolio(a).eqHist;
assert.equal(h.length, 120, 'höchstens 120 Punkte');
assert.equal(h.at(-1), 100_299, 'neuester Punkt, auf Euro gerundet');
assert.equal(h[0], 100_180);
assert.equal(a.eqHist.length, 300, 'gespeicherter Verlauf bleibt unverändert');
});
test('Depot: Handelsschluss schreibt einen Punkt, der im Depot ankommt', async () => {
const { g } = setup();
const a = (await g.register({ name: 'Schluss', password: 'geheim123' })).player;
g.prog.dayClose();
const h = g.portfolio(a).eqHist;
assert.equal(h.length, 1);
assert.equal(h[0], Math.round(g.equity(a)));
});