| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | MARKDOWN_IMAGE_MAX_HEIGHT, |
| 5 | MARKDOWN_IMAGE_MAX_WIDTH, |
| 6 | resolveMarkdownImageDisplaySize, |
| 7 | resolveSvgImageSize, |
| 8 | } from "./markdownImageSize"; |
| 9 | |
| 10 | describe("resolveMarkdownImageDisplaySize", () => { |
| 11 | it("keeps small images at their intrinsic size", () => { |
| 12 | expect( |
| 13 | resolveMarkdownImageDisplaySize({ |
| 14 | sourceWidth: 96, |
| 15 | sourceHeight: 96, |
| 16 | availableWidth: 332, |
| 17 | }), |
| 18 | ).toEqual({ width: 96, height: 96 }); |
| 19 | }); |
| 20 | |
| 21 | it("fits wide images to the available chat width", () => { |
| 22 | expect( |
| 23 | resolveMarkdownImageDisplaySize({ |
| 24 | sourceWidth: 960, |
| 25 | sourceHeight: 540, |
| 26 | availableWidth: 332, |
| 27 | }), |
| 28 | ).toEqual({ width: 332, height: 186.75 }); |
| 29 | }); |
| 30 | |
| 31 | it("caps wide images at 480 points on larger screens", () => { |
| 32 | expect( |
| 33 | resolveMarkdownImageDisplaySize({ |
| 34 | sourceWidth: 960, |
| 35 | sourceHeight: 540, |
| 36 | availableWidth: 900, |
| 37 | }), |
| 38 | ).toEqual({ width: MARKDOWN_IMAGE_MAX_WIDTH, height: 270 }); |
| 39 | }); |
| 40 | |
| 41 | it("caps tall images by height without changing their aspect ratio", () => { |
| 42 | expect( |
| 43 | resolveMarkdownImageDisplaySize({ |
| 44 | sourceWidth: 400, |
| 45 | sourceHeight: 800, |
| 46 | availableWidth: 332, |
| 47 | }), |
| 48 | ).toEqual({ width: 240, height: MARKDOWN_IMAGE_MAX_HEIGHT }); |
| 49 | }); |
| 50 | |
| 51 | it("rejects dimensions that cannot produce a stable layout", () => { |
| 52 | expect( |
| 53 | resolveMarkdownImageDisplaySize({ sourceWidth: 0, sourceHeight: 100, availableWidth: 332 }), |
| 54 | ).toBeNull(); |
| 55 | expect( |
| 56 | resolveMarkdownImageDisplaySize({ |
| 57 | sourceWidth: 100, |
| 58 | sourceHeight: Number.NaN, |
| 59 | availableWidth: 332, |
| 60 | }), |
| 61 | ).toBeNull(); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | describe("resolveSvgImageSize", () => { |
| 66 | it("uses pixel dimensions before the viewBox", () => { |
| 67 | expect(resolveSvgImageSize({ width: "32px", height: "48", viewBox: "0 0 100 100" })).toEqual({ |
| 68 | width: 32, |
| 69 | height: 48, |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | it("uses the viewBox for responsive SVGs", () => { |
| 74 | expect( |
| 75 | resolveSvgImageSize({ width: "100%", height: "100%", viewBox: "-10, -20, 120, 240" }), |
| 76 | ).toEqual({ width: 120, height: 240 }); |
| 77 | expect(resolveSvgImageSize({ viewBox: "0 0 100 NaN" })).toBeNull(); |
| 78 | expect(resolveSvgImageSize({ viewBox: "0 0 0 100" })).toBeNull(); |
| 79 | }); |
| 80 | }); |