From 5fabbeb09aea782b7df05e8827b552e5ec44cf83 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Fri, 16 Oct 2026 09:57:22 +0200 Subject: feat(user): helpers for the user page Parsing userctl's output and locale -a, and the git name preview, kept out of QML so node can test them. Co-Authored-By: Claude Sonnet 5.5 --- desktop/modules/user/test_user.js | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 desktop/modules/user/test_user.js (limited to 'desktop/modules/user/test_user.js') diff --git a/desktop/modules/user/test_user.js b/desktop/modules/user/test_user.js new file mode 100644 index 0000000..3ab22a1 --- /dev/null +++ b/desktop/modules/user/test_user.js @@ -0,0 +1,47 @@ +// 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"); -- cgit v1.2.3