|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { AsyncResult } from "effect/reactivity"; |
| 3 | +import { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +const mint = vi.hoisted(() => vi.fn()); |
| 8 | +// Atoms derived from the server config run in the real registry over a missing config. |
| 9 | +vi.mock(import("../state/server"), async (original) => { |
| 10 | + const actual = await original(); |
| 11 | + const { Atom } = await import("effect/reactivity"); |
| 12 | + const noConfig = Atom.make(null); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => noConfig) as never }, |
| 16 | + }; |
| 17 | +}); |
| 18 | +vi.mock(import("../state/use-atom-query-runner"), async (original) => ({ |
| 19 | + ...(await original()), |
| 20 | + useAtomQueryRunner: (() => mint) as never, |
| 21 | +})); |
| 22 | +vi.mock(import("../state/session"), async (original) => ({ |
| 23 | + ...(await original()), |
| 24 | + readEnvironmentScope: () => false, |
| 25 | + useEnvironmentScope: () => false, |
| 26 | + readPreparedConnection: (() => ({ httpBaseUrl: "https://host.test" })) as never, |
| 27 | +})); |
| 28 | + |
| 29 | +import ChatMarkdown from "./ChatMarkdown"; |
| 30 | + |
| 31 | +const roots: Root[] = []; |
| 32 | +beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true)); |
| 33 | +afterEach(async () => { |
| 34 | + for (const root of roots.splice(0)) await act(async () => root.unmount()); |
| 35 | + document.body.replaceChildren(); |
| 36 | + vi.unstubAllGlobals(); |
| 37 | +}); |
| 38 | + |
| 39 | +it("opens host media through server authorization before the client grant loads", async () => { |
| 40 | + mint.mockResolvedValue( |
| 41 | + AsyncResult.success({ relativeUrl: "/api/assets/image.png", expiresAt: 1 }), |
| 42 | + ); |
| 43 | + const onImageExpand = vi.fn(); |
| 44 | + const container = document.body.appendChild(document.createElement("div")); |
| 45 | + const root = createRoot(container); |
| 46 | + roots.push(root); |
| 47 | + await act(async () => |
| 48 | + root.render( |
| 49 | + <ChatMarkdown |
| 50 | + cwd="/repo" |
| 51 | + threadRef={{ |
| 52 | + environmentId: EnvironmentId.make("media-environment"), |
| 53 | + threadId: ThreadId.make("media-thread"), |
| 54 | + }} |
| 55 | + text="[Open image](/tmp/image.png)" |
| 56 | + onImageExpand={onImageExpand} |
| 57 | + />, |
| 58 | + ), |
| 59 | + ); |
| 60 | + const link = container.querySelector<HTMLAnchorElement>('a[href="/tmp/image.png"]'); |
| 61 | + expect(link).not.toBeNull(); |
| 62 | + await act(async () => link!.click()); |
| 63 | + await vi.waitFor(() => |
| 64 | + expect(onImageExpand).toHaveBeenCalledWith( |
| 65 | + expect.objectContaining({ |
| 66 | + images: [expect.objectContaining({ src: "https://host.test/api/assets/image.png" })], |
| 67 | + }), |
| 68 | + ), |
| 69 | + ); |
| 70 | +}); |
0 commit comments