mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
60 lines
2.4 KiB
TypeScript
60 lines
2.4 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 { 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 type { ScheduleWorktreeManager } from '../../src/services/schedule-worktree'
|
|
import { createInternalTestApp } from '../helpers/internal-test-app'
|
|
|
|
describe('internal-schedules routes', () => {
|
|
let db: Database
|
|
let scheduleService: ScheduleService
|
|
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 }), listWorktrees: () => [] } as unknown as ScheduleWorktreeManager
|
|
scheduleService = new ScheduleService(db, openCodeClient, stubWorktreeManager)
|
|
app = new Hono()
|
|
app.route('/api/internal', createInternalTestApp(db, { scheduleService }))
|
|
token = getOrCreateInternalToken(db)
|
|
})
|
|
|
|
it('GET /api/internal/schedules/all returns 401 without bearer token', async () => {
|
|
const res = await app.request('/api/internal/schedules/all')
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('GET /api/internal/schedules/all returns 200 with bearer token', async () => {
|
|
const res = await app.request('/api/internal/schedules/all', {
|
|
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)
|
|
})
|
|
|
|
it('GET /api/internal/schedules/all/runs returns 200 with bearer token', async () => {
|
|
const res = await app.request('/api/internal/schedules/all/runs', {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json() as { runs: unknown[] }
|
|
expect(body).toHaveProperty('runs')
|
|
})
|
|
|
|
it('POST /api/internal/repos/:id/schedules/:jobId/run returns 401 without bearer token', async () => {
|
|
const res = await app.request('/api/internal/repos/1/schedules/1/run', {
|
|
method: 'POST',
|
|
})
|
|
expect(res.status).toBe(401)
|
|
})
|
|
})
|