⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
import { describe, expect, it, vi } from "vite-plus/test";
import {
buildAvatarUrl,
decodeAvatarUpload,
isUploadedAvatarUrl,
MAX_AVATAR_BYTES,
MAX_AVATAR_INPUT_CHARS,
normalizeAvatarMimeType,
} from "../../../apps/api/src/user/avatar";
const PNG_HEADER = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
function encode(bytes: number[]) {
return Buffer.from(bytes).toString("base64");
}
function pngOfSize(size: number) {
const bytes = Buffer.alloc(size);
Buffer.from(PNG_HEADER).copy(bytes);
return bytes.toString("base64");
}
describe("decodeAvatarUpload", () => {
it("accepts a PNG upload", () => {
const result = decodeAvatarUpload({
contentType: "image/png",
data: encode([...PNG_HEADER, 1, 2, 3]),
});
expect(result.mimeType).toBe("image/png");
expect(result.bytes.length).toBe(11);
});
it("accepts a JPEG upload", () => {
const result = decodeAvatarUpload({
contentType: "image/jpeg",
data: encode([0xff, 0xd8, 0xff, 0xe0, 0x00]),
});
expect(result.mimeType).toBe("image/jpeg");
});
it("accepts a WebP upload", () => {
const riff = Buffer.concat([
Buffer.from("RIFF", "ascii"),
Buffer.from([0x00, 0x00, 0x00, 0x00]),
Buffer.from("WEBP", "ascii"),
Buffer.from([0x00]),
]);
const result = decodeAvatarUpload({
contentType: "image/webp",
data: riff.toString("base64"),
});
expect(result.mimeType).toBe("image/webp");
});
it("normalizes image/jpg to image/jpeg", () => {
expect(normalizeAvatarMimeType("IMAGE/JPG")).toBe("image/jpeg");
const result = decodeAvatarUpload({
contentType: "image/jpg",
data: encode([0xff, 0xd8, 0xff, 0x00]),
});
expect(result.mimeType).toBe("image/jpeg");
});
it("strips a data URL prefix", () => {
const result = decodeAvatarUpload({
contentType: "image/png",
data: `data:image/png;base64,${encode(PNG_HEADER)}`,
});
expect(result.bytes.length).toBe(8);
});
it("rejects an unsupported image type", () => {
expect(() =>
decodeAvatarUpload({
contentType: "image/svg+xml",
data: encode(PNG_HEADER),
}),
).toThrow(/Unsupported image type/);
});
it("rejects data that is not valid base64", () => {
expect(() =>
decodeAvatarUpload({ contentType: "image/png", data: "not base64!!" }),
).toThrow(/base64/);
});
it("rejects empty data", () => {
expect(() =>
decodeAvatarUpload({ contentType: "image/png", data: "" }),
).toThrow(/base64/);
});
it("rejects uploads above the size limit", () => {
expect(() =>
decodeAvatarUpload({
contentType: "image/png",
data: pngOfSize(MAX_AVATAR_BYTES + 1),
}),
).toThrow(/maximum avatar size/);
});
it("accepts an avatar at the exact decoded byte limit", () => {
expect(
decodeAvatarUpload({
contentType: "image/png",
data: pngOfSize(MAX_AVATAR_BYTES),
}).bytes.length,
).toBe(MAX_AVATAR_BYTES);
});
it.each([" ", "A"])(
"rejects oversized raw %s data before cleanup or decoding",
(character) => {
const data = character.repeat(MAX_AVATAR_INPUT_CHARS + 1);
const from = vi.spyOn(Buffer, "from");
try {
expect(() =>
decodeAvatarUpload({ contentType: "image/png", data }),
).toThrow(/maximum avatar size/);
expect(from).not.toHaveBeenCalled();
} finally {
from.mockRestore();
}
},
);
it("rejects a decoded-size overflow before allocating its Buffer", () => {
const data = pngOfSize(MAX_AVATAR_BYTES + 1);
const from = vi.spyOn(Buffer, "from");
try {
expect(() =>
decodeAvatarUpload({ contentType: "image/png", data }),
).toThrow(/maximum avatar size/);
expect(from).not.toHaveBeenCalled();
} finally {
from.mockRestore();
}
});
it("rejects bytes that do not match the declared type", () => {
expect(() =>
decodeAvatarUpload({
contentType: "image/png",
data: Buffer.from("<html>hello</html>").toString("base64"),
}),
).toThrow(/does not match the declared/);
});
});
describe("buildAvatarUrl", () => {
it("builds an API-relative URL", () => {
expect(buildAvatarUrl("abc123")).toBe("/api/user/avatar/abc123");
});
});
describe("isUploadedAvatarUrl", () => {
it("recognizes an avatar uploaded to Kaneo", () => {
expect(isUploadedAvatarUrl(buildAvatarUrl("abc123"))).toBe(true);
});
it.each([
null,
undefined,
"",
"/api/user/avatar/",
"/api/user/avatar/abc/def",
"/api/user/avatar/abc?size=64",
"https://cdn.example.com/api/user/avatar/abc123",
])("does not treat %p as an upload", (image) => {
expect(isUploadedAvatarUrl(image)).toBe(false);
});
});