/* * Copyright 2026 Oliver Herb * SPDX-License-Identifier: MPL-2.0 * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ "use strict"; /* * Unit tests for src/lib/marker.js: initials from names and addresses, * template placeholders, the marker variables, and the comment node that is * inserted into the mail (structure, inline styles, escaping). The pure * string functions are called directly; the DOM functions run against a * jsdom document. */ const { test } = require("node:test"); const assert = require("node:assert/strict"); const { JSDOM } = require("jsdom"); const marker = require("../src/lib/marker.js"); const { sanitizeSettings } = require("../src/lib/settings.js"); const { createDocument } = require("./helpers/dom.js"); test("initials from the display name", () => { assert.equal(marker.initialsOf("Max Mustermann", ""), "MM"); assert.equal(marker.initialsOf("Jürgen Beispiel", ""), "JB"); assert.equal(marker.initialsOf("Hans-Peter Müller", ""), "HPM"); assert.equal(marker.initialsOf("Dr. Max Mustermann", ""), "MM"); assert.equal(marker.initialsOf("Dipl.-Ing. Anna Berg", ""), "AB"); assert.equal(marker.initialsOf("Oliver Herb (ACME)", ""), "OH"); assert.equal(marker.initialsOf("ölaf über", ""), "ÖÜ"); }); test("initials for 'Last name, First name' and a company suffix", () => { assert.equal(marker.initialsOf("Herb, Oliver", ""), "OH"); assert.equal(marker.initialsOf("Oliver Herb, Example Corp", ""), "OH"); }); test("initials from the address when there is no name", () => { assert.equal(marker.initialsOf("", "jane.doe@example.com"), "JD"); assert.equal(marker.initialsOf("", "info@example.com"), "I"); assert.equal(marker.initialsOf("", ""), "?"); assert.equal(marker.initialsOf(undefined, undefined), "?"); }); test("at most four initials", () => { assert.equal(marker.initialsOf("A B C D E F", ""), "ABCD"); }); test("placeholders are replaced, unknown ones are kept", () => { const vars = { name: "Max", initials: "MM", email: "m@example.com", date: "24.11.2026", time: "09:30", }; assert.equal(marker.renderTemplate("[{initials}]:", vars), "[MM]:"); assert.equal( marker.renderTemplate( "{name} <{email}> {date} {time} {initials}{initials}", vars, ), "Max 24.11.2026 09:30 MMMM", ); assert.equal(marker.renderTemplate("{foo} {name}", vars), "{foo} Max"); assert.equal(marker.renderTemplate("{name}", {}), "{name}"); }); test("markerVariables: name falls back to the address, date in the requested locale format", () => { const now = new Date(2026, 10, 24, 9, 5); const vars = marker.markerVariables( { name: "", email: "max@example.com" }, now, "de-DE", ); assert.equal(vars.name, "max@example.com"); assert.equal(vars.initials, "M"); assert.equal(vars.date, "24.11.2026"); assert.equal(vars.time, "09:05"); const none = marker.markerVariables(null, now, "de-DE"); assert.equal(none.initials, "?"); }); test("createCommentNode: structure, inline CSS only, id", () => { const { doc } = createDocument(""); const settings = sanitizeSettings({}); const node = marker.createCommentNode(doc, "[MM]:", "a", settings, "ic-1"); assert.equal(node.ownerDocument, doc); assert.equal(node.isConnected, false); assert.equal(node.localName, "div"); assert.deepEqual( [...node.attributes].map((attribute) => attribute.name), ["data-inline-comment", "style"], ); assert.equal(node.getAttribute("data-inline-comment"), "ic-1"); assert.equal(node.getAttribute("style"), marker.commentStyle(settings)); assert.match(node.getAttribute("style"), /^color:#0056b3;/); assert.equal(node.childNodes.length, 2); const [span, textNode] = node.childNodes; assert.equal(span.localName, "span"); assert.deepEqual( [...span.attributes].map((attribute) => attribute.name), ["data-inline-comment-marker", "style"], ); assert.equal(span.getAttribute("data-inline-comment-marker"), ""); assert.equal( span.getAttribute("style"), "color:#003d80;font-weight:bold;font-style:normal;", ); assert.equal(span.textContent, "[MM]:"); assert.equal(textNode.nodeType, doc.TEXT_NODE); assert.equal(textNode.data, "\u00a0a"); }); test("createCommentNode: outerHTML has the inline-CSS comment markup", () => { const { doc } = createDocument(""); const settings = sanitizeSettings({}); const html = marker.createCommentNode( doc, "[MM]:", "a b", settings, "ic-1", ).outerHTML; assert.equal( html, `
` + `[MM]:` + " a b
", ); assert.doesNotMatch(html, /class=|