Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 38 additions & 7 deletions src/renderer/src/components/settings/settings-project-list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { toast } from 'sonner'
import type { ProjectHostSetup } from '../../../../shared/project-types'
import type { Repo } from '../../../../shared/repo-types'
import {
Expand All @@ -13,6 +14,8 @@ import {
resolveSettingsTargetRepoId
} from './settings-project-list'

vi.mock('sonner', () => ({ toast: { error: vi.fn() } }))

function makeRepo(overrides: Partial<Repo> & Pick<Repo, 'id'>): Repo {
return {
path: `/repos/${overrides.id}`,
Expand Down Expand Up @@ -262,15 +265,22 @@ describe('deep-link resolution', () => {
})

describe('removeSettingsProjectFromAllHosts', () => {
beforeEach(() => {
vi.mocked(toast.error).mockReset()
})

const resolveHostLabel = (hostId: string): string =>
hostId === 'runtime:home-mac' ? 'Home Mac' : hostId

it('removes every host setup with its own hostId and skips setups without a repo row', async () => {
const removeProject = vi.fn().mockResolvedValue(undefined)
const removeProject = vi.fn().mockResolvedValue({ status: 'removed' })
const setups = [
makeSetup({ hostId: 'local', repoId: 'local-1' }),
makeSetup({ hostId: 'ssh:box', repoId: ' ' }),
makeSetup({ hostId: 'runtime:home-mac', repoId: 'remote-9' })
]

await removeSettingsProjectFromAllHosts(setups, removeProject)
await removeSettingsProjectFromAllHosts(setups, removeProject, resolveHostLabel)

// errorFeedback: this is a user-initiated removal, so a failure must surface (#11994).
expect(removeProject.mock.calls).toEqual([
Expand All @@ -285,22 +295,43 @@ describe('removeSettingsProjectFromAllHosts', () => {
.fn()
.mockImplementationOnce(
() =>
new Promise<void>((resolve) => {
resolveFirst = resolve
new Promise<{ status: 'removed' }>((resolve) => {
resolveFirst = () => resolve({ status: 'removed' })
})
)
.mockResolvedValue(undefined)
.mockResolvedValue({ status: 'removed' })
const setups = [
makeSetup({ hostId: 'local', repoId: 'local-1' }),
makeSetup({ hostId: 'runtime:home-mac', repoId: 'remote-9' })
]

const pending = removeSettingsProjectFromAllHosts(setups, removeProject)
const pending = removeSettingsProjectFromAllHosts(setups, removeProject, resolveHostLabel)
await Promise.resolve()
expect(removeProject).toHaveBeenCalledTimes(1)

resolveFirst?.()
await pending
expect(removeProject).toHaveBeenCalledTimes(2)
})

// The store keeps its toast for failures only, so this pane has to voice the unanswered host
// itself — otherwise a removal that changed nothing looks like it worked.
it('says which host went unanswered instead of looking successful', async () => {
const removeProject = vi
.fn()
.mockResolvedValueOnce({ status: 'removed' })
.mockResolvedValueOnce({ status: 'owner-unverifiable' })
const setups = [
makeSetup({ hostId: 'local', repoId: 'local-1' }),
makeSetup({ hostId: 'runtime:home-mac', repoId: 'remote-9' })
]

await removeSettingsProjectFromAllHosts(setups, removeProject, resolveHostLabel)

expect(toast.error).toHaveBeenCalledTimes(1)
// The host's own name, never the raw `runtime:` routing id.
const description = vi.mocked(toast.error).mock.calls[0]?.[1]?.description
expect(description).toContain('Home Mac')
expect(description).not.toContain('runtime:home-mac')
})
})
32 changes: 30 additions & 2 deletions src/renderer/src/components/settings/settings-project-list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { toast } from 'sonner'
import type { Project, ProjectHostSetup } from '../../../../shared/project-types'
import type { Repo } from '../../../../shared/repo-types'
import type { RemoveProjectOutcome } from '../../store/repos/project-removal-outcome'
import { translate } from '@/i18n/i18n'
import {
getRepoExecutionHostId,
LOCAL_EXECUTION_HOST_ID,
Expand Down Expand Up @@ -177,12 +180,37 @@ export async function removeSettingsProjectFromAllHosts(
removeProject: (
repoId: string,
options: { hostId: ExecutionHostId; errorFeedback?: 'toast' | 'silent' }
) => Promise<void>
) => Promise<RemoveProjectOutcome>,
// Why: required, not defaulted — the fallback label is a raw host id, which means nothing to a
// reader, and a caller that forgets to pass this would ship it into the toast below.
resolveHostLabel: (hostId: ExecutionHostId) => string
): Promise<void> {
for (const setup of setups) {
if (setup.repoId.trim().length > 0) {
// Why: user-initiated single-project removal, so a failure must be visible rather than silent (#11994).
await removeProject(setup.repoId, { hostId: setup.hostId, errorFeedback: 'toast' })
const outcome = await removeProject(setup.repoId, {
hostId: setup.hostId,
errorFeedback: 'toast'
})
// Why: an unanswered host is not a store failure, so it carries no toast of its own. This
// pane has no per-host forget affordance, so say what happened rather than look successful.
// Retrying is safe once the host answers: a removal that did land reports `repo_not_found`,
// which the store reconciles instead of failing on.
if (outcome.status === 'owner-unverifiable') {
toast.error(
translate(
'auto.components.settings.settingsProjectList.removeOwnerUnverifiable',
'Could not reach the host that owns this project'
),
{
description: translate(
'auto.components.settings.settingsProjectList.removeOwnerUnverifiableDescription',
'Orca could not confirm the removal with {{host}}. Reconnect that host and try again, or remove the project from the sidebar to clear this computer’s records only.',
{ host: resolveHostLabel(setup.hostId) }
)
}
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { OrcaHooks } from '../../../../shared/orca-yaml-hook-types'
import type { ProjectHostSetup } from '../../../../shared/project-types'
import { DEFAULT_APP_FONT_FAMILY } from '../../../../shared/constants'
import { useAppStore } from '../../store'
import { selectExecutionHostDisplayLabel } from '@/lib/execution-host-display-label'
import { useSystemPrefersDark } from '@/components/terminal-pane/use-system-prefers-dark'
import { isMacUserAgent, isWindowsUserAgent } from '@/components/terminal-pane/pane-helpers'
import { useConfirmationDialog } from '@/components/confirmation-dialog-context'
Expand Down Expand Up @@ -72,7 +73,11 @@ export function useSettingsStoreModel() {
// Why: pane-level "Remove Project" removes every host setup, not just the selected host (per-host remove lives in "Available Hosts").
const removeProjectAllHosts = useCallback(
(setups: readonly ProjectHostSetup[]): Promise<void> =>
removeSettingsProjectFromAllHosts(setups, removeProject),
// Why: read the host catalogs when the removal reports back, not at render — a rename or a
// newly-hydrated environment landing mid-removal should still name the host correctly.
removeSettingsProjectFromAllHosts(setups, removeProject, (hostId) =>
selectExecutionHostDisplayLabel(useAppStore.getState(), hostId)
),
[removeProject]
)

Expand Down
118 changes: 101 additions & 17 deletions src/renderer/src/components/sidebar/RemoveFolderDialog.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
// @vitest-environment happy-dom

import '@testing-library/jest-dom/vitest'
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { renderToStaticMarkup } from 'react-dom/server'
import type { ReactNode } from 'react'
import type { ButtonHTMLAttributes, ReactNode } from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { Repo } from '../../../../shared/repo-types'

const mocks = vi.hoisted(() => ({
state: {
activeModal: 'confirm-remove-folder' as string | null,
modalData: {
repoId: 'repo-1',
displayName: 'Example',
hostId: 'ssh:target-1'
} as Record<string, unknown>,
repos: [] as Repo[],
sshTargetLabels: new Map<string, string>(),
removedSshTargetLabels: new Map<string, string>(),
closeModal: vi.fn(),
removeProject: vi.fn()
const mocks = vi.hoisted(() => {
const runtimeEnvironments: { id: string; name: string }[] = []
return {
state: {
activeModal: 'confirm-remove-folder' as string | null,
modalData: {
repoId: 'repo-1',
displayName: 'Example',
hostId: 'ssh:target-1'
} as Record<string, unknown>,
repos: [] as Repo[],
// Read by selectExecutionHostDisplayLabel: a per-host rename override wins over the name.
settings: null,
runtimeEnvironments,
sshTargetLabels: new Map<string, string>(),
removedSshTargetLabels: new Map<string, string>(),
closeModal: vi.fn(),
removeProject: vi.fn()
}
}
}))
})

vi.mock('@/store', () => ({
useAppStore: (selector: (state: typeof mocks.state) => unknown) => selector(mocks.state)
Expand All @@ -34,13 +45,21 @@ vi.mock('@/components/ui/dialog', () => ({
}))

vi.mock('@/components/ui/button', () => ({
Button: ({ children }: { children: ReactNode }) => <button>{children}</button>
Button: ({
children,
onClick,
disabled
}: ButtonHTMLAttributes<HTMLButtonElement> & { children: ReactNode }) => (
<button type="button" onClick={onClick} disabled={disabled}>
{children}
</button>
)
}))

vi.mock('@/i18n/i18n', () => ({
translate: (_key: string, fallback: string, values?: Record<string, string>) =>
Object.entries(values ?? {}).reduce(
(text, [key, value]) => text.replace(`{{${key}}}`, value),
(text, [key, value]) => text.replaceAll(`{{${key}}}`, value),
fallback
)
}))
Expand All @@ -62,14 +81,18 @@ function repo(connectionId: string | null, executionHostId: Repo['executionHostI

describe('RemoveFolderDialog', () => {
beforeEach(() => {
cleanup()
vi.clearAllMocks()
mocks.state.modalData = {
repoId: 'repo-1',
displayName: 'Example',
hostId: 'ssh:target-1'
}
mocks.state.repos = []
mocks.state.runtimeEnvironments = []
mocks.state.sshTargetLabels = new Map([['target-1', 'Persistent host']])
mocks.state.removedSshTargetLabels = new Map()
mocks.state.removeProject.mockResolvedValue({ status: 'removed' })
})

it('warns that VM recipe cleanup controls file deletion', () => {
Expand All @@ -91,4 +114,65 @@ describe('RemoveFolderDialog', () => {
expect(html).toContain('Its files stay on Persistent host')
expect(html).not.toContain('VM recipe')
})

it('closes on a removal the owning host confirmed', async () => {
mocks.state.repos = [repo('target-1', 'ssh:target-1')]
render(<RemoveFolderDialog />)

await userEvent.click(screen.getByRole('button', { name: 'Remove' }))

expect(mocks.state.removeProject).toHaveBeenCalledWith('repo-1', {
hostId: 'ssh:target-1',
errorFeedback: 'toast'
})
expect(mocks.state.closeModal).toHaveBeenCalledTimes(1)
})

// The owning runtime never answered, so the project is still registered there. The
// dialog must stay open and name the host rather than close on a removal that did not happen.
it('re-offers a client-only forget when the owning host never answers', async () => {
mocks.state.modalData.hostId = 'runtime:env-1'
mocks.state.repos = [repo(null, 'runtime:env-1')]
mocks.state.runtimeEnvironments = [{ id: 'env-1', name: 'alexdevbox2' }]
mocks.state.removeProject.mockResolvedValueOnce({ status: 'owner-unverifiable' })
render(<RemoveFolderDialog />)

await userEvent.click(screen.getByRole('button', { name: 'Remove' }))

expect(mocks.state.closeModal).not.toHaveBeenCalled()
// Names the host, and claims nothing about what it did — a lost answer is not a removal that
// did not happen.
expect(screen.getByText(/Orca could not reach alexdevbox2/)).toBeInTheDocument()
expect(screen.getByText(/was removed there is unknown/)).toBeInTheDocument()
expect(screen.getByText(/if it is still registered on alexdevbox2/)).toBeInTheDocument()

await userEvent.click(screen.getByRole('button', { name: 'Remove from Orca' }))

expect(mocks.state.removeProject).toHaveBeenLastCalledWith('repo-1', {
hostId: 'runtime:env-1',
errorFeedback: 'toast',
mode: 'forget-local'
})
expect(mocks.state.closeModal).toHaveBeenCalledTimes(1)
})

// Cancel stays live during the ~15s host call. closeModal is global, so a late answer from a
// dismissed invocation must not dismiss whichever dialog the user opened next.
it('ignores a removal that finishes after its dialog was dismissed', async () => {
mocks.state.repos = [repo('target-1', 'ssh:target-1')]
let finishRemoval: ((outcome: { status: string }) => void) | undefined
mocks.state.removeProject.mockReturnValueOnce(
new Promise((resolve) => {
finishRemoval = resolve
})
)
const view = render(<RemoveFolderDialog />)

await userEvent.click(screen.getByRole('button', { name: 'Remove' }))
view.unmount()
finishRemoval?.({ status: 'removed' })
await Promise.resolve()

expect(mocks.state.closeModal).not.toHaveBeenCalled()
})
})
Loading