aboutsummaryrefslogtreecommitdiffstats
path: root/desktop
diff options
context:
space:
mode:
Diffstat (limited to 'desktop')
-rw-r--r--desktop/modules/user/User.js46
-rw-r--r--desktop/modules/user/test_user.js47
2 files changed, 93 insertions, 0 deletions
diff --git a/desktop/modules/user/User.js b/desktop/modules/user/User.js
new file mode 100644
index 0000000..8160b7f
--- /dev/null
+++ b/desktop/modules/user/User.js
@@ -0,0 +1,46 @@
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// 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.
+
+.pragma library
+
+// Pure helpers for the user page, tested by test_user.js under node.
+
+// `userctl get` output into an object.
+function parse(text) {
+ const out = {};
+ for (const line of text.split("\n")) {
+ const i = line.indexOf("=");
+ if (i > 0) out[line.slice(0, i)] = line.slice(i + 1);
+ }
+ return out;
+}
+
+// `userctl keys` output into [{fpr, uid}].
+function keys(text) {
+ return text.split("\n").filter(l => l.includes("\t"))
+ .map(l => ({ fpr: l.slice(0, l.indexOf("\t")), uid: l.slice(l.indexOf("\t") + 1) }));
+}
+
+// `locale -a` prints it_IT.utf8; AccountsService stores it_IT.UTF-8.
+function locales(text) {
+ return text.split("\n")
+ .filter(l => /\.utf-?8$/i.test(l) && !/^C\./.test(l))
+ .map(l => l.replace(/\.utf-?8$/i, ".UTF-8"))
+ .sort();
+}
+
+// The name git gets, for the page's preview. ponytail: duplicates userctl's
+// gitname; both tests pin the same cases, keep them in step.
+function gitName(name) {
+ const w = name.trim().split(/\s+/).filter(s => s !== "");
+ if (w.length < 2) return w.join(" ");
+ return w.slice(0, -1).join(" ") + " " + Array.from(w[w.length - 1])[0] + ".";
+}
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. <danix@danix.xyz>
+//
+// 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 <j@example.org>\nBBBB\tWork\n"),
+ [{ fpr: "AAAA", uid: "Jane <j@example.org>" }, { 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");