|
| 1 | +import { base64ImageSchema } from "@/lib/zod/schemas/misc"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +describe("base64ImageSchema", () => { |
| 5 | + it("should validate a correct base64 PNG image", () => { |
| 6 | + const validBase64Image = |
| 7 | + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; |
| 8 | + expect(() => base64ImageSchema.parse(validBase64Image)).not.toThrow(); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should reject an invalid image type", () => { |
| 12 | + const invalidImageType = |
| 13 | + "data:image/invalid;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; |
| 14 | + expect(() => base64ImageSchema.parse(invalidImageType)).toThrow( |
| 15 | + "Invalid image format", |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should reject malformed base64 data", () => { |
| 20 | + const malformedBase64 = "data:image/png;base64,invalid-base64-data"; |
| 21 | + expect(() => base64ImageSchema.parse(malformedBase64)).toThrow( |
| 22 | + "Invalid base64 content", |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should reject a string without data URI prefix", () => { |
| 27 | + const noPrefix = |
| 28 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; |
| 29 | + expect(() => base64ImageSchema.parse(noPrefix)).toThrow( |
| 30 | + "Invalid image format", |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should reject URLs", () => { |
| 35 | + const url = "https://github.com/dubinc/dub"; |
| 36 | + expect(() => base64ImageSchema.parse(url)).toThrow("Invalid image format"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should reject URLs with image extensions", () => { |
| 40 | + const imageUrl = "https://example.com/image.png"; |
| 41 | + expect(() => base64ImageSchema.parse(imageUrl)).toThrow( |
| 42 | + "Invalid image format", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should reject URLs with data URI prefix but invalid format", () => { |
| 47 | + const invalidDataUri = |
| 48 | + "data:image/png;base64,https://example.com/image.png"; |
| 49 | + expect(() => base64ImageSchema.parse(invalidDataUri)).toThrow( |
| 50 | + "Invalid base64 content", |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
0 commit comments