@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 128 lines 4.29 KB
Raw
1 import { ThreadId } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import { resolveMediaSource } from "./mediaSource.ts";
5
6 const threadId = ThreadId.make("thread-1");
7
8 describe("resolveMediaSource", () => {
9 describe("direct URLs", () => {
10 it.each(["https://cdn.example.com/tone.mp3", "data:audio/wav;base64,AAAA"])(
11 "keeps %s out of the image and video viewer",
12 (href) => {
13 expect(resolveMediaSource(href, { threadId })).toBeNull();
14 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toBeNull();
15 },
16 );
17
18 it("keeps the authored URL and decodes the display name once", () => {
19 const href = "https://cdn.example.com/clip%20one%2520%2Emp4?signature=a%2fb#t=2";
20 expect(resolveMediaSource(href, { threadId })).toEqual({
21 kind: "video",
22 mimeType: "video/mp4",
23 name: "clip one%20.mp4",
24 reference: { kind: "url", url: href },
25 srcFragment: "#t=2",
26 access: "direct",
27 uri: href,
28 });
29 });
30
31 it("accepts extensionless image embeds only when asked", () => {
32 const href = "https://cdn.example.com/render?id=42";
33 expect(resolveMediaSource(href, { threadId })).toBeNull();
34 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toMatchObject({
35 kind: "image",
36 mimeType: "image/*",
37 name: "render",
38 });
39 });
40
41 it("names a data URI by its kind, not its payload", () => {
42 const href =
43 "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciLz4=";
44 expect(resolveMediaSource(href, { threadId, imageEmbed: true })).toMatchObject({
45 kind: "image",
46 name: "image",
47 });
48 });
49 });
50
51 describe("host paths", () => {
52 it.each(["tone.wav", "tone.mp3", "tone.oga", "tone.webm-audio"])(
53 "keeps workspace audio %s out of the image fallback",
54 (path) => {
55 expect(resolveMediaSource(path, { threadId, imageEmbed: true })).toBeNull();
56 expect(
57 resolveMediaSource("authored.wav", {
58 threadId,
59 imageEmbed: true,
60 resolvedFilePath: path,
61 }),
62 ).toBeNull();
63 },
64 );
65
66 it.each([
67 ["/tmp/frame%23one.png:12", "/tmp/frame#one.png"],
68 ["/tmp/frame%2523one.png:12", "/tmp/frame%23one.png"],
69 ])("resolves %s through the environment", (href, path) => {
70 expect(resolveMediaSource(href, { threadId, workspaceRoot: "/repo" })).toEqual({
71 kind: "image",
72 mimeType: "image/png",
73 name: path.split(/[\\/]/).at(-1),
74 reference: { kind: "file", path },
75 srcFragment: "",
76 access: "environment",
77 resource: { _tag: "media-file", threadId, path },
78 });
79 });
80
81 it("joins relative paths to the workspace and records the relative reference", () => {
82 expect(
83 resolveMediaSource("screens/logo.svg?v=2#mark", { threadId, workspaceRoot: "/repo" }),
84 ).toEqual({
85 kind: "image",
86 mimeType: "image/svg+xml",
87 name: "logo.svg",
88 reference: {
89 kind: "file",
90 path: "/repo/screens/logo.svg",
91 relativePath: "screens/logo.svg",
92 },
93 srcFragment: "#mark",
94 access: "environment",
95 resource: { _tag: "media-file", threadId, path: "/repo/screens/logo.svg", hostOnly: true },
96 });
97 });
98
99 it("prefers a caller-resolved path over classifying the source", () => {
100 expect(
101 resolveMediaSource("logo.svg", {
102 threadId,
103 workspaceRoot: "/repo",
104 resolvedFilePath: "/repo/docs/logo.svg",
105 }),
106 ).toMatchObject({
107 access: "environment",
108 resource: { _tag: "media-file", path: "/repo/docs/logo.svg" },
109 reference: { relativePath: "docs/logo.svg" },
110 });
111 });
112
113 it("separates a playback fragment from literal filename characters", () => {
114 expect(resolveMediaSource("/tmp/clip%23one.mp4#t=2", { threadId })).toMatchObject({
115 kind: "video",
116 srcFragment: "#t=2",
117 resource: { path: "/tmp/clip#one.mp4" },
118 });
119 });
120
121 it("cannot be loaded without a thread to mint through", () => {
122 expect(resolveMediaSource("/tmp/clip.mp4", { threadId: undefined })).toMatchObject({
123 kind: "video",
124 access: "unavailable",
125 });
126 });
127 });
128 });