Open History to see the last commit
1import * as NodeModule from "node:module";
2import { act, useLayoutEffect } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5
6const harness = vi.hoisted(() => ({
7 dispatch: (_action: unknown) => {},
8 getState: (): unknown => undefined,
9 getRoute: (): unknown => undefined,
10}));
11
12vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
13vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({
14 useAdaptiveWorkspaceLayout: () => ({ fileInspector: { supported: true } }),
15}));
16vi.mock("@react-navigation/native", async () => {
17 const { createRequire } = await import("node:module");
18 const requireNavigation = createRequire(
19 createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
20 );
21 return {
22 StackActions: requireNavigation("@react-navigation/routers").StackActions,
23 useNavigation: () => ({ dispatch: harness.dispatch, getState: harness.getState }),
24 useRoute: () => harness.getRoute(),
25 };
26});
27
28import { useOpenThreadInspector } from "./thread-inspector-navigation";
29import { useOpenLinkedThread } from "./useOpenLinkedThread";
30
31const requireNavigation = NodeModule.createRequire(
32 NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
33);
34const { StackRouter, StackActions } = requireNavigation("@react-navigation/routers") as {
35 StackRouter: typeof import("@react-navigation/native").StackRouter;
36 StackActions: typeof import("@react-navigation/native").StackActions;
37};
38
39function setup(fromAgents: boolean, fullscreen = false) {
40 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
41 const router = StackRouter({});
42 const options = {
43 routeNames: ["Home", "Thread", "ThreadAgents", "ThreadInspectorFullscreen"],
44 routeParamList: {},
45 routeGetIdList: {},
46 };
47 let state = router.getInitialState(options);
48 const dispatch = (action: Parameters<typeof router.getStateForAction>[1]) => {
49 const next = router.getStateForAction(state, action, options);
50 if (!next) throw new Error(`Rejected ${action.type}`);
51 state = router.getRehydratedState(next, options);
52 };
53 harness.dispatch = (action) => dispatch(action as Parameters<typeof dispatch>[0]);
54 harness.getState = () => state;
55 harness.getRoute = () => state.routes.at(-1);
56 const source = { environmentId: "remote-environment", threadId: "parent" };
57 dispatch(StackActions.push("Thread", { ...source, prompt: "retained prompt" }));
58 const parentKey = state.routes.at(-1)!.key;
59 if (fullscreen)
60 dispatch(StackActions.push("ThreadInspectorFullscreen", { ...source, threadId: "child" }));
61 if (fromAgents)
62 dispatch(
63 StackActions.push("ThreadAgents", fullscreen ? { ...source, threadId: "child" } : source),
64 );
65 let openInspector: ReturnType<typeof useOpenThreadInspector>;
66 let openLinkedThread: ReturnType<typeof useOpenLinkedThread>;
67 function Probe() {
68 const open = useOpenThreadInspector();
69 const openLinked = useOpenLinkedThread();
70 useLayoutEffect(() => {
71 openInspector = open;
72 openLinkedThread = openLinked;
73 });
74 return null;
75 }
76 const root = createRoot(document.createElement("div"));
77 act(() => root.render(<Probe />));
78 return {
79 source,
80 parentKey,
81 dispatch,
82 get state() {
83 return state;
84 },
85 open(target: Parameters<typeof openInspector>[1], from = source) {
86 let opened = false;
87 act(() => {
88 opened = openInspector(from, target);
89 });
90 return opened;
91 },
92 openLinked(threadId: string) {
93 act(() => openLinkedThread({ environmentId: source.environmentId, threadId }));
94 },
95 [Symbol.dispose]() {
96 act(() => root.unmount());
97 },
98 };
99}
100
101it("opens a subagent from the Agents sheet without replacing the parent thread", () => {
102 using screen = setup(true);
103 const inspector = {
104 type: "thread",
105 environmentId: screen.source.environmentId,
106 threadId: "child",
107 } as const;
108 screen.open(inspector);
109 expect(screen.state.routes.map((route) => route.name)).toEqual(["Home", "Thread"]);
110 expect(screen.state.routes.at(-1)).toMatchObject({
111 key: screen.parentKey,
112 params: { ...screen.source, prompt: "retained prompt", inspector },
113 });
114 screen.dispatch(StackActions.push("ThreadInspectorFullscreen", inspector));
115 screen.dispatch(StackActions.pop());
116 expect(screen.state.routes.at(-1)).toMatchObject({
117 key: screen.parentKey,
118 params: { ...screen.source, inspector },
119 });
120});
121
122it("opens a completion link in the parent inspector", () => {
123 using screen = setup(false);
124 screen.openLinked("child");
125 expect(screen.state.routes).toHaveLength(2);
126 expect(screen.state.routes.at(-1)).toMatchObject({
127 key: screen.parentKey,
128 params: {
129 ...screen.source,
130 inspector: { type: "thread", environmentId: screen.source.environmentId, threadId: "child" },
131 },
132 });
133});
134
135it("switches between subagents and devices in the same parent route", () => {
136 using screen = setup(false);
137 screen.open({ type: "thread", environmentId: screen.source.environmentId, threadId: "child" });
138 const inspector = { type: "device", ...screen.source } as const;
139 screen.open(inspector);
140 expect(screen.state.routes).toHaveLength(2);
141 expect(screen.state.routes.at(-1)).toMatchObject({
142 key: screen.parentKey,
143 params: { ...screen.source, inspector },
144 });
145});
146
147it.each(["thread", "device"] as const)(
148 "keeps the parent when a maximized child opens a %s preview",
149 (type) => {
150 using screen = setup(false, true);
151 const child = { ...screen.source, threadId: "child" };
152 const originalState = screen.state;
153 expect(screen.open({ type, ...child, threadId: "grandchild" }, child)).toBe(false);
154 expect(screen.state).toEqual(originalState);
155 screen.dispatch(StackActions.pop());
156 expect(screen.state.routes.at(-1)).toMatchObject({
157 key: screen.parentKey,
158 params: screen.source,
159 });
160 },
161);
162
163it("keeps the parent when the maximized child's Agents sheet opens another child", () => {
164 using screen = setup(true, true);
165 const child = { ...screen.source, threadId: "child" };
166 const originalState = screen.state;
167 expect(screen.open({ type: "thread", ...child, threadId: "grandchild" }, child)).toBe(false);
168 expect(screen.state).toEqual(originalState);
169});