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 notificationRoute from './notification'
|
|
|
|
|
import { notificationService } from '@/services/notification/notification'
|
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/notification/notification')
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
describe('notification api', () => {
|
2026-05-25 23:51:37 +08:00
|
|
|
const app = new Hono()
|
|
|
|
|
.use('*', async (c, next) => {
|
|
|
|
|
c.set('user', { id: 'user1', name: 'Test User' })
|
|
|
|
|
await next()
|
|
|
|
|
})
|
|
|
|
|
.route('/', notificationRoute)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.restoreAllMocks()
|
2026-05-25 23:51:37 +08:00
|
|
|
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('GET /teams/:teamId/notifications', async () => {
|
2026-05-25 23:51:37 +08:00
|
|
|
vi.mocked(notificationService.list).mockResolvedValue({
|
|
|
|
|
data: [{ id: 'n1', type: 'comment_created' }] as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
2026-05-19 16:05:09 +08:00
|
|
|
pageInfo: { total: 1, cursor: 'abc' },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await app.request('/teams/t1/notifications?unreadOnly=true&pageSize=10')
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
expect(json.data).toHaveLength(1)
|
|
|
|
|
expect(json.pageInfo.total).toBe(1)
|
|
|
|
|
expect(json.pageInfo.cursor).toBe('abc')
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(notificationService.list).toHaveBeenCalledWith('t1', 'user1', {
|
2026-05-19 16:05:09 +08:00
|
|
|
unreadOnly: true,
|
|
|
|
|
after: undefined,
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
})
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Read,
|
|
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: 't1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('POST /teams/:teamId/notifications/read', 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(notificationService.markRead).mockResolvedValue(undefined as any)
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
const res = await app.request('/teams/t1/notifications/read', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ notificationId: 'n1' }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(res.status).toBe(204)
|
2026-05-25 23:51:37 +08:00
|
|
|
expect(notificationService.markRead).toHaveBeenCalledWith('t1', 'user1', 'n1')
|
|
|
|
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
|
|
|
|
user: expect.anything(),
|
|
|
|
|
permission: Permission.Read,
|
|
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: 't1',
|
|
|
|
|
})
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
})
|