From 79408ae88bccf691106ce8f7c18f4be107ad3c4e Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 26 Nov 2026 11:08:49 +0100 Subject: feat(mimeapps): parse and edit mimeapps.list Pure functions over the file text for the default apps page: read [Default Applications], set or remove one type, and map the .desktop files' MimeType= lines to desktop ids. Every edit touches one line and leaves [Added Associations], comments and blank lines as they were; a remove keeps the section header so the text written is never empty. The spec's stale-app check moves to DesktopEntries.byId: an installed app without a MimeType= line is absent from the scan. Co-Authored-By: Claude Opus 5.5 --- desktop/modules/mimeapps/test_mimeapps.js | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 desktop/modules/mimeapps/test_mimeapps.js (limited to 'desktop/modules/mimeapps/test_mimeapps.js') diff --git a/desktop/modules/mimeapps/test_mimeapps.js b/desktop/modules/mimeapps/test_mimeapps.js new file mode 100644 index 0000000..469c9a4 --- /dev/null +++ b/desktop/modules/mimeapps/test_mimeapps.js @@ -0,0 +1,69 @@ +// 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/mimeapps/test_mimeapps.js +// MimeApps.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 + "/MimeApps.js", "utf8").replace(/^\.pragma library$/m, ""); +const M = new Function(src + "\nreturn { ROLES, parse, set, remove, scan, dirs, role, handlers, allMimes };")(); + +// Added Associations is not read as defaults; comments, blank lines and +// other sections survive every edit byte for byte. +const file = "[Added Associations]\ntext/plain=qpdfview.desktop;\n\n" + + "[Default Applications]\n# browser\ntext/html=firefox.desktop\ntext/plain=featherpad.desktop;okular.desktop;\n\n" + + "[Other]\nk=v\n"; +assert.deepStrictEqual(M.parse(file), { "text/html": "firefox.desktop", "text/plain": "featherpad.desktop" }); +assert.deepStrictEqual(M.parse(""), {}); + +// set: in place, or after the section's last entry (before its blank line), +// or a new section at the end, or the section alone in an empty file. +assert.strictEqual(M.set(file, "text/html", "chromium.desktop"), + file.replace("text/html=firefox.desktop", "text/html=chromium.desktop;")); +assert.strictEqual(M.set(file, "image/png", "lximage-qt.desktop"), + file.replace("okular.desktop;\n", "okular.desktop;\nimage/png=lximage-qt.desktop;\n")); +assert.strictEqual(M.set("[Added Associations]\na/b=x.desktop;\n", "a/b", "y.desktop"), + "[Added Associations]\na/b=x.desktop;\n\n[Default Applications]\na/b=y.desktop;\n"); +assert.strictEqual(M.set("", "a/b", "y.desktop"), "[Default Applications]\na/b=y.desktop;\n"); +// A key that only starts with the type is a different key. +assert.strictEqual(M.set("[Default Applications]\ntext/plainx=a.desktop;\n", "text/plain", "b.desktop"), + "[Default Applications]\ntext/plainx=a.desktop;\ntext/plain=b.desktop;\n"); + +// remove: only that line; the header stays, so the text is never empty. +assert.strictEqual(M.remove(file, "text/html"), file.replace("text/html=firefox.desktop\n", "")); +assert.strictEqual(M.remove(file, "nope/x"), file); +assert.strictEqual(M.remove("[Default Applications]\na/b=y.desktop;\n", "a/b"), "[Default Applications]\n"); + +// dirs: the user's first, trailing slashes dropped. +const dirs = M.dirs("/home/you", "", "/usr/local/share/:/usr/share"); +assert.deepStrictEqual(dirs, ["/home/you/.local/share/applications", "/usr/local/share/applications", "/usr/share/applications"]); +assert.deepStrictEqual(M.dirs("/home/you", "/x", ""), ["/x/applications", "/usr/local/share/applications", "/usr/share/applications"]); + +// scan: the first dir wins for one id; a subdirectory joins with "-". +const out = "/home/you/.local/share/applications/firefox.desktop:MimeType=text/html;\n" + + "/usr/share/applications/firefox.desktop:MimeType=text/html;image/png;\n" + + "/usr/share/applications/kde4/okular.desktop:MimeType=application/pdf;\n"; +assert.deepStrictEqual(M.scan(out, dirs), { "firefox.desktop": ["text/html"], "kde4-okular.desktop": ["application/pdf"] }); +assert.deepStrictEqual(M.scan("", dirs), {}); + +// role: the app most types point at, and how many distinct apps. +assert.deepStrictEqual(M.role({ a: "x.desktop", b: "y.desktop", c: "x.desktop" }, ["a", "b", "c", "d"]), { id: "x.desktop", apps: 2 }); +assert.deepStrictEqual(M.role({}, ["a"]), { id: "", apps: 0 }); + +assert.deepStrictEqual(M.handlers({ "f.desktop": ["text/html"], "o.desktop": ["application/pdf"] }, ["text/html", "x/y"]), ["f.desktop"]); +assert.deepStrictEqual(M.allMimes({ f: ["b", "a"], g: ["a"] }), ["a", "b"]); + +for (const r of M.ROLES) assert.ok(r.label && r.mimes.length > 0, r.label); + +console.log("ok"); -- cgit v1.2.3