// 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 };")(); // 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(""), ""); console.log("test_user: ok");