|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import type { |
| 3 | + PreviewStreamEvents, |
| 4 | + PreviewStreamInput, |
| 5 | +} from "@t3tools/client-runtime/preview/server-browser-stream"; |
| 6 | + |
| 7 | +import "~/index.css"; |
| 8 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 9 | + |
| 10 | +import { render } from "../../test/render"; |
| 11 | + |
| 12 | +const mocks = vi.hoisted(() => ({ |
| 13 | + send: vi.fn<(input: PreviewStreamInput) => void>(), |
| 14 | + connect: vi.fn(), |
| 15 | + events: null as PreviewStreamEvents | null, |
| 16 | + painted: null as (() => void) | null, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock(import("@t3tools/client-runtime/preview/server-browser-stream"), async (original) => ({ |
| 20 | + ...(await original()), |
| 21 | + createPreviewFramePainter: (_canvas, onPainted) => { |
| 22 | + mocks.painted = onPainted ?? null; |
| 23 | + return { paint: () => undefined, stop: () => undefined }; |
| 24 | + }, |
| 25 | + createPreviewStreamClient: (_options, events) => { |
| 26 | + mocks.events = events; |
| 27 | + mocks.connect(); |
| 28 | + return { send: mocks.send, stop: () => undefined } as never; |
| 29 | + }, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock(import("~/state/previewStream"), async (original) => ({ |
| 33 | + ...(await original()), |
| 34 | + usePreviewStreamAccess: () => ({}) as never, |
| 35 | +})); |
| 36 | + |
| 37 | +import { ServerBrowserSurface } from "./ServerBrowserSurface"; |
| 38 | + |
| 39 | +async function renderSurface() { |
| 40 | + mocks.send.mockReset(); |
| 41 | + mocks.connect.mockReset(); |
| 42 | + const container = document.createElement("div"); |
| 43 | + container.style.cssText = "width: 400px; height: 300px;"; |
| 44 | + await using stack = new AsyncDisposableStack(); |
| 45 | + stack.use( |
| 46 | + await render( |
| 47 | + <ServerBrowserSurface |
| 48 | + environmentId={EnvironmentId.make("environment-1")} |
| 49 | + threadId={ThreadId.make("thread-1")} |
| 50 | + tabId="tab-1" |
| 51 | + visible |
| 52 | + />, |
| 53 | + { container }, |
| 54 | + ), |
| 55 | + ); |
| 56 | + const canvas = container.querySelector("canvas"); |
| 57 | + if (!canvas) throw new Error("The surface did not render its canvas."); |
| 58 | + // The stream connects once the surface has measured its size. |
| 59 | + await vi.waitFor(() => expect(mocks.connect).toHaveBeenCalled()); |
| 60 | + return Object.assign(stack.move(), { canvas }); |
| 61 | +} |
| 62 | + |
| 63 | +const mouseUp = (canvas: HTMLCanvasElement, button: number) => { |
| 64 | + const event = new MouseEvent("mouseup", { button, bubbles: true, cancelable: true }); |
| 65 | + canvas.dispatchEvent(event); |
| 66 | + return event; |
| 67 | +}; |
| 68 | + |
| 69 | +describe("ServerBrowserSurface", () => { |
| 70 | + it("routes mouse thumb buttons to the preview's history instead of the app", async () => { |
| 71 | + await using surface = await renderSurface(); |
| 72 | + const { canvas } = surface; |
| 73 | + |
| 74 | + expect(mouseUp(canvas, 3).defaultPrevented).toBe(true); |
| 75 | + expect(mouseUp(canvas, 4).defaultPrevented).toBe(true); |
| 76 | + expect(mocks.send.mock.calls).toEqual([ |
| 77 | + [{ type: "history", delta: -1 }], |
| 78 | + [{ type: "history", delta: 1 }], |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it("leaves other mouse buttons alone", async () => { |
| 83 | + await using surface = await renderSurface(); |
| 84 | + const { canvas } = surface; |
| 85 | + |
| 86 | + expect(mouseUp(canvas, 0).defaultPrevented).toBe(false); |
| 87 | + expect(mocks.send).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("does not forward thumb-button presses to the page", async () => { |
| 91 | + await using surface = await renderSurface(); |
| 92 | + const { canvas } = surface; |
| 93 | + canvas.width = 400; |
| 94 | + canvas.height = 300; |
| 95 | + // Synthetic pointers have no active pointer id to capture. |
| 96 | + canvas.setPointerCapture = () => undefined; |
| 97 | + mocks.painted?.(); |
| 98 | + mocks.events?.onViewport({ width: 400, height: 300 }); |
| 99 | + mocks.events?.onControl?.({ canOperate: true, controller: "you", generation: 1, dialog: null }); |
| 100 | + const { left, top } = canvas.getBoundingClientRect(); |
| 101 | + const press = (type: "pointerdown" | "pointerup", button: number) => |
| 102 | + canvas.dispatchEvent( |
| 103 | + new PointerEvent(type, { |
| 104 | + button, |
| 105 | + buttons: type === "pointerdown" ? 1 << button : 0, |
| 106 | + clientX: left + 10, |
| 107 | + clientY: top + 10, |
| 108 | + pointerType: "mouse", |
| 109 | + bubbles: true, |
| 110 | + }), |
| 111 | + ); |
| 112 | + const mouseInputs = () => mocks.send.mock.calls.filter(([input]) => input.type === "mouse"); |
| 113 | + |
| 114 | + for (const button of [3, 4]) { |
| 115 | + press("pointerdown", button); |
| 116 | + press("pointerup", button); |
| 117 | + } |
| 118 | + expect(mouseInputs()).toEqual([]); |
| 119 | + |
| 120 | + press("pointerdown", 0); |
| 121 | + expect(mouseInputs()).not.toEqual([]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments