1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { assetImageSource } from "./assetImageSource";
5
6const environmentId = EnvironmentId.make("environment-1");
7
8describe("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("keeps workspace files keyed by URL because they can change on disk", () => {
50 const source = assetImageSource(
51 environmentId,
52 { _tag: "draft-workspace-file", cwd: "/repo", path: "logo.png" },
53 "https://host/api/assets/token-a/logo.png",
54 );
55
56 expect(source).toStrictEqual({ uri: "https://host/api/assets/token-a/logo.png" });
57 });
58});