Files
wibosim/server/src/phase34.test.js
T
db9979andClaude Opus 5.5 4b5cfe09f8 Politik, Übernahmen, Szenarien: Randfälle behoben
- Regierung: Wahl brach mit TypeError ab, wenn die Umfragen eine inzwischen
  aufgelöste Spielerpartei enthielten (Koalitionssuche nur mit bestehenden Parteien)
- Regierung: Regierungschef/in, der/die austritt oder gelöscht wird, behielt das Amt
  (headPid); jetzt neue Parteispitze oder kommissarisch
- Regierung: Minister/innen konnten über den Bestand hinaus verkaufen und so in der
  eigenen Branche short gehen; Abbau nur bis null, offene Orders zählen mit
- Regierung: eine Wahl beendet ein laufendes Misstrauensvotum (es bezog sich auf
  das alte Parlament); kein Absturz, wenn keine Koalition mehr besteht
- Haushalt: NaN in Schulden/Defizit verdarb Rating (Absturz in ratingOf) und
  Anleihen dauerhaft – Werte werden abgesichert
- Parlament: Altstand mit laufender Abstimmung (ohne Länderkammer) ergab
  Wahrscheinlichkeit NaN und scheiterte immer in der Länderkammer
- Übernahme: zweites Angebot für ein Ziel/einen Bieter, das schon in einer
  Übernahme steckt oder kontrolliert wird, sowie Nicht-Aktien als Bieter abgelehnt
- Szenario: dasselbe Szenario zweimal anzuwenden verdoppelte die Effekte
- Tests in src/phase34.test.js

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
2026-12-03 20:14:43 +00:00

162 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Randfälle der Phase-3/4-Module: Regierung, Parlament, Unternehmensereignisse, Szenarien
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');
const { Politics } = await import('./politics.js');
const { BILLS, MINISTRIES } = await import('./world.js');
Object.assign(settings.game, { dayMs: 60_000, tickMs: 1000, staticCorridor: 0.5, dynamicCorridor: 0.3, marketHaltPct: 0.5, recallsEnabled: false });
settings.news.enabled = false;
function setup(dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-p-')), t0 = Date.now()) {
const clock = { t: t0 };
const store = new Store(dir);
const g = new Game({ store, now: () => clock.t });
return { g, clock, dir, store };
}
const step = ({ g, clock }, n = 1) => { for (let i = 0; i < n; i++) { clock.t += 1000; g.tick(); } };
const until = (ctx, pred, max = 400) => { for (let i = 0; i < max && !pred(); i++) step(ctx); assert.ok(pred(), 'Bedingung nicht erreicht'); };
const player = async (g, name) => (await g.register({ name, password: 'geheim123' })).player;
/** Spielerpartei gewinnt die Wahl; Gründer wird Regierungschef/in und Minister/in */
async function playerGovernment(g, prefix) {
const ps = [];
for (let i = 0; i < 6; i++) ps.push(await player(g, `${prefix}${i}`));
g.gov.found(ps[0], { name: `${prefix}partei`, short: prefix.slice(0, 3).toUpperCase(), pos: { markt: 1, staat: -1, oeko: 0 } });
const id = g.gov.parties[0].id;
g.gov.join(ps[1], id); g.gov.join(ps[2], id);
for (const p of ps) g.gov.vote(p, id);
const w = settings.game.playerWeight;
settings.game.playerWeight = 1;
try { g.gov.holdNow(); } finally { settings.game.playerWeight = w; }
assert.equal(g.gov.coalition.leader, id);
return { ps, id };
}
test('Regierung: Wahl nach Auflösung einer Partei aus den Umfragen läuft durch', async () => {
const { g } = setup();
const ps = [];
for (let i = 0; i < 3; i++) ps.push(await player(g, `Kurz${i}`));
g.gov.found(ps[0], { name: 'Kurzlebige Partei', short: 'KLP', pos: { markt: 0.5, staat: 0, oeko: 0 } });
const id = g.gov.parties[0].id;
g.gov.join(ps[1], id); g.gov.join(ps[2], id);
// Umfragen sahen die Partei deutlich über der Hürde
const shares = { ...g.gov.npcShares(), [id]: 0.3 };
for (let i = 0; i < 3; i++) g.gov.polls.unshift({ ts: Date.now(), day: g.day, institute: 'Test', shares });
for (const p of ps) g.gov.leave(p);
assert.equal(g.gov.parties.length, 0, 'Partei aufgelöst');
assert.doesNotThrow(() => g.gov.holdNow());
assert.ok(g.gov.election.done);
});
test('Regierung: Regierungschef/in verlässt die Partei oder wird gelöscht – Amt wird frei', async () => {
const { g } = setup();
const { ps } = await playerGovernment(g, 'Chef');
assert.equal(g.gov.coalition.headPid, ps[0].id);
g.gov.leave(ps[0]);
assert.notEqual(g.gov.coalition.headPid, ps[0].id);
assert.equal(g.gov.officeOf(ps[0].id), null, 'kein Amt mehr');
assert.notEqual(g.gov.coalition.head, ps[0].name);
// Löschen eines Amtsträgers
const next = g.gov.coalition.headPid;
if (next) {
g.adminAction({ id: 'admin', name: 'Admin', isAdmin: true }, 'delete', { id: next });
assert.notEqual(g.gov.coalition.headPid, next);
}
});
test('Regierung: Minister/in darf Aktien der eigenen Branche nur abbauen, nicht über null hinaus verkaufen', async () => {
const ctx = setup();
const { g } = ctx;
const a = await player(g, 'Ressort');
until(ctx, () => g.market.phase === 'continuous');
const mi = MINISTRIES[0];
const sym = [...g.stocks.values()].find((s) => s.sector === mi.sectors[0]).symbol;
g.placeOrder(a, { symbol: sym, side: 'buy', type: 'market', qty: 10 });
assert.equal(a.positions[sym].qty, 10);
g.gov.ministers[0].pid = a.id;
assert.throws(() => g.placeOrder(a, { symbol: sym, side: 'sell', type: 'market', qty: 20 }), /Interessenkonflikt/, 'Verkauf über den Bestand hinaus = neuer Leerverkauf');
const last = g.stocks.get(sym).last;
g.placeOrder(a, { symbol: sym, side: 'sell', type: 'limit', price: Math.round(last * 1.15 * 100) / 100, qty: 10 });
assert.throws(() => g.placeOrder(a, { symbol: sym, side: 'sell', type: 'market', qty: 5 }), /Interessenkonflikt/, 'offene Verkaufsorders zählen mit');
});
test('Regierung: Wahl beendet ein laufendes Misstrauensvotum', async () => {
const { g } = setup();
const alt = { parties: g.gov.coalition.parties, leader: g.gov.coalition.leader };
g.gov.noConf = { id: 'x', alt, endsAt: Date.now() + 3_600_000, votes: {}, baseline: 0.9, approval: 0.3 };
g.gov.holdNow();
assert.equal(g.gov.noConf, null);
});
test('Regierung: Haushalt bleibt endlich, auch wenn ein Wert ungültig wurde', async () => {
const ctx = setup();
const { g } = ctx;
g.gov.budget.structural = NaN;
g.gov.budget.debt = NaN;
g.gov.newDay();
assert.ok(Number.isFinite(g.gov.budget.debt) && Number.isFinite(g.gov.budget.deficit) && Number.isFinite(g.gov.budget.structural));
assert.ok(typeof g.gov.budget.rating === 'string');
g.gov.applyCost(NaN);
assert.ok(Number.isFinite(g.gov.budget.structural));
});
test('Parlament: Altstand mit einer laufenden Abstimmung wird mit endlicher Wahrscheinlichkeit übernommen', async () => {
const { g } = setup();
const now = Date.now();
const cur = { id: 'alt1', bill: BILLS[0], votes: {}, baseline: 0.6, startedAt: now, endsAt: now + 60_000, secretFrom: now + 50_000, p: 0.6 };
const pol = new Politics(g, { cur, laws: [], history: [] });
g.pol = pol;
const e = pol.pipeline[0];
const pl = await player(g, 'Altwähler');
pol.vote(pl, e.id, true);
assert.ok(Number.isFinite(e.p) && e.p > 0, `Wahrscheinlichkeit ${e.p}`);
for (const s of g.stocks.values()) assert.ok(Number.isFinite(s.fair), s.symbol);
});
test('Parlament: Vorlage eines gelöschten Antragstellers läuft weiter', async () => {
const { g } = setup();
const [a, b, c] = [await player(g, 'Antr'), await player(g, 'Mz1'), await player(g, 'Mz2')];
g.gov.join(a, 'ga');
g.pol.propose(a, { template: 'foerder', sector: 'auto' });
const id = g.pol.proposals[0].id;
g.pol.cosign(b, id); g.pol.cosign(c, id);
g.pol.pipeline = [];
g.pol.newBill();
const e = g.pol.pipeline[0];
assert.equal(e.proposerId, a.id);
g.adminAction({ id: 'admin', name: 'Admin', isAdmin: true }, 'delete', { id: a.id });
assert.doesNotThrow(() => g.pol.view());
for (let i = 0; i < 6 && g.pol.pipeline.includes(e); i++) { e.stageEndsAt = 0; e.baseline = 0.9; g.pol.tick(); }
assert.ok(!g.pol.pipeline.includes(e));
});
test('Übernahme: kein zweites Angebot für ein Ziel oder einen Bieter, das schon in einer Übernahme steckt', async () => {
const { g } = setup();
g.corp.takeover('GRNH', 'HLIO');
assert.throws(() => g.corp.takeover('GRNH', 'NVLT'), /Übernahme/);
assert.throws(() => g.corp.takeover('HLIO'), /Übernahme/, 'Bieter ist selbst kein Ziel');
assert.throws(() => g.corp.takeover('KRMA', 'GRNH'), /Übernahme/, 'Ziel kann nicht gleichzeitig bieten');
assert.throws(() => g.corp.takeover('KRMA', 'OEL'), /Bieter/, 'nur Aktiengesellschaften bieten');
assert.equal(g.corp.offers.length, 1);
});
test('Szenario: zweimal dasselbe Szenario wirkt nicht doppelt', async () => {
const { g } = setup();
const { applyScenario } = await import('./scenarios.js');
applyScenario(g, 'kiblase');
const mis = g.stocks.get('NVLT').mis, sent = g.macro.sentiment;
assert.throws(() => g.adminAction({ id: 'admin', name: 'Admin', isAdmin: true }, 'scenario', { value: 'kiblase' }), /bereits/);
assert.equal(g.stocks.get('NVLT').mis, mis);
assert.equal(g.macro.sentiment, sent);
});