aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/mimeapps/test_mimeapps.js
blob: 469c9a42c44ae272404dbb1d904c7cc41ae3b6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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/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");