73 lines
3.0 KiB
JavaScript
73 lines
3.0 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), "");
|
|
});
|