Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions apps/mobile/src/features/projects/AddProjectScreen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const state = vi.hoisted(() => ({
repositoryIdentity?: { canonicalKey: string };
}[],
cloneDestinations: [] as string[],
draftsError: null as Error | null,
createResult: null as Promise<unknown> | null,
createNewResult: null as Promise<unknown> | null,
startCloneResult: null as Promise<unknown> | null,
Expand Down Expand Up @@ -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: () => {},
}));
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"))),
);
Expand All @@ -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;
}
Expand Down
26 changes: 13 additions & 13 deletions apps/mobile/src/features/projects/AddProjectScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof getActiveRoute>,
Expand All @@ -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 ||
Expand All @@ -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,
);
Expand Down Expand Up @@ -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;
}
Expand Down
Loading