import test from 'node:test' import assert from 'node:assert/strict' import { loadModule } from './load.mjs' // Kunden-Reiter "Funktionen": Register der Funktionen (News, Terminplaner), Aktionen je Zustand, Antwort einarbeiten const { FUNKTIONEN, ZUSTAND, funktionOf, aktionen, mergeFunktion, siteKeyOf } = await loadModule('src/lib/funktionenView.js') test('FUNKTIONEN: News und Terminplaner mit den Bestätigungstexten', () => { assert.deepEqual(FUNKTIONEN, [ { key: 'news', label: 'News', bestaetigen: { activate: 'News ohne Zahlung aktivieren?', disable: 'News abschalten? Die Website zeigt dann keine Beiträge mehr.', }, }, { key: 'termine', label: 'Terminplaner', bestaetigen: { activate: 'Terminplaner ohne Zahlung aktivieren?', disable: 'Terminplaner abschalten? Auf der Website sind dann keine Buchungen mehr möglich.', }, }, ]) }) test('ZUSTAND: Ton und Beschriftung je Zustand', () => { assert.deepEqual(ZUSTAND, { off: ['muted', 'Aus'], ordered: ['warn', 'Bestellt'], setup: ['info', 'Einrichtung'], active: ['ok', 'Aktiv'], disabled: ['muted', 'Abgeschaltet'], }) }) test('funktionOf: Ansicht der Funktion, ohne Eintrag "off"', () => { const project = { projectId: 'p1', features: { news: { state: 'active', ticketWoid: '100' }, termine: { state: 'setup' } } } assert.deepEqual(funktionOf(project, 'news'), { state: 'active', ticketWoid: '100' }) assert.deepEqual(funktionOf(project, 'termine'), { state: 'setup' }) assert.deepEqual(funktionOf({ projectId: 'p2', features: { news: { state: 'active' } } }, 'termine'), { state: 'off' }) assert.deepEqual(funktionOf({ projectId: 'p3' }, 'news'), { state: 'off' }) assert.deepEqual(funktionOf(null, 'news'), { state: 'off' }) }) test('aktionen: Aktivieren außer bei "active", Abschalten bei Bestellt, Einrichtung und Aktiv', () => { assert.deepEqual(aktionen('off'), ['activate']) assert.deepEqual(aktionen('ordered'), ['activate', 'disable']) assert.deepEqual(aktionen('setup'), ['activate', 'disable']) assert.deepEqual(aktionen('active'), ['disable']) assert.deepEqual(aktionen('disabled'), ['activate']) assert.deepEqual(aktionen(undefined), ['activate']) }) const projects = () => [ { projectId: 'p1', projectName: 'Eins', features: { news: { state: 'off' }, termine: { state: 'active', siteKey: 'abc' } } }, { projectId: 'p2', projectName: 'Zwei', features: { news: { state: 'active' }, termine: { state: 'off' } } }, ] test('mergeFunktion: übernimmt nur die gewählte Funktion, die andere bleibt', () => { const antwort = { projectId: 'p1', projectName: 'Eins (neu)', features: { news: { state: 'setup', ticketWoid: '7' }, termine: { state: 'off' } }, } const merged = mergeFunktion(projects(), 'p1', 'news', antwort) assert.deepEqual(merged[0].features.news, { state: 'setup', ticketWoid: '7' }) assert.deepEqual(merged[0].features.termine, { state: 'active', siteKey: 'abc' }) assert.equal(merged[0].projectName, 'Eins') assert.deepEqual(merged[1], projects()[1]) }) test('mergeFunktion: Terminplaner überschreibt News nicht', () => { const antwort = { projectId: 'p2', features: { news: { state: 'off' }, termine: { state: 'ordered' } } } const merged = mergeFunktion(projects(), 'p2', 'termine', antwort) assert.deepEqual(merged[1].features, { news: { state: 'active' }, termine: { state: 'ordered' } }) }) test('mergeFunktion: verändert die Eingabe nicht, ohne Antwort oder ohne Funktion bleibt alles', () => { const list = projects() const merged = mergeFunktion(list, 'p1', 'news', { projectId: 'p1', features: { news: { state: 'active' } } }) assert.notEqual(merged, list) assert.deepEqual(list, projects()) assert.deepEqual(mergeFunktion(list, 'p1', 'news', undefined), projects()) assert.deepEqual(mergeFunktion(list, 'p1', 'news', { projectId: 'p1', features: {} }), projects()) assert.deepEqual(mergeFunktion(list, 'unbekannt', 'news', { features: { news: { state: 'active' } } }), projects()) }) test('mergeFunktion: Projekt ohne bisherige features bekommt die Funktion', () => { const merged = mergeFunktion([{ projectId: 'p9' }], 'p9', 'termine', { features: { termine: { state: 'setup' } } }) assert.deepEqual(merged, [{ projectId: 'p9', features: { termine: { state: 'setup' } } }]) }) test('siteKeyOf: nur der Terminplaner zeigt seinen Site-Key, nur wenn das Portal einen nennt', () => { assert.equal(siteKeyOf({ state: 'active', siteKey: 'abcdefghijklmnop' }, 'termine'), 'abcdefghijklmnop') assert.equal(siteKeyOf({ state: 'active', siteKey: ' abcdefghijklmnop ' }, 'termine'), 'abcdefghijklmnop', 'getrimmt') assert.equal(siteKeyOf({ state: 'active', siteKey: 'abcdefghijklmnop' }, 'news'), '', 'News: kein Site-Key in der Tabelle') for (const feature of [{ state: 'off' }, { state: 'active', siteKey: null }, { state: 'active', siteKey: '' }, { state: 'active', siteKey: 5 }, null, undefined]) { assert.equal(siteKeyOf(feature, 'termine'), '', JSON.stringify(feature)) } })