Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownMedia.test.ts

88 lines3.1 KB

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"],
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("resolves protocol-relative media for native APIs without rewriting its signed query", () => {
76 expect(
77 resolveMarkdownMediaPreview("//cdn.example.com/clip.mp4?signature=a%2fb#t=2", input),
78 ).toMatchObject({
79 kind: "video",
80 source: {
81 uri: "https://cdn.example.com/clip.mp4?signature=a%2fb#t=2",
82 actionsSource: {
83 reference: { kind: "url", url: "//cdn.example.com/clip.mp4?signature=a%2fb#t=2" },
84 },
85 },
86 });
87 });
88});