Skip to content

Commit f1e3c5e

Browse files
test(mobile): trim question card file link test to one case
AI-assisted; verified with the tests and tools listed above.
1 parent 819c17e commit f1e3c5e

1 file changed

Lines changed: 23 additions & 91 deletions

File tree

Lines changed: 23 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
2-
import * as NodeModule from "node:module";
32
import type { ReactNode } from "react";
43
import { act } from "react";
54
import { createRoot } from "react-dom/client";
@@ -8,11 +7,8 @@ import { expect, it, vi } from "vite-plus/test";
87
import type { PendingUserInput } from "../../lib/threadActivity";
98

109
const harness = vi.hoisted(() => ({
11-
dispatch: (_action: unknown) => {},
12-
navigate: (_name: string, _params?: object) => {},
13-
getState: (): unknown => undefined,
14-
getRoute: (): unknown => undefined,
15-
openURL: (_url: string): Promise<unknown> => Promise.resolve(true),
10+
navigate: [] as Array<[string, object | undefined]>,
11+
openedUrls: [] as string[],
1612
}));
1713

1814
type Children = { children?: ReactNode };
@@ -24,7 +20,6 @@ vi.mock("react-native", () => ({
2420
Pressable: ({ children, onPress }: Children & { onPress?: () => void }) => (
2521
<div onClick={onPress}>{children}</div>
2622
),
27-
Linking: { openURL: (url: string) => harness.openURL(url) },
2823
}));
2924
vi.mock("react-native-reanimated", () => {
3025
const animation = { duration: () => ({ easing: () => ({}) }) };
@@ -60,71 +55,25 @@ vi.mock("../../native/SelectableMarkdownText", () => ({
6055
}));
6156
vi.mock("../../state/entities", () => ({
6257
useLiveThreadLinkLabels: (markdown: string) => markdown,
63-
useProjects: () => [],
64-
useThreadShell: () => null,
6558
}));
66-
vi.mock("../../state/server", async () => {
67-
const { Atom } = await import("effect/reactivity");
68-
return { environmentServerConfigsAtom: Atom.make(new Map()) };
69-
});
70-
vi.mock("../workspace/useSelectWorkspaceDetail", () => ({
71-
useSelectWorkspaceDetail: () => vi.fn(),
59+
vi.mock("../../native/useOpenPullRequest", () => ({
60+
useOpenPullRequest: () => async (url: string) => {
61+
harness.openedUrls.push(url);
62+
return true;
63+
},
64+
}));
65+
vi.mock("@react-navigation/native", () => ({
66+
useNavigation: () => ({
67+
navigate: (name: string, params?: object) => harness.navigate.push([name, params]),
68+
}),
7269
}));
73-
vi.mock("@react-navigation/native", async () => {
74-
const { createRequire } = await import("node:module");
75-
const requireNavigation = createRequire(
76-
createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
77-
);
78-
const routers = requireNavigation("@react-navigation/routers");
79-
return {
80-
StackActions: routers.StackActions,
81-
CommonActions: routers.CommonActions,
82-
useNavigation: () => ({
83-
dispatch: harness.dispatch,
84-
navigate: harness.navigate,
85-
getState: harness.getState,
86-
}),
87-
useRoute: () => harness.getRoute(),
88-
};
89-
});
9070

9171
import { PendingUserInputCard } from "./PendingUserInputCard";
9272

93-
const requireNavigation = NodeModule.createRequire(
94-
NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
95-
);
96-
const { StackRouter, StackActions, CommonActions } = requireNavigation(
97-
"@react-navigation/routers",
98-
) as {
99-
StackRouter: typeof import("@react-navigation/native").StackRouter;
100-
StackActions: typeof import("@react-navigation/native").StackActions;
101-
CommonActions: typeof import("@react-navigation/native").CommonActions;
102-
};
103-
10473
function setup(questionMarkdown: string) {
10574
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
106-
const router = StackRouter({});
107-
const options = {
108-
routeNames: ["Home", "Thread", "ThreadFile", "PullRequest"],
109-
routeParamList: {},
110-
routeGetIdList: {},
111-
};
112-
let state = router.getInitialState(options);
113-
const dispatch = (action: Parameters<typeof router.getStateForAction>[1]) => {
114-
const next = router.getStateForAction(state, action, options);
115-
if (!next) throw new Error(`Rejected ${action.type}`);
116-
state = router.getRehydratedState(next, options);
117-
};
118-
const openedUrls: string[] = [];
119-
harness.dispatch = (action) => dispatch(action as Parameters<typeof dispatch>[0]);
120-
harness.navigate = (name, params) => dispatch(CommonActions.navigate(name, params));
121-
harness.getState = () => state;
122-
harness.getRoute = () => state.routes.at(-1);
123-
harness.openURL = (url) => {
124-
openedUrls.push(url);
125-
return Promise.reject(new Error(`No activity found to handle ${url}`));
126-
};
127-
dispatch(StackActions.push("Thread", { environmentId: "env-1", threadId: "thread-1" }));
75+
harness.navigate = [];
76+
harness.openedUrls = [];
12877

12978
const pendingUserInput = {
13079
requestId: "request-1" as RuntimeRequestId,
@@ -150,6 +99,8 @@ function setup(questionMarkdown: string) {
15099
canOperateThread
151100
pendingUserInput={pendingUserInput}
152101
maxHeight={{ value: 600 } as never}
102+
cardProgress={{ value: 1, set: () => {} } as never}
103+
cardCoverage={{ value: 0, set: () => {} } as never}
153104
collapsed={false}
154105
onToggleCollapsed={() => {}}
155106
drafts={{}}
@@ -163,10 +114,6 @@ function setup(questionMarkdown: string) {
163114
),
164115
);
165116
return {
166-
openedUrls,
167-
get state() {
168-
return state;
169-
},
170117
async pressLink(label: string) {
171118
const link = [...container.querySelectorAll("button")].find(
172119
(button) => button.textContent === label,
@@ -185,26 +132,11 @@ it("opens a workspace file link in a question in the thread's file viewer", asyn
185132

186133
await screen.pressLink("probe.md");
187134

188-
expect(screen.openedUrls).toEqual([]);
189-
expect(screen.state.routes.at(-1)).toMatchObject({
190-
name: "ThreadFile",
191-
params: {
192-
environmentId: "env-1",
193-
threadId: "thread-1",
194-
path: ["docs", "probe.md"],
195-
line: "12",
196-
},
197-
});
198-
});
199-
200-
it("opens an absolute file link under the workspace root by its workspace path", async () => {
201-
using screen = setup("Should I update [probe.md](/repo/docs/probe.md)?");
202-
203-
await screen.pressLink("probe.md");
204-
205-
expect(screen.openedUrls).toEqual([]);
206-
expect(screen.state.routes.at(-1)).toMatchObject({
207-
name: "ThreadFile",
208-
params: { environmentId: "env-1", threadId: "thread-1", path: ["docs", "probe.md"] },
209-
});
135+
expect(harness.openedUrls).toEqual([]);
136+
expect(harness.navigate).toEqual([
137+
[
138+
"ThreadFile",
139+
{ environmentId: "env-1", threadId: "thread-1", path: ["docs", "probe.md"], line: "12" },
140+
],
141+
]);
210142
});

0 commit comments

Comments
 (0)