Files
RuneStone/tests/unit/features/pm/images.spec.ts
T
2026-11-03 00:23:07 -07:00

108 lines
4.1 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
imageMarkdown,
imageReferences,
imagePath,
parseImage,
saveImages,
maxImageBytes,
} from "../../../../src/lib/features/pm/storage/images.ts";
import type { Workspace } from "../../../../src/lib/features/pm/model/model.ts";
const id = "12345678-1234-1234-1234-123456789abc";
const data = "data:image/png;base64,aGVsbG8=";
test("image references preserve surrounding text and restrict private paths", () => {
const markdown = imageMarkdown("shot [one]\n\\test.png", id);
assert.equal(markdown, `![shot one test.png](pm-image:${id})`);
const body = `Before\n${markdown}\nAfter`;
assert.deepEqual(imageReferences(body), [
{ id, name: "shot one test.png", markdown, index: 7 },
]);
assert.deepEqual(
imageReferences(
"![remote](https://example.com/a.png) ![bad](pm-image:../../secret)",
),
[],
);
assert.throws(() => imagePath("../../secret"), /invalid/);
});
test("saved image data rejects unsupported and executable sources", () => {
assert.equal(parseImage(JSON.stringify({ schema: 1, data })), data);
for (const value of [
null,
{},
{ schema: 1, data: "https://example.com/image.png" },
{ schema: 1, data: "data:image/svg+xml;base64,PHN2Zz4=" },
{ schema: 1, data: "data:image/png;base64,***=" },
])
assert.throws(() => parseImage(JSON.stringify(value)));
assert.throws(
() => parseImage("x".repeat(Math.ceil(maxImageBytes / 3) * 4 + 257)),
/50 MiB/,
);
});
test("save writes only referenced images, protects existing files, and permits retry", async () => {
const files = new Map<string, string>();
const writes: string[] = [];
const workspace: Workspace = {
async list() {
return [];
},
async read(path) {
return files.get(path)!;
},
async readOptional(path) {
return files.get(path) ?? null;
},
async create(path, source) {
if (files.has(path)) throw new Error("Already exists");
files.set(path, source);
writes.push(path);
},
async write() {
throw new Error("Unexpected overwrite");
},
};
const pending = new Map([
[id, data],
["87654321-1234-1234-1234-123456789abc", data],
]);
const body = imageMarkdown("Screenshot", id);
await saveImages(workspace, body, pending);
assert.deepEqual(writes, [".runestone/pm/.gitignore", imagePath(id)]);
await saveImages(workspace, body, pending);
assert.equal(writes.length, 2);
files.set(imagePath(id), "other image");
await assert.rejects(saveImages(workspace, body, pending), /Already exists/);
assert.equal(files.get(imagePath(id)), "other image");
});
test("attachment metadata saves staged images without description references", async () => {
const files = new Map<string, string>();
const workspace: Workspace = {
async list() { return []; },
async read(path) { return files.get(path)!; },
async readOptional(path) { return files.get(path) ?? null; },
async create(path, source) { files.set(path, source); },
async write() { throw new Error("Unexpected overwrite"); },
};
await saveImages(workspace, "", new Map([[id, data]]), [{ id, name: "Attachment.png" }]);
assert.equal(parseImage(files.get(imagePath(id))!), data);
});
test("image storage accepts exactly 50 MiB and rejects one extra byte", async () => {
assert.equal(maxImageBytes, 50 * 1024 * 1024);
const data = `data:image/png;base64,${Buffer.alloc(maxImageBytes).toString("base64")}`;
const files = new Map<string, string>();
const workspace: Workspace = {
async list() { return []; }, async read(path) { return files.get(path)!; }, async readOptional(path) { return files.get(path) ?? null; },
async write() { throw new Error("Unexpected overwrite"); },
async create(path, source) { files.set(path, source); },
};
await saveImages(workspace, "", new Map([[id, data]]), [{ id, name: "large.png" }]);
assert.equal(parseImage(files.get(imagePath(id))!), data);
const tooLarge = `data:image/png;base64,${Buffer.alloc(maxImageBytes + 1).toString("base64")}`;
assert.throws(() => parseImage(JSON.stringify({ schema: 1, data: tooLarge })), /50 MiB/);
});