|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { DEFAULT_ATTACHMENT_LIMITS, validateAttachments, type Attachment } from "./index" |
| 3 | + |
| 4 | +function makeFile(name: string, bytes: number, type = "image/png"): File { |
| 5 | + const blob = new Blob([new Uint8Array(bytes)], { type }) |
| 6 | + return new File([blob], name, { type }) |
| 7 | +} |
| 8 | + |
| 9 | +function makeExisting(size: number, mime = "image/png"): Attachment { |
| 10 | + return { |
| 11 | + id: "x", |
| 12 | + blob: new Blob([new Uint8Array(size)], { type: mime }), |
| 13 | + filename: "existing.png", |
| 14 | + mime, |
| 15 | + size, |
| 16 | + isImage: mime.startsWith("image/"), |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +test("accepts a single small image", () => { |
| 21 | + const result = validateAttachments([makeFile("a.png", 100)], [], DEFAULT_ATTACHMENT_LIMITS) |
| 22 | + expect(result.accepted).toHaveLength(1) |
| 23 | + expect(result.rejected).toHaveLength(0) |
| 24 | + expect(result.accepted[0]?.isImage).toBe(true) |
| 25 | +}) |
| 26 | + |
| 27 | +test("rejects a file over per-file cap", () => { |
| 28 | + const big = makeFile("big.png", DEFAULT_ATTACHMENT_LIMITS.maxFileBytes + 1) |
| 29 | + const result = validateAttachments([big], [], DEFAULT_ATTACHMENT_LIMITS) |
| 30 | + expect(result.accepted).toHaveLength(0) |
| 31 | + expect(result.rejected).toEqual([{ filename: "big.png", reason: "too-large" }]) |
| 32 | +}) |
| 33 | + |
| 34 | +test("rejects when count would exceed maxCount", () => { |
| 35 | + const existing = Array.from({ length: 5 }, () => makeExisting(10)) |
| 36 | + const result = validateAttachments([makeFile("new.png", 10)], existing, DEFAULT_ATTACHMENT_LIMITS) |
| 37 | + expect(result.accepted).toHaveLength(0) |
| 38 | + expect(result.rejected).toEqual([{ filename: "new.png", reason: "count-exceeded" }]) |
| 39 | +}) |
| 40 | + |
| 41 | +test("rejects when total bytes would exceed maxTotalBytes", () => { |
| 42 | + const existing = [makeExisting(20 * 1024 * 1024)] |
| 43 | + const result = validateAttachments( |
| 44 | + [makeFile("a.png", 10 * 1024 * 1024)], |
| 45 | + existing, |
| 46 | + DEFAULT_ATTACHMENT_LIMITS, |
| 47 | + ) |
| 48 | + expect(result.accepted).toHaveLength(0) |
| 49 | + expect(result.rejected).toEqual([{ filename: "a.png", reason: "total-exceeded" }]) |
| 50 | +}) |
| 51 | + |
| 52 | +test("rejects denylisted mime", () => { |
| 53 | + const exe = makeFile("evil.exe", 100, "application/x-msdownload") |
| 54 | + const result = validateAttachments([exe], [], DEFAULT_ATTACHMENT_LIMITS) |
| 55 | + expect(result.rejected).toEqual([{ filename: "evil.exe", reason: "denied-mime" }]) |
| 56 | +}) |
| 57 | + |
| 58 | +test("rejects denylisted extension even if mime is benign", () => { |
| 59 | + const fake = makeFile("script.sh", 100, "text/plain") |
| 60 | + const result = validateAttachments([fake], [], DEFAULT_ATTACHMENT_LIMITS) |
| 61 | + expect(result.rejected).toEqual([{ filename: "script.sh", reason: "denied-mime" }]) |
| 62 | +}) |
| 63 | + |
| 64 | +test("accumulates errors instead of bailing on first", () => { |
| 65 | + const ok = makeFile("ok.png", 100) |
| 66 | + const big = makeFile("big.png", DEFAULT_ATTACHMENT_LIMITS.maxFileBytes + 1) |
| 67 | + const evil = makeFile("evil.exe", 100, "application/x-msdownload") |
| 68 | + const result = validateAttachments([ok, big, evil], [], DEFAULT_ATTACHMENT_LIMITS) |
| 69 | + expect(result.accepted).toHaveLength(1) |
| 70 | + expect(result.accepted[0]?.filename).toBe("ok.png") |
| 71 | + expect(result.rejected).toEqual([ |
| 72 | + { filename: "big.png", reason: "too-large" }, |
| 73 | + { filename: "evil.exe", reason: "denied-mime" }, |
| 74 | + ]) |
| 75 | +}) |
| 76 | + |
| 77 | +test("count check considers files added earlier in the same batch", () => { |
| 78 | + const existing = [makeExisting(10), makeExisting(10), makeExisting(10), makeExisting(10)] |
| 79 | + const a = makeFile("a.png", 10) |
| 80 | + const b = makeFile("b.png", 10) |
| 81 | + const result = validateAttachments([a, b], existing, DEFAULT_ATTACHMENT_LIMITS) |
| 82 | + expect(result.accepted).toHaveLength(1) |
| 83 | + expect(result.rejected).toEqual([{ filename: "b.png", reason: "count-exceeded" }]) |
| 84 | +}) |
| 85 | + |
| 86 | +test("zero-byte file is rejected as unreadable", () => { |
| 87 | + const empty = makeFile("empty.png", 0) |
| 88 | + const result = validateAttachments([empty], [], DEFAULT_ATTACHMENT_LIMITS) |
| 89 | + expect(result.rejected).toEqual([{ filename: "empty.png", reason: "unreadable" }]) |
| 90 | +}) |
| 91 | + |
| 92 | +test("missing mime defaults to application/octet-stream", () => { |
| 93 | + const blob = new Blob([new Uint8Array(100)]) |
| 94 | + const file = new File([blob], "data.bin") |
| 95 | + const result = validateAttachments([file], [], DEFAULT_ATTACHMENT_LIMITS) |
| 96 | + expect(result.accepted[0]?.mime).toBe("application/octet-stream") |
| 97 | +}) |
0 commit comments