Files

61 lines
1.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { startNativeForward, stopNativeForward } from '../../../src/components/connection/forwardRuntime'
import type { ForwardRule } from '../../../src/components/connection/forwardRules'
const localRule: ForwardRule = {
id: 'pf-local',
type: 'local',
name: 'db',
localPort: 3306,
localBindHost: '127.0.0.1',
remoteHost: '127.0.0.1',
remotePort: 3306,
}
describe('forwardRuntime', () => {
afterEach(() => {
vi.restoreAllMocks()
})
it('starts a local forward through the native API', async () => {
const sshForwardLocal = vi
.spyOn(window.opsxApi, 'sshForwardLocal')
.mockResolvedValue({ success: true, forwardId: 'pf-local' })
await expect(startNativeForward('conn-1', localRule)).resolves.toEqual({
success: true,
error: undefined,
errorCode: undefined,
})
expect(sshForwardLocal).toHaveBeenCalledWith('conn-1', 3306, '127.0.0.1', 3306, 'pf-local', '127.0.0.1')
})
it('stops by runtime tunnel id when the persisted rule id misses', async () => {
const sshForwardStop = vi
.spyOn(window.opsxApi, 'sshForwardStop')
.mockResolvedValueOnce({ success: false, error: 'missing' })
.mockResolvedValueOnce({ success: true })
vi.spyOn(window.opsxApi, 'sshForwardList').mockResolvedValue({
success: true,
tunnels: [
{
forwardId: 'runtime-9',
connectionId: 'conn-1',
kind: 'local',
bindHost: '127.0.0.1',
boundPort: 3306,
startedAt: 1,
running: true,
activeConnections: 0,
bytesUp: 0,
bytesDown: 0,
},
],
})
await expect(stopNativeForward('conn-1', localRule)).resolves.toMatchObject({ success: true })
expect(sshForwardStop).toHaveBeenNthCalledWith(1, 'pf-local')
expect(sshForwardStop).toHaveBeenNthCalledWith(2, 'runtime-9')
})
})