The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 83 lines 2.75 KB
Raw
1 import { EnvironmentId } from "@t3tools/contracts";
2 import { describe, expect, it, vi } from "vite-plus/test";
3 import { userEvent } from "vite-plus/test/browser";
4
5 import "~/index.css";
6 import { render, type Screen } from "../../../test/render";
7
8 const assetUrl = vi.hoisted(() => ({ current: "about:blank" }));
9
10 vi.mock(import("~/assets/longLivedAssetUrls"), () => ({
11 useLongLivedAssetUrl: () => ({
12 _tag: "Success",
13 url: assetUrl.current,
14 expiresAt: Date.now() + 60 * 60_000,
15 }),
16 }));
17 vi.mock(import("~/assets/assetUrls"), async (importOriginal) => ({
18 ...(await importOriginal()),
19 useAssetUrlRefresh: () => async () => "about:blank",
20 }));
21
22 import { HtmlRenderFrame } from "./HtmlRenderFrame";
23
24 function setup() {
25 assetUrl.current = "about:blank";
26 }
27
28 const sizeChanged = (height: number) => ({
29 jsonrpc: "2.0",
30 method: "ui/notifications/size-changed",
31 params: { height },
32 });
33
34 describe("HtmlRenderFrame", () => {
35 it("reports leaving full screen when it unmounts while expanded", async () => {
36 setup();
37 const onFullscreenChange = vi.fn();
38 await using screen = await render(
39 <HtmlRenderFrame
40 environmentId={EnvironmentId.make("environment")}
41 htmlRender={{ attachmentId: "attachment", title: "Report", height: 200 }}
42 threadRef={undefined}
43 onFullscreenChange={onFullscreenChange}
44 />,
45 );
46
47 await userEvent.click(screen.getByRole("button", { name: "Open full screen" }));
48 await expect.element(screen.getByRole("dialog")).toBeVisible();
49 expect(onFullscreenChange).toHaveBeenLastCalledWith(true);
50
51 await screen.unmount();
52 expect(onFullscreenChange).toHaveBeenLastCalledWith(false);
53 });
54
55 it("reopens at the page height it last measured", async () => {
56 setup();
57 using page = (() => {
58 const html = `<script>parent.postMessage(${JSON.stringify(sizeChanged(1500))}, "*");</script>`;
59 const url = URL.createObjectURL(new Blob([html], { type: "text/html" }));
60 return { url, [Symbol.dispose]: () => URL.revokeObjectURL(url) };
61 })();
62 const frame = () => (
63 <HtmlRenderFrame
64 environmentId={EnvironmentId.make("environment")}
65 htmlRender={{ attachmentId: "measured", title: "Mockup", height: 200 }}
66 threadRef={undefined}
67 />
68 );
69 const frameHeight = (screen: Screen) =>
70 screen.getByTitle("Mockup").element().parentElement?.style.height;
71
72 assetUrl.current = page.url;
73 {
74 await using first = await render(frame());
75 await vi.waitFor(() => expect(frameHeight(first)).toBe("1500px"));
76 }
77
78 // A page that never reports its height, so only the remembered measurement can size the frame.
79 assetUrl.current = "about:blank";
80 await using second = await render(frame());
81 expect(frameHeight(second)).toBe("1500px");
82 });
83 });