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");
7
8describe("resolveMediaSource", () => {
9 describe("direct URLs", () => {
10 it("keeps the authored URL and decodes the display name once", () => {
11 const href = "https://cdn.example.com/clip%20one%2520%2Emp4?signature=a%2fb#t=2";
12 expect(resolveMediaSource(href, { threadId })).toEqual({
13 kind: "video",
14 mimeType: "video/mp4",
15 name: "clip one%20.mp4",
16 reference: { kind: "url", url: href },
17 srcFragment: "#t=2",
18 access: "direct",
19 uri: href,
20 });
21 });
22
23 it("accepts extensionless image embeds only when asked", () => {
24 const href = "https://cdn.example.com/render?id=42";
25 expect(resolveMediaSource(href, { threadId })).toBeNull();
26 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toMatchObject({
27 kind: "image",
28 mimeType: "image/*",
29 name: "render",
30 });
31 });
32 });
33
34 describe("host paths", () => {
35 it.each([
36 ["/tmp/frame%23one.png:12", "/tmp/frame#one.png"],
37 ["/tmp/frame%2523one.png:12", "/tmp/frame%23one.png"],
38 ["file://server/share/frame.png", "\\\\server\\share\\frame.png"],
39 ])("resolves %s through the environment", (href, path) => {
40 expect(resolveMediaSource(href, { threadId, workspaceRoot: "/repo" })).toEqual({
41 kind: "image",
42 mimeType: "image/png",
43 name: path.split(/[\\/]/).at(-1),
44 reference: { kind: "file", path },
45 srcFragment: "",
46 access: "environment",
47 resource: { _tag: "media-file", threadId, path },
48 });
49 });
50
51 it("joins relative paths to the workspace and records the relative reference", () => {
52 expect(
53 resolveMediaSource("screens/logo.svg?v=2#mark", { threadId, workspaceRoot: "/repo" }),
54 ).toEqual({
55 kind: "image",
56 mimeType: "image/svg+xml",
57 name: "logo.svg",
58 reference: {
59 kind: "file",
60 path: "/repo/screens/logo.svg",
61 relativePath: "screens/logo.svg",
62 },
63 srcFragment: "#mark",
64 access: "environment",
65 resource: { _tag: "media-file", threadId, path: "/repo/screens/logo.svg" },
66 });
67 });
68
69 it("prefers a caller-resolved path over classifying the source", () => {
70 expect(
71 resolveMediaSource("logo.svg", {
72 threadId,
73 workspaceRoot: "/repo",
74 resolvedFilePath: "/repo/docs/logo.svg",
75 }),
76 ).toMatchObject({
77 access: "environment",
78 resource: { _tag: "media-file", path: "/repo/docs/logo.svg" },
79 reference: { relativePath: "docs/logo.svg" },
80 });
81 });
82
83 it("separates a playback fragment from literal filename characters", () => {
84 expect(resolveMediaSource("/tmp/clip%23one.mp4#t=2", { threadId })).toMatchObject({
85 kind: "video",
86 srcFragment: "#t=2",
87 resource: { path: "/tmp/clip#one.mp4" },
88 });
89 });
90
91 it("cannot be loaded without a thread to mint through", () => {
92 expect(resolveMediaSource("/tmp/clip.mp4", { threadId: undefined })).toMatchObject({
93 kind: "video",
94 access: "unavailable",
95 });
96 });
97 });
98});