The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { EnvironmentId, ThreadId, TurnItemId } 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 { act, fakeTimers, render } from "../../../test/render"; |
| 7 | |
| 8 | // Asks to close twice when told to, and answers only the first teardown, so |
| 9 | // the second one is left to finish on the host's timeout. |
| 10 | const APP_HTML = `<script> |
| 11 | let answered = false; |
| 12 | addEventListener("message", (event) => { |
| 13 | const message = event.data; |
| 14 | if (message === "close") { |
| 15 | parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/request-teardown" }, "*"); |
| 16 | parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/request-teardown" }, "*"); |
| 17 | } else if (message?.method === "ui/resource-teardown" && !answered) { |
| 18 | answered = true; |
| 19 | parent.postMessage({ jsonrpc: "2.0", id: message.id, result: {} }, "*"); |
| 20 | } |
| 21 | }); |
| 22 | parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/initialized" }, "*"); |
| 23 | </script>`; |
| 24 | |
| 25 | vi.mock(import("~/assets/longLivedAssetUrls"), () => ({ |
| 26 | useLongLivedAssetUrl: () => ({ |
| 27 | _tag: "Success", |
| 28 | url: `data:text/html,${encodeURIComponent(APP_HTML)}`, |
| 29 | expiresAt: Date.now() + 60 * 60_000, |
| 30 | }), |
| 31 | })); |
| 32 | |
| 33 | import { McpAppFrame } from "./McpAppFrame"; |
| 34 | |
| 35 | const nextInitializedFrom = () => |
| 36 | new Promise<MessageEventSource>((resolve) => { |
| 37 | const listen = (event: MessageEvent) => { |
| 38 | if (event.source === null || event.data?.method !== "ui/notifications/initialized") return; |
| 39 | window.removeEventListener("message", listen); |
| 40 | resolve(event.source); |
| 41 | }; |
| 42 | window.addEventListener("message", listen); |
| 43 | }); |
| 44 | |
| 45 | describe("McpAppFrame", () => { |
| 46 | it("keeps a reopened app when the old document's second teardown finishes late", async () => { |
| 47 | const firstInitialized = nextInitializedFrom(); |
| 48 | await using screen = await render( |
| 49 | <McpAppFrame |
| 50 | environmentId={EnvironmentId.make("environment")} |
| 51 | threadId={ThreadId.make("thread")} |
| 52 | conversationThreadId={ThreadId.make("thread")} |
| 53 | itemId={TurnItemId.make("item")} |
| 54 | revision="1" |
| 55 | app={{ |
| 56 | attachmentId: "attachment", |
| 57 | server: "Demo", |
| 58 | tool: "show", |
| 59 | resourceUri: "ui://demo/app", |
| 60 | }} |
| 61 | onSendMessage={undefined} |
| 62 | />, |
| 63 | ); |
| 64 | const firstApp = await firstInitialized; |
| 65 | using _timers = fakeTimers(); |
| 66 | |
| 67 | (firstApp as Window).postMessage("close", "*"); |
| 68 | await userEvent.click(screen.getByRole("button", { name: "Show app" })); |
| 69 | await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument(); |
| 70 | |
| 71 | await act(() => vi.advanceTimersByTimeAsync(2_000)); |
| 72 | expect(screen.getByRole("button", { name: "Show app" }).query()).toBeNull(); |
| 73 | await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument(); |
| 74 | }); |
| 75 | }); |