Open History to see the last commit
1import { ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { resolveMediaSource } from "./mediaSource.ts";
5
6const threadId = ThreadId.make("thread-1");
7const attachmentId =
8 "11111111-1111-4111-8111-111111111111-22222222-2222-4222-8222-222222222222-mp4";
9
10describe("resolveMediaSource", () => {
11 describe("direct URLs", () => {
12 it("keeps the authored URL and decodes the display name once", () => {
13 const href = "https://cdn.example.com/clip%20one%2520%2Emp4?signature=a%2fb#t=2";
14 expect(resolveMediaSource(href, { threadId })).toEqual({
15 kind: "video",
16 mimeType: "video/mp4",
17 name: "clip one%20.mp4",
18 reference: { kind: "url", url: href },
19 srcFragment: "#t=2",
20 access: "direct",
21 uri: href,
22 });
23 });
24
25 it("keeps protocol-relative URLs as authored; clients add the scheme", () => {
26 const href = "//cdn.example.com/clip.mp4?sig=1#t=2";
27 expect(resolveMediaSource(href, { threadId })).toMatchObject({
28 access: "direct",
29 uri: href,
30 reference: { kind: "url", url: href },
31 });
32 });
33
34 it("accepts extensionless image embeds only when asked", () => {
35 const href = "https://cdn.example.com/render?id=42";
36 expect(resolveMediaSource(href, { threadId })).toBeNull();
37 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toMatchObject({
38 kind: "image",
39 mimeType: "image/*",
40 name: "render",
41 });
42 });
43
44 it.each(["data:image/png;base64,AAAA", "blob:https://app.t3.codes/id"])(
45 "loads %s directly",
46 (href) => {
47 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toMatchObject({
48 access: "direct",
49 uri: href,
50 });
51 },
52 );
53 });
54
55 describe("host paths", () => {
56 it.each([
57 ["/tmp/frame%23one.png:12", "/tmp/frame#one.png"],
58 ["/tmp/frame%3Fone.png:12:3", "/tmp/frame?one.png"],
59 ["/tmp/frame%2523one.png:12", "/tmp/frame%23one.png"],
60 ["C:\\Users\\demo\\frame.png", "C:\\Users\\demo\\frame.png"],
61 ["/C:/Users/demo/frame.png", "C:/Users/demo/frame.png"],
62 ["file:///C:/Users/demo/frame.png", "C:/Users/demo/frame.png"],
63 ["file://server/share/frame.png", "\\\\server\\share\\frame.png"],
64 ["\\\\server\\share\\frame.png", "\\\\server\\share\\frame.png"],
65 ])("resolves %s through the environment", (href, path) => {
66 expect(resolveMediaSource(href, { threadId, workspaceRoot: "/repo" })).toEqual({
67 kind: "image",
68 mimeType: "image/png",
69 name: path.split(/[\\/]/).at(-1),
70 reference: { kind: "file", path },
71 srcFragment: "",
72 access: "environment",
73 resource: { _tag: "media-file", threadId, path },
74 });
75 });
76
77 it("joins relative paths to the workspace and records the relative reference", () => {
78 expect(
79 resolveMediaSource("screens/logo.svg?v=2#mark", { threadId, workspaceRoot: "/repo" }),
80 ).toEqual({
81 kind: "image",
82 mimeType: "image/svg+xml",
83 name: "logo.svg",
84 reference: {
85 kind: "file",
86 path: "/repo/screens/logo.svg",
87 relativePath: "screens/logo.svg",
88 },
89 srcFragment: "#mark",
90 access: "environment",
91 resource: { _tag: "media-file", threadId, path: "/repo/screens/logo.svg" },
92 });
93 });
94
95 it("prefers a caller-resolved path over classifying the source", () => {
96 expect(
97 resolveMediaSource("logo.svg", {
98 threadId,
99 workspaceRoot: "/repo",
100 resolvedFilePath: "/repo/docs/logo.svg",
101 }),
102 ).toMatchObject({
103 access: "environment",
104 resource: { _tag: "media-file", path: "/repo/docs/logo.svg" },
105 reference: { relativePath: "docs/logo.svg" },
106 });
107 });
108
109 it("separates a playback fragment from literal filename characters", () => {
110 expect(resolveMediaSource("/tmp/clip%23one.mp4#t=2", { threadId })).toMatchObject({
111 kind: "video",
112 srcFragment: "#t=2",
113 resource: { path: "/tmp/clip#one.mp4" },
114 });
115 });
116
117 it("cannot be loaded without a thread to mint through", () => {
118 expect(resolveMediaSource("/tmp/clip.mp4", { threadId: undefined })).toMatchObject({
119 kind: "video",
120 access: "unavailable",
121 });
122 });
123
124 it.each(["notes.txt", "/repo/README", "/repo/archive.zip"])(
125 "returns null for the non-media path %s",
126 (href) => {
127 expect(resolveMediaSource(href, { threadId, workspaceRoot: "/repo" })).toBeNull();
128 },
129 );
130 });
131
132 it("serves T3 attachment files in place like any other host path", () => {
133 const path = `/home/demo/.t3/userdata/attachments/${attachmentId}.mp4`;
134 expect(resolveMediaSource(path, { threadId, workspaceRoot: "/repo" })).toMatchObject({
135 kind: "video",
136 access: "environment",
137 resource: { _tag: "media-file", threadId, path },
138 });
139 });
140});