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

mediaSource.test.ts

97 lines3.2 KB

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 ])("resolves %s through the environment", (href, path) => {
39 expect(resolveMediaSource(href, { threadId, workspaceRoot: "/repo" })).toEqual({
40 kind: "image",
41 mimeType: "image/png",
42 name: path.split(/[\\/]/).at(-1),
43 reference: { kind: "file", path },
44 srcFragment: "",
45 access: "environment",
46 resource: { _tag: "media-file", threadId, path },
47 });
48 });
49​
50 it("joins relative paths to the workspace and records the relative reference", () => {
51 expect(
52 resolveMediaSource("screens/logo.svg?v=2#mark", { threadId, workspaceRoot: "/repo" }),
53 ).toEqual({
54 kind: "image",
55 mimeType: "image/svg+xml",
56 name: "logo.svg",
57 reference: {
58 kind: "file",
59 path: "/repo/screens/logo.svg",
60 relativePath: "screens/logo.svg",
61 },
62 srcFragment: "#mark",
63 access: "environment",
64 resource: { _tag: "media-file", threadId, path: "/repo/screens/logo.svg" },
65 });
66 });
67​
68 it("prefers a caller-resolved path over classifying the source", () => {
69 expect(
70 resolveMediaSource("logo.svg", {
71 threadId,
72 workspaceRoot: "/repo",
73 resolvedFilePath: "/repo/docs/logo.svg",
74 }),
75 ).toMatchObject({
76 access: "environment",
77 resource: { _tag: "media-file", path: "/repo/docs/logo.svg" },
78 reference: { relativePath: "docs/logo.svg" },
79 });
80 });
81​
82 it("separates a playback fragment from literal filename characters", () => {
83 expect(resolveMediaSource("/tmp/clip%23one.mp4#t=2", { threadId })).toMatchObject({
84 kind: "video",
85 srcFragment: "#t=2",
86 resource: { path: "/tmp/clip#one.mp4" },
87 });
88 });
89​
90 it("cannot be loaded without a thread to mint through", () => {
91 expect(resolveMediaSource("/tmp/clip.mp4", { threadId: undefined })).toMatchObject({
92 kind: "video",
93 access: "unavailable",
94 });
95 });
96 });
97});