The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET, ThreadId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { assetImageCacheKey, assetImageSource } from "./assetImageSource"; |
| 5 | |
| 6 | const environmentId = EnvironmentId.make("environment-1"); |
| 7 | |
| 8 | describe("assetImageSource", () => { |
| 9 | it("caches an attachment under one key across re-signed URLs", () => { |
| 10 | const resource = { _tag: "attachment", attachmentId: "thread-1-photo.jpg" } as const; |
| 11 | const first = assetImageSource( |
| 12 | environmentId, |
| 13 | resource, |
| 14 | "https://host/api/assets/token-a/p.jpg", |
| 15 | ); |
| 16 | const second = assetImageSource( |
| 17 | environmentId, |
| 18 | resource, |
| 19 | "https://host/api/assets/token-b/p.jpg", |
| 20 | ); |
| 21 | |
| 22 | expect(first.cacheKey).toBeDefined(); |
| 23 | expect(second).toStrictEqual({ |
| 24 | uri: "https://host/api/assets/token-b/p.jpg", |
| 25 | cacheKey: first.cacheKey, |
| 26 | }); |
| 27 | expect( |
| 28 | assetImageSource(EnvironmentId.make("environment-2"), resource, "https://other/p.jpg") |
| 29 | .cacheKey, |
| 30 | ).not.toBe(first.cacheKey); |
| 31 | }); |
| 32 | |
| 33 | it("keeps ids that contain colons from sharing a key", () => { |
| 34 | const uri = "https://host/api/assets/token/p.jpg"; |
| 35 | const left = assetImageSource( |
| 36 | EnvironmentId.make("a"), |
| 37 | { _tag: "attachment", attachmentId: "b:c" }, |
| 38 | uri, |
| 39 | ); |
| 40 | const right = assetImageSource( |
| 41 | EnvironmentId.make("a:b"), |
| 42 | { _tag: "attachment", attachmentId: "c" }, |
| 43 | uri, |
| 44 | ); |
| 45 | |
| 46 | expect(left.cacheKey).not.toBe(right.cacheKey); |
| 47 | }); |
| 48 | |
| 49 | it("keys host files by the revision the server read, and by URL without one", () => { |
| 50 | const resource = { |
| 51 | _tag: "media-file", |
| 52 | threadId: ThreadId.make("thread-1"), |
| 53 | path: "docs/shot.png", |
| 54 | } as const; |
| 55 | const first = assetImageSource( |
| 56 | environmentId, |
| 57 | resource, |
| 58 | "https://host/api/assets/token-a/shot.png", |
| 59 | "1:2:5:1000:1000", |
| 60 | ); |
| 61 | |
| 62 | expect(first.cacheKey).toBeDefined(); |
| 63 | expect( |
| 64 | assetImageSource( |
| 65 | environmentId, |
| 66 | resource, |
| 67 | "https://host/api/assets/token-b/shot.png", |
| 68 | "1:2:5:1000:1000", |
| 69 | ), |
| 70 | ).toStrictEqual({ uri: "https://host/api/assets/token-b/shot.png", cacheKey: first.cacheKey }); |
| 71 | expect(assetImageCacheKey(environmentId, resource, "1:2:5:2000:2000")).not.toBe(first.cacheKey); |
| 72 | expect( |
| 73 | assetImageCacheKey(environmentId, { ...resource, path: "docs/other.png" }, "1:2:5:1000:1000"), |
| 74 | ).not.toBe(first.cacheKey); |
| 75 | expect( |
| 76 | assetImageCacheKey(EnvironmentId.make("environment-2"), resource, "1:2:5:1000:1000"), |
| 77 | ).not.toBe(first.cacheKey); |
| 78 | expect( |
| 79 | assetImageSource( |
| 80 | environmentId, |
| 81 | { _tag: "draft-workspace-file", cwd: "/repo", path: "logo.png" }, |
| 82 | "https://host/api/assets/token-a/logo.png", |
| 83 | ), |
| 84 | ).toStrictEqual({ uri: "https://host/api/assets/token-a/logo.png" }); |
| 85 | }); |
| 86 | |
| 87 | it("keys source control media by URL only when its bytes cannot change", () => { |
| 88 | const sha = "0123456789abcdef0123456789abcdef01234567"; |
| 89 | const key = (url: string, environment = environmentId) => |
| 90 | assetImageCacheKey(environment, { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: "/repo", url }); |
| 91 | const pinned = key(`https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`); |
| 92 | |
| 93 | expect(pinned).toBeDefined(); |
| 94 | expect(key(`https://github.com/owner/repo/blob/${sha}/docs/shot.png`)).toBe(pinned); |
| 95 | expect(key(`https://raw.githubusercontent.com/owner/repo/${sha}/docs/other.png`)).not.toBe( |
| 96 | pinned, |
| 97 | ); |
| 98 | expect( |
| 99 | key( |
| 100 | `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`, |
| 101 | EnvironmentId.make("environment-2"), |
| 102 | ), |
| 103 | ).not.toBe(pinned); |
| 104 | expect(key("https://raw.githubusercontent.com/owner/repo/main/docs/shot.png")).toBeUndefined(); |
| 105 | }); |
| 106 | }); |