Files
opencode-manager/backend/test/routes/internal-opencode-workspaces.test.ts
Chris Scott b13707b5b3 Merge forge/terminal-dev-loop into beta
feat: integrated terminal, project actions and dev server preview (OM-17, OM-18, OM-19)

Integration with #380 and #379:
- Adopt the git-config identity model: drop the branch's identity stubs
  (getShellEnv, getAssignedGitIdentityEnv, repo identity ids, duplicate
  createGitService and getMainCheckoutPath); terminals receive only the
  GitHub token env and take their commit identity from git config.
- Add RepoWorkspaceService as the single owner of OpenCode workspace
  side effects: worktree setup commands on create and terminal cleanup on
  remove, used by the workspace routes, the session launcher (ocm tool and
  multi-run) and multi-run discard, with one worktree-sibling matcher.
- Run repo-delete terminal cleanup inside the worktree branch deletion
  flow.
- Combine the CreateWorktreeDialog tests and update docs for the identity
  model and setup command coverage.
2026-10-03 23:50:10 -04:00

151 lines
5.6 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import type { SessionPermissionModeService } from '../../src/services/session-permission-modes'
import type { RepoWorkspaceService } from '../../src/services/repo-workspace'
import { Hono } from 'hono'
import type { Database } from 'bun:sqlite'
import { createInternalRoutes } from '../../src/routes/internal'
import type { ScheduleService } from '../../src/services/schedules'
import type { NotificationService } from '../../src/services/notification'
import type { SettingsService } from '../../src/services/settings'
import type { OpenCodeClient } from '../../src/services/opencode/client'
import type { Repo } from '../../src/types/repo'
const mockDb = {
prepare: vi.fn().mockReturnValue({
run: vi.fn(),
get: vi.fn(),
all: vi.fn(),
}),
exec: vi.fn(),
close: vi.fn(),
transaction: vi.fn((fn: () => void) => fn()),
} as unknown as Database
vi.mock('bun:sqlite', () => ({
Database: vi.fn(() => mockDb),
}))
const mockListRepos = vi.fn()
vi.mock('../../src/db/queries', async () => {
const { getRepoDisplayName } = await vi.importActual<typeof import('@opencode-manager/shared/utils')>('@opencode-manager/shared/utils')
return {
listRepos: (...args: unknown[]) => mockListRepos(...args),
getRepoName: (repo: Parameters<typeof getRepoDisplayName>[0]) => getRepoDisplayName(repo),
}
})
vi.mock('../../src/db/migration-runner', () => ({
migrate: vi.fn(),
}))
vi.mock('../../src/services/internal-token', () => ({
getOrCreateInternalToken: vi.fn().mockReturnValue('test-internal-token'),
}))
vi.mock('../../src/services/schedules', () => ({
ScheduleService: vi.fn(),
}))
vi.mock('../../src/services/notification', () => ({
NotificationService: vi.fn(),
}))
vi.mock('../../src/services/settings', () => ({
SettingsService: vi.fn(),
}))
vi.mock('../../src/services/opencode/client', () => ({
createOpenCodeClient: vi.fn(),
}))
function makeRepo(overrides: Partial<Repo>): Repo {
return {
id: 1,
localPath: 'test-repo',
fullPath: '/tmp/test-repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
...overrides,
}
}
describe('internal-opencode-workspaces routes', () => {
let app: Hono
let token: string
beforeEach(() => {
vi.clearAllMocks()
mockListRepos.mockReturnValue([])
const scheduleService = {} as ScheduleService
const notificationService = {} as NotificationService
const settingsService = {} as SettingsService
const openCodeClient = {
forwardRaw: vi.fn(),
} as unknown as OpenCodeClient
app = new Hono()
app.route('/api/internal', createInternalRoutes(mockDb, scheduleService, notificationService, settingsService, openCodeClient, {} as SessionPermissionModeService, {} as unknown as RepoWorkspaceService))
token = 'test-internal-token'
})
it('GET /api/internal/opencode-workspaces returns 401 without bearer token', async () => {
const res = await app.request('/api/internal/opencode-workspaces')
expect(res.status).toBe(401)
})
it('GET /api/internal/opencode-workspaces returns 200 with bearer token', async () => {
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: unknown[] }
expect(body).toHaveProperty('workspaces')
expect(Array.isArray(body.workspaces)).toBe(true)
})
it('GET /api/internal/opencode-workspaces only returns ready repos', async () => {
mockListRepos.mockReturnValue([
makeRepo({ id: 1, cloneStatus: 'ready', localPath: 'ready-repo' }),
makeRepo({ id: 2, cloneStatus: 'cloning', localPath: 'cloning-repo' }),
makeRepo({ id: 3, cloneStatus: 'error', localPath: 'error-repo' }),
])
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: Array<{ repoId: number; cloneStatus: string }> }
expect(body.workspaces.length).toBe(1)
expect(body.workspaces[0]?.cloneStatus).toBe('ready')
expect(body.workspaces[0]?.repoId).toBeDefined()
})
it('GET /api/internal/opencode-workspaces returns workspace structure', async () => {
mockListRepos.mockReturnValue([
makeRepo({ id: 1, localPath: 'test-repo', cloneStatus: 'ready' }),
makeRepo({ id: 2, localPath: 'worktree-repo', cloneStatus: 'ready', isWorktree: true }),
])
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: Array<{ repoId: number; name: string; branch: string | null; cloneStatus: string; directory: string; isWorktree: boolean; extra: { repoId: number; localPath: string; fullPath: string } }> }
expect(body.workspaces.length).toBe(2)
const workspace = body.workspaces.find((w) => w.repoId === 1)!
expect(workspace).toHaveProperty('repoId')
expect(workspace).toHaveProperty('name')
expect(workspace).toHaveProperty('branch')
expect(workspace).toHaveProperty('cloneStatus')
expect(workspace).toHaveProperty('directory')
expect(workspace).toHaveProperty('projectId')
expect(workspace).toHaveProperty('extra')
expect(workspace.extra).toHaveProperty('repoId')
expect(workspace.extra).toHaveProperty('localPath')
expect(workspace.extra).toHaveProperty('fullPath')
const worktreeWorkspace = body.workspaces.find((w) => w.repoId === 2)!
expect(workspace.isWorktree).toBe(false)
expect(worktreeWorkspace.isWorktree).toBe(true)
})
})