Skip to content

Commit aacf4b5

Browse files
fix(mobile): stabilize composer search debounce targets
1 parent 22dc1da commit aacf4b5

3 files changed

Lines changed: 118 additions & 6 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { AuthFilesystemReadScope, EnvironmentId } from "@t3tools/contracts";
2+
import { act, useLayoutEffect } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
const mocks = vi.hoisted(() => ({
7+
sessionAtom: {},
8+
searchEntries: vi.fn(() => null),
9+
}));
10+
11+
vi.mock("./session", () => ({
12+
environmentSession: { sessionStateAtom: () => mocks.sessionAtom },
13+
}));
14+
vi.mock("./presentation", () => ({
15+
useEnvironmentPresentation: () => ({
16+
isReady: true,
17+
presentation: { connection: { phase: "connected", error: null } },
18+
}),
19+
}));
20+
vi.mock("./pull-requests", () => ({ composerPullRequests: {} }));
21+
vi.mock("./projects", () => ({
22+
projectEnvironment: { searchEntries: mocks.searchEntries },
23+
}));
24+
vi.mock("./atom-registry", () => ({ appAtomRegistry: {} }));
25+
vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} }));
26+
vi.mock("./vcs", () => ({ vcsEnvironment: {} }));
27+
vi.mock("./query", () => ({
28+
useEnvironmentQuery: (atom: unknown) => ({
29+
data:
30+
atom === mocks.sessionAtom
31+
? { authenticated: true, scopes: [AuthFilesystemReadScope] }
32+
: null,
33+
error: null,
34+
isPending: false,
35+
refresh: () => {},
36+
}),
37+
}));
38+
39+
import { useComposerPathSearch, type ComposerPathSearchTarget } from "./queries";
40+
41+
function setup() {
42+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
43+
vi.useFakeTimers();
44+
mocks.searchEntries.mockClear();
45+
const root = createRoot(document.createElement("div"));
46+
let commits = 0;
47+
let isPending = false;
48+
49+
function Probe({ target }: { readonly target: ComposerPathSearchTarget }) {
50+
const result = useComposerPathSearch(target);
51+
useLayoutEffect(() => {
52+
commits += 1;
53+
isPending = result.isPending;
54+
});
55+
return null;
56+
}
57+
58+
return {
59+
render(query: string | null) {
60+
act(() =>
61+
root.render(
62+
<Probe
63+
target={{ environmentId: EnvironmentId.make("test-environment"), cwd: "/repo", query }}
64+
/>,
65+
),
66+
);
67+
},
68+
get commits() {
69+
return commits;
70+
},
71+
get isPending() {
72+
return isPending;
73+
},
74+
[Symbol.dispose]() {
75+
act(() => root.unmount());
76+
vi.useRealTimers();
77+
},
78+
};
79+
}
80+
81+
it.each([null, "src"])("stops rendering after the path target settles (%s)", (query) => {
82+
using harness = setup();
83+
harness.render(query);
84+
// A parent render before the debounce fires used to start a self-sustaining loop.
85+
harness.render(query);
86+
act(() => vi.advanceTimersByTime(200));
87+
const settledCommits = harness.commits;
88+
89+
act(() => vi.advanceTimersByTime(200));
90+
expect(harness.commits).toBe(settledCommits);
91+
act(() => vi.advanceTimersByTime(2_000));
92+
expect(harness.commits).toBe(settledCommits);
93+
expect(harness.isPending).toBe(false);
94+
if (query === null) expect(mocks.searchEntries).not.toHaveBeenCalled();
95+
});
96+
97+
it("still debounces a changed path query before searching", () => {
98+
using harness = setup();
99+
harness.render(null);
100+
harness.render(" src ");
101+
expect(harness.isPending).toBe(true);
102+
act(() => vi.advanceTimersByTime(199));
103+
expect(mocks.searchEntries).not.toHaveBeenCalled();
104+
105+
act(() => vi.advanceTimersByTime(1));
106+
expect(mocks.searchEntries).toHaveBeenLastCalledWith({
107+
environmentId: EnvironmentId.make("test-environment"),
108+
input: { cwd: "/repo", query: "src", limit: 20 },
109+
});
110+
expect(harness.isPending).toBe(false);
111+
});

‎apps/mobile/src/state/queries.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as Cause from "effect/Cause";
2424
import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail";
2525
import * as Option from "effect/Option";
2626
import { AsyncResult, Atom } from "effect/reactivity";
27-
import { useEffect, useState } from "react";
27+
import { useEffect, useMemo, useState } from "react";
2828

2929
import { appAtomRegistry } from "./atom-registry";
3030
import { orchestrationEnvironment } from "./orchestration";
@@ -303,11 +303,11 @@ export function usePaginatedBranches(target: VcsRefTarget) {
303303
}
304304

305305
export function useComposerPathSearch(target: ComposerPathSearchTarget) {
306-
const normalizedTarget = {
307-
environmentId: target.environmentId,
308-
cwd: target.cwd,
309-
query: normalizeComposerPathSearchQuery(target.query),
310-
};
306+
const query = normalizeComposerPathSearchQuery(target.query);
307+
const normalizedTarget = useMemo(
308+
() => ({ environmentId: target.environmentId, cwd: target.cwd, query }),
309+
[target.environmentId, target.cwd, query],
310+
);
311311
const debouncedTarget = useDebouncedValue(normalizedTarget, COMPOSER_PATH_SEARCH_DEBOUNCE_MS);
312312
const fileAccessSession = useEnvironmentQuery(
313313
debouncedTarget.environmentId === null

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const reactTests = [
77
"src/features/settings/components/BranchNamingSettings.test.tsx",
88
"src/native/NativeHeaderToolbar.test.tsx",
99
"src/native/useRestingTopOffset.test.tsx",
10+
"src/state/queries.debounce.test.tsx",
1011
"src/state/use-selected-thread-git-actions.test.tsx",
1112
];
1213

0 commit comments

Comments
 (0)