mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 21:44:09 +01:00
Kam am Zahlentag zufällig noch eine Kapitalmaßnahme mit neuer Note hinzu, wich die aktuelle Note vom Stichtag ab und der Test schlug fehl. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
161 lines
7.8 KiB
JavaScript
161 lines
7.8 KiB
JavaScript
// Historie von Bilanz und Bonität: Bilanzstichtage je Quartal, Ratingwechsel mit Anlass, Migration alter Spielstände.
|
|
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-chist-'));
|
|
const { settings } = await import('./settings.js');
|
|
const { Store } = await import('./store.js');
|
|
const { Game } = await import('./game.js');
|
|
const { metrics, quarterHistory, notchAt, BS_HIST_MAX, RATING_HIST_MAX } = await import('./credit.js');
|
|
const { RATINGS } = await import('./ratings.js');
|
|
settings.news.enabled = false;
|
|
|
|
const newGame = (dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-chist-g-'))) => ({ g: new Game({ store: new Store(dir), now: () => Date.now() }), dir });
|
|
const stocks = (g) => [...g.stocks.values()].filter((s) => s.kind === 'stock');
|
|
|
|
function forceEarnings(g, s, surprise = 0.02) {
|
|
g.day += 1;
|
|
s.fund.nextEarningsDay = g.day;
|
|
s.fund.pre = { surprise, drift: 0, cutF: 1 };
|
|
s.fund.warningIssued = true;
|
|
g.econ.newDay();
|
|
}
|
|
|
|
test('Historie: Startbilanz und je Quartalszahlen ein Bilanzstichtag mit Kennzahlen und Rating (höchstens 12)', () => {
|
|
const { g } = newGame();
|
|
const s = stocks(g).find((x) => !x.fund.bs.bank);
|
|
assert.equal(s.fund.bsHist.length, 1, 'Startbilanz');
|
|
assert.equal(s.fund.bsHist[0].label, 'Start');
|
|
for (let i = 0; i < 3; i++) forceEarnings(g, s);
|
|
const h = s.fund.bsHist;
|
|
assert.equal(h.length, 4);
|
|
const last = h.at(-1);
|
|
assert.equal(last.day, g.day);
|
|
assert.equal(last.label, s.fund.quarters.at(-1).label);
|
|
assert.equal(last.eps, s.fund.quarters.at(-1).eps);
|
|
// Rating zum Stichtag; kam am selben Tag noch eine Kapitalmaßnahme (Kapitalerhöhung, Rückkauf, Übernahme)
|
|
// mit neuer Note dazu, zeigt der Stichtag zu Recht die Note vom Zahlentag
|
|
const laterChange = (s.fund.cr.hist ?? []).some((x) => x.day === g.day && x.reason !== 'Quartalszahlen');
|
|
if (laterChange) assert.ok(RATINGS.includes(last.rating), 'gültige Note');
|
|
else assert.equal(last.rating, RATINGS[s.fund.cr.n]);
|
|
assert.ok(Number.isFinite(last.lev) && Number.isFinite(last.icr) && last.eq > 0 && last.bvps > 0, 'Kennzahlen vorhanden');
|
|
const m = metrics(s);
|
|
assert.ok(Math.abs(last.nd - m.nd) < 1, 'Nettoschulden zum Stichtag');
|
|
// Ansicht: neueste zuerst
|
|
const v = g.credit.companyView(s);
|
|
assert.equal(v.qhist[0].day, g.day);
|
|
assert.ok(v.qhist.every((r, i, a) => i === 0 || a[i - 1].day >= r.day), 'absteigend sortiert');
|
|
assert.equal(v.day, g.day);
|
|
// Begrenzung
|
|
for (let i = 0; i < 14; i++) forceEarnings(g, s);
|
|
assert.equal(s.fund.bsHist.length, BS_HIST_MAX);
|
|
assert.equal(s.fund.bsHist.at(-1).day, g.day);
|
|
// Banken: Eigenkapitalquote statt Verschuldungsgrad
|
|
const bank = stocks(g).find((x) => x.fund.bs.bank);
|
|
forceEarnings(g, bank);
|
|
const bl = bank.fund.bsHist.at(-1);
|
|
assert.ok(bl.bank && bl.eqRatio > 0 && bl.lev === null);
|
|
});
|
|
|
|
test('Ratingwechsel: Anlass, Begründung, Ausblick und Art werden festgehalten (höchstens 20), Ausfall mit Sanierung', () => {
|
|
const { g } = newGame();
|
|
const s = stocks(g).filter((x) => !x.fund.bs.bank).sort((a, b) => a.fund.cr.n - b.fund.cr.n)[3];
|
|
const m = metrics(s);
|
|
s.fund.bs.nd = m.ebitda * 6.5; s.fund.bs.debt = s.fund.bs.nd + s.fund.bs.cash;
|
|
for (let i = 0; i < 4; i++) g.credit.review(s, 'Kapitalerhöhung');
|
|
const h = s.fund.cr.hist[0];
|
|
assert.equal(h.kind, 'down');
|
|
assert.equal(h.reason, 'Kapitalerhöhung');
|
|
assert.match(h.why, /Nettoschulden/);
|
|
assert.ok(['positiv', 'stabil', 'negativ'].includes(h.outlook));
|
|
assert.ok(h.ts > 0 && h.day === g.day);
|
|
// Begrenzung auf 20
|
|
s.fund.cr.hist = Array.from({ length: 30 }, (_, i) => ({ day: 1, from: 'BBB', to: 'BBB-', kind: 'down', i }));
|
|
s.fund.bs.nd = 0; s.fund.bs.debt = s.fund.bs.cash;
|
|
for (let i = 0; i < 6; i++) g.credit.review(s, 'Test');
|
|
assert.equal(s.fund.cr.hist.length, RATING_HIST_MAX);
|
|
assert.equal(s.fund.cr.hist[0].kind, 'up');
|
|
// Ausfall: zwei Einträge (Ausfall, neue Note)
|
|
g.credit.default(s, 0.3);
|
|
assert.equal(s.fund.cr.hist[0].kind, 'restructure');
|
|
assert.equal(s.fund.cr.hist[1].kind, 'default');
|
|
assert.equal(s.fund.cr.hist[1].to, 'D');
|
|
assert.match(s.fund.cr.hist[1].reason, /30 %/);
|
|
});
|
|
|
|
test('Note zu einem früheren Tag wird aus der Ratinghistorie zurückgerechnet', () => {
|
|
const c = { n: RATINGS.indexOf('BB+'), hist: [{ day: 30, from: 'BBB-', to: 'BB+' }, { day: 20, from: 'BBB', to: 'BBB-' }] };
|
|
assert.equal(notchAt(c, 10), 'BBB');
|
|
assert.equal(notchAt(c, 20), 'BBB-'); // Wechsel am Tag der Zahlen zählt zum Stichtag
|
|
assert.equal(notchAt(c, 25), 'BBB-');
|
|
assert.equal(notchAt(c, 30), 'BB+');
|
|
assert.equal(notchAt(c, 40), 'BB+');
|
|
});
|
|
|
|
test('Migration: alter Spielstand ohne Historie lädt, Historie wird aus den Quartalszahlen rekonstruiert und danach fortgeschrieben', () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-chist-old-'));
|
|
const { g } = newGame(dir);
|
|
const s0 = stocks(g).find((x) => !x.fund.bs.bank);
|
|
const sym = s0.symbol;
|
|
for (let i = 0; i < 3; i++) forceEarnings(g, s0);
|
|
g.flush(true);
|
|
g.store.close();
|
|
// Stand vor der Historie: kein bsHist, Ratinghistorie im alten Format (ohne Anlass) bzw. ganz ohne
|
|
const store = new Store(dir);
|
|
const { meta } = store.load();
|
|
let n = 0;
|
|
for (const v of Object.values(meta.stocks)) {
|
|
if (!v.fund?.cr) continue;
|
|
delete v.fund.bsHist;
|
|
if (n++ % 2) delete v.fund.cr.hist;
|
|
else v.fund.cr.hist = [{ day: 2, from: 'A', to: 'A-' }];
|
|
}
|
|
store.write({ meta });
|
|
store.close();
|
|
const g2 = new Game({ store: new Store(dir), now: () => Date.now() });
|
|
for (const s of stocks(g2)) {
|
|
assert.ok(Array.isArray(s.fund.bsHist) && Array.isArray(s.fund.cr.hist), `${s.symbol}: Historie ergänzt`);
|
|
const v = g2.credit.companyView(s);
|
|
assert.ok(Array.isArray(v.qhist));
|
|
for (const r of v.qhist) assert.ok(r.partial && r.day > 0 && Number.isFinite(r.eps), 'rekonstruiert aus den Quartalszahlen');
|
|
}
|
|
const s = g2.stocks.get(sym);
|
|
const v = g2.credit.companyView(s);
|
|
assert.equal(v.qhist.length, s.fund.quarters.filter((q) => q.day > 0).length, 'alle Quartale im Spiel rekonstruiert');
|
|
assert.equal(quarterHistory(s).length, v.qhist.length);
|
|
assert.ok(v.qhist.every((r) => RATINGS.includes(r.rating)), 'Note je Quartal aus der Ratinghistorie');
|
|
// Fortschreiben: das neue Quartal ist vollständig, die alten bleiben rekonstruiert
|
|
forceEarnings(g2, s);
|
|
const v2 = g2.credit.companyView(s);
|
|
assert.equal(v2.qhist[0].partial, false);
|
|
assert.ok(Number.isFinite(v2.qhist[0].lev));
|
|
assert.ok(v2.qhist.slice(1).every((r) => r.partial));
|
|
// Anleihe: Emittentenansicht trägt die Historie mit
|
|
const bond = [...g2.stocks.values()].find((b) => b.kind === 'bond' && b.issuer);
|
|
const bv = g2.credit.bondView(bond);
|
|
assert.ok(Array.isArray(bv.metrics.qhist) && Array.isArray(bv.hist));
|
|
// Speichern und erneut laden: Historie bleibt
|
|
g2.flush(true);
|
|
const saved = g2.store.load().meta;
|
|
assert.ok(saved.stocks[sym].fund.bsHist.length >= 1, 'Historie im Spielstand');
|
|
});
|
|
|
|
test('Quartalsbezeichnung läuft fort, auch wenn die Liste auf 12 Quartale begrenzt ist', async () => {
|
|
const { nextQuarterLabel } = await import('./fundamentals.js');
|
|
assert.equal(nextQuarterLabel([{ label: 'Q-2' }, { label: 'Q-1' }]), 'Q1/1');
|
|
assert.equal(nextQuarterLabel([{ label: 'Q2/1' }]), 'Q3/1');
|
|
assert.equal(nextQuarterLabel([{ label: 'Q4/3' }]), 'Q1/4');
|
|
assert.equal(nextQuarterLabel([]), 'Q1/1');
|
|
const { g } = newGame();
|
|
const s = stocks(g)[0];
|
|
for (let i = 0; i < 16; i++) forceEarnings(g, s);
|
|
const labels = s.fund.quarters.map((q) => q.label);
|
|
assert.equal(new Set(labels).size, labels.length, `keine doppelten Bezeichnungen (${labels.join(', ')})`);
|
|
assert.equal(labels.at(-1), 'Q4/4');
|
|
});
|