aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/assistant/test_ops.js
blob: c0162412159add27201706f10d8396a701e1b190 (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
// 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/assistant/test_ops.js
// Ops.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 + "/Ops.js", "utf8")
    .replace(/^\.pragma library$/m, "");
const O = new Function(src + "\nreturn { offered, COMPRESS };")();

const f = (name, kind) => ({ path: "/x/" + name, name, kind });
const labels = files => O.offered(files).map(o => o.label);

assert.deepStrictEqual(labels([f("a.pdf", "pdf")]), ["To .md", "To images", "Compress…"]);
assert.deepStrictEqual(labels([f("a.pdf", "pdf"), f("b.pdf", "pdf")]),   // To .md: one PDF only
                       ["To images", "Merge", "Compress…"]);
assert.deepStrictEqual(labels([f("a.pdf", "pdf"), f("p.png", "image")]), ["Compress…"]);
// An image's own format is not offered.
assert.deepStrictEqual(labels([f("p.png", "image")]), ["To JPEG", "To WebP", "Resize 50%", "Compress…"]);
assert.deepStrictEqual(labels([f("p.JPG", "image")]), ["To PNG", "To WebP", "Resize 50%", "Compress…"]);
// Mixed formats: offered while any file would change.
assert.deepStrictEqual(labels([f("p.png", "image"), f("q.jpg", "image")]),
                       ["To PNG", "To JPEG", "To WebP", "Resize 50%", "Compress…"]);
assert.deepStrictEqual(labels([f("c.mp4", "video")]),
                       ["Extract audio", "To WebM", "To GIF", "Trackcrop", "Compress…"]);
assert.deepStrictEqual(labels([f("c.mkv", "video")]),
                       ["Extract audio", "To MP4", "To WebM", "To GIF", "Trackcrop", "Compress…"]);
assert.deepStrictEqual(labels([f("b.tar.gz", "archive")]), ["Extract", "Compress…"]);
assert.deepStrictEqual(labels([f("n.txt", "text")]), ["Compress…"]);
assert.deepStrictEqual(labels([]), []);
assert.deepStrictEqual(O.COMPRESS.map(c => c.op), ["zip", "tgz", "txz", "tbz2"]);

console.log("ok");