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