// 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"); // Duplicate keys: parse takes the last; set edits the first and drops the rest; remove drops all. const dup = "[Default Applications]\ntext/html=a.desktop;\ntext/html=b.desktop;\n"; assert.strictEqual(M.parse(dup)["text/html"], "b.desktop"); assert.strictEqual(M.set(dup, "text/html", "c.desktop"), "[Default Applications]\ntext/html=c.desktop;\n"); assert.strictEqual(M.remove(dup, "text/html"), "[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), {}); // A CRLF .desktop file leaves a \r on the line; it is not a mime type. assert.deepStrictEqual(M.scan("/usr/share/applications/a.desktop:MimeType=a/b;c/d;\r\n", dirs), { "a.desktop": ["a/b", "c/d"] }); // 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");