mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
Merge forge/terminal-dev-loop integration (b13707b5b) into beta
Bring the terminal, project actions and dev server preview merge (OM-17, OM-18, OM-19) onto beta after 4a2e695ec. No conflicts; the merged CreateWorktreeDialog keeps BaseBranchSelect with origin/ remote bases and the worktree setup terminal navigation.
This commit is contained in:
commit
cf9fa61118
135 files changed
+12726
-492
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { renderHook, act, render, screen } from '@testing-library/react'
|
||||
import { MemoryRouter, useNavigate } from 'react-router-dom'
|
||||
import { useDialogParam } from './useDialogParam'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { dialogSearch, openDialogParam, useDialogParam } from './useDialogParam'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
|
||||
describe('useDialogParam', () => {
|
||||
const createWrapper = (initialEntries?: string[]) => {
|
||||
@@ -114,4 +114,48 @@ describe('useDialogParam', () => {
|
||||
act(() => { screen.getByText('back').click() })
|
||||
expect(screen.getByTestId('dialog-state').textContent).toBe('closed')
|
||||
})
|
||||
|
||||
it('openDialogParam sets the dialog and extra params in one push while clearing mobileTab', () => {
|
||||
const updateParams = vi.fn()
|
||||
openDialogParam(updateParams, 'terminal', { terminal: 'pty-1' })
|
||||
|
||||
expect(updateParams).toHaveBeenCalledTimes(1)
|
||||
const [updater, mode] = updateParams.mock.calls[0]
|
||||
expect(mode).toBe('push')
|
||||
|
||||
const params = new URLSearchParams('mobileTab=more&keep=1')
|
||||
updater(params)
|
||||
|
||||
expect(params.get('dialog')).toBe('terminal')
|
||||
expect(params.get('terminal')).toBe('pty-1')
|
||||
expect(params.get('keep')).toBe('1')
|
||||
expect(params.has('mobileTab')).toBe(false)
|
||||
})
|
||||
|
||||
it('openDialogParam without extra params only sets the dialog', () => {
|
||||
const updateParams = vi.fn()
|
||||
openDialogParam(updateParams, 'mcp')
|
||||
|
||||
const [updater] = updateParams.mock.calls[0]
|
||||
const params = new URLSearchParams()
|
||||
updater(params)
|
||||
|
||||
expect(params.get('dialog')).toBe('mcp')
|
||||
expect([...params.keys()]).toEqual(['dialog'])
|
||||
})
|
||||
|
||||
it('dialogSearch builds a query string from the dialog name and extra params', () => {
|
||||
expect(dialogSearch('terminal')).toBe('?dialog=terminal')
|
||||
expect(dialogSearch('terminal', { terminal: 'pty-1' })).toBe('?dialog=terminal&terminal=pty-1')
|
||||
expect(dialogSearch('preview', { previewPort: '5173', previewPath: '/' })).toBe(
|
||||
'?dialog=preview&previewPort=5173&previewPath=%2F',
|
||||
)
|
||||
})
|
||||
|
||||
it('dialogSearch URL-encodes extra params and omits mobileTab', () => {
|
||||
const search = dialogSearch('terminal', { terminal: 'pty 1/2' })
|
||||
|
||||
expect(search).toBe('?dialog=terminal&terminal=pty+1%2F2')
|
||||
expect(search).not.toContain('mobileTab')
|
||||
})
|
||||
})
|
||||
@@ -1,12 +1,32 @@
|
||||
import { useCallback } from 'react'
|
||||
import { useUrlParams } from './useUrlParams'
|
||||
|
||||
function setDialogParams(
|
||||
params: URLSearchParams,
|
||||
name: string,
|
||||
extraParams?: Record<string, string>,
|
||||
): void {
|
||||
params.set('dialog', name)
|
||||
if (extraParams) {
|
||||
for (const [key, value] of Object.entries(extraParams)) {
|
||||
params.set(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function dialogSearch(name: string, extraParams?: Record<string, string>): string {
|
||||
const params = new URLSearchParams()
|
||||
setDialogParams(params, name, extraParams)
|
||||
return `?${params.toString()}`
|
||||
}
|
||||
|
||||
export function openDialogParam(
|
||||
updateParams: ReturnType<typeof useUrlParams>['updateParams'],
|
||||
name: string,
|
||||
extraParams?: Record<string, string>,
|
||||
): void {
|
||||
updateParams((p) => {
|
||||
p.set('dialog', name)
|
||||
setDialogParams(p, name, extraParams)
|
||||
p.delete('mobileTab')
|
||||
}, 'push')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useCallback } from 'react'
|
||||
import { openDialogParam } from './useDialogParam'
|
||||
import { useUrlParams } from './useUrlParams'
|
||||
|
||||
export function useOpenPreview(): (port: number, path?: string) => void {
|
||||
const { updateParams } = useUrlParams()
|
||||
|
||||
return useCallback(
|
||||
(port: number, path = '/') => {
|
||||
openDialogParam(updateParams, 'preview', {
|
||||
previewPort: String(port),
|
||||
previewPath: path,
|
||||
})
|
||||
},
|
||||
[updateParams],
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { act } from '@testing-library/react'
|
||||
import { renderHookWithRouterAndLocation } from '@/test/test-utils'
|
||||
import { useOpenTerminal } from './useOpenTerminal'
|
||||
|
||||
describe('useOpenTerminal', () => {
|
||||
it('opens the terminal dialog with the terminal id', () => {
|
||||
const { result, capturedSearch } = renderHookWithRouterAndLocation(
|
||||
() => useOpenTerminal(),
|
||||
['/repos/1'],
|
||||
)
|
||||
|
||||
act(() => {
|
||||
result.current('pty-1')
|
||||
})
|
||||
|
||||
const params = new URLSearchParams(capturedSearch.current)
|
||||
expect(params.get('dialog')).toBe('terminal')
|
||||
expect(params.get('terminal')).toBe('pty-1')
|
||||
})
|
||||
|
||||
it('merges extra params and clears mobileTab in the same navigation', () => {
|
||||
const { result, capturedSearch } = renderHookWithRouterAndLocation(
|
||||
() => useOpenTerminal(),
|
||||
['/repos/1?mobileTab=more'],
|
||||
)
|
||||
|
||||
act(() => {
|
||||
result.current('pty-2', { repoTab: 'workspaces' })
|
||||
})
|
||||
|
||||
const params = new URLSearchParams(capturedSearch.current)
|
||||
expect(params.get('dialog')).toBe('terminal')
|
||||
expect(params.get('terminal')).toBe('pty-2')
|
||||
expect(params.get('repoTab')).toBe('workspaces')
|
||||
expect(params.has('mobileTab')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useCallback } from 'react'
|
||||
import { openDialogParam } from './useDialogParam'
|
||||
import { useUrlParams } from './useUrlParams'
|
||||
|
||||
export function useOpenTerminal(): (
|
||||
terminalId: string,
|
||||
extraParams?: Record<string, string>,
|
||||
) => void {
|
||||
const { updateParams } = useUrlParams()
|
||||
|
||||
return useCallback(
|
||||
(terminalId: string, extraParams?: Record<string, string>) => {
|
||||
openDialogParam(updateParams, 'terminal', { terminal: terminalId, ...extraParams })
|
||||
},
|
||||
[updateParams],
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { act, renderHook } from '@testing-library/react'
|
||||
import { useTerminalSocket } from './useTerminalSocket'
|
||||
|
||||
class FakeWebSocket {
|
||||
static CONNECTING = 0
|
||||
static OPEN = 1
|
||||
static CLOSING = 2
|
||||
static CLOSED = 3
|
||||
static instances: FakeWebSocket[] = []
|
||||
|
||||
url: string
|
||||
binaryType = 'blob'
|
||||
readyState = FakeWebSocket.CONNECTING
|
||||
sent: string[] = []
|
||||
onopen: ((event: Event) => void) | null = null
|
||||
onmessage: ((event: MessageEvent) => void) | null = null
|
||||
onclose: ((event: CloseEvent) => void) | null = null
|
||||
onerror: ((event: Event) => void) | null = null
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url
|
||||
FakeWebSocket.instances.push(this)
|
||||
}
|
||||
|
||||
send(data: string) {
|
||||
this.sent.push(data)
|
||||
}
|
||||
|
||||
close() {
|
||||
this.readyState = FakeWebSocket.CLOSED
|
||||
}
|
||||
|
||||
open() {
|
||||
this.readyState = FakeWebSocket.OPEN
|
||||
this.onopen?.(new Event('open'))
|
||||
}
|
||||
|
||||
message(data: string | ArrayBuffer) {
|
||||
this.onmessage?.({ data } as MessageEvent)
|
||||
}
|
||||
|
||||
serverClose(code: number) {
|
||||
this.readyState = FakeWebSocket.CLOSED
|
||||
this.onclose?.({ code } as CloseEvent)
|
||||
}
|
||||
}
|
||||
|
||||
function metaFrame(cursor: number): ArrayBuffer {
|
||||
const payload = new TextEncoder().encode(JSON.stringify({ cursor }))
|
||||
const bytes = new Uint8Array(payload.length + 1)
|
||||
bytes[0] = 0x00
|
||||
bytes.set(payload, 1)
|
||||
return bytes.buffer
|
||||
}
|
||||
|
||||
function renderSocket(overrides: Partial<Parameters<typeof useTerminalSocket>[0]> = {}) {
|
||||
return renderHook(() =>
|
||||
useTerminalSocket({
|
||||
repoId: 1,
|
||||
directory: '/work/repo',
|
||||
ptyID: 'pty-1',
|
||||
onOutput: vi.fn(),
|
||||
onExit: vi.fn(),
|
||||
enabled: true,
|
||||
...overrides,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
describe('useTerminalSocket', () => {
|
||||
beforeEach(() => {
|
||||
FakeWebSocket.instances = []
|
||||
vi.stubGlobal('WebSocket', FakeWebSocket)
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('includes the directory in the connection url', () => {
|
||||
renderSocket()
|
||||
const url = new URL(FakeWebSocket.instances[0].url)
|
||||
expect(url.pathname).toBe('/api/repos/1/terminals/pty-1/connect')
|
||||
expect(url.searchParams.get('directory')).toBe('/work/repo')
|
||||
})
|
||||
|
||||
it('accumulates the cursor and sends it when reconnecting after 1006', () => {
|
||||
renderSocket()
|
||||
const first = FakeWebSocket.instances[0]
|
||||
|
||||
act(() => {
|
||||
first.open()
|
||||
first.message(metaFrame(5))
|
||||
first.message('abc')
|
||||
})
|
||||
|
||||
act(() => {
|
||||
first.serverClose(1006)
|
||||
})
|
||||
|
||||
expect(FakeWebSocket.instances).toHaveLength(1)
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1000)
|
||||
})
|
||||
|
||||
expect(FakeWebSocket.instances).toHaveLength(2)
|
||||
const second = new URL(FakeWebSocket.instances[1].url)
|
||||
expect(second.searchParams.get('cursor')).toBe('8')
|
||||
expect(second.searchParams.get('directory')).toBe('/work/repo')
|
||||
})
|
||||
|
||||
it('calls onExit without reconnecting on close code 4404', () => {
|
||||
const onExit = vi.fn()
|
||||
renderSocket({ onExit })
|
||||
|
||||
act(() => {
|
||||
FakeWebSocket.instances[0].serverClose(4404)
|
||||
})
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(60000)
|
||||
})
|
||||
|
||||
expect(onExit).toHaveBeenCalledTimes(1)
|
||||
expect(FakeWebSocket.instances).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('exhausts the retry budget when upstream connections keep failing after open', () => {
|
||||
const { result } = renderSocket()
|
||||
const delays = [1000, 2000, 5000, 5000, 5000]
|
||||
|
||||
for (const delay of delays) {
|
||||
const socket = FakeWebSocket.instances[FakeWebSocket.instances.length - 1]
|
||||
act(() => {
|
||||
socket.open()
|
||||
socket.serverClose(1011)
|
||||
})
|
||||
expect(result.current.status).toBe('reconnecting')
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(delay)
|
||||
})
|
||||
}
|
||||
|
||||
const finalSocket = FakeWebSocket.instances[FakeWebSocket.instances.length - 1]
|
||||
act(() => {
|
||||
finalSocket.open()
|
||||
finalSocket.serverClose(1011)
|
||||
})
|
||||
|
||||
expect(result.current.status).toBe('closed')
|
||||
expect(FakeWebSocket.instances).toHaveLength(6)
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(60000)
|
||||
})
|
||||
expect(FakeWebSocket.instances).toHaveLength(6)
|
||||
})
|
||||
|
||||
it('resets the retry budget only after a healthy upstream session', () => {
|
||||
const { result } = renderSocket()
|
||||
|
||||
act(() => {
|
||||
FakeWebSocket.instances[0].open()
|
||||
FakeWebSocket.instances[0].message(metaFrame(0))
|
||||
FakeWebSocket.instances[0].serverClose(1011)
|
||||
})
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1000)
|
||||
})
|
||||
|
||||
expect(FakeWebSocket.instances).toHaveLength(2)
|
||||
act(() => {
|
||||
FakeWebSocket.instances[1].open()
|
||||
FakeWebSocket.instances[1].serverClose(1011)
|
||||
})
|
||||
expect(result.current.status).toBe('reconnecting')
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(2000)
|
||||
})
|
||||
|
||||
expect(FakeWebSocket.instances).toHaveLength(3)
|
||||
})
|
||||
|
||||
it('forwards send to the open socket', () => {
|
||||
const { result } = renderSocket()
|
||||
const socket = FakeWebSocket.instances[0]
|
||||
|
||||
act(() => {
|
||||
socket.open()
|
||||
})
|
||||
act(() => {
|
||||
result.current.send('ls\n')
|
||||
})
|
||||
|
||||
expect(socket.sent).toContain('ls\n')
|
||||
})
|
||||
|
||||
it('does not connect when disabled', () => {
|
||||
renderSocket({ enabled: false })
|
||||
expect(FakeWebSocket.instances).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,135 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { TerminalFrameDecoder } from '@/lib/terminal-protocol'
|
||||
import { toWebSocketUrl } from '@/lib/websocket-url'
|
||||
|
||||
export type TerminalSocketStatus = 'connecting' | 'open' | 'reconnecting' | 'closed'
|
||||
|
||||
export interface UseTerminalSocketOptions {
|
||||
repoId: number
|
||||
directory: string | undefined
|
||||
ptyID: string
|
||||
onOutput: (text: string) => void
|
||||
onExit: () => void
|
||||
enabled: boolean
|
||||
}
|
||||
|
||||
const RECONNECT_DELAYS_MS = [1000, 2000, 5000, 5000, 5000]
|
||||
const TERMINAL_CLOSE_CODES = new Set([1000, 4404])
|
||||
|
||||
function buildSocketUrl(
|
||||
repoId: number,
|
||||
ptyID: string,
|
||||
directory: string | undefined,
|
||||
cursor: number | undefined,
|
||||
): string {
|
||||
const params = new URLSearchParams()
|
||||
if (directory !== undefined) params.set('directory', directory)
|
||||
if (cursor !== undefined) params.set('cursor', String(cursor))
|
||||
const query = params.toString()
|
||||
return toWebSocketUrl(
|
||||
`/api/repos/${repoId}/terminals/${encodeURIComponent(ptyID)}/connect${query ? `?${query}` : ''}`,
|
||||
)
|
||||
}
|
||||
|
||||
export function useTerminalSocket({
|
||||
repoId,
|
||||
directory,
|
||||
ptyID,
|
||||
onOutput,
|
||||
onExit,
|
||||
enabled,
|
||||
}: UseTerminalSocketOptions) {
|
||||
const socketRef = useRef<WebSocket | null>(null)
|
||||
const cursorRef = useRef<number | undefined>(undefined)
|
||||
const onOutputRef = useRef(onOutput)
|
||||
const onExitRef = useRef(onExit)
|
||||
onOutputRef.current = onOutput
|
||||
onExitRef.current = onExit
|
||||
|
||||
const [status, setStatus] = useState<TerminalSocketStatus>(enabled ? 'connecting' : 'closed')
|
||||
|
||||
const send = useCallback((data: string) => {
|
||||
const socket = socketRef.current
|
||||
if (socket && socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(data)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {
|
||||
setStatus('closed')
|
||||
return
|
||||
}
|
||||
|
||||
let disposed = false
|
||||
let attempt = 0
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
const decoder = new TerminalFrameDecoder()
|
||||
cursorRef.current = undefined
|
||||
|
||||
const connect = () => {
|
||||
if (disposed) return
|
||||
setStatus(attempt === 0 ? 'connecting' : 'reconnecting')
|
||||
|
||||
const socket = new WebSocket(buildSocketUrl(repoId, ptyID, directory, cursorRef.current))
|
||||
socket.binaryType = 'arraybuffer'
|
||||
socketRef.current = socket
|
||||
|
||||
socket.onopen = () => {
|
||||
if (disposed || socketRef.current !== socket) return
|
||||
setStatus('open')
|
||||
}
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
if (disposed || socketRef.current !== socket) return
|
||||
const frame = decoder.decode(event.data as string | ArrayBuffer)
|
||||
if (frame.type === 'cursor') {
|
||||
attempt = 0
|
||||
cursorRef.current = frame.cursor
|
||||
return
|
||||
}
|
||||
cursorRef.current = (cursorRef.current ?? 0) + frame.text.length
|
||||
onOutputRef.current(frame.text)
|
||||
}
|
||||
|
||||
socket.onclose = (event) => {
|
||||
if (disposed || socketRef.current !== socket) return
|
||||
socketRef.current = null
|
||||
|
||||
if (TERMINAL_CLOSE_CODES.has(event.code)) {
|
||||
setStatus('closed')
|
||||
onExitRef.current()
|
||||
return
|
||||
}
|
||||
|
||||
if (attempt >= RECONNECT_DELAYS_MS.length) {
|
||||
setStatus('closed')
|
||||
return
|
||||
}
|
||||
|
||||
const delay = RECONNECT_DELAYS_MS[attempt]
|
||||
attempt += 1
|
||||
setStatus('reconnecting')
|
||||
reconnectTimer = setTimeout(connect, delay)
|
||||
}
|
||||
}
|
||||
|
||||
connect()
|
||||
|
||||
return () => {
|
||||
disposed = true
|
||||
if (reconnectTimer) clearTimeout(reconnectTimer)
|
||||
const socket = socketRef.current
|
||||
socketRef.current = null
|
||||
if (socket) {
|
||||
socket.onopen = null
|
||||
socket.onmessage = null
|
||||
socket.onclose = null
|
||||
socket.onerror = null
|
||||
socket.close()
|
||||
}
|
||||
}
|
||||
}, [repoId, directory, ptyID, enabled])
|
||||
|
||||
return { send, status }
|
||||
}
|
||||
Reference in new issue
Block a user