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

ChatMarkdownMedia.browser.test.tsx

102 lines3.8 KB

1import type { PreparedConnection } from "@t3tools/client-runtime/connection";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { markdownTree } from "~/markdown/wordsHast";
4import { describe, expect, it, vi } from "vite-plus/test";
5​
6import { render } from "../../../test/render";
7import { ChatMarkdownAssetImage, markdownImageCopy } from "./ChatMarkdownMedia";
8​
9// Every evaluation of the URL query is one mint: the first read, then one per refresh.
10const assetQuery = vi.hoisted(() => ({ mints: 0 }));
11vi.mock(import("~/state/assets"), async (original) => {
12 const { AsyncResult, Atom } = await import("effect/reactivity");
13 const createUrl = Atom.family((_key: string) =>
14 Atom.make(() => {
15 assetQuery.mints += 1;
16 return AsyncResult.success({
17 relativeUrl:
18 "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
19 expiresAt: Number.MAX_SAFE_INTEGER,
20 });
21 }).pipe(Atom.keepAlive),
22 );
23 const actual = await original();
24 return {
25 ...actual,
26 assetEnvironment: {
27 ...actual.assetEnvironment,
28 createUrl: (target: { readonly input: unknown }) => createUrl(JSON.stringify(target.input)),
29 },
30 };
31});
32vi.mock(import("~/state/session"), async (original) => {
33 const { Option } = await import("effect");
34 const { PrimaryConnectionTarget } = await import("@t3tools/client-runtime/connection");
35 const { EnvironmentId } = await import("@t3tools/contracts");
36 const target = new PrimaryConnectionTarget({
37 environmentId: EnvironmentId.make("environment-1"),
38 label: "Local",
39 httpBaseUrl: "http://localhost:1",
40 wsBaseUrl: "ws://localhost:1",
41 });
42 const prepared: PreparedConnection = {
43 environmentId: target.environmentId,
44 label: target.label,
45 httpBaseUrl: target.httpBaseUrl,
46 socketUrl: target.wsBaseUrl,
47 httpAuthorization: null,
48 target,
49 };
50 return {
51 ...(await original()),
52 usePreparedConnection: () => Option.some(prepared),
53 };
54});
55vi.mock(import("~/state/filesystem"), async (original) => ({
56 ...(await original()),
57 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
58}));
59​
60describe("ChatMarkdownAssetImage", () => {
61 it("does not re-mint an unchanged workspace image's URL on remount", async () => {
62 assetQuery.mints = 0;
63 const image = (
64 <ChatMarkdownAssetImage
65 environmentId={EnvironmentId.make("environment-1")}
66 resource={{
67 _tag: "workspace-file",
68 threadId: ThreadId.make("thread-1"),
69 path: "docs/diagram.png",
70 }}
71 alt="diagram"
72 workspaceMutationId="mutation-1"
73 />
74 );
75​
76 {
77 await using screen = await render(image);
78 await expect.element(screen.getByRole("img", { name: "diagram" })).toBeInTheDocument();
79 }
80 // The first mount at a known revision re-mints the cached URL once.
81 expect(assetQuery.mints).toBe(2);
82​
83 await using screen = await render(<ChatMarkdownAssetImage {...image.props} />);
84 await expect.element(screen.getByRole("img", { name: "diagram" })).toBeInTheDocument();
85 expect(assetQuery.mints).toBe(2);
86 });
87});
88​
89describe("markdownImageCopy", () => {
90 it("copies an image whose path has a space as Markdown that is still that image", () => {
91 // Raw HTML <img> and <picture> sources reach the copy with the space unencoded.
92 const copied = markdownImageCopy("Shot", "/work/my shot.png", undefined);
93 const [block] = markdownTree(copied).children;
94 const images =
95 block?.type === "element" && block.tagName === "p"
96 ? block.children.filter((node) => node.type === "element" && node.tagName === "img")
97 : [];
98 expect(
99 images.map((image) => image.type === "element" && decodeURI(String(image.properties.src))),
100 ).toEqual(["/work/my shot.png"]);
101 });
102});