mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(sandbox): verify worktree back-links, fail fast on root lookup, and tidy settings saves
This commit is contained in:
1 parent
1d001bc43e
commit
2d2921a6c6
10 files changed
+290
-51
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { SessionSandboxResourcesDialog } from './SessionSandboxResourcesDialog'
|
||||
import { useSetSessionSandbox } from '@/hooks/useSessionSandbox'
|
||||
import type { SessionSandboxResources } from '@opencode-manager/shared/schemas'
|
||||
|
||||
vi.mock('@/hooks/useSessionSandbox')
|
||||
|
||||
function mockSetSessionSandbox() {
|
||||
vi.mocked(useSetSessionSandbox).mockReturnValue({
|
||||
mutate: vi.fn(),
|
||||
mutateAsync: vi.fn().mockResolvedValue(undefined),
|
||||
isPending: false,
|
||||
} as unknown as ReturnType<typeof useSetSessionSandbox>)
|
||||
}
|
||||
|
||||
interface DialogProps {
|
||||
open: boolean
|
||||
override: SessionSandboxResources | null
|
||||
effective: SessionSandboxResources
|
||||
}
|
||||
|
||||
function renderDialog(props: DialogProps) {
|
||||
return render(
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID="ses_1"
|
||||
open={props.open}
|
||||
onOpenChange={vi.fn()}
|
||||
override={props.override}
|
||||
effective={props.effective}
|
||||
/>,
|
||||
)
|
||||
}
|
||||
|
||||
describe('SessionSandboxResourcesDialog', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockSetSessionSandbox()
|
||||
})
|
||||
|
||||
it('keeps a typed value when the effective resources change while open', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { rerender } = renderDialog({ open: true, override: null, effective: { cpus: 2, memoryMib: 4096 } })
|
||||
|
||||
const cpus = screen.getByLabelText('CPUs')
|
||||
await user.clear(cpus)
|
||||
await user.type(cpus, '8')
|
||||
|
||||
rerender(
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID="ses_1"
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
override={null}
|
||||
effective={{ cpus: 4, memoryMib: 8192 }}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(screen.getByLabelText('CPUs')).toHaveValue(8)
|
||||
})
|
||||
|
||||
it('re-initializes the draft from the resources when reopened', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { rerender } = renderDialog({ open: true, override: null, effective: { cpus: 2, memoryMib: 4096 } })
|
||||
|
||||
const cpus = screen.getByLabelText('CPUs')
|
||||
await user.clear(cpus)
|
||||
await user.type(cpus, '8')
|
||||
|
||||
rerender(
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID="ses_1"
|
||||
open={false}
|
||||
onOpenChange={vi.fn()}
|
||||
override={null}
|
||||
effective={{ cpus: 2, memoryMib: 4096 }}
|
||||
/>,
|
||||
)
|
||||
|
||||
rerender(
|
||||
<SessionSandboxResourcesDialog
|
||||
sessionID="ses_1"
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
override={{ cpus: 4, memoryMib: 8192 }}
|
||||
effective={{ cpus: 2, memoryMib: 4096 }}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(screen.getByLabelText('CPUs')).toHaveValue(4)
|
||||
expect(screen.getByLabelText('Memory (GiB)')).toHaveValue(8)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -27,13 +27,16 @@ export function SessionSandboxResourcesDialog({
|
||||
const [cpus, setCpus] = useState('')
|
||||
const [memoryGib, setMemoryGib] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const wasOpen = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
const source = override ?? effective
|
||||
setCpus(String(source.cpus))
|
||||
setMemoryGib(formatSandboxMemory(source.memoryMib))
|
||||
setError(null)
|
||||
if (open && !wasOpen.current) {
|
||||
const source = override ?? effective
|
||||
setCpus(String(source.cpus))
|
||||
setMemoryGib(formatSandboxMemory(source.memoryMib))
|
||||
setError(null)
|
||||
}
|
||||
wasOpen.current = open
|
||||
}, [open, override, effective])
|
||||
|
||||
const submit = async (resources: SessionSandboxResources | null) => {
|
||||
|
||||
@@ -380,4 +380,60 @@ describe('SandboxSettings', () => {
|
||||
|
||||
expect(vi.mocked(showToast.error)).toHaveBeenCalledWith('Default memory exceeds the host memory')
|
||||
})
|
||||
|
||||
it('does not save CPUs when blurred without a change', async () => {
|
||||
const { updateSettingsAsync } = mockUseSettings({ preferences: { sandbox: { enabled: true, cpus: 4 } } })
|
||||
mockHealth({ available: true, enforced: false })
|
||||
|
||||
const user = await renderSandbox()
|
||||
const cpus = screen.getByLabelText('Default CPUs')
|
||||
await user.click(cpus)
|
||||
await user.tab()
|
||||
|
||||
expect(updateSettingsAsync).not.toHaveBeenCalled()
|
||||
expect(vi.mocked(showToast.success)).not.toHaveBeenCalled()
|
||||
expect(vi.mocked(showToast.error)).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not save CPUs when a blank value already uses the server default', async () => {
|
||||
const { updateSettingsAsync } = mockUseSettings({ preferences: { sandbox: { enabled: true } } })
|
||||
mockHealth({ available: true, enforced: false })
|
||||
|
||||
const user = await renderSandbox()
|
||||
const cpus = screen.getByLabelText('Default CPUs')
|
||||
await user.click(cpus)
|
||||
await user.tab()
|
||||
|
||||
expect(updateSettingsAsync).not.toHaveBeenCalled()
|
||||
expect(vi.mocked(showToast.success)).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not save memory when blurred without a change', async () => {
|
||||
const { updateSettingsAsync } = mockUseSettings({
|
||||
preferences: { sandbox: { enabled: true, memoryMib: 4096 } },
|
||||
})
|
||||
mockHealth({ available: true, enforced: false })
|
||||
|
||||
const user = await renderSandbox()
|
||||
const memory = screen.getByLabelText('Default memory (GiB)')
|
||||
await user.click(memory)
|
||||
await user.tab()
|
||||
|
||||
expect(updateSettingsAsync).not.toHaveBeenCalled()
|
||||
expect(vi.mocked(showToast.success)).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not save idle stop or max running VMs when blurred at the defaults', async () => {
|
||||
const { updateSettingsAsync } = mockUseSettings({ preferences: { sandbox: { enabled: true } } })
|
||||
mockHealth({ available: true, enforced: false })
|
||||
|
||||
const user = await renderSandbox()
|
||||
await user.click(screen.getByLabelText('Remove idle sandbox VMs after (minutes)'))
|
||||
await user.tab()
|
||||
await user.click(screen.getByLabelText('Max running sandbox VMs'))
|
||||
await user.tab()
|
||||
|
||||
expect(updateSettingsAsync).not.toHaveBeenCalled()
|
||||
expect(vi.mocked(showToast.success)).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -96,6 +96,7 @@ export function SandboxSettings() {
|
||||
const commitCpus = () => {
|
||||
const trimmed = draft.cpus.trim()
|
||||
if (trimmed === '') {
|
||||
if (sandboxPreferences?.cpus === undefined) return
|
||||
void saveSandbox({ cpus: undefined }, 'Default CPUs reset to the server default')
|
||||
return
|
||||
}
|
||||
@@ -108,12 +109,14 @@ export function SandboxSettings() {
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (value === sandboxPreferences?.cpus) return
|
||||
void saveSandbox({ cpus: value }, 'Default CPUs saved')
|
||||
}
|
||||
|
||||
const commitMemory = () => {
|
||||
const trimmed = draft.memoryGib.trim()
|
||||
if (trimmed === '') {
|
||||
if (sandboxPreferences?.memoryMib === undefined) return
|
||||
void saveSandbox({ memoryMib: undefined }, 'Default memory reset to the server default')
|
||||
return
|
||||
}
|
||||
@@ -129,6 +132,7 @@ export function SandboxSettings() {
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (memoryMib === sandboxPreferences?.memoryMib) return
|
||||
void saveSandbox({ memoryMib }, 'Default memory saved')
|
||||
}
|
||||
|
||||
@@ -144,6 +148,7 @@ export function SandboxSettings() {
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (value === (sandboxPreferences?.idleStopMinutes ?? DEFAULT_SANDBOX_IDLE_STOP_MINUTES)) return
|
||||
void saveSandbox({ idleStopMinutes: value }, 'Idle removal timeout saved')
|
||||
}
|
||||
|
||||
@@ -159,6 +164,7 @@ export function SandboxSettings() {
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (value === (sandboxPreferences?.maxRunningVms ?? DEFAULT_SANDBOX_MAX_RUNNING_VMS)) return
|
||||
void saveSandbox({ maxRunningVms: value }, 'Max running sandbox VMs saved')
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user