Files
opencode-manager/backend/test/routes/git.test.ts
T
Chris Scott e0331b43bf feat(secrets): repository secrets and variables injected into agent shells and terminals
Add a GitHub Actions-style store for secrets and variables so tests can read
credentials from the environment without .env files in the repo.

- secrets table keyed by kind, repo (NULL for global) and environment;
  secrets are encrypted with encryptSecret and write-only, variables are plain
  text. Names follow GitHub's rules and reject Manager-reserved names.
- Global (/api/secrets, /api/variables) and repo
  (/api/repos/:id/secrets|variables) routes.
- The trusted .ocm/project.json gains environment and env fields that map
  names to ${{ secrets.X }} / ${{ vars.X }} expressions. Both are part of the
  trust hash; files without them hash as before, so existing trust survives.
- RepoEnvService.resolveForDirectory resolves the repo and approved mapping
  for any directory, including worktrees outside the repo folder, with
  environment > repo > global precedence. It never throws.
- /api/internal/shell-env replaces /git-credentials/gh-env and feeds the
  ocm-gh-env plugin, the sandbox shell plan and TerminalService.create.
- The plugin masks injected secret values as *** in completed tool results.
- The sandbox shell plan is now the only source of the guest environment: the
  plugin pins OCM_SANDBOX_FORWARD_ENV and the shim forwards only those names.
2026-10-23 22:59:57 -04:00

513 lines
18 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, type MockedFunction } from 'vitest'
import { createRepoRoutes } from '../../src/routes/repos'
import type { Hono } from 'hono'
import { getRepoById } from '../../src/db/queries'
import type { Database } from 'bun:sqlite'
import type { Repo } from '../../../shared/src/types'
import type { GitAuthService } from '../../src/services/git-auth'
import type { ScheduleService } from '../../src/services/schedules'
import type { TerminalService } from '../../src/services/terminal'
import type { ProjectConfigService } from '../../src/services/project-config'
import type { SecretsService } from '../../src/services/secrets'
import type { RepoWorkspaceService } from '../../src/services/repo-workspace'
import { createStubOpenCodeClient } from '../helpers/stub-opencode-client'
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(),
}))
const getRepoByIdMock = getRepoById as MockedFunction<typeof getRepoById>
const createMockRepo = (overrides: Partial<Repo> = {}): Repo => ({
id: 1,
localPath: 'test-repo',
fullPath: '/nonexistent/path',
defaultBranch: 'main',
cloneStatus: 'ready' as const,
clonedAt: Date.now(),
...overrides,
} as Repo)
describe('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 = createRepoRoutes(mockDatabase, mockGitAuthService, {} as unknown as ScheduleService, createStubOpenCodeClient(), {} as unknown as TerminalService, {} as unknown as ProjectConfigService, {} as unknown as RepoWorkspaceService, {} as unknown as SecretsService)
})
describe('GET /:id/git/status', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/status')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 500 when git operation fails for non-existent path', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/status')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /discover', () => {
it('should reject invalid maxDepth values', async () => {
const response = await app.request('/discover', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ rootPath: '/Users/test/projects', maxDepth: 99 }),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/fetch', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/fetch', { method: 'POST' })
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/fetch', { method: 'POST' })
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/pull', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/pull', { method: 'POST' })
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/pull', { method: 'POST' })
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/push', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/push', { method: 'POST' })
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should accept setUpstream parameter', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/push', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ setUpstream: true }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should work without setUpstream parameter', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/push', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/push', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/commit', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/commit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: 'Test' }),
})
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 400 when message is missing', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/commit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'message is required')
})
it('should accept message and stagedPaths', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/commit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: 'Test commit', stagedPaths: ['file1.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/commit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: 'Test' }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/stage', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/stage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 400 when paths is not an array', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/stage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: 'not-an-array' }),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'paths is required and must be an array')
})
it('should return 400 when paths is missing', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/stage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'paths is required and must be an array')
})
it('should accept array of paths', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/stage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts', 'file2.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/stage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('POST /:id/git/unstage', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/unstage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 400 when paths is not an array', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/unstage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: 'not-an-array' }),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'paths is required and must be an array')
})
it('should return 400 when paths is missing', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/unstage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'paths is required and must be an array')
})
it('should accept array of paths', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/unstage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts', 'file2.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/unstage', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths: ['file1.ts'] }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('GET /:id/git/log', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/log')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should use default limit when not provided', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/log')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should use custom limit when provided', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/log?limit=5')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/log')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
describe('GET /:id/git/diff', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/diff?path=file.ts')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 400 when path parameter is missing', async () => {
const response = await app.request('/1/git/diff')
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'path query parameter is required')
})
it('should accept path parameter', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/diff?path=file.ts')
await response.text()
expect([200, 500]).toContain(response.status)
})
it('should handle special characters in path', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/diff?path=src%2Fcomponents%2FButton.tsx')
expect([200, 500]).toContain(response.status)
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/diff?path=file.ts')
expect([200, 500]).toContain(response.status)
})
})
describe('POST /:id/git/reset', () => {
it('should return 404 when repo does not exist', async () => {
getRepoByIdMock.mockReturnValue(null)
const response = await app.request('/999/git/reset', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ commitHash: 'abc123' }),
})
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('error', 'Repo not found')
})
it('should return 400 when commitHash is missing', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/reset', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('error', 'commitHash is required')
})
it('should accept commitHash parameter', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/reset', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ commitHash: 'abc123' }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
it('should return 500 when git operation fails', async () => {
getRepoByIdMock.mockReturnValue(createMockRepo())
const response = await app.request('/1/git/reset', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ commitHash: 'abc123' }),
})
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('error')
})
})
})