| 1 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { resolveMarkdownMediaPreview } from "./markdownMedia"; |
| 5 | |
| 6 | const input = { |
| 7 | environmentId: EnvironmentId.make("environment-1"), |
| 8 | threadId: ThreadId.make("thread-1"), |
| 9 | workspaceRoot: "/repo", |
| 10 | }; |
| 11 | |
| 12 | describe("resolveMarkdownMediaPreview", () => { |
| 13 | it("decodes remote filenames once without changing the authored URL", () => { |
| 14 | const href = "https://cdn.example.com/clip%20one%2520%2Emp4?signature=a%2fb#t=2"; |
| 15 | expect(resolveMarkdownMediaPreview(href, input)).toMatchObject({ |
| 16 | kind: "video", |
| 17 | source: { |
| 18 | uri: href, |
| 19 | actionsSource: { |
| 20 | name: "clip one%20.mp4", |
| 21 | mimeType: "video/mp4", |
| 22 | reference: { kind: "url", url: href }, |
| 23 | }, |
| 24 | }, |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | it("provides extensionless image actions only for image embeds", () => { |
| 29 | const href = "https://cdn.example.com/render?id=42"; |
| 30 | expect(resolveMarkdownMediaPreview(href, input)).toBeNull(); |
| 31 | expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed: true })).toMatchObject({ |
| 32 | kind: "image", |
| 33 | source: { actionsSource: { reference: { kind: "url", url: href }, mimeType: "image/*" } }, |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | it.each([ |
| 38 | ["/tmp/frame%23one.png:12", "/tmp/frame#one.png"], |
| 39 | ["/tmp/frame%3Fone.png:12:3", "/tmp/frame?one.png"], |
| 40 | ["/tmp/frame%2523one.png:12", "/tmp/frame%23one.png"], |
| 41 | ["file://server/share/frame.png", "\\\\server\\share\\frame.png"], |
| 42 | ["\\\\server\\share\\frame.png", "\\\\server\\share\\frame.png"], |
| 43 | ])("keeps encoded filename and UNC semantics for %s", (href, path) => { |
| 44 | expect(resolveMarkdownMediaPreview(href, input)).toMatchObject({ |
| 45 | kind: "image", |
| 46 | source: { |
| 47 | resource: { path }, |
| 48 | actionsSource: { reference: { kind: "file", path } }, |
| 49 | }, |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | it("separates a video playback fragment from literal filename characters", () => { |
| 54 | expect(resolveMarkdownMediaPreview("/tmp/clip%23one.mp4#t=2", input)).toMatchObject({ |
| 55 | kind: "video", |
| 56 | source: { |
| 57 | srcFragment: "#t=2", |
| 58 | resource: { path: "/tmp/clip#one.mp4" }, |
| 59 | actionsSource: { reference: { kind: "file", path: "/tmp/clip#one.mp4" } }, |
| 60 | }, |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | it("serves a linked T3 attachment file in place like any other host path", () => { |
| 65 | const path = "/home/demo/.t3/userdata/attachments/11111111-1111-4111-8111-111111111111-mp4.mp4"; |
| 66 | expect(resolveMarkdownMediaPreview(path, input)).toMatchObject({ |
| 67 | kind: "video", |
| 68 | source: { |
| 69 | resource: { _tag: "media-file", threadId: input.threadId, path }, |
| 70 | actionsSource: { resource: { _tag: "media-file", path } }, |
| 71 | }, |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | it.each(["./image.png", "./clip.mp4", "./report.pdf"])( |
| 76 | "opens relative media through the environment: %s", |
| 77 | (href) => { |
| 78 | const preview = resolveMarkdownMediaPreview(href, input); |
| 79 | expect(preview?.source).toMatchObject({ |
| 80 | environmentId: input.environmentId, |
| 81 | resource: { _tag: "media-file", threadId: input.threadId, path: `/repo/${href}` }, |
| 82 | }); |
| 83 | }, |
| 84 | ); |
| 85 | |
| 86 | it("preserves a signed PDF URL and its authored name", () => { |
| 87 | const href = "//cdn.example.com/report%20one.pdf?signature=a%2fb#page=2"; |
| 88 | expect(resolveMarkdownMediaPreview(href, input)).toMatchObject({ |
| 89 | kind: "pdf", |
| 90 | source: { uri: `https:${href}`, name: "report one.pdf", mimeType: "application/pdf" }, |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it("recognizes a URL-encoded PDF extension and filename without rewriting the URL", () => { |
| 95 | const href = "https://cdn.example.com/report%23one%2520%2Epdf?signature=a%2fb"; |
| 96 | expect(resolveMarkdownMediaPreview(href, input)).toMatchObject({ |
| 97 | kind: "pdf", |
| 98 | source: { uri: href, name: "report#one%20.pdf" }, |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | it("opens encoded host PDFs without interpreting literal filename characters twice", () => { |
| 103 | expect(resolveMarkdownMediaPreview("/tmp/report%23one.pdf:12#page=2", input)).toMatchObject({ |
| 104 | kind: "pdf", |
| 105 | source: { resource: { path: "/tmp/report#one.pdf" }, srcFragment: "#page=2" }, |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | it.each(["./image.png", "./clip.mp4", "./report.pdf"])( |
| 110 | "uses the draft workspace when no thread exists: %s", |
| 111 | (href) => { |
| 112 | expect( |
| 113 | resolveMarkdownMediaPreview(href, { ...input, threadId: undefined })?.source, |
| 114 | ).toMatchObject({ |
| 115 | resource: { _tag: "draft-workspace-file", cwd: "/repo", path: `/repo/${href}` }, |
| 116 | }); |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | it("keeps captured host media unavailable while allowing direct media", () => { |
| 121 | const captured = { ...input, captured: true }; |
| 122 | for (const href of ["./image.png", "./clip.mp4", "./report.pdf"]) { |
| 123 | expect(resolveMarkdownMediaPreview(href, captured)).toBeNull(); |
| 124 | } |
| 125 | expect(resolveMarkdownMediaPreview("https://cdn.example.com/report.pdf", captured)?.kind).toBe( |
| 126 | "pdf", |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | it("resolves protocol-relative media for native APIs without rewriting its signed query", () => { |
| 131 | expect( |
| 132 | resolveMarkdownMediaPreview("//cdn.example.com/clip.mp4?signature=a%2fb#t=2", input), |
| 133 | ).toMatchObject({ |
| 134 | kind: "video", |
| 135 | source: { |
| 136 | uri: "https://cdn.example.com/clip.mp4?signature=a%2fb#t=2", |
| 137 | actionsSource: { |
| 138 | reference: { kind: "url", url: "//cdn.example.com/clip.mp4?signature=a%2fb#t=2" }, |
| 139 | }, |
| 140 | }, |
| 141 | }); |
| 142 | }); |
| 143 | }); |