Files
wibosim/server/src/notifyprefs.test.js
T
db9979andClaude Opus 5.5 de18706873 Benachrichtigungen je Spieler: Themen, Wege, Ruhezeiten
Neue Einstellungsseite (Kontomenü, Profil, Posteingang): je Kategorie
In der App, Posteingang, Push und Taby wählbar, optionale Ruhezeiten.
Alle Sende-Stellen laufen über eine zentrale Prüfung (notifycore.js,
notifyprefs.js); Interviews standardmäßig nur bei Bezug zum Rivalen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
2026-12-05 07:52:37 +00:00

151 lines
8.6 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');
const { normPrefs, defaultPrefs, route, inQuiet, kindCat, NOTIF_CATS } = await import('../public/js/notifycore.js');
const { isRelated, noticeAll, prefsOf } = await import('./notifyprefs.js');
Object.assign(settings.game, { dayMs: 60_000, tickMs: 1000, rivalsEnabled: false, ipoEveryDays: 0, agmEnabled: false });
settings.news.enabled = false;
async function setup(names = ['Hinweis']) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-notif-'));
const sent = [];
const g = new Game({ store: new Store(dir), now: () => Date.now(), sendTo: (pid, m) => sent.push({ pid, m }) });
const ps = [];
for (const name of names) { const p = (await g.register({ name, password: 'geheim123' })).player; p.inbox = []; ps.push(p); }
sent.length = 0;
return { g, ps, sent };
}
const at = (hh, mm = 0) => Date.UTC(2026, 5, 1, hh, mm); // Juni: Europe/Berlin = UTC+2
test('Benachrichtigungen: Vorgaben und Prüfung der Eingaben', () => {
const d = defaultPrefs();
assert.equal(d.scope.interview, 'related');
assert.equal(d.c.interview.inbox, false);
assert.equal(d.c.alert.taby, true);
assert.equal(d.c.corporate.taby, undefined, 'Taby nur, wo angeboten');
const n = normPrefs({
c: { politics: { inbox: false, toast: 'ja' }, corporate: { taby: true }, gibtsnicht: { inbox: true }, __proto__: { x: 1 } },
scope: { interview: 'irgendwas' },
quiet: { on: true, from: '25:00', to: '06:30', tz: 'Mond/Basis', toast: 1 },
});
assert.equal(n.c.politics.inbox, false);
assert.equal(n.c.politics.toast, d.c.politics.toast, 'kein Boolean → Vorgabe');
assert.equal(n.c.corporate.taby, undefined);
assert.equal(n.c.gibtsnicht, undefined);
assert.equal(n.scope.interview, 'related');
assert.deepEqual(n.quiet, { on: true, from: '22:00', to: '06:30', tz: null, toast: false });
assert.equal(normPrefs({ quiet: { on: true, tz: 'Europe/Berlin' } }).quiet.tz, 'Europe/Berlin');
assert.equal(kindCat('tournament'), 'battle');
assert.equal(kindCat('margin'), null, 'Pflichtmeldung');
for (const c of NOTIF_CATS) assert.ok(c.ch.every((k) => typeof c.def[k] === 'boolean'), `Vorgabe für ${c.k}`);
});
test('Benachrichtigungen: Ruhezeit über Mitternacht in der Zeitzone des Spielers', () => {
const q = { on: true, from: '22:00', to: '07:00', tz: 'Europe/Berlin' };
assert.equal(inQuiet(q, at(21, 30)), true, '23:30 Berlin');
assert.equal(inQuiet(q, at(4, 59)), true, '06:59 Berlin');
assert.equal(inQuiet(q, at(5, 0)), false, '07:00 Berlin');
assert.equal(inQuiet(q, at(12)), false);
assert.equal(inQuiet({ ...q, from: '13:00', to: '15:00' }, at(12)), true, '14:00 Berlin');
assert.equal(inQuiet({ ...q, on: false }, at(21, 30)), false);
assert.equal(inQuiet({ ...q, from: '08:00', to: '08:00' }, at(6)), false, 'gleiche Zeiten = keine Ruhezeit');
assert.equal(inQuiet({ on: true, from: '22:00', to: '07:00', tz: null }, at(21, 30), 'Europe/Berlin'), true, 'Ersatz-Zeitzone');
const pr = normPrefs({ quiet: q });
const r = route(pr, 'alert', { now: at(22) });
assert.deepEqual([r.inbox, r.toast, r.push, r.taby, r.quiet], [true, true, false, false, true], 'Posteingang sammelt weiter, Push/Taby schweigen');
assert.equal(route(normPrefs({ quiet: { ...q, toast: true } }), 'alert', { now: at(22) }).toast, false);
assert.equal(route(pr, 'alert', { now: at(10) }).push, true);
const sys = route(pr, null, { now: at(22) });
assert.deepEqual([sys.inbox, sys.push], [true, true], 'Pflichtmeldungen ignorieren die Ruhezeit');
});
test('Benachrichtigungen: Posteingang folgt den Einstellungen, Pflichtmeldungen kommen immer', async () => {
const { g, ps: [p], sent } = await setup();
const off = { c: Object.fromEntries(NOTIF_CATS.map((c) => [c.k, Object.fromEntries(c.ch.map((k) => [k, false]))])) };
await g.updateProfile(p, { notifyPrefs: { c: { politics: { inbox: false, toast: true, push: false } } } });
assert.equal(g.profile(p).notifyPrefs.c.politics.inbox, false, 'Profil liefert die Einstellungen');
g.notify(p.id, { kind: 'politics', title: 'Abstimmung beendet: angenommen', text: 'Testgesetz' });
assert.equal(p.inbox.length, 0, 'nicht im Posteingang');
assert.equal(sent.at(-1).m.type, 'notice', 'aber als Hinweis in der App');
assert.deepEqual([sent.at(-1).m.ch.toast, sent.at(-1).m.ch.push], [true, false]);
await g.updateProfile(p, { notifyPrefs: off });
sent.length = 0;
assert.equal(g.social.notify(p.id, { kind: 'corporate', title: 'Aktiensplit', text: 'x' }), false);
assert.equal(sent.length, 0, 'alles aus: gar nichts');
g.notify(p.id, { kind: 'margin', title: 'Margin Call', text: 'y' });
g.notify(p.id, { kind: 'order', title: 'Order gelöscht' });
assert.equal(p.inbox.length, 2, 'Pflichtmeldungen landen trotzdem im Posteingang');
assert.ok(sent.every((s) => s.m.type === 'inbox' && s.m.ch.inbox));
await assert.rejects(() => g.updateProfile(p, { notifyPrefs: 'alles' }), /Ungültige/);
});
test('Benachrichtigungen: Live-Hinweise (Erfolg, Battle) bringen die erlaubten Wege mit', async () => {
const { g, ps: [p], sent } = await setup();
g.sendTo(p.id, { type: 'achievement', achievement: { id: 'x' } });
assert.equal(sent.at(-1).m.ch.toast, true);
await g.updateProfile(p, { notifyPrefs: { c: { achievement: { toast: false }, battle: { push: false } } } });
g.sendTo(p.id, { type: 'levelUp', level: 3 });
assert.equal(sent.at(-1).m.ch.toast, false);
g.sendTo(p.id, { type: 'battleInvite', invite: {} });
assert.deepEqual([sent.at(-1).m.ch.toast, sent.at(-1).m.ch.push], [true, false]);
g.sendTo(p.id, { type: 'fill', qty: 1 });
assert.equal(sent.at(-1).m.ch, undefined, 'andere Nachrichten unverändert');
});
test('Benachrichtigungen: „Interview erschienen“ standardmäßig nur bei Bezug zum Rivalen', async () => {
const { g, ps: [rival, fan, holder, fremd, alle], sent } = await setup(['Rivalo', 'Fanny', 'Halter', 'Fremder', 'Allesleser']);
rival.rival = { char: Object.keys((await import('./rivals.js')).CHARACTERS)[0] };
await g.social.follow(fan, 'Rivalo');
const sym = [...g.stocks.keys()][0];
holder.positions[sym] = { qty: 10, avg: 50 };
await g.updateProfile(alle, { notifyPrefs: { scope: { interview: 'all' } } });
assert.equal(isRelated(g, fan, rival.id), true);
assert.equal(isRelated(g, holder, rival.id, sym), true);
assert.equal(isRelated(g, fremd, rival.id, sym), false);
g.bat.list.push({ id: 'bt1', a: fremd.id, b: rival.id, status: 'done' });
assert.equal(isRelated(g, fremd, rival.id), true, 'früheres Battle zählt');
g.bat.list.pop();
sent.length = 0;
const n = noticeAll(g, { kind: 'interview', title: 'Wirtschaftsblatt: Test', text: 'Rivalo im Interview.', action: { interview: 'iv1' }, rel: { id: rival.id, symbol: sym } });
const to = new Set(sent.filter((s) => s.m.type === 'notice').map((s) => s.pid));
assert.deepEqual([...to].sort(), [fan.id, holder.id, alle.id].sort());
assert.equal(n, 3);
assert.ok(!to.has(rival.id), 'Rivalen selbst bekommen keine Hinweise');
assert.equal(fan.inbox.length, 0, 'Interviews standardmäßig nicht im Posteingang');
// echtes Interview über stage.js
sent.length = 0;
g.stage.interview(rival, 7.5);
assert.ok(sent.some((s) => s.pid === fan.id && s.m.type === 'notice' && s.m.item.kind === 'interview' && s.m.item.action?.interview));
assert.ok(!sent.some((s) => s.pid === fremd.id && s.m.type === 'notice'));
});
test('Benachrichtigungen: neue Zeitungsausgabe nur einmal je Handelstag als Hinweis', async () => {
const { g, ps: [a, b], sent } = await setup(['Leser', 'Nichtleser']);
await g.updateProfile(b, { notifyPrefs: { c: { paper: { toast: false, push: false, inbox: false } } } });
await g.updateProfile(a, { notifyPrefs: { c: { paper: { toast: true, inbox: true } } } });
sent.length = 0;
g.press.publish({ kind: 'abend' });
const notes = sent.filter((s) => (s.m.type === 'notice' || s.m.type === 'inbox') && s.m.item.kind === 'paper');
assert.deepEqual(notes.map((s) => s.pid), [a.id]);
assert.equal(a.inbox[0].kind, 'paper');
assert.ok(a.inbox[0].action.paper);
sent.length = 0;
g.press.publish({ kind: 'abend' }); // gleiche Ausgabe neu gesetzt
assert.equal(sent.filter((s) => s.m.item?.kind === 'paper').length, 0, 'Neufassung desselben Tages ohne neuen Hinweis');
assert.equal(prefsOf(b).c.paper.toast, false);
});