// 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/assistant/test_files.js // Files.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 + "/Files.js", "utf8") .replace(/^\.pragma library$/m, ""); const F = new Function(src + "\nreturn { kind, chats, fromUrl, file, name, fromMime };")(); const cases = { "/a/report.pdf": "pdf", "/a/Report.PDF": "pdf", "/a/x.png": "image", "/a/x.JPEG": "image", "/a/clip.mp4": "video", "/a/c.MKV": "video", "/a/src.tar.gz": "archive", "/a/b.7z": "archive", "/a/main.py": "code", "/a/notes.md": "text", "/a/pkg.SlackBuild": "code", "/a/data.csv": "text", "/a/README.rst": "text", "/a/a.txt": "text", "/a/c.json": "code", "/a/Makefile": "other", "/a/.bashrc": "other", "/a/x.bin": "other", "/a/dir.d/noext": "other", }; for (const [p, k] of Object.entries(cases)) assert.strictEqual(F.kind(p), k, p); assert.ok(F.chats("pdf") && F.chats("image") && F.chats("text") && F.chats("code")); assert.ok(!F.chats("video") && !F.chats("archive") && !F.chats("other")); assert.strictEqual(F.fromUrl("file:///home/you/My%20Docs/caf%C3%A9.pdf"), "/home/you/My Docs/café.pdf"); assert.strictEqual(F.fromUrl("file:///tmp/a.txt"), "/tmp/a.txt"); assert.strictEqual(F.fromUrl("https://example.org/a.pdf"), ""); assert.strictEqual(F.fromUrl("file://localhost/tmp/a.pdf"), "/tmp/a.pdf"); assert.strictEqual(F.fromUrl("file://otherhost/tmp/a.pdf"), ""); assert.strictEqual(F.fromUrl("file:///bad%E0%A4%A"), ""); // malformed escape assert.deepStrictEqual(F.file("/x/y/a b.pdf"), { path: "/x/y/a b.pdf", name: "a b.pdf", kind: "pdf" }); // A file the name says nothing about is told by its mime type (file -b). const mimes = { "text/x-shellscript": "code", "text/x-script.python": "code", "text/x-c": "code", "text/x-makefile": "code", "application/json": "code", "text/plain": "text", "text/csv": "text", "text/markdown": "text", "image/png": "image", "application/pdf": "pdf", "video/mp4": "video", "application/zip": "archive", "application/x-xz": "archive", "application/octet-stream": "other", "application/x-executable": "other", "": "other", }; for (const [m, k] of Object.entries(mimes)) assert.strictEqual(F.fromMime(m), k, m); console.log("ok");