2026-05-25 23:51:37 +08:00
|
|
|
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
|
|
|
|
import { Hono, type Context, type Next } from 'hono'
|
2026-05-19 16:05:09 +08:00
|
|
|
import providerRoute from './provider'
|
|
|
|
|
import { providerService } from '@/services/provider/provider'
|
2026-05-25 23:51:37 +08:00
|
|
|
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
vi.mock('@/api/middleware/auth', () => ({
|
2026-05-25 23:51:37 +08:00
|
|
|
authMiddleware: async (c: Context, next: Next) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
c.set('user', { id: 'user1', name: 'Test User' })
|
|
|
|
|
await next()
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
vi.mock('@/services/authz/authz')
|
|
|
|
|
vi.mock('@/services/provider/provider')
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
describe('provider api', () => {
|
|
|
|
|
const app = new Hono()
|
|
|
|
|
.use('*', async (c, next) => {
|
|
|
|
|
c.set('user', { id: 'user1', name: 'Test User' })
|
|
|
|
|
await next()
|
|
|
|
|
})
|
|
|
|
|
.route('/', providerRoute)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2026-05-25 23:51:37 +08:00
|
|
|
vi.restoreAllMocks()
|
2026-05-19 16:05:09 +08:00
|
|
|
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('GET /teams/:teamId/providers returns providers', async () => {
|
2026-05-25 23:51:37 +08:00
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.listByTeam).mockResolvedValue([
|
|
|
|
|
{ id: 'p1', name: 'openai', config: {} },
|
|
|
|
|
] as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
const res = await app.request('/teams/t1/providers')
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
expect(data).toHaveLength(1)
|
|
|
|
|
expect(data[0].id).toBe('p1')
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: 't1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('GET /providers/:id/models returns models', async () => {
|
|
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
2026-05-19 16:05:09 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2026-05-25 23:51:37 +08:00
|
|
|
vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
|
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.listModelsByProvider).mockResolvedValue([
|
|
|
|
|
{ modelId: 'm1', name: 'model1' },
|
|
|
|
|
] as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/providers/p1/models')
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
expect(data).toHaveLength(1)
|
|
|
|
|
expect(data[0].modelId).toBe('m1')
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(providerService.listModelsByProvider).toHaveBeenCalledWith('t1', 'p1')
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Provider,
|
|
|
|
|
id: 'p1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('POST /teams/:teamId/providers creates provider', async () => {
|
|
|
|
|
const config = {
|
|
|
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
}
|
|
|
|
|
const models = [
|
|
|
|
|
{
|
|
|
|
|
modelId: 'gpt-4',
|
|
|
|
|
name: 'GPT-4',
|
|
|
|
|
config: {
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
reasoning: false,
|
|
|
|
|
input: ['text'],
|
|
|
|
|
contextWindow: 128000,
|
|
|
|
|
maxTokens: 4096,
|
|
|
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
2026-05-25 23:51:37 +08:00
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.create).mockResolvedValue({
|
|
|
|
|
id: 'p1',
|
|
|
|
|
name: 'openai',
|
|
|
|
|
config,
|
|
|
|
|
models,
|
|
|
|
|
} as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
const res = await app.request('/teams/t1/providers', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ name: 'openai', config, models }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
expect(data.name).toBe('openai')
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(providerService.create).toHaveBeenCalledWith('t1', 'openai', config, models)
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: 't1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('PUT /providers/:id updates provider', async () => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const config = {
|
|
|
|
|
baseUrl: 'https://new-url.com',
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
}
|
|
|
|
|
const models = [
|
|
|
|
|
{
|
|
|
|
|
modelId: 'gpt-4',
|
|
|
|
|
name: 'GPT-4',
|
|
|
|
|
config: {
|
|
|
|
|
api: 'openai-completions',
|
|
|
|
|
reasoning: false,
|
|
|
|
|
input: ['text'],
|
|
|
|
|
contextWindow: 128000,
|
|
|
|
|
maxTokens: 4096,
|
|
|
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
2026-05-25 23:51:37 +08:00
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
|
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.update).mockResolvedValue({
|
|
|
|
|
id: 'p1',
|
|
|
|
|
name: 'openai',
|
|
|
|
|
config,
|
|
|
|
|
models,
|
|
|
|
|
} as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/providers/p1', {
|
2026-05-19 16:05:09 +08:00
|
|
|
method: 'PUT',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ config, models }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
expect(data.config.baseUrl).toBe('https://new-url.com')
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(providerService.update).toHaveBeenCalledWith('t1', 'p1', config, models)
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Provider,
|
|
|
|
|
id: 'p1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
it('DELETE /providers/:id deletes provider', async () => {
|
|
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.getById).mockResolvedValue({ id: 'p1', teamId: 't1' } as any)
|
|
|
|
|
// Using any here because mocking complex service return types or Hono context is overly verbose for this test.
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
vi.mocked(providerService.delete).mockResolvedValue({ id: 'p1' } as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const res = await app.request('/providers/p1', {
|
2026-05-19 16:05:09 +08:00
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
expect(data.success).toBe(true)
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(providerService.delete).toHaveBeenCalledWith('t1', 'p1')
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Admin,
|
|
|
|
|
type: ResourceType.Provider,
|
|
|
|
|
id: 'p1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|