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

51 lines
1.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", () => {
assert.equal(mention(" /a/b "), "@/a/b");
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");
});