// Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Run: node desktop/modules/user/test_user.js // User.js is a QML library, so its .pragma line is stripped before node // evaluates it. const fs = require("fs"); const assert = require("assert"); const src = fs.readFileSync(__dirname + "/User.js", "utf8") .replace(/^\.pragma library$/m, ""); const U = new Function(src + "\nreturn { parse, keys, locales, gitName, places, greeting };")(); // parse: key=value lines; a value may itself hold "=". assert.deepStrictEqual(U.parse("name=Jane Doèr\nlocation=a=b\n\n"), { name: "Jane Doèr", location: "a=b" }); assert.deepStrictEqual(U.parse(""), {}); // userctl get prints a lone error= line when AccountsService cannot be read. assert.deepStrictEqual(U.parse("error=boom\n"), { error: "boom" }); // keys: userctl keys lines into {fpr, uid}. assert.deepStrictEqual(U.keys("AAAA\tJane \nBBBB\tWork\n"), [{ fpr: "AAAA", uid: "Jane " }, { fpr: "BBBB", uid: "Work" }]); // locales: UTF-8 entries of locale -a, spelled as AccountsService stores // them; C and POSIX left out; sorted. assert.deepStrictEqual(U.locales("C\nC.utf8\nPOSIX\nit_IT.utf8\nen_US.UTF-8\nde_DE\nfr_FR.utf8\n"), ["en_US.UTF-8", "fr_FR.UTF-8", "it_IT.UTF-8"]); // gitName: the same rule as userctl's gitname. assert.strictEqual(U.gitName("Jane Doèr"), "Jane D."); assert.strictEqual(U.gitName("Anna Maria Rossi"), "Anna Maria R."); assert.strictEqual(U.gitName("Cher"), "Cher"); assert.strictEqual(U.gitName("Ève Àlba"), "Ève À."); assert.strictEqual(U.gitName(" Jane Doèr "), "Jane D."); assert.strictEqual(U.gitName(""), ""); // places: Nominatim jsonv2 results as "place, region, country", the most // specific settlement name first, duplicates and repeated parts dropped. const nominatim = JSON.stringify([ { name: "Roma", address: { city: "Roma", state: "Lazio", country: "Italia" } }, { name: "Rome", address: { town: "Rome", county: "Floyd County", state: "Georgia", country: "United States" } }, { name: "Roma", address: { city: "Roma", state: "Lazio", country: "Italia" } }, { name: "Monaco", address: { city: "Monaco", country: "Monaco" } }, { name: "Piazza", address: { village: "Piazza", country: "Italia" } }, { name: "Somewhere" }, ]); assert.deepStrictEqual(U.places(nominatim), [ "Roma, Lazio, Italia", "Rome, Georgia, United States", "Monaco", "Piazza, Italia", "Somewhere", ]); assert.deepStrictEqual(U.places(""), []); assert.deepStrictEqual(U.places("rate limited"), []); // greeting: comments and blanks skipped, {user} filled, never the same twice // running, and the built-in set when the file has nothing. assert.strictEqual(U.greeting("# c\n\n Yo {user} \n", "jd", 0), "Yo jd"); assert.strictEqual(U.greeting("a\nb", "jd", 0), "b"); assert.strictEqual(U.greeting("a\nb", "jd", 0), "a"); assert.ok(U.greeting("", "jd", 0).endsWith("jd")); console.log("test_user: ok");