Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { resolveMarkdownMediaPreview } from "./markdownMedia";
5
6const input = {
7 environmentId: EnvironmentId.make("environment-1"),
8 threadId: ThreadId.make("thread-1"),
9 workspaceRoot: "/repo",
10};
11
12describe("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", "\\\\server\\share"],
42 ["\\\\server\\share\\frame.png", "\\\\server\\share\\frame.png", "\\\\server\\share"],
43 ])("keeps encoded filename and UNC semantics for %s", (href, path, workspaceRoot = "/repo") => {
44 expect(resolveMarkdownMediaPreview(href, { ...input, workspaceRoot })).toMatchObject({
45 kind: "image",
46 source: {
47 resource: { path },
48 actionsSource: { reference: { kind: "file", path } },
49 },
50 });
51 });
52
53 it("serves a linked T3 attachment file in place like any other host path", () => {
54 const path = "/home/demo/.t3/userdata/attachments/11111111-1111-4111-8111-111111111111-mp4.mp4";
55 expect(resolveMarkdownMediaPreview(path, input)).toMatchObject({
56 kind: "video",
57 source: {
58 resource: { _tag: "media-file", threadId: input.threadId, path },
59 actionsSource: { resource: { _tag: "media-file", path } },
60 },
61 });
62 });
63
64 it("preserves a signed PDF URL and its authored name", () => {
65 const href = "//cdn.example.com/report%20one.pdf?signature=a%2fb#page=2";
66 expect(resolveMarkdownMediaPreview(href, input)).toMatchObject({
67 kind: "pdf",
68 source: { uri: `https:${href}`, name: "report one.pdf", mimeType: "application/pdf" },
69 });
70 });
71
72 it.each(["./image.png", "./report.pdf"])(
73 "uses the draft workspace when no thread exists: %s",
74 (href) => {
75 expect(
76 resolveMarkdownMediaPreview(href, { ...input, threadId: undefined })?.source,
77 ).toMatchObject({
78 resource: { _tag: "draft-workspace-file", cwd: "/repo", path: `/repo/${href}` },
79 });
80 },
81 );
82
83 it("keeps captured host media unavailable while allowing direct media", () => {
84 const captured = { ...input, captured: true };
85 for (const href of ["./image.png", "./clip.mp4", "./report.pdf"]) {
86 expect(resolveMarkdownMediaPreview(href, captured)).toBeNull();
87 }
88 expect(resolveMarkdownMediaPreview("https://cdn.example.com/report.pdf", captured)?.kind).toBe(
89 "pdf",
90 );
91 });
92});