Skip to content

Commit 54c1d52

Browse files
only21milcodex
andcommitted
fix(mobile): read branch refs with a finite action query
Use a one-shot refs query for confirmation instead of waiting for the live refs cache stream to complete. Cover the stream's waiting result so the confirmation must continue after fresh refs arrive. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 76874f9 commit 54c1d52

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎apps/mobile/src/state/use-selected-thread-git-actions.test.tsx‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { VcsListRefsResult } from "@t3tools/contracts";
44
import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
55
import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
66
import * as Effect from "effect/Effect";
7+
import * as Stream from "effect/Stream";
78
import { AsyncResult, Atom } from "effect/reactivity";
89
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
910

@@ -18,7 +19,9 @@ const harness = vi.hoisted(() => ({
1819
renderCount: 0,
1920
onRefreshSettled: () => {},
2021
branchAtom: null as Atom.Atom<AsyncResult.AsyncResult<VcsListRefsResult, unknown>> | null,
22+
refsQueryAtom: null as Atom.Atom<AsyncResult.AsyncResult<VcsListRefsResult, unknown>> | null,
2123
listRefs: vi.fn(),
24+
refsQuery: vi.fn(),
2225
showGitActionResult: vi.fn(),
2326
}));
2427

@@ -34,6 +37,7 @@ vi.mock("./use-atom-command", () => ({
3437
}));
3538
vi.mock("./vcs", () => ({
3639
vcsEnvironment: { refreshStatus: "refreshStatus", listRefs: harness.listRefs },
40+
vcsRefsQuery: harness.refsQuery,
3741
vcsActionManager: { runStackedAction: () => "runStackedAction", track: vi.fn() },
3842
}));
3943
vi.mock("./threads", () => ({ threadEnvironment: { updateMetadata: "updateMetadata" } }));
@@ -98,6 +102,7 @@ beforeEach(() => {
98102
harness.renderCount = 0;
99103
harness.onRefreshSettled = () => rerender();
100104
harness.listRefs.mockReset().mockImplementation(() => harness.branchAtom);
105+
harness.refsQuery.mockReset().mockImplementation(() => harness.refsQueryAtom);
101106
harness.showGitActionResult.mockClear();
102107
});
103108

@@ -121,9 +126,18 @@ describe("useSelectedThreadGitActions", () => {
121126
totalCount: 0,
122127
};
123128
let response = cached ? Promise.resolve(empty) : request.promise;
124-
harness.branchAtom = Atom.make(Effect.tryPromise(() => response)).pipe(Atom.keepAlive);
129+
const fetchRefs = Effect.tryPromise(() => response);
130+
harness.branchAtom = Atom.make(
131+
Stream.concat(Stream.fromEffect(fetchRefs), Stream.never),
132+
).pipe(Atom.keepAlive);
133+
harness.refsQueryAtom = Atom.make(fetchRefs).pipe(Atom.keepAlive);
134+
appAtomRegistry.get(harness.branchAtom);
125135
if (cached) {
126-
await executeAtomQuery(appAtomRegistry, harness.branchAtom);
136+
await vi.waitFor(() =>
137+
expect(appAtomRegistry.get(harness.branchAtom)._tag).toBe("Success"),
138+
);
139+
expect(appAtomRegistry.get(harness.branchAtom).waiting).toBe(true);
140+
await executeAtomQuery(appAtomRegistry, harness.refsQueryAtom);
127141
response = request.promise;
128142
}
129143
root = createRoot(document.createElement("div"));
@@ -136,10 +150,6 @@ describe("useSelectedThreadGitActions", () => {
136150
});
137151
await flush();
138152
expect(settled).toBe(false);
139-
expect(harness.listRefs).toHaveBeenCalledWith({
140-
environmentId: "environment-1",
141-
input: { cwd: "/repo", limit: 100 },
142-
});
143153
const local = {
144154
name: "feature/update",
145155
isRemote: false,
@@ -157,7 +167,16 @@ describe("useSelectedThreadGitActions", () => {
157167
hasPrimaryRemote: true,
158168
totalCount: 2,
159169
});
170+
await vi.waitFor(() => expect(settled).toBe(true));
171+
expect(appAtomRegistry.get(harness.branchAtom)).toMatchObject({
172+
_tag: "Success",
173+
waiting: true,
174+
});
160175
const branches = await refresh;
176+
expect(harness.refsQuery).toHaveBeenCalledWith({
177+
environmentId: "environment-1",
178+
input: { cwd: "/repo", limit: 100 },
179+
});
161180
expect(branches).toEqual([local]);
162181
expect(resolveAutoFeatureBranchName(branches!.map((branch) => branch.name))).toBe(
163182
"feature/update-2",
@@ -174,6 +193,7 @@ describe("useSelectedThreadGitActions", () => {
174193
catch: (error) => error,
175194
}),
176195
);
196+
harness.refsQueryAtom = harness.branchAtom;
177197
root = createRoot(document.createElement("div"));
178198
act(() => root!.render(<Consumer />));
179199
const refresh = actions.refreshSelectedThreadBranches();

‎apps/mobile/src/state/use-selected-thread-git-actions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { AsyncResult } from "effect/reactivity";
2222

2323
import { useBranches } from "../state/queries";
2424
import { threadEnvironment } from "../state/threads";
25-
import { vcsActionManager, vcsEnvironment } from "../state/vcs";
25+
import { vcsActionManager, vcsEnvironment, vcsRefsQuery } from "../state/vcs";
2626
import { uuidv4 } from "../lib/uuid";
2727
import { appAtomRegistry } from "./atom-registry";
2828
import { readEnvironmentScope, useEnvironmentScope } from "./session";
@@ -223,7 +223,7 @@ export function useSelectedThreadGitActions() {
223223
if (branchTarget.environmentId === null || branchTarget.cwd === null) return null;
224224
const result = await executeAtomQuery(
225225
appAtomRegistry,
226-
vcsEnvironment.listRefs({
226+
vcsRefsQuery({
227227
environmentId: branchTarget.environmentId,
228228
input: { cwd: branchTarget.cwd, limit: 100 },
229229
}),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import {
22
createVcsActionManager,
33
createVcsEnvironmentAtoms,
44
} from "@t3tools/client-runtime/state/vcs";
5+
import { createEnvironmentRpcQueryAtomFamily } from "@t3tools/client-runtime/state/runtime";
6+
import { WS_METHODS } from "@t3tools/contracts";
57

68
import { connectionAtomRuntime } from "../connection/runtime";
79

810
export const vcsEnvironment = createVcsEnvironmentAtoms(connectionAtomRuntime);
911
export const vcsActionManager = createVcsActionManager(connectionAtomRuntime);
12+
13+
// Action confirmations need a finite request; listRefs stays subscribed for cache updates.
14+
export const vcsRefsQuery = createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {
15+
label: "mobile:vcs:refs-query",
16+
tag: WS_METHODS.vcsListRefs,
17+
});

0 commit comments

Comments
 (0)