import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync, writeFileSync, mkdtempSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import { aboMeldung } from '../server/services/featureOrder.js' import { featureConfig } from '../server/services/featureRegistry.js' // public/funktionen.js wirklich laden (die absoluten Importe /analyse-view.js und /geraete.js werden auf die Dateien umgeleitet) const dir = mkdtempSync(join(tmpdir(), 'fn-tiles-')) const fileUrl = (name) => pathToFileURL(new URL(`../public/${name}`, import.meta.url).pathname).href const src = readFileSync(new URL('../public/funktionen.js', import.meta.url), 'utf8') .replace("'/analyse-view.js'", `'${fileUrl('analyse-view.js')}'`) .replace("'/geraete.js'", `'${fileUrl('geraete.js')}'`) const file = join(dir, 'funktionen.mjs') writeFileSync(file, src) const { initFunktionen, initNews, initAnalyse, renderAnalyseOff } = await import(pathToFileURL(file).href) const news = { state: 'off' } const project = (id, analyse) => ({ projectId: id, projectName: `P ${id}`, liveUrl: '', features: { news }, analyse }) async function render(projects) { globalThis.fetch = async () => ({ ok: true, status: 200, json: async () => ({ projects }) }) const root = { innerHTML: '', querySelector: () => null } await initFunktionen(root) return root.innerHTML } const tile = (html, key) => html.slice(html.indexOf(`data-fn-tile="${key}"`), html.indexOf('', html.indexOf(`data-fn-tile="${key}"`))) test('Kacheln rendern ohne Fehler: News, Analyse comingSoon/aus/aktiv/gesperrt', async () => { const soon = await render([project('a', { comingSoon: true })]) assert.match(tile(soon, 'news'), /News-Pinnwand/) assert.match(soon, /Kommt bald/) assert.doesNotMatch(soon, /data-fn-tile="analyse"/) const off = await render([project('a', { state: 'off', eligible: true, devMode: false })]) assert.match(tile(off, 'analyse'), /Einrichten/) assert.doesNotMatch(off, /Dev-Modus/) const dev = await render([project('a', { state: 'active', eligible: true, devMode: true })]) assert.match(tile(dev, 'analyse'), /Auswertung ansehen/) assert.match(tile(dev, 'analyse'), /Im Dev-Modus wird nicht gemessen/) assert.doesNotMatch(tile(dev, 'news'), /Dev-Modus/) const live = await render([project('a', { state: 'active', eligible: true, devMode: false })]) assert.match(tile(live, 'analyse'), /Auswertung ansehen/) assert.doesNotMatch(live, /Dev-Modus/) const disabled = await render([project('a', { state: 'disabled' })]) assert.match(tile(disabled, 'analyse'), /Bitte melde dich bei uns/) assert.doesNotMatch(disabled, /Dev-Modus/) }) // ---- News-Kachel und -Buchungsseite: Preis und Zustimmungstext aus dem Register (features.news), Rückfall ohne features async function renderMit(projects, extra) { globalThis.fetch = async () => ({ ok: true, status: 200, json: async () => ({ projects, ...extra }) }) const root = { innerHTML: '', querySelector: () => null } await initFunktionen(root) return root.innerHTML } test('News-Kachel: Rückfall ohne features ⇒ bisheriger Preis 30 €', async () => { const html = await render([project('a', { comingSoon: true })]) assert.match(tile(html, 'news'), /Einrichten lassen · 30 €/) // auch bei leerem oder unvollständigem features-Objekt for (const features of [{}, { news: {} }, { news: { priceLabel: '' } }, { news: { priceLabel: 31 } }, { termine: { priceLabel: '149 €' } }]) { const rueck = await renderMit([project('a', { comingSoon: true })], { features }) assert.match(tile(rueck, 'news'), /Einrichten lassen · 30 €/, JSON.stringify(features)) } }) test('News-Kachel: Preis aus dem Register ⇒ „Einrichten lassen · 31 €“', async () => { const html = await renderMit([project('a', { comingSoon: true })], { features: { news: { priceLabel: '31 €' } } }) assert.match(tile(html, 'news'), /Einrichten lassen · 31 €/) assert.doesNotMatch(tile(html, 'news'), /30 €/) // der Preis kommt maskiert in die Kachel const bose = await renderMit([project('a', { comingSoon: true })], { features: { news: { priceLabel: '' } } }) assert.doesNotMatch(bose, / { calls.push({ url: String(url), method: opts?.method, body: opts?.body ? JSON.parse(opts.body) : undefined }) if (String(url) === '/api/funktionen/news/checkout') return { ok: true, status: 200, json: async () => ({ url: 'https://checkout.example.test/s1' }) } return { ok: true, status: 200, json: async () => antwort } } const stage = fakeStage() const root = fakeEl() root.querySelector = (sel) => (sel === '#news-stage' ? stage : null) try { await initNews(root, { projectId: 'p1' }) // Haken setzen und bestellen const box = stage.els['#news-consent'] box.checked = true box.handlers.change() const button = stage.els['#news-order'] const knopfAktiv = button.disabled === false await button.handlers.click() return { stage, calls, knopfAktiv, href: globalThis.window.location.href } } finally { delete globalThis.window } } test('News-Buchungsseite: Preis, Knopf und Zustimmungstext aus features.news, sendet weiter consent: true', async () => { const register = { priceLabel: '31 €', consent: { version: 'v', knopf: 'Jetzt kaufen · 31 €', items: [{ id: 'beginn', text: 'Registertext der Zustimmung', haken: true }] } } const { stage, calls, knopfAktiv, href } = await renderBuchung({ projects: [freiesProjekt], setupEstimate: 'meist innerhalb von 1–2 Wochen', consentText: 'Alter Text', features: { news: register }, }) assert.match(stage.innerHTML, /31 €<\/strong>/) assert.match(stage.innerHTML, /inklusive Mehrwertsteuer/, 'der Wortlaut bleibt bis Block F') assert.match(stage.innerHTML, /id="news-order" disabled>Jetzt kaufen · 31 € c.url === '/api/funktionen/news/checkout') assert.equal(bestellung.length, 1) assert.equal(bestellung[0].method, 'POST') assert.deepEqual(bestellung[0].body, { projectId: 'p1', consent: true }) assert.equal(href, 'https://checkout.example.test/s1') }) test('News-Buchungsseite: Rückfall ohne features ⇒ 30 €, Standardknopf, consentText der Antwort', async () => { const { stage, calls } = await renderBuchung({ projects: [freiesProjekt], setupEstimate: '', consentText: 'Alter Text' }) assert.match(stage.innerHTML, /30 €<\/strong>/) assert.match(stage.innerHTML, /id="news-order" disabled>Zahlungspflichtig bestellen · 30 € c.url === '/api/funktionen/news/checkout').body, { projectId: 'p1', consent: true }) }) test('News-Buchungsseite: Preis ohne Knopftext ⇒ Standardknopf mit Registerpreis, Knopftext kommt maskiert', async () => { const nurPreis = await renderBuchung({ projects: [freiesProjekt], consentText: 'Alter Text', features: { news: { priceLabel: '31 €' } } }) assert.match(nurPreis.stage.innerHTML, /id="news-order" disabled>Zahlungspflichtig bestellen · 31 €', items: [] } } } }) assert.doesNotMatch(bose.stage.innerHTML, / { assert.match(src, /request\('\/api\/funktionen\/news\/checkout',\s*\{[^}]*body:\s*\{[^}]*consent: true/) }) // ---- Terminplaner-Kachel: nur mit Schalter (termine.verfuegbar), Zustand je Projekt aus features.termine const withTermine = (id, state, extra = {}) => ({ projectId: id, projectName: `P ${id}`, liveUrl: '', features: { news, termine: { state, ...extra } } }) const noAnalyse = { comingSoon: true } // Standardantwort bei verfuegbar: true wie GET /api/funktionen (Kunde mit Wartungs-Abo); abo ueberschreibt einzelne Felder const ABO_OK = { bestellbar: true, grund: null, preis: '149 €', einrichtung: 'meist innerhalb von 1–2 Wochen' } async function renderTermine(projects, { verfuegbar, anfragen = {}, abo = {} } = {}) { const calls = [] globalThis.fetch = async (url) => { calls.push(String(url)) if (String(url).startsWith('/api/termine/einstellungen')) { const id = new URL(url, 'http://x').searchParams.get('projectId') return { ok: true, status: 200, json: async () => ({ anfragenOffen: anfragen[id] ?? 0 }) } } const termine = verfuegbar === undefined ? {} : { termine: verfuegbar ? { verfuegbar, ...ABO_OK, ...abo } : { verfuegbar } } return { ok: true, status: 200, json: async () => ({ projects, ...termine }) } } const count = { textContent: '', hidden: true } const root = { innerHTML: '', querySelector: (sel) => (sel === '[data-termine-count]' ? count : null) } await initFunktionen(root) return { html: root.innerHTML, count, calls } } test('Terminplaner-Kachel: fehlt termine.verfuegbar oder ist false ⇒ „Kommt bald“ ohne Knopf', async () => { for (const verfuegbar of [undefined, false]) { const { html, calls } = await renderTermine([{ ...withTermine('a', 'active'), analyse: noAnalyse }], { verfuegbar }) assert.doesNotMatch(html, /data-fn-tile="termine"/) assert.match(html, /fn-tile-soon[\s\S]*Terminplaner/) assert.match(html, /Kommt bald/) assert.doesNotMatch(html, /data-view="termine"/) assert.deepEqual(calls, ['/api/funktionen'], 'ohne Schalter keine Abfrage an /api/termine') } }) // Die Abo-Hinweise der Kachel kommen aus aboMeldung (server/services/featureOrder.js, einzige Quelle der Abo-Hinweistexte); // die Kachel steht im Browser und kennt den Text nur als Kopie: dieser Test haelt beide gleich. const TERMINE_REGISTER = featureConfig('termine') const KEIN_ABO_HINWEIS = /Im Wartungs-Abo enthalten\.