Skip to content

Commit 2d219bb

Browse files
fix(mobile): preserve prompts when existing project drafts fail
1 parent 19b9f64 commit 2d219bb

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎apps/mobile/src/features/projects/AddProjectScreen.test.ts‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const state = vi.hoisted(() => ({
2828
cloneCalls: [] as string[],
2929
alerts: [] as string[],
3030
navigations: [] as unknown[],
31+
draftLoadError: false,
32+
stateUpdates: [] as unknown[],
3133
draftTexts: [] as Array<{ draftId: string; text: string }>,
3234
}));
3335

@@ -42,7 +44,10 @@ vi.mock("react", async (importOriginal) => ({
4244
...(await importOriginal<typeof import("react")>()),
4345
useCallback: (callback: unknown) => callback,
4446
useMemo: (factory: () => unknown) => factory(),
45-
useState: (initial: unknown) => [typeof initial === "function" ? initial() : initial, () => {}],
47+
useState: (initial: unknown) => [
48+
typeof initial === "function" ? initial() : initial,
49+
(value: unknown) => state.stateUpdates.push(value),
50+
],
4651
useRef: (current: unknown) => ({ current }),
4752
useEffect: () => {},
4853
}));
@@ -80,7 +85,9 @@ vi.mock("../../state/session", () => ({
8085
readEnvironmentScope: (_environmentId: string, scope: string) => state.scopes.has(scope),
8186
}));
8287
vi.mock("../../state/use-composer-drafts", () => ({
83-
waitForComposerDraftsLoaded: async () => {},
88+
waitForComposerDraftsLoaded: async () => {
89+
if (state.draftLoadError) throw new Error("Draft storage unavailable");
90+
},
8491
createNewTaskDraft: () => "draft",
8592
setComposerDraftText: (draftId: string, text: string) => state.draftTexts.push({ draftId, text }),
8693
}));
@@ -276,6 +283,8 @@ describe("adding an already registered project", () => {
276283
state.alerts = [];
277284
state.navigations = [];
278285
state.draftTexts = [];
286+
state.draftLoadError = false;
287+
state.stateUpdates = [];
279288
state.supportsCloneTracking = false;
280289
state.home = "/home/tester";
281290
state.os = "linux";
@@ -332,6 +341,33 @@ describe("adding an already registered project", () => {
332341
]);
333342
});
334343

344+
it("keeps the prompt available and reports draft failure before retrying", async () => {
345+
state.baseDirectory = "~/repro";
346+
state.draftLoadError = true;
347+
const action = findAction(
348+
AddProjectDestinationScreen({
349+
environmentId: "environment",
350+
remoteUrl: "https://example.com/bike-documents.git",
351+
repositoryName: "bike-documents",
352+
prompt: "Fix the bike",
353+
}),
354+
"Clone project",
355+
);
356+
if (!action) throw new Error("Clone action missing");
357+
await action();
358+
359+
expect(state.navigations).toEqual([]);
360+
expect(state.cloneCalls).toEqual([]);
361+
expect(state.stateUpdates).toContain("Draft storage unavailable");
362+
expect(state.stateUpdates.at(-1)).toBe(false);
363+
364+
state.draftLoadError = false;
365+
await action();
366+
expect(state.draftTexts).toEqual([{ draftId: "draft", text: "Fix the bike" }]);
367+
expect(state.navigations).toHaveLength(1);
368+
expect(state.cloneCalls).toEqual([]);
369+
});
370+
335371
it.each([false, true])(
336372
"opens the existing project's draft with the prompt before cloning (clone tracking: %s)",
337373
async (supportsCloneTracking) => {

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,7 @@ function useOpenExistingProject(environment: EnvironmentOption | null) {
717717
projectId: existing.id,
718718
title: existing.title,
719719
...(prompt
720-
? await draftWithPrompt(existing.environmentId, existing.id, prompt).then(
721-
(draftId) => ({ draftId }),
722-
() => ({}),
723-
)
720+
? { draftId: await draftWithPrompt(existing.environmentId, existing.id, prompt) }
724721
: {}),
725722
});
726723
return true;
@@ -1353,7 +1350,13 @@ export function AddProjectDestinationScreen(props: {
13531350
}
13541351

13551352
setIsSubmitting(true);
1356-
if (await openExistingProject(await resolveHomePath(resolved.path), prompt)) {
1353+
try {
1354+
if (await openExistingProject(await resolveHomePath(resolved.path), prompt)) {
1355+
setIsSubmitting(false);
1356+
return;
1357+
}
1358+
} catch (error) {
1359+
setError(errorMessage(error));
13571360
setIsSubmitting(false);
13581361
return;
13591362
}

‎scripts/fixtures/mobile-existing-project.sh‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@ set -euo pipefail
44
# Register through the CLI so fixtures have coherent project events, even with a running server.
55
repo_root=$(cd "$(dirname "$0")/../.." && pwd)
66
fixture_home=${1:?Usage: mobile-existing-project.sh ISOLATED_T3_HOME [FIXTURE_ROOT]}
7-
fixture_root=${2:-"$HOME/t3-23246-repro"}
87
fixture_home=$(realpath -m "$fixture_home")
98
if [[ "$fixture_home" == "$(realpath -m "$HOME/.t3")" ]]; then
109
echo 'Use an isolated T3 home.' >&2
1110
exit 1
1211
fi
12+
fixture_root=${2:-$(mktemp -d "${TMPDIR:-/tmp}/t3-mobile-existing-project.XXXXXX")}
1313
mkdir -p "$fixture_root"
1414
fixture_root=$(cd "$fixture_root" && pwd)
1515

16+
# The original script's empty initial commit also identifies fixtures recorded before this guard.
17+
for name in bike-documents unregistered-local; do
18+
folder="$fixture_root/$name"
19+
if [[ -e "$folder" || -L "$folder" ]]; then
20+
if [[ -L "$folder" || ! -d "$folder/.git" ]] ||
21+
[[ "$(git -C "$folder" log --max-parents=0 --format='%s%n%an%n%ae' 2>/dev/null)" != $'Initialize mobile project fixture\nMobile fixture\n[email protected]' ]] ||
22+
[[ -n "$(git -C "$folder" ls-tree "$(git -C "$folder" rev-list --max-parents=0 HEAD)" 2>/dev/null)" ]]; then
23+
printf 'Refusing to modify non-fixture folder: %s\n' "$folder" >&2
24+
exit 1
25+
fi
26+
fi
27+
done
28+
1629
for name in bike-documents unregistered-local; do
1730
folder="$fixture_root/$name"
1831
mkdir -p "$folder"

0 commit comments

Comments
 (0)