aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/companion/test_lines.js
blob: f70a9ff58a7793a4670687d62228b57be64810c1 (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
// 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/companion/test_lines.js
// Lines.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 + "/Lines.js", "utf8")
    .replace(/^\.pragma library$/m, "");
const L = new Function(src +
    "\nreturn { lines, apps, monologue, appMap, appList, pickApp, pick, toMap };")();

// The defaults are what she says until a set is saved.
assert.strictEqual(L.pickApp("firefox", null, 0), L.apps.firefox[0]);
assert.strictEqual(L.pickApp("firefox", null, 0.99), L.apps.firefox[L.apps.firefox.length - 1]);
assert.strictEqual(L.pickApp("nope", null, 0), "");
assert.strictEqual(L.appMap(null), L.apps);
// A hand edit with a typo reads as the defaults, not as silence.
assert.strictEqual(L.appMap("typo"), L.apps);
assert.strictEqual(L.appMap(["not", "a", "map"]), L.apps);

// A saved set replaces the defaults, class by class.
assert.strictEqual(L.pickApp("firefox", { firefox: ["hi"] }, 0), "hi");
assert.strictEqual(L.pickApp("kitty", { firefox: ["hi"] }, 0), "");
assert.strictEqual(L.pick("app:firefox", 0, { firefox: ["custom"] }), "custom");
assert.strictEqual(L.pick("app:firefox", 0), L.apps.firefox[0]);

// Empty phrases are skipped when picking; a class with only empty ones says
// nothing.
assert.strictEqual(L.pickApp("firefox", { firefox: ["", "  ", "only"] }, 0), "only");
assert.strictEqual(L.pickApp("firefox", { firefox: ["", "  "] }, 0), "");

// Reactions still come from lines.
assert.strictEqual(L.pick("shown", 0), L.lines.shown[0]);
assert.strictEqual(L.pick("nope", 0), "");

// The saved set as an ordered list, for the settings page.
const list = L.appList(null);
assert.strictEqual(list.length, Object.keys(L.apps).length);
assert.deepStrictEqual(list[0], { cls: "firefox", lines: L.apps.firefox.slice() });
assert.deepStrictEqual(L.appList({ kitty: ["a"] }), [{ cls: "kitty", lines: ["a"] }]);

// Back to a map: trim, lowercase the class, drop empty classes and empty
// phrases, and a repeated class keeps its last entry.
assert.deepStrictEqual(L.toMap([
    { cls: "  Firefox ", lines: [" one ", "", "  ", "two"] },
    { cls: "", lines: ["dropped"] },
    { cls: "kitty", lines: ["", " "] },
    { cls: "firefox", lines: ["last"] },
]), { firefox: ["last"] });

console.log("ok");