@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 194 lines 7.47 KB
Raw
1 import { useState } from "react";
2 import type { SnapShotSource } from "@t3tools/contracts";
3 import { describe, expect, it, vi } from "vite-plus/test";
4 import { page, userEvent } from "vite-plus/test/browser";
5
6 import "~/index.css";
7 import { render } from "../../../test/render";
8 import { ExpandedImageDialog } from "./ExpandedImageDialog";
9
10 const PIXEL =
11 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
12
13 const snapShotSource: SnapShotSource = {
14 kind: "snap-shot",
15 capturedAt: "2026-01-01T00:00:00.000Z",
16 appName: "Finder",
17 windowTitle: "Downloads",
18 };
19
20 const D2_SVG =
21 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1174 920"><rect width="1174" height="920"/></svg>';
22
23 function ImagePreviewOpener() {
24 const [open, setOpen] = useState(false);
25 return (
26 <>
27 <button type="button" onClick={() => setOpen(true)}>
28 Open preview
29 </button>
30 {open && (
31 <ExpandedImageDialog
32 preview={{ images: [{ src: PIXEL, name: "shot.png", source: snapShotSource }], index: 0 }}
33 onClose={() => setOpen(false)}
34 />
35 )}
36 </>
37 );
38 }
39
40 describe("ExpandedImageDialog", () => {
41 it.each([
42 { opener: "mouse", focusVisible: false },
43 { opener: "keyboard", focusVisible: true },
44 ])("returns $opener focus to the opener on close", async (c) => {
45 await using screen = await render(<ImagePreviewOpener />, {
46 // The opener renders no links and reads no route.
47 router: false,
48 });
49 const opener = screen.getByRole("button", { name: "Open preview" });
50 if (c.opener === "mouse") {
51 await userEvent.click(opener);
52 } else {
53 await userEvent.keyboard("{Tab}");
54 await userEvent.keyboard("{Enter}");
55 }
56 await expect
57 .element(page.getByRole("button", { name: "Back, close image preview" }))
58 .toHaveFocus();
59 await userEvent.keyboard("{Escape}");
60 await expect.element(opener).toHaveFocus();
61 expect(opener.element().matches(":focus-visible")).toBe(c.focusVisible);
62 });
63
64 it.each(["/work/pipeline.svg", "/work/diagram#v2.svg", "/work/diagram?v2.svg"])(
65 "fits a markdown SVG without intrinsic size to the viewer: %s",
66 async (path) => {
67 const src = URL.createObjectURL(new Blob([D2_SVG], { type: "image/svg+xml" }));
68 using _src = { [Symbol.dispose]: () => URL.revokeObjectURL(src) };
69 await using screen = await render(
70 <ExpandedImageDialog
71 preview={{
72 images: [
73 {
74 src,
75 name: "Pipeline",
76 actionsSource: {
77 kind: "image",
78 name: "Pipeline",
79 src,
80 reference: { kind: "file", path },
81 },
82 },
83 ],
84 index: 0,
85 }}
86 onClose={vi.fn()}
87 />,
88 // The dialog renders no links and reads no route.
89 { router: false },
90 );
91
92 const viewer = screen.getByRole("region", { name: "Pipeline, zoomable image" });
93 const availableWidth = window.innerWidth * 0.92 - (window.innerWidth >= 640 ? 96 : 0);
94 await vi.waitFor(() =>
95 expect(viewer.element().getBoundingClientRect().width).toBeCloseTo(availableWidth, 0),
96 );
97 },
98 );
99
100 it("Escape closes a popup opened inside the dialog before the dialog", async () => {
101 const onClose = vi.fn();
102 await using screen = await render(
103 <ExpandedImageDialog
104 preview={{ images: [{ src: PIXEL, name: "shot.png", source: snapShotSource }], index: 0 }}
105 onClose={onClose}
106 />,
107 // The dialog renders no links and reads no route.
108 { router: false },
109 );
110
111 await userEvent.click(screen.getByRole("button", { name: "No accessibility data" }));
112 await expect.element(screen.getByText("Accessibility data")).toBeVisible();
113
114 await userEvent.keyboard("{Escape}");
115 await expect.element(screen.getByText("Accessibility data")).not.toBeInTheDocument();
116 expect(onClose).not.toHaveBeenCalled();
117
118 // Focus returns to the popup's trigger and opens its tooltip; leave it for a plain control.
119 (
120 screen.getByRole("button", { name: "Back, close image preview" }).element() as HTMLElement
121 ).focus();
122 await userEvent.keyboard("{Escape}");
123 await vi.waitFor(() => expect(onClose).toHaveBeenCalledOnce());
124 });
125
126 it("scrolling pans the image while Ctrl+wheel zooms around the pointer", async () => {
127 const canvas = document.createElement("canvas");
128 canvas.width = 1600;
129 canvas.height = 1200;
130 await using screen = await render(
131 <ExpandedImageDialog
132 preview={{ images: [{ src: canvas.toDataURL(), name: "large.png" }], index: 0 }}
133 onClose={() => {}}
134 />,
135 // The dialog renders no links and reads no route.
136 { router: false },
137 );
138 const viewportLocator = screen.getByRole("region", { name: "large.png, zoomable image" });
139 const viewport = viewportLocator.element() as HTMLElement;
140 const image = screen.getByRole("img", { name: "large.png" }).element() as HTMLElement;
141 await vi.waitFor(() => expect(image.style.width).not.toBe(""));
142 const fitWidth = image.getBoundingClientRect().width;
143 const wheels: { ctrlKey: boolean; canceled: boolean }[] = [];
144 const recordWheel = (event: WheelEvent) =>
145 wheels.push({ ctrlKey: event.ctrlKey, canceled: event.defaultPrevented });
146 document.addEventListener("wheel", recordWheel);
147 using _wheelListener = {
148 [Symbol.dispose]: () => document.removeEventListener("wheel", recordWheel),
149 };
150 // Wheels land on the viewport's center; this is the image fraction under it.
151 const pointerFraction = () => {
152 const viewportBounds = viewport.getBoundingClientRect();
153 const imageBounds = image.getBoundingClientRect();
154 return {
155 x: (viewportBounds.left + viewportBounds.width / 2 - imageBounds.left) / imageBounds.width,
156 y: (viewportBounds.top + viewportBounds.height / 2 - imageBounds.top) / imageBounds.height,
157 };
158 };
159 const ctrlWheel = async (deltaY: number) => {
160 await userEvent.keyboard("{Control>}");
161 await userEvent.wheel(viewportLocator, { delta: { y: deltaY } });
162 await userEvent.keyboard("{/Control}");
163 };
164
165 await userEvent.wheel(viewportLocator, { delta: { y: 120 } });
166 expect(wheels.at(-1)).toEqual({ ctrlKey: false, canceled: false });
167 expect(image.getBoundingClientRect().width).toBe(fitWidth);
168
169 await ctrlWheel(-100);
170 expect(wheels.at(-1)).toEqual({ ctrlKey: true, canceled: true });
171 const zoomedWidth = image.getBoundingClientRect().width;
172 expect(zoomedWidth).toBeCloseTo(fitWidth * Math.E, 0);
173
174 const pan = async (delta: { x: number; y: number }) => {
175 const { scrollLeft, scrollTop } = viewport;
176 await userEvent.wheel(viewportLocator, { delta });
177 await vi.waitFor(() => {
178 expect(Math.sign(viewport.scrollLeft - scrollLeft)).toBe(Math.sign(delta.x));
179 expect(Math.sign(viewport.scrollTop - scrollTop)).toBe(Math.sign(delta.y));
180 });
181 };
182 await pan({ x: 60, y: 0 });
183 await pan({ x: 0, y: 60 });
184 await pan({ x: -40, y: -40 });
185 expect(wheels.filter((wheel) => !wheel.ctrlKey).every((wheel) => !wheel.canceled)).toBe(true);
186 expect(image.getBoundingClientRect().width).toBe(zoomedWidth);
187
188 const anchor = pointerFraction();
189 await ctrlWheel(-50);
190 expect(image.getBoundingClientRect().width).toBeGreaterThan(zoomedWidth);
191 expect(pointerFraction().x).toBeCloseTo(anchor.x, 2);
192 expect(pointerFraction().y).toBeCloseTo(anchor.y, 2);
193 });
194 });