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
17 changes: 17 additions & 0 deletions packages/app/src/new-session/workspace/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ describe("new session workspace selection", () => {
expect(resolveNewSessionWorktree(input)).toBe(expected)
})

test("keeps the worktree used to open a draft ahead of the saved destination", () => {
const input = {
enabled: true,
directory: "C:\\repos\\app-wt1",
projectWorktree: "C:\\repos\\app",
workspaces: ["C:\\repos\\app-wt1"],
}

expect(resolveNewSessionWorktree({ ...input, fallback: "main" })).toBe(input.directory)
expect(resolveNewSessionWorktree({ ...input, fallback: "create" })).toBe(input.directory)
expect(resolveNewSessionWorktree({ ...input, selected: "main" })).toBe("main")
expect(resolveNewSessionWorktree({ ...input, directory: input.projectWorktree, fallback: "create" })).toBe(
"create",
)
expect(resolveNewSessionWorktree({ ...input, workspaces: [], fallback: "main" })).toBe("main")
})

const branch = (worktree: string) => (worktree === "/project/feature" ? "feature" : undefined)

test.each([
Expand Down
23 changes: 22 additions & 1 deletion packages/app/src/new-session/workspace/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,28 @@ import {
workspaceSelectionDestination,
} from "@/workspaces/paths"

export function resolveNewSessionWorktree(input: { enabled: boolean; selected?: string; fallback?: string }) {
export function resolveNewSessionWorktree(input: {
enabled: boolean
selected?: string
fallback?: string
directory?: string
projectWorktree?: string
workspaces?: readonly string[]
}) {
if (!input.enabled) return "main"

if (input.selected) return input.selected

const directory = input.directory

if (
directory &&
input.projectWorktree &&
!sameDirectory(directory, input.projectWorktree) &&
input.workspaces?.some((workspace) => sameDirectory(workspace, directory))
)
return directory

return input.fallback ?? "main"
}

Expand Down Expand Up @@ -63,6 +80,7 @@ export function createNewSessionWorkspaceController(input: {

const [state, setState] = createStore({
search: "",
// SAFETY: No destination is selected initially; later selections carry both project and directory.
existing: undefined as { projectID: string; directory: string } | undefined,
})

Expand Down Expand Up @@ -178,6 +196,9 @@ export function createNewSessionWorkspaceController(input: {
enabled: visible(),
selected: selected(),
fallback: fallback(),
directory: sdk().directory,
projectWorktree: currentProject()?.worktree,
workspaces: worktreeDirectories(),
}),
)

Expand Down
Loading