Skip to content

Commit efb8e23

Browse files
fix(tui): move session when recovering missing directory (anomalyco#51096)
Co-authored-by: vimtor <36263538+vimtor@users.noreply.github.com>
1 parent ad53e39 commit efb8e23

2 files changed

Lines changed: 117 additions & 4 deletions

File tree

‎packages/tui/src/routes/session/location-missing.tsx‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,56 @@ import { useTheme } from "../../context/theme"
44
import { Locale } from "../../util/locale"
55
import { abbreviateHome } from "../../util/path-format"
66
import { SessionQuestion } from "./permission"
7-
import { usePromptMove } from "../../component/prompt/move"
7+
import { useDialog } from "../../ui/dialog"
8+
import { useClient } from "../../context/client"
9+
import { useToast } from "../../ui/toast"
10+
import { errorMessage } from "../../util/error"
11+
import { DialogWorkspaces, type WorkspaceSelection } from "../../component/dialog-workspaces"
12+
import { useData } from "../../context/data"
813

914
export function SessionLocationMissing(props: { directory: string; projectID: string; sessionID: string }) {
10-
const move = usePromptMove({ projectID: () => props.projectID, sessionID: () => props.sessionID })
11-
return <SessionLocationUnavailable directory={props.directory} onMove={move.open} />
15+
const dialog = useDialog()
16+
const client = useClient()
17+
const toast = useToast()
18+
const data = useData()
19+
20+
function open() {
21+
dialog.replace(() => (
22+
<DialogWorkspaces
23+
projectID={props.projectID}
24+
location={{ directory: props.directory }}
25+
current={{
26+
type: "directory",
27+
directory: props.directory,
28+
subdirectory: !!data.session.get(props.sessionID)?.subpath,
29+
}}
30+
onSelect={(selection) => void select(selection)}
31+
/>
32+
))
33+
}
34+
35+
async function select(selection: WorkspaceSelection) {
36+
dialog.clear()
37+
const directory =
38+
selection.type === "directory"
39+
? selection.directory
40+
: await client.api.worktree
41+
.create({ projectID: props.projectID, name: selection.name })
42+
.then((result) => {
43+
if (!result.directory) throw new Error("No worktree directory returned")
44+
return result.directory
45+
})
46+
.catch((error) => {
47+
toast.show({ title: "Creating workspace failed", message: errorMessage(error), variant: "error" })
48+
return undefined
49+
})
50+
if (!directory) return
51+
await client.api.session.move({ sessionID: props.sessionID, directory }).catch((error) => {
52+
toast.show({ title: "Failed to move session", message: errorMessage(error), variant: "error" })
53+
})
54+
}
55+
56+
return <SessionLocationUnavailable directory={props.directory} onMove={open} />
1257
}
1358

1459
export function SessionLocationUnavailable(props: { directory: string; onMove: () => void }) {

‎packages/tui/test/cli/tui/prompt-move.test.tsx‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { RouteProvider, useRoute } from "../../../src/context/route"
1212
import { ThemeProvider } from "../../../src/context/theme"
1313
import { DialogProvider } from "../../../src/ui/dialog"
1414
import { ToastProvider, useToast } from "../../../src/ui/toast"
15+
import { SessionLocationMissing } from "../../../src/routes/session/location-missing"
1516
import { emptyThemeSource } from "../../fixture/fixture"
1617
import { createApi, createEventStream, createFetch, json } from "../../fixture/tui-client"
1718
import { TestTuiContexts } from "../../fixture/tui-environment"
@@ -175,6 +176,68 @@ test.each([false, true])("Ctrl+M moves only an existing session (home=%s)", asyn
175176
}
176177
})
177178

179+
test("choosing a directory recovers the session when its location is unavailable", async () => {
180+
const fixture = await renderMove({ directory: clone, unavailable: "location", showMissingLocation: true })
181+
try {
182+
await fixture.app.waitForFrame((frame) => frame.includes("Session location unavailable"))
183+
fixture.app.mockInput.pressEnter()
184+
await fixture.app.waitForFrame((frame) => frame.includes("Worktrees") && frame.includes(linked))
185+
await fixture.app.waitFor(() => fixture.app.renderer.currentFocusedEditor instanceof InputRenderable)
186+
await fixture.app.mockInput.typeText("linked")
187+
await fixture.app.waitForFrame((frame) => frame.includes(linked) && !frame.includes(main))
188+
fixture.app.mockInput.pressEnter()
189+
await fixture.app.waitFor(() => fixture.moves.length === 1)
190+
191+
expect(fixture.moves).toEqual([{ directory: linked }])
192+
expect(fixture.route.data).toEqual({ type: "session", sessionID: "ses_clone" })
193+
expect(fixture.requests).toEqual([])
194+
} finally {
195+
fixture.app.renderer.destroy()
196+
}
197+
})
198+
199+
test("creating a worktree recovers the session without reading its removed location", async () => {
200+
const fixture = await renderMove({ directory: clone, unavailable: "location", showMissingLocation: true })
201+
try {
202+
await fixture.app.waitForFrame((frame) => frame.includes("Session location unavailable"))
203+
fixture.app.mockInput.pressEnter()
204+
await fixture.app.waitForFrame((frame) => frame.includes("Worktrees") && frame.includes(linked))
205+
fixture.app.mockInput.pressKey("a", { ctrl: true })
206+
await fixture.app.waitForFrame((frame) => frame.includes("Name worktree"))
207+
await fixture.app.waitFor(() => fixture.app.renderer.currentFocusedEditor instanceof InputRenderable)
208+
await fixture.app.mockInput.typeText("fresh")
209+
fixture.app.mockInput.pressEnter()
210+
await fixture.app.waitFor(() => fixture.moves.length === 1)
211+
212+
expect(fixture.requests).toEqual([{ payload: { projectID: "proj_test", name: "fresh" }, directory: null }])
213+
expect(fixture.moves).toEqual([{ directory: created }])
214+
expect(fixture.route.data).toEqual({ type: "session", sessionID: "ses_clone" })
215+
expect(fixture.reads.locations).not.toContain(clone)
216+
} finally {
217+
fixture.app.renderer.destroy()
218+
}
219+
})
220+
221+
test("failed recovery does not navigate away from the session", async () => {
222+
const fixture = await renderMove({ directory: clone, unavailable: "location", showMissingLocation: true, moveFails: true })
223+
try {
224+
await fixture.app.waitForFrame((frame) => frame.includes("Session location unavailable"))
225+
fixture.app.mockInput.pressEnter()
226+
await fixture.app.waitForFrame((frame) => frame.includes("Worktrees") && frame.includes(linked))
227+
await fixture.app.waitFor(() => fixture.app.renderer.currentFocusedEditor instanceof InputRenderable)
228+
await fixture.app.mockInput.typeText("linked")
229+
await fixture.app.waitForFrame((frame) => frame.includes(linked) && !frame.includes(main))
230+
fixture.app.mockInput.pressEnter()
231+
await fixture.app.waitFor(() => fixture.toast.currentToast !== null)
232+
233+
expect(fixture.moves).toEqual([{ directory: linked }])
234+
expect(fixture.route.data).toEqual({ type: "session", sessionID: "ses_clone" })
235+
expect(fixture.toast.currentToast).toMatchObject({ title: "Failed to move session", variant: "error" })
236+
} finally {
237+
fixture.app.renderer.destroy()
238+
}
239+
})
240+
178241
test.each([
179242
{ name: "session", unavailable: "session" as const },
180243
{ name: "location", unavailable: "location" as const },
@@ -223,6 +286,8 @@ async function renderMove(input: {
223286
launch?: string
224287
launchProjectID?: string
225288
unavailable?: "session" | "location"
289+
showMissingLocation?: boolean
290+
moveFails?: boolean
226291
}) {
227292
const launch = input.launch ?? (input.home ? input.directory : main)
228293
const requests: unknown[] = []
@@ -294,6 +359,7 @@ async function renderMove(input: {
294359
}
295360
if (url.pathname === "/api/session/ses_clone/move") {
296361
moves.push(await request.json())
362+
if (input.moveFails) return json({ message: "Destination unavailable" }, { status: 503 })
297363
return new Response(null, { status: 204 })
298364
}
299365
return undefined
@@ -313,7 +379,9 @@ async function renderMove(input: {
313379
projectID: () => (input.home ? data.location.info()?.project.id : "proj_test"),
314380
sessionID: () => (input.home ? undefined : "ses_clone"),
315381
})
316-
return null
382+
return input.showMissingLocation ? (
383+
<SessionLocationMissing directory={input.directory} projectID="proj_test" sessionID="ses_clone" />
384+
) : null
317385
}
318386

319387
const app = await testRender(

0 commit comments

Comments
 (0)