@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 169 lines 5.57 KB
Raw
1 import { RegistryContext } from "@effect/atom-react";
2 import {
3 NavigationContext,
4 type NavigationProp,
5 type ParamListBase,
6 } from "@react-navigation/native";
7 import { EnvironmentId } from "@t3tools/contracts";
8 import { AtomRegistry } from "effect/reactivity";
9 import { act, type ReactNode } from "react";
10 import { createRoot } from "react-dom/client";
11 import { expect, it, vi } from "vite-plus/test";
12
13 // Each listing probes every linked machine, so the test counts the fetches it starts and cuts.
14 const fetches = vi.hoisted(() => ({
15 started: 0,
16 interrupted: 0,
17 gate: Promise.resolve(),
18 open: () => {},
19 }));
20
21 vi.mock("@react-navigation/native", async () => {
22 const { createRequire } = await import("node:module");
23 const { realpathSync } = await import("node:fs");
24 const require = createRequire(import.meta.url);
25 const nativeRequire = createRequire(
26 realpathSync(require.resolve("@react-navigation/native/package.json")),
27 );
28 // Use the shipped focus hook without importing the React Native view layer.
29 const { NavigationContext, useFocusEffect }: typeof import("@react-navigation/native") =
30 nativeRequire("@react-navigation/core");
31 return { NavigationContext, useFocusEffect };
32 });
33 const appState = vi.hoisted(() => ({ listeners: new Set<(state: string) => void>() }));
34
35 vi.mock("react-native", () => ({
36 AppState: {
37 addEventListener: (_event: "change", listener: (state: string) => void) => {
38 appState.listeners.add(listener);
39 return { remove: () => appState.listeners.delete(listener) };
40 },
41 },
42 View: ({ children }: { children?: ReactNode }) => children,
43 }));
44 vi.mock("../../components/ui/AppText", () => ({
45 AppText: ({ children }: { children?: ReactNode }) => children,
46 }));
47 vi.mock("../settings/components/SettingsSection", () => ({
48 SettingsSection: ({ children }: { children?: ReactNode }) => children,
49 }));
50 vi.mock("../../state/entities", () => ({
51 useEnvironmentServerConfig: () => ({
52 environment: { capabilities: { peerLinks: true } },
53 settings: { linkedEnvironmentsEnabled: true },
54 }),
55 }));
56 vi.mock("../../state/session", () => ({ useEnvironmentScope: () => true }));
57 vi.mock("../../state/peerLinks", async () => {
58 const { Atom } = await import("effect/reactivity");
59 const EffectModule = await import("effect/Effect");
60 // Waits for the gate, as a list waits on its slowest linked machine.
61 const list = Atom.make(
62 EffectModule.gen(function* () {
63 fetches.started += 1;
64 yield* EffectModule.promise(() => fetches.gate);
65 return { links: [] };
66 }).pipe(
67 EffectModule.onInterrupt(() =>
68 EffectModule.sync(() => {
69 fetches.interrupted += 1;
70 }),
71 ),
72 ),
73 );
74 return { peerLinkEnvironment: { list: () => list } };
75 });
76
77 import { LinkedEnvironmentsSection } from "./LinkedEnvironmentsSection";
78
79 function createNavigation() {
80 const listeners = { focus: new Set<() => void>(), blur: new Set<() => void>() };
81 let focused = true;
82 return {
83 value: {
84 isFocused: () => focused,
85 addListener: (event: keyof typeof listeners, listener: () => void) => {
86 listeners[event].add(listener);
87 return () => listeners[event].delete(listener);
88 },
89 } as unknown as NavigationProp<ParamListBase>,
90 focus(next: boolean) {
91 focused = next;
92 act(() => {
93 for (const listener of listeners[next ? "focus" : "blur"]) listener();
94 });
95 },
96 };
97 }
98
99 function setup() {
100 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
101 fetches.started = 0;
102 fetches.interrupted = 0;
103 fetches.gate = new Promise((resolve) => {
104 fetches.open = resolve;
105 });
106 const registry = AtomRegistry.make();
107 const root = createRoot(document.createElement("div"));
108 const navigation = createNavigation();
109 act(() =>
110 root.render(
111 <RegistryContext.Provider value={registry}>
112 <NavigationContext.Provider value={navigation.value}>
113 <LinkedEnvironmentsSection
114 environmentId={EnvironmentId.make("mac")}
115 environmentLabel="Mac"
116 />
117 </NavigationContext.Provider>
118 </RegistryContext.Provider>,
119 ),
120 );
121 return {
122 focus: navigation.focus,
123 moveApp: (state: "active" | "background") =>
124 act(() => {
125 for (const listener of appState.listeners) listener(state);
126 }),
127 settle: () =>
128 act(async () => {
129 fetches.open();
130 await fetches.gate;
131 }),
132 [Symbol.dispose]() {
133 act(() => root.unmount());
134 registry.dispose();
135 },
136 };
137 }
138
139 it("asks again on focus only once the list it is waiting for has answered", async () => {
140 using screen = setup();
141 // The first focus comes while the first fetch runs, which must not be cut and restarted.
142 expect(fetches).toMatchObject({ started: 1, interrupted: 0 });
143 screen.focus(false);
144 screen.focus(true);
145 expect(fetches).toMatchObject({ started: 1, interrupted: 0 });
146
147 await screen.settle();
148 screen.focus(false);
149 screen.focus(true);
150 expect(fetches).toMatchObject({ started: 2, interrupted: 0 });
151 });
152
153 it("asks again when the app comes back to the front on this screen", async () => {
154 using screen = setup();
155 await screen.settle();
156 expect(fetches).toMatchObject({ started: 1, interrupted: 0 });
157 fetches.gate = new Promise((resolve) => {
158 fetches.open = resolve;
159 });
160
161 screen.moveApp("background");
162 screen.moveApp("active");
163 expect(fetches).toMatchObject({ started: 2, interrupted: 0 });
164
165 // Coming back while that answer is still out must not cut it and start again.
166 screen.moveApp("background");
167 screen.moveApp("active");
168 expect(fetches).toMatchObject({ started: 2, interrupted: 0 });
169 });