| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { classifyMarkdownImageSource, markdownImageSourceFragment } from "./markdownImages.js"; |
| 4 | |
| 5 | describe("classifyMarkdownImageSource", () => { |
| 6 | it.each([ |
| 7 | "https://example.com/image.png", |
| 8 | "HTTP://example.com/image.png", |
| 9 | "data:image/png;base64,AAAA", |
| 10 | "blob:https://app.t3.codes/image-id", |
| 11 | "//cdn.example.com/image.png", |
| 12 | ])("keeps %s directly loadable", (uri) => { |
| 13 | expect(classifyMarkdownImageSource(uri, "/workspace/project")).toEqual({ |
| 14 | _tag: "Direct", |
| 15 | uri, |
| 16 | }); |
| 17 | }); |
| 18 | |
| 19 | it.each([ |
| 20 | ["images/result.png", "/workspace/project", "/workspace/project/images/result.png"], |
| 21 | ["./images/result.png", "/workspace/project", "/workspace/project/./images/result.png"], |
| 22 | [ |
| 23 | "images/result.png", |
| 24 | "C:\\Users\\dara\\project", |
| 25 | "C:\\Users\\dara\\project\\images\\result.png", |
| 26 | ], |
| 27 | [ |
| 28 | "images\\result.png", |
| 29 | "C:\\Users\\dara\\project", |
| 30 | "C:\\Users\\dara\\project\\images\\result.png", |
| 31 | ], |
| 32 | ["/workspace/project/image.png", null, "/workspace/project/image.png"], |
| 33 | ["/C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"], |
| 34 | ["C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"], |
| 35 | ["\\\\server\\share\\image.png", null, "\\\\server\\share\\image.png"], |
| 36 | ["file:///workspace/project/image%20one.png", null, "/workspace/project/image one.png"], |
| 37 | ["file:///C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"], |
| 38 | ["file://localhost/C:/Users/dara/project/image.png", null, "C:/Users/dara/project/image.png"], |
| 39 | ["file://server/share/image.png", null, "\\\\server\\share\\image.png"], |
| 40 | ])("maps %s to a workspace file", (source, workspaceRoot, path) => { |
| 41 | expect(classifyMarkdownImageSource(source, workspaceRoot)).toEqual({ |
| 42 | _tag: "WorkspaceFile", |
| 43 | path, |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | it.each([ |
| 48 | null, |
| 49 | "", |
| 50 | "#image", |
| 51 | "?image=1", |
| 52 | "image.png", |
| 53 | "~/image.png", |
| 54 | "javascript:alert(1)", |
| 55 | "ftp://example.com/image.png", |
| 56 | "content://media/image/1", |
| 57 | "custom:image.png", |
| 58 | "file://%", |
| 59 | ])("blocks unsupported or unresolved source %s", (source) => { |
| 60 | expect(classifyMarkdownImageSource(source)).toEqual({ _tag: "Blocked" }); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("markdownImageSourceFragment", () => { |
| 65 | it.each([ |
| 66 | ["<icons.svg?version=2#logo>", "#logo"], |
| 67 | ["icons.svg?version=2", ""], |
| 68 | ])("extracts %s as %s", (source, fragment) => { |
| 69 | expect(markdownImageSourceFragment(source)).toBe(fragment); |
| 70 | }); |
| 71 | }); |