@devries-cameron20 / t3code-1 / commits / 97efada6a9
Fork 0

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

test(web): file browser and attachment image tests run in a real browser (#21297)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 18, 2026 at 14:08 UTC 97efada6a91c95d925591c352da19419d8a23cba
2 files changed +89 -57
apps/web/src/components/files/FileBrowserPanel.browser.test.tsx renamed
+39 -28
@@ -1,11 +1,8 @@
1 -import { FileTree } from "@pierre/trees/react";
2 -import type { FileTree as FileTreeModel } from "@pierre/trees";
1 import { EnvironmentId, type ProjectEntry } from "@t3tools/contracts";
2 import { act, type ReactNode } from "react";
5 -import { create, type ReactTestRenderer } from "react-test-renderer";
3 +import { createRoot, type Root } from "react-dom/client";
4 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5
8 -import { InputGroupInput } from "~/components/ui/input-group";
6 import FileBrowserPanel from "./FileBrowserPanel";
7
8 const files = vi.hoisted(() => ({
@@ -13,6 +10,7 @@ const files = vi.hoisted(() => ({
10 searchEntries: [] as ProjectEntry[],
11 searchPending: false,
12 searchError: null as string | null,
13 + loadingDirectories: new Set<string>() as ReadonlySet<string>,
14 load: vi.fn(async () => {}),
15 refresh: vi.fn(),
16 }));
@@ -20,6 +18,7 @@ const files = vi.hoisted(() => ({
18 vi.mock("./useDirectoryEntries", () => ({
19 useDirectoryEntries: () => ({
20 entries: files.directoryEntries,
21 + loadingDirectories: files.loadingDirectories,
22 load: files.load,
23 refresh: files.refresh,
24 ready: true,
@@ -50,7 +49,8 @@ vi.mock("~/components/ui/tooltip", () => ({
49 }));
50
51 describe("Files panel search", () => {
53 - let renderer: ReactTestRenderer | undefined;
52 + let root: Root | undefined;
53 + let container: HTMLDivElement | undefined;
54 const props = {
55 environmentId: EnvironmentId.make("test-environment"),
56 cwd: "/workspace",
@@ -62,36 +62,47 @@ describe("Files panel search", () => {
62 };
63
64 async function render() {
65 - await act(async () => {
66 - if (renderer) renderer.update(<FileBrowserPanel {...props} />);
67 - else renderer = create(<FileBrowserPanel {...props} />);
68 - });
65 + if (!container) {
66 + container = document.body.appendChild(document.createElement("div"));
67 + // The tree virtualizes its rows, so it needs a real viewport to render any.
68 + container.style.cssText =
69 + "display: flex; flex-direction: column; width: 320px; height: 480px;";
70 + root = createRoot(container);
71 + }
72 + await act(async () => root!.render(<FileBrowserPanel {...props} />));
73 + }
74 +
75 + function searchInput() {
76 + const input = container!.querySelector<HTMLInputElement>('input[type="search"]');
77 + expect(input).not.toBeNull();
78 + return input!;
79 }
80
81 async function search(value: string) {
82 + const input = searchInput();
83 + // React tracks the last value it saw on the element, so set it through the native setter.
84 + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, value);
85 await act(async () => {
73 - renderer!.root.findByType(InputGroupInput).props.onChange({ target: { value } });
86 + input.dispatchEvent(new Event("input", { bubbles: true }));
87 });
88 }
89
90 function visiblePaths() {
78 - const trees = renderer!.root.findAllByType(FileTree);
79 - if (trees.length === 0) return [];
80 - const model: FileTreeModel = trees[0]!.props.model;
81 - return model.getVisibleRows(0, model.getVisibleCount()).map((row) => row.path);
91 + const tree = container!.querySelector("file-tree-container");
92 + if (!tree) return [];
93 + return [...tree.shadowRoot!.querySelectorAll("[data-item-path]")].map((row) =>
94 + row.getAttribute("data-item-path"),
95 + );
96 }
97
98 function hasMessage(message: string) {
85 - return (
86 - renderer!.root.findAll((node) => node.type === "div" && node.props.children === message)
87 - .length > 0
99 + return [...container!.querySelectorAll("div")].some(
100 + (node) => node.childNodes.length === 1 && node.textContent === message,
101 );
102 }
103
104 beforeEach(() => {
92 - vi.useFakeTimers();
105 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
94 - vi.stubGlobal("document", new EventTarget());
106 files.directoryEntries = [
107 { path: "src", kind: "directory" },
108 { path: "src/app.ts", kind: "file" },
@@ -103,10 +114,10 @@ describe("Files panel search", () => {
114 });
115
116 afterEach(async () => {
106 - await act(async () => renderer?.unmount());
107 - renderer = undefined;
108 - vi.runOnlyPendingTimers();
109 - vi.useRealTimers();
117 + await act(async () => root?.unmount());
118 + container?.remove();
119 + root = undefined;
120 + container = undefined;
121 vi.unstubAllGlobals();
122 });
123
@@ -154,12 +165,12 @@ describe("Files panel search", () => {
165 await search("missing-file");
166 expect(visiblePaths()).toEqual([]);
167
168 + const input = searchInput();
169 + input.focus();
170 await act(async () => {
158 - renderer!.root.findByType(InputGroupInput).props.onKeyDown({
159 - key: "Escape",
160 - currentTarget: { blur: vi.fn() },
161 - });
171 + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
172 });
173 + expect(document.activeElement).not.toBe(input);
174 expect(visiblePaths()).toEqual(["src/", "package.json"]);
175 expect(hasMessage("No matching files.")).toBe(false);
176 });
@@ -170,6 +181,6 @@ describe("Files panel search", () => {
181 await search("missing-file");
182 expect(visiblePaths()).toEqual([]);
183 expect(hasMessage("No matching files.")).toBe(false);
173 - expect(JSON.stringify(renderer!.toJSON())).toContain("Workspace search failed.");
184 + expect(container!.textContent).toContain("Workspace search failed.");
185 });
186 });
apps/web/src/components/media/AttachmentImage.browser.test.tsx renamed
+50 -29
@@ -1,5 +1,5 @@
1 -import { act, type SyntheticEvent } from "react";
2 -import { create, type ReactTestRenderer } from "react-test-renderer";
1 +import { act, type ReactNode } from "react";
2 +import { createRoot, type Root } from "react-dom/client";
3 import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4 import { AttachmentImage } from "./AttachmentImage";
5 import { prepareImageForAttachment } from "../../lib/imageCompression";
@@ -9,10 +9,10 @@ vi.mock("../../lib/imageCompression", async (importOriginal) => ({
9 prepareImageForAttachment: vi.fn(),
10 }));
11
12 -let renderer: ReactTestRenderer;
12 +let root: Root | undefined;
13 +let container: HTMLDivElement | undefined;
14 const fetchImage = vi.fn();
15 const revokeUrl = vi.fn();
15 -const decodeError = {} as SyntheticEvent<HTMLImageElement>;
16 const jpeg = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" });
17
18 beforeEach(() => {
@@ -29,53 +29,76 @@ beforeEach(() => {
29 });
30
31 afterEach(async () => {
32 - await act(() => renderer?.unmount());
32 + await act(() => root?.unmount());
33 + container?.remove();
34 + root = undefined;
35 + container = undefined;
36 vi.restoreAllMocks();
37 vi.unstubAllGlobals();
38 vi.clearAllMocks();
39 });
40
41 +async function render(element: ReactNode) {
42 + if (!container) {
43 + container = document.body.appendChild(document.createElement("div"));
44 + root = createRoot(container);
45 + }
46 + await act(() => root!.render(element));
47 +}
48 +
49 +function image() {
50 + const img = container!.querySelector("img");
51 + expect(img).not.toBeNull();
52 + return img!;
53 +}
54 +
55 +// Stands in for the browser failing to decode the current source.
56 +async function failDecode() {
57 + const img = image();
58 + await act(() => {
59 + img.dispatchEvent(new Event("error"));
60 + });
61 + return img;
62 +}
63 +
64 +function expectDecodeErrorFor(onError: ReturnType<typeof vi.fn>, img: HTMLImageElement) {
65 + expect(onError).toHaveBeenCalledWith(expect.objectContaining({ type: "error", target: img }));
66 +}
67 +
68 it.each([
69 { name: "photo.HEIC", mimeType: undefined },
70 { name: "photo", mimeType: "image/heif" },
71 ])(
72 "converts $name after native decoding fails and releases its preview",
73 async ({ name, mimeType }) => {
44 - await act(() => {
45 - renderer = create(
46 - <AttachmentImage src="https://environment.test/photo" name={name} mimeType={mimeType} />,
47 - );
48 - });
74 + await render(
75 + <AttachmentImage src="https://environment.test/photo" name={name} mimeType={mimeType} />,
76 + );
77 expect(fetchImage).not.toHaveBeenCalled();
50 - await act(() => renderer.root.findByType("img").props.onError(decodeError));
51 - expect(renderer.root.findByType("img").props.src).toBe("blob:jpeg");
78 + await failDecode();
79 + expect(image().getAttribute("src")).toBe("blob:jpeg");
80 expect(prepareImageForAttachment).toHaveBeenCalledWith(
81 expect.objectContaining({ name, type: "image/heic" }),
82 50 * 1024 * 1024,
83 );
56 - await act(() => renderer.unmount());
84 + await act(() => root!.unmount());
85 + root = undefined;
86 expect(revokeUrl).toHaveBeenCalledWith("blob:jpeg");
87 },
88 );
89
90 it("leaves PNG errors to the caller without fetching or converting", async () => {
91 const onError = vi.fn();
63 - await act(() => {
64 - renderer = create(<AttachmentImage src="blob:png" name="photo.png" onError={onError} />);
65 - });
66 - await act(() => renderer.root.findByType("img").props.onError(decodeError));
67 - expect(onError).toHaveBeenCalledWith(decodeError);
92 + await render(<AttachmentImage src="blob:png" name="photo.png" onError={onError} />);
93 + expectDecodeErrorFor(onError, await failDecode());
94 expect(fetchImage).not.toHaveBeenCalled();
95 });
96
97 it("reports failed HEIC conversion through the existing error handler", async () => {
98 const onError = vi.fn();
99 vi.mocked(prepareImageForAttachment).mockResolvedValue({ ok: false, reason: "unreadable" });
74 - await act(() => {
75 - renderer = create(<AttachmentImage src="blob:heic" name="photo.heif" onError={onError} />);
76 - });
77 - await act(() => renderer.root.findByType("img").props.onError(decodeError));
78 - expect(onError).toHaveBeenCalledWith(decodeError);
100 + await render(<AttachmentImage src="blob:heic" name="photo.heif" onError={onError} />);
101 + expectDecodeErrorFor(onError, await failDecode());
102 expect(URL.createObjectURL).not.toHaveBeenCalled();
103 });
104
@@ -86,13 +109,11 @@ it("discards an old decode when navigating to a different source", async () => {
109 finishDecode = resolve;
110 }),
111 );
89 - await act(() => {
90 - renderer = create(<AttachmentImage src="blob:first" name="photo.heic" />);
91 - });
92 - await act(() => renderer.root.findByType("img").props.onError(decodeError));
93 - await act(() => renderer.update(<AttachmentImage src="blob:second" name="photo.heic" />));
112 + await render(<AttachmentImage src="blob:first" name="photo.heic" />);
113 + await failDecode();
114 + await render(<AttachmentImage src="blob:second" name="photo.heic" />);
115 await act(() => finishDecode({ ok: true, file: jpeg, recompressed: true }));
95 - expect(renderer.root.findByType("img").props.src).toBe("blob:second");
116 + expect(image().getAttribute("src")).toBe("blob:second");
117 expect(URL.createObjectURL).not.toHaveBeenCalled();
118 expect(fetchImage.mock.calls[0]?.[1].signal.aborted).toBe(true);
119 });