/* * 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/settings.js: validating and normalizing user * settings, cleaning up the stored per-identity profiles, and resolving the * effective settings for an identity. Pure functions, called directly with * plain objects; no DOM or WebExtension API is needed. Some invalid inputs * are arbitrary German words (e.g. "blau", "unbekannt"); only their * invalidity matters. */ const { test } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const settings = require("../src/lib/settings.js"); const { DEFAULT_SETTINGS, sanitizeSettings, normalizeStore, resolveSettings } = settings; test("empty input yields the defaults", () => { assert.deepEqual(sanitizeSettings(undefined), { ...DEFAULT_SETTINGS }); assert.deepEqual(sanitizeSettings("quatsch"), { ...DEFAULT_SETTINGS }); }); test("valid values are kept, colors are lower-cased", () => { const result = sanitizeSettings({ template: "{name}:", markerColor: "#AABBCC", textBold: true, splitMode: "manual", }); assert.equal(result.template, "{name}:"); assert.equal(result.markerColor, "#aabbcc"); assert.equal(result.textBold, true); assert.equal(result.splitMode, "manual"); }); test("invalid values fall back to the defaults, unknown keys are dropped", () => { const result = sanitizeSettings({ template: " ", markerColor: "blau", textColor: "#12345", textBold: "ja", splitMode: "irgendwie", unbekannt: 42, }); assert.equal(result.template, DEFAULT_SETTINGS.template); assert.equal(result.markerColor, DEFAULT_SETTINGS.markerColor); assert.equal(result.textColor, DEFAULT_SETTINGS.textColor); assert.equal(result.textBold, DEFAULT_SETTINGS.textBold); assert.equal(result.splitMode, DEFAULT_SETTINGS.splitMode); assert.equal("unbekannt" in result, false); }); test("a template that is too long is truncated", () => { const result = sanitizeSettings({ template: "x".repeat(500) }); assert.equal(result.template.length, settings.MAX_TEMPLATE_LENGTH); }); test("normalizeStore drops broken profiles", () => { const store = normalizeStore({ global: { template: "[G]:" }, identities: { id1: { template: "[A]:" }, id2: null, id3: "x" }, }); assert.equal(store.global.template, "[G]:"); assert.deepEqual(Object.keys(store.identities), ["id1"]); }); test("resolveSettings: an identity's own profile wins over the global settings", () => { const stored = { global: { template: "[G]:" }, identities: { id1: { template: "[A]:" } }, }; assert.equal(resolveSettings(stored, "id1").template, "[A]:"); assert.equal(resolveSettings(stored, "id2").template, "[G]:"); assert.equal(resolveSettings(stored, null).template, "[G]:"); assert.equal( resolveSettings(undefined, "id1").template, DEFAULT_SETTINGS.template, ); }); test("options page template field uses MAX_TEMPLATE_LENGTH", () => { const html = fs.readFileSync( path.join(__dirname, "..", "src", "options", "options.html"), "utf8", ); const match = html.match(/id="template"[^>]*maxlength="(\d+)"/s); assert.ok(match, "maxlength of the template field not found"); assert.equal(Number(match[1]), settings.MAX_TEMPLATE_LENGTH); });