Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6
7const mint = vi.hoisted(() => vi.fn());
8// Atoms derived from the server config run in the real registry over a missing config.
9vi.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});
18vi.mock(import("../state/use-atom-query-runner"), async (original) => ({
19 ...(await original()),
20 useAtomQueryRunner: (() => mint) as never,
21}));
22vi.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
29import ChatMarkdown from "./ChatMarkdown";
30
31const roots: Root[] = [];
32beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true));
33afterEach(async () => {
34 for (const root of roots.splice(0)) await act(async () => root.unmount());
35 document.body.replaceChildren();
36 vi.unstubAllGlobals();
37});
38
39it("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});