Skip to content

Commit f467fa4

Browse files
test(web): mount the scratch machine selector in a compiled browser test
The Node unit test called useScratchProject() as a plain function, bypassing React rendering and the React Compiler. Replace it with a browser test that mounts a probe component and asserts the selector falls back from an unavailable current machine only to a sole connected alternative, keeps a connected current machine, and declines to choose between several. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ac4f5b3 commit f467fa4

2 files changed

Lines changed: 74 additions & 47 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5+
6+
const state = vi.hoisted(() => ({
7+
environments: [] as Array<{
8+
environmentId: string;
9+
connection: { phase: string };
10+
serverConfig: { scratchWorkspaceRoot: string };
11+
}>,
12+
}));
13+
vi.mock(import("../state/environments"), async (original) => ({
14+
...(await original()),
15+
useEnvironments: (() => ({ environments: state.environments })) as never,
16+
}));
17+
vi.mock(import("../state/use-atom-command"), async (original) => ({
18+
...(await original()),
19+
useAtomCommand: (() => vi.fn()) as never,
20+
}));
21+
vi.mock(import("./useHandleNewThread"), async (original) => ({
22+
...(await original()),
23+
useNewThreadHandler: (() => vi.fn()) as never,
24+
}));
25+
import { useScratchProject } from "./useScratchProject";
26+
27+
const LOCAL = EnvironmentId.make("local");
28+
const REMOTE = EnvironmentId.make("remote");
29+
const OTHER = EnvironmentId.make("other");
30+
31+
function environment(environmentId: EnvironmentId, phase: string) {
32+
return {
33+
environmentId,
34+
connection: { phase },
35+
serverConfig: { scratchWorkspaceRoot: `/scratch/${environmentId}` },
36+
};
37+
}
38+
39+
function Probe({ current }: { current: EnvironmentId }) {
40+
return <output>{useScratchProject().scratchEnvironmentId(current) ?? "none"}</output>;
41+
}
42+
43+
let root: Root;
44+
let container: HTMLDivElement;
45+
46+
beforeEach(() => {
47+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
48+
container = document.createElement("div");
49+
root = createRoot(container);
50+
});
51+
afterEach(async () => {
52+
await act(async () => root.unmount());
53+
vi.unstubAllGlobals();
54+
});
55+
56+
async function scratchTargetFrom(current: EnvironmentId) {
57+
await act(async () => root.render(<Probe current={current} />));
58+
return container.textContent;
59+
}
60+
61+
it("falls back from an unavailable current machine only to a sole connected alternative", async () => {
62+
state.environments = [environment(REMOTE, "reconnecting"), environment(LOCAL, "connected")];
63+
expect(await scratchTargetFrom(REMOTE)).toBe(LOCAL);
64+
65+
state.environments = [environment(REMOTE, "connected"), environment(LOCAL, "connected")];
66+
expect(await scratchTargetFrom(REMOTE)).toBe(REMOTE);
67+
68+
state.environments = [
69+
environment(REMOTE, "reconnecting"),
70+
environment(LOCAL, "connected"),
71+
environment(OTHER, "connected"),
72+
];
73+
expect(await scratchTargetFrom(REMOTE)).toBe("none");
74+
});

‎apps/web/src/hooks/useScratchProject.test.ts‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)