Files
herdrMario/tests/attachments.test.mjs
T
2026-11-19 12:09:32 +01:00

92 lines
3.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { LIMITS, validate, validateMany, mention, removeMention } from "../mod/ovis-composer/attachments.js";
test("validate rejects over 32 MiB", () => {
const r = validate({ bytes: 33 * 2 ** 20 });
assert.equal(r.ok, false);
assert.equal(r.reason, "Plik przekracza limit 32 MiB");
});
test("validate accepts a normal file and an image", () => {
assert.deepEqual(validate({ name: "a.txt", bytes: 10 }), { ok: true });
assert.deepEqual(validate({ name: "a.png", bytes: 10 }), { ok: true });
});
test("validate rejects junk and unknown kind", () => {
assert.equal(validate(null).ok, false);
assert.equal(validate({ bytes: NaN }).ok, false);
assert.equal(validate({ kind: "video", bytes: 1 }).ok, false);
});
test("validateMany enforces count limit", () => {
assert.equal(LIMITS.filesPerAction, 100);
const files = Array.from({ length: 101 }, () => ({ name: "a", bytes: 1 }));
assert.equal(validateMany(files).ok, false);
assert.equal(validateMany(files.slice(0, 100)).ok, true);
});
test("mention: bare path, or quoted when it has whitespace", () => {
assert.equal(mention(" /a/b "), "@/a/b");
assert.equal(mention("/a b/c"), '@"/a b/c"');
assert.equal(mention("C:\\Moje pliki\\x.png"), '@"C:\\Moje pliki\\x.png"');
assert.equal(mention("/a\tb"), '@"/a\tb"');
assert.equal(mention(""), "");
assert.equal(mention(5), "");
});
test("removeMention removes only the standalone token", () => {
assert.equal(removeMention("see @src/a.rs now", "src/a.rs"), "see now");
assert.equal(removeMention("see @src/a.rsx now", "src/a.rs"), "see @src/a.rsx now");
});
test("mention and removeMention with a space and Polish letters in the path", () => {
const path = "/home/zażółć/gęsia jaźń/ęą.png";
assert.equal(mention(path), `@"${path}"`);
assert.equal(removeMention(`zobacz @"${path}" proszę`, path), "zobacz proszę");
assert.equal(removeMention(`@"${path}"`, path), "");
});
test("removeMention leaves a mention glued to a Polish letter alone", () => {
assert.equal(removeMention("@src/aę a", "src/a"), "@src/aę a");
assert.equal(removeMention("ę@src/a", "src/a"), "ę@src/a");
});
test("removeMention takes the quoted form and the bare form out", () => {
assert.equal(removeMention('see @"/a b/ęą.png" now', "/a b/ęą.png"), "see now");
assert.equal(removeMention('@"/a b/ęą.png"', "/a b/ęą.png"), "");
assert.equal(removeMention("see @/a/ęą.png now", "/a/ęą.png"), "see now");
assert.equal(removeMention('see @"/a b/c.png"x now', "/a b/c.png"), 'see @"/a b/c.png"x now');
});
import { formatBytes } from "../mod/ovis-composer/attachments.js";
test("formatBytes", () => {
assert.equal(formatBytes(0), "0 B");
assert.equal(formatBytes(512), "512 B");
assert.equal(formatBytes(2048), "2 KB");
assert.equal(formatBytes(1536), "1,5 KB");
assert.equal(formatBytes(5 * 1024 * 1024), "5 MB");
assert.equal(formatBytes(undefined), "");
assert.equal(formatBytes(NaN), "");
});
import { relativeTo } from "../mod/ovis-composer/attachments.js";
test("relativeTo: a file inside the folder, forward slashes, any case", () => {
assert.equal(
relativeTo("C:\\Users\\Factor\\.config\\herdr-app", "C:\\Users\\Factor\\.config\\herdr-app\\data\\ovis-composer\\paste\\paste-1.png"),
"data/ovis-composer/paste/paste-1.png",
);
assert.equal(relativeTo("c:/users/factor/.config/herdr-app/", "C:\\Users\\Factor\\.config\\herdr-app\\a b\\ę.png"), "a b/ę.png");
});
test("relativeTo: outside the folder, the folder itself, or rubbish is nothing", () => {
assert.equal(relativeTo("C:\\app", "D:\\pics\\a.png"), null);
assert.equal(relativeTo("C:\\app", "C:\\application\\a.png"), null);
assert.equal(relativeTo("C:\\app", "C:\\app"), null);
assert.equal(relativeTo("C:\\app", "C:\\app\\"), null);
assert.equal(relativeTo("", "C:\\a.png"), null);
assert.equal(relativeTo("C:\\app", undefined), null);
});