diff --git a/apps/mobile/src/features/projects/AddProjectScreen.test.ts b/apps/mobile/src/features/projects/AddProjectScreen.test.ts index 4718133f7a5a5..d0c6ea2abe3d5 100644 --- a/apps/mobile/src/features/projects/AddProjectScreen.test.ts +++ b/apps/mobile/src/features/projects/AddProjectScreen.test.ts @@ -29,6 +29,7 @@ const state = vi.hoisted(() => ({ repositoryIdentity?: { canonicalKey: string }; }[], cloneDestinations: [] as string[], + draftsError: null as Error | null, createResult: null as Promise | null, createNewResult: null as Promise | null, startCloneResult: null as Promise | null, @@ -181,7 +182,9 @@ vi.mock("../../state/projects", () => ({ })); vi.mock("../../state/use-composer-drafts", () => ({ - waitForComposerDraftsLoaded: async () => {}, + waitForComposerDraftsLoaded: async () => { + if (state.draftsError) throw state.draftsError; + }, createNewTaskDraft: () => "prompt-draft", setComposerDraftText: () => {}, })); @@ -240,13 +243,14 @@ function renderScreen(render: () => ReactNode) { return screen; } -function cloneAction() { +function cloneAction(prompt?: string) { const action = findAction( renderScreen(() => AddProjectDestinationScreen({ environmentId: "environment", remoteUrl: "https://example.com/owner/repo.git", repositoryName: "repo", + ...(prompt ? { prompt } : {}), }), ), "Clone project", @@ -295,11 +299,25 @@ describe("clone project permissions", () => { "opens the existing project of that repository instead of cloning into its folder", "example.com/owner/repo", false, + true, + ], + [ + "leaves a folder holding another repository to the clone", + "example.com/owner/other", + true, + false, + ], + [ + "does not open the existing project without the prompt it could not store", + "example.com/owner/repo", + false, + false, ], - ["leaves a folder holding another repository to the clone", "example.com/owner/other", true], - ])("%s", async (_name, canonicalKey, clones) => { + ])("%s", async (name, canonicalKey, clones, opens) => { state.scopes.add(AuthOrchestrationOperateScope); state.cloneDestinations = []; + state.alert.mockReset(); + state.draftsError = name.includes("could not store") ? new Error("Drafts unavailable") : null; state.startCloneResult = Promise.resolve( AsyncResult.failure(Cause.fail(new Error("Destination is not empty"))), ); @@ -313,10 +331,12 @@ describe("clone project permissions", () => { }, ]; try { - await cloneAction()(); + await cloneAction("Fix the bike")(); expect(state.cloneDestinations.length > 0).toBe(clones); + expect(state.alert.mock.calls.length > 0).toBe(opens); } finally { + state.draftsError = null; state.existingProjects = []; state.startCloneResult = null; } diff --git a/apps/mobile/src/features/projects/AddProjectScreen.tsx b/apps/mobile/src/features/projects/AddProjectScreen.tsx index 8de3c67a1825c..b9a0df8942c01 100644 --- a/apps/mobile/src/features/projects/AddProjectScreen.tsx +++ b/apps/mobile/src/features/projects/AddProjectScreen.tsx @@ -796,6 +796,7 @@ function useOpenExistingProject(environment: EnvironmentOption | null) { }); const projects = useProjects(); + /** Opens the project registered at `path`, if any. Rejects when the prompt cannot be stored. */ return async ( path: string, originRoute: ReturnType, @@ -816,7 +817,9 @@ function useOpenExistingProject(environment: EnvironmentOption | null) { ) { return false; } - const prompt = clone?.prompt; + const draftId = clone?.prompt + ? await draftWithPrompt(existing.environmentId, existing.id, clone.prompt) + : null; // Adding an archived project's folder again brings the project back. const unarchived = existing.archivedAt == null || @@ -837,12 +840,7 @@ function useOpenExistingProject(environment: EnvironmentOption | null) { environmentId: existing.environmentId, projectId: existing.id, title: existing.title, - ...(prompt - ? await draftWithPrompt(existing.environmentId, existing.id, prompt).then( - (draftId) => ({ draftId }), - () => ({}), - ) - : {}), + ...(draftId === null ? {} : { draftId }), }, originRoute, ); @@ -1588,12 +1586,14 @@ export function AddProjectDestinationScreen(props: { const originRoute = getActiveRoute(navigation); setIsSubmitting(true); - if ( - await openExistingProject(await resolveHomePath(resolved.path), originRoute, { - remoteUrl, - prompt: prompt ?? null, - }) - ) { + const handled = await openExistingProject(await resolveHomePath(resolved.path), originRoute, { + remoteUrl, + prompt: prompt ?? null, + }).catch((cause: unknown) => { + setError(errorMessage(cause)); + return true; + }); + if (handled) { setIsSubmitting(false); return; }