import { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts"; import { describe, expect, it, vi } from "vite-plus/test"; import { userEvent } from "vite-plus/test/browser"; import "~/index.css"; import { act, fakeTimers, render } from "../../../test/render"; // Asks to close twice when told to, and answers only the first teardown, so // the second one is left to finish on the host's timeout. const APP_HTML = ``; vi.mock(import("~/assets/longLivedAssetUrls"), () => ({ useLongLivedAssetUrl: () => ({ _tag: "Success", url: `data:text/html,${encodeURIComponent(APP_HTML)}`, expiresAt: Date.now() + 60 * 60_000, }), })); import { McpAppFrame } from "./McpAppFrame"; const nextInitializedFrom = () => new Promise((resolve) => { const listen = (event: MessageEvent) => { if (event.source === null || event.data?.method !== "ui/notifications/initialized") return; window.removeEventListener("message", listen); resolve(event.source); }; window.addEventListener("message", listen); }); describe("McpAppFrame", () => { it("opens a clicked same-environment thread in the current router", async () => { const initialized = nextInitializedFrom(); await using screen = await render( , ); await initialized; using external = vi.spyOn(window, "open").mockReturnValue(null); await userEvent.click(screen.getByTitle("Demo app")); await expect .poll(() => screen.router?.state.location.pathname) .toBe("/environment/thread%3Aprovider%252F1"); expect(external).not.toHaveBeenCalled(); }); it("keeps a reopened app when the old document's second teardown finishes late", async () => { const firstInitialized = nextInitializedFrom(); await using screen = await render( , ); const firstApp = await firstInitialized; using _timers = fakeTimers(); (firstApp as Window).postMessage("close", "*"); await userEvent.click(screen.getByRole("button", { name: "Show app" })); await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument(); await act(() => vi.advanceTimersByTimeAsync(2_000)); expect(screen.getByRole("button", { name: "Show app" }).query()).toBeNull(); await expect.element(screen.getByTitle("Demo app")).toBeInTheDocument(); }); });