Files
opencode-manager/backend/test/routes/repo-git.test.ts
T
Chris Scott 414f39b809 feat: SSH Authentication Keys v2 (#113)
* feat: add SSH authentication keys v2 for Git operations

Add comprehensive SSH key authentication support for Git operations including:
- SSH host key verification with user approval flow
- Encrypted SSH key storage with passphrase support
- SSH connection testing endpoint
- Real-time host key change warnings

Backend changes:
- New SSHHostKeyHandler for managing host key verification via IPC
- Add passphrase handler for locked SSH keys
- Add SSH key manager utilities (key writing, validation, cleanup)
- Add crypto utilities for secure key/secret encryption
- Add SSH validation utilities for key format checking
- Add SSH routes for host key response handling
- Enhance GitAuthService with SSH setup and environment methods
- Update GitService to handle SSH authentication for push/fetch/pull
- Update OpenCode server to support SSH keys with SSH config generation
- Add database migration for trusted_ssh_hosts table
- Add settings endpoint for SSH connection testing

Frontend changes:
- Add SSHHostKeyDialog for host key verification UI
- Add PassphraseModal for SSH key passphrase input
- Update GitCredentialDialog with SSH key type and testing
- Update GitSettings to manage SSH keys
- Add SSH API client and hooks (useSSH, usePassphraseHandler)

Shared changes:
- Add SSH schemas and types

Test coverage:
- Integration tests for SSH flow
- IPC handler tests for host key and passphrase
- SSH route tests
- SSH utility validation tests
- Updated existing tests for SSH-aware changes
2026-02-08 00:06:44 -05:00

303 lines
11 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, type MockedFunction } from 'vitest'
import { Hono } from 'hono'
import type { Database } from 'bun:sqlite'
import type { GitAuthService } from '../../src/services/git-auth'
import { createRepoGitRoutes } from '../../src/routes/repo-git'
import * as db from '../../src/db/queries'
vi.mock('bun:sqlite', () => ({
Database: vi.fn(),
}))
vi.mock('../../src/utils/logger', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
},
}))
vi.mock('../../src/db/queries', () => ({
getRepoById: vi.fn(),
}))
vi.mock('../../src/utils/process', () => ({
executeCommand: vi.fn(),
}))
vi.mock('@opencode-manager/shared/config/env', () => ({
getReposPath: vi.fn(() => '/repos'),
getWorkspacePath: vi.fn(() => '/tmp/test-workspace'),
ENV: {
OPENCODE: { PORT: 5551, HOST: '127.0.0.1' },
SERVER: { PORT: 5001, HOST: '0.0.0.0', CORS_ORIGIN: '*', NODE_ENV: 'test' },
AUTH: { SECRET: 'test-secret' },
},
}))
vi.mock('@opencode-manager/shared/config', () => ({
DEFAULTS: {
SSE: {
RECONNECT_DELAY_MS: 1000,
MAX_RECONNECT_DELAY_MS: 30000,
IDLE_GRACE_PERIOD_MS: 120000,
},
},
}))
vi.mock('eventsource', () => ({
EventSource: vi.fn(),
}))
const getRepoByIdMock = db.getRepoById as MockedFunction<typeof db.getRepoById>
describe('Repo Git Routes', () => {
let app: Hono
let mockDatabase: Database
let mockGitAuthService: GitAuthService
beforeEach(() => {
vi.clearAllMocks()
mockDatabase = {
run: vi.fn(),
prepare: vi.fn(() => ({
run: vi.fn(),
get: vi.fn(),
all: vi.fn(),
iterate: vi.fn(),
values: vi.fn(),
})),
exec: vi.fn(),
query: vi.fn(),
inTransaction: vi.fn(),
close: vi.fn(),
} as unknown as Database
mockGitAuthService = {
getGitEnvironment: vi.fn().mockReturnValue({}),
} as unknown as GitAuthService
app = createRepoGitRoutes(mockDatabase, mockGitAuthService)
})
describe('POST /git-status-batch', () => {
it('returns 400 when repoIds is not an array', async () => {
const response = await app.request('/git-status-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ repoIds: 'not-an-array' }),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'repoIds must be an array of numbers')
})
it('returns 400 when repoIds contains non-numbers', async () => {
const response = await app.request('/git-status-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ repoIds: [1, 'two', 3] }),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'repoIds must be an array of numbers')
})
it('returns empty object when no repos found', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/git-status-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ repoIds: [1, 2, 3] }),
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toEqual({})
})
it('returns status for multiple repos', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockImplementation((_, id) => {
if (id === 1) return { id: 1, fullPath: '/repo1' } as any
if (id === 2) return { id: 2, fullPath: '/repo2' } as any
return null
})
executeCommandMock.mockImplementation((args) => {
if (args.includes('rev-parse')) return Promise.resolve('main')
if (args.includes('rev-list')) return Promise.resolve('0 0')
if (args.includes('status')) return Promise.resolve('')
return Promise.resolve('')
})
const response = await app.request('/git-status-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ repoIds: [1, 2] }),
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('1')
expect(body).toHaveProperty('2')
})
it('skips repos that fail and continues with others', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockImplementation((_, id) => {
if (id === 1) return { id: 1, fullPath: '/repo1' } as any
if (id === 2) return null
return null
})
executeCommandMock.mockImplementation((args) => {
if (args.includes('rev-parse')) return Promise.resolve('main')
if (args.includes('rev-list')) return Promise.resolve('0 0')
if (args.includes('status')) return Promise.resolve('')
return Promise.resolve('')
})
const response = await app.request('/git-status-batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ repoIds: [1, 2] }),
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('1')
expect(body).not.toHaveProperty('2')
})
})
describe('GET /:id/git/diff-full', () => {
it('returns 400 when path parameter is missing', async () => {
const response = await app.request('/1/git/diff-full')
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'path query parameter is required')
})
it('returns 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/diff-full?path=file.ts')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('returns diff with includeStaged=true', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockReturnValue({ id: 1, localPath: 'test-repo', fullPath: '/repos/test-repo' } as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('status')) return Promise.resolve('M file.ts')
if (args.includes('rev-parse')) return Promise.resolve('abc123')
if (args.includes('diff')) return Promise.resolve('+added line')
return Promise.resolve('')
})
const response = await app.request('/1/git/diff-full?path=file.ts&includeStaged=true')
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('path')
expect(body).toHaveProperty('diff')
expect(body).toHaveProperty('additions')
expect(body).toHaveProperty('deletions')
})
it('returns diff with includeStaged=false', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockReturnValue({ id: 1, localPath: 'test-repo', fullPath: '/repos/test-repo' } as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('status')) return Promise.resolve('M file.ts')
if (args.includes('rev-parse')) return Promise.resolve('abc123')
if (args.includes('diff')) return Promise.resolve('-removed line')
return Promise.resolve('')
})
const response = await app.request('/1/git/diff-full?path=file.ts&includeStaged=false')
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('deletions')
})
it('returns 500 when diff operation fails', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockReturnValue({ id: 1, localPath: 'test-repo' } as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('status')) return Promise.resolve('M file.ts')
if (args.includes('rev-parse')) return Promise.resolve('abc123')
if (args.includes('diff')) return Promise.reject(new Error('Diff failed'))
return Promise.resolve('')
})
const response = await app.request('/1/git/diff-full?path=file.ts')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('GET /:id/git/branches', () => {
it('returns 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/branches')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('returns branches and status', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: '/path/to/repo' } as any)
executeCommandMock.mockImplementation((args) => {
if (args.includes('rev-parse')) return Promise.resolve('main')
if (args.includes('branch')) return Promise.resolve('* main abc123 [origin/main] Initial commit')
if (args.includes('rev-list')) return Promise.resolve('0 0')
return Promise.resolve('')
})
const response = await app.request('/1/git/branches')
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('branches')
expect(body).toHaveProperty('status')
expect(Array.isArray((body as { branches: unknown[] }).branches)).toBe(true)
})
it('returns 500 when branch operation fails', async () => {
const { executeCommand } = await import('../../src/utils/process')
const executeCommandMock = executeCommand as MockedFunction<typeof executeCommand>
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: '/path/to/repo' } as any)
executeCommandMock.mockRejectedValue(new Error('Git operation failed'))
const response = await app.request('/1/git/branches')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
})