|
| 1 | +import { formatResponse } from "../responses" |
| 2 | +import { IMAGE_MIME_TYPES } from "../../tools/helpers/imageHelpers" |
| 3 | +import { getImageMimeType, isSupportedImageMimeType } from "../../../utils/imageMime" |
| 4 | + |
| 5 | +const supportedMimeTypes = ["image/jpeg", "image/png", "image/gif", "image/webp"] as const |
| 6 | +const supportedMimeTypeSet = new Set<string>(supportedMimeTypes) |
| 7 | +const unsupportedMimeTypes = [ |
| 8 | + ...new Set([...Object.values(IMAGE_MIME_TYPES), "image/heic", "application/octet-stream", "image/svg+xml"]), |
| 9 | +].filter((mimeType) => !supportedMimeTypeSet.has(mimeType)) |
| 10 | +const payload = "c3ludGhldGlj" |
| 11 | + |
| 12 | +describe("image MIME utilities", () => { |
| 13 | + it.each(supportedMimeTypes)("extracts and accepts the SDK MIME type %s", (mimeType) => { |
| 14 | + expect(getImageMimeType(`data:${mimeType};base64,${payload}`)).toBe(mimeType) |
| 15 | + expect(isSupportedImageMimeType(mimeType)).toBe(true) |
| 16 | + }) |
| 17 | + |
| 18 | + it.each(unsupportedMimeTypes)("extracts but rejects the unsupported MIME type %s", (mimeType) => { |
| 19 | + expect(getImageMimeType(`data:${mimeType};base64,${payload}`)).toBe(mimeType) |
| 20 | + expect(isSupportedImageMimeType(mimeType)).toBe(false) |
| 21 | + }) |
| 22 | + |
| 23 | + it("returns no MIME for a missing header and rejects absent or empty MIME values", () => { |
| 24 | + expect(getImageMimeType("")).toBeUndefined() |
| 25 | + expect(isSupportedImageMimeType(undefined)).toBe(false) |
| 26 | + expect(isSupportedImageMimeType("")).toBe(false) |
| 27 | + }) |
| 28 | +}) |
| 29 | + |
| 30 | +describe("shared image-block MIME contract", () => { |
| 31 | + it.each(supportedMimeTypes)("preserves the complete image block for %s through both entry points", (mimeType) => { |
| 32 | + const dataUrl = `data:${mimeType};base64,${payload}` |
| 33 | + const image = { type: "image", source: { type: "base64", media_type: mimeType, data: payload } } |
| 34 | + expect(formatResponse.imageBlocks([dataUrl])).toEqual([image]) |
| 35 | + expect(formatResponse.toolResult("read result", [dataUrl])).toEqual([ |
| 36 | + { type: "text", text: "read result" }, |
| 37 | + image, |
| 38 | + ]) |
| 39 | + }) |
| 40 | + |
| 41 | + it.each(unsupportedMimeTypes)("rejects %s through both entry points before returning a payload", (mimeType) => { |
| 42 | + const dataUrl = `data:${mimeType};base64,${payload}` |
| 43 | + const error = new Error(`Unsupported image MIME type: ${mimeType}. Use JPEG, PNG, GIF, or WebP.`) |
| 44 | + expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error) |
| 45 | + expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error) |
| 46 | + }) |
| 47 | + |
| 48 | + it.each([ |
| 49 | + "", |
| 50 | + "not a data URL", |
| 51 | + "data:;base64,c3ludGhldGlj", |
| 52 | + "data:image/png,c3ludGhldGlj", |
| 53 | + "data:image/png;base64", |
| 54 | + "data:image/png;base64;c3ludGhldGlj", |
| 55 | + "data:image/png;charset=utf-8;base64,c3ludGhldGlj", |
| 56 | + "DATA:image/png;base64,c3ludGhldGlj", |
| 57 | + "data:image/png;BASE64,c3ludGhldGlj", |
| 58 | + "prefix data:image/png;base64,c3ludGhldGlj", |
| 59 | + " data:image/png;base64,c3ludGhldGlj", |
| 60 | + "\uFEFFdata:image/png;base64,c3ludGhldGlj", |
| 61 | + "data\uFF1Aimage/png;base64,c3ludGhldGlj", |
| 62 | + "data:image/png;base64\uFF0Cc3ludGhldGlj", |
| 63 | + ])("rejects a malformed or noncanonical header: %j", (dataUrl) => { |
| 64 | + const error = new Error("Unsupported image MIME type: unknown. Use JPEG, PNG, GIF, or WebP.") |
| 65 | + expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error) |
| 66 | + expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error) |
| 67 | + }) |
| 68 | + |
| 69 | + it.each(["image/PNG", "image/jpg", "image/png ", "image/p\u200Bng", "image/\u0440ng", "image\uFF0Fpng"])( |
| 70 | + "does not normalize unsupported MIME spelling %j into an allowed type", |
| 71 | + (mimeType) => { |
| 72 | + const dataUrl = `data:${mimeType};base64,${payload}` |
| 73 | + const error = new Error(`Unsupported image MIME type: ${mimeType}. Use JPEG, PNG, GIF, or WebP.`) |
| 74 | + expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error) |
| 75 | + expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error) |
| 76 | + }, |
| 77 | + ) |
| 78 | + |
| 79 | + it.each([{ images: undefined }, { images: [] }])("preserves the no-image response for $images", ({ images }) => { |
| 80 | + expect(formatResponse.imageBlocks(images)).toEqual([]) |
| 81 | + expect(formatResponse.toolResult("read result", images)).toBe("read result") |
| 82 | + expect(formatResponse.toolResult("", images)).toBe("") |
| 83 | + }) |
| 84 | + |
| 85 | + it("preserves image order, duplicate images, Unicode feedback, and the input array", () => { |
| 86 | + const images = [ |
| 87 | + "data:image/png;base64,Zmlyc3Q=", |
| 88 | + "data:image/jpeg;base64,c2Vjb25k", |
| 89 | + "data:image/png;base64,Zmlyc3Q=", |
| 90 | + ] |
| 91 | + const originalImages = [...images] |
| 92 | + const expected = [ |
| 93 | + { type: "image", source: { type: "base64", media_type: "image/png", data: "Zmlyc3Q=" } }, |
| 94 | + { type: "image", source: { type: "base64", media_type: "image/jpeg", data: "c2Vjb25k" } }, |
| 95 | + { type: "image", source: { type: "base64", media_type: "image/png", data: "Zmlyc3Q=" } }, |
| 96 | + ] |
| 97 | + const text = "\u041E\u0442\u0432\u0435\u0442 \uD83D\uDDBC\uFE0F e\u0301" |
| 98 | + expect(formatResponse.imageBlocks(images)).toEqual(expected) |
| 99 | + expect(formatResponse.toolResult(text, images)).toEqual([{ type: "text", text }, ...expected]) |
| 100 | + expect(images).toEqual(originalImages) |
| 101 | + }) |
| 102 | + |
| 103 | + it.each([0, 1, 2])("rejects an unsupported image at index %i without changing the input", (index) => { |
| 104 | + const images = Array<string>(3).fill(`data:image/png;base64,${payload}`) |
| 105 | + images[index] = `data:image/bmp;base64,${payload}` |
| 106 | + const originalImages = [...images] |
| 107 | + const error = new Error("Unsupported image MIME type: image/bmp. Use JPEG, PNG, GIF, or WebP.") |
| 108 | + expect(() => formatResponse.imageBlocks(images)).toThrowError(error) |
| 109 | + expect(() => formatResponse.toolResult("read result", images)).toThrowError(error) |
| 110 | + expect(images).toEqual(originalImages) |
| 111 | + }) |
| 112 | + |
| 113 | + it.each(["", "not decoded here", "first,second", "\uD83D\uDE00e\u0301"])( |
| 114 | + "preserves the opaque payload %j without adding byte or base64 validation", |
| 115 | + (data) => { |
| 116 | + const dataUrl = `data:image/png;base64,${data}` |
| 117 | + const image = { type: "image", source: { type: "base64", media_type: "image/png", data } } |
| 118 | + expect(formatResponse.imageBlocks([dataUrl])).toEqual([image]) |
| 119 | + expect(formatResponse.toolResult("", [dataUrl])).toEqual([{ type: "text", text: "" }, image]) |
| 120 | + }, |
| 121 | + ) |
| 122 | +}) |
0 commit comments