mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
165 lines
5.9 KiB
TypeScript
165 lines
5.9 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest'
|
|
import { Hono } from 'hono'
|
|
import { Database } from 'bun:sqlite'
|
|
import { ScheduleService } from '../../src/services/schedules'
|
|
import { SettingsService } from '../../src/services/settings'
|
|
import { createOpenCodeClient } from '../../src/services/opencode/client'
|
|
import { allMigrations } from '../../src/db/migrations'
|
|
import { getOrCreateInternalToken } from '../../src/services/internal-token'
|
|
import { migrate } from '../../src/db/migration-runner'
|
|
import { createRepo } from '../../src/db/queries'
|
|
import type { CreateRepoInput } from '../../src/types/repo'
|
|
import type { ScheduleWorktreeManager } from '../../src/services/schedule-worktree'
|
|
import { createInternalTestApp } from '../helpers/internal-test-app'
|
|
|
|
describe('internal-repos routes', () => {
|
|
let db: Database
|
|
let scheduleService: ScheduleService
|
|
let settingsService: SettingsService
|
|
let app: Hono
|
|
let token: string
|
|
|
|
beforeEach(() => {
|
|
db = new Database(':memory:')
|
|
migrate(db, allMigrations)
|
|
const openCodeClient = createOpenCodeClient()
|
|
const stubWorktreeManager = { prepare: () => Promise.resolve(null), finalize: () => Promise.resolve({ commitHash: null }) } as unknown as ScheduleWorktreeManager
|
|
scheduleService = new ScheduleService(db, openCodeClient, stubWorktreeManager)
|
|
settingsService = new SettingsService(db)
|
|
app = new Hono()
|
|
app.route('/api/internal', createInternalTestApp(db, { scheduleService, settingsService }))
|
|
token = getOrCreateInternalToken(db)
|
|
})
|
|
|
|
it('GET /api/internal/repos returns 401 without bearer token', async () => {
|
|
const res = await app.request('/api/internal/repos')
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('GET /api/internal/repos returns 200 with bearer token', async () => {
|
|
const res = await app.request('/api/internal/repos', {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { repos: unknown[] }
|
|
expect(body).toHaveProperty('repos')
|
|
expect(Array.isArray(body.repos)).toBe(true)
|
|
})
|
|
|
|
it('GET /api/internal/repos returns repos in default order', async () => {
|
|
const repo1Input: CreateRepoInput = {
|
|
localPath: 'repo1',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}
|
|
const repo2Input: CreateRepoInput = {
|
|
localPath: 'repo2',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}
|
|
createRepo(db, repo1Input)
|
|
createRepo(db, repo2Input)
|
|
|
|
const res = await app.request('/api/internal/repos', {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { repos: Array<{ id: number; localPath: string }> }
|
|
expect(body.repos.length).toBe(2)
|
|
})
|
|
|
|
it('GET /api/internal/repos respects repoOrder preference', async () => {
|
|
const repo1Input: CreateRepoInput = {
|
|
localPath: 'repo1',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}
|
|
const repo2Input: CreateRepoInput = {
|
|
localPath: 'repo2',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}
|
|
const repo1 = createRepo(db, repo1Input)
|
|
const repo2 = createRepo(db, repo2Input)
|
|
|
|
settingsService.updateSettings({
|
|
repoOrder: [repo2.id, repo1.id],
|
|
})
|
|
|
|
const res = await app.request('/api/internal/repos', {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { repos: Array<{ id: number; localPath: string }> }
|
|
expect(body.repos.length).toBe(2)
|
|
expect(body.repos[0]?.id).toBe(repo2.id)
|
|
expect(body.repos[1]?.id).toBe(repo1.id)
|
|
})
|
|
|
|
it('POST /api/internal/repos returns 401 without bearer token', async () => {
|
|
const res = await app.request('/api/internal/repos', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({ repoUrl: 'https://github.com/owner/name' }),
|
|
})
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('POST /api/internal/repos rejects a missing repoUrl, unknown keys, and invalid JSON', async () => {
|
|
const bodies = [JSON.stringify({}), JSON.stringify({ repoUrl: 'https://github.com/owner/name', localPath: '/etc' }), 'not json']
|
|
for (const body of bodies) {
|
|
const res = await app.request('/api/internal/repos', {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
body,
|
|
})
|
|
expect(res.status).toBe(400)
|
|
}
|
|
})
|
|
|
|
it('POST /api/internal/repos returns the already registered repo for the same URL', async () => {
|
|
const existing = createRepo(db, {
|
|
repoUrl: 'https://github.com/owner/name',
|
|
localPath: 'name',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
})
|
|
|
|
const res = await app.request('/api/internal/repos', {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
body: JSON.stringify({ repoUrl: 'https://github.com/owner/name.git' }),
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { id: number }
|
|
expect(body.id).toBe(existing.id)
|
|
})
|
|
|
|
it('GET /api/internal/repos/:id/schedules still works after adding repos route', async () => {
|
|
const repoInput: CreateRepoInput = {
|
|
localPath: 'test-repo',
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
}
|
|
const repo = createRepo(db, repoInput)
|
|
|
|
const res = await app.request(`/api/internal/repos/${repo.id}/schedules`, {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { jobs: unknown[] }
|
|
expect(body).toHaveProperty('jobs')
|
|
expect(Array.isArray(body.jobs)).toBe(true)
|
|
})
|
|
}) |