freshyo/apps/backend/tests/unit/admin-notif.test.ts
2026-10-29 23:04:42 +05:30

129 lines
3.9 KiB
TypeScript

/**
* Unit tests for `notifyRegionAdmins`.
*
* The db helper and the queue are mocked, so no device is queried and nothing
* is sent. These cover region scoping, the fan-out shape (one queue message per
* five tokens) and the ids forwarded for the tap handler.
*/
jest.mock('@/src/dbService', () => ({
getStaffNotifTokensForRegion: jest.fn(),
getAllStaffNotifTokens: jest.fn(),
}))
jest.mock('@/src/lib/queue-data-pusher', () => ({
queueDataPusher: { pushNotifQueue: jest.fn(async () => true) },
}))
import { getAllStaffNotifTokens, getStaffNotifTokensForRegion } from '@/src/dbService'
import { queueDataPusher } from '@/src/lib/queue-data-pusher'
import { notifyAllAdmins, notifyRegionAdmins } from '@/src/lib/admin-notif'
const mockedTokens = getStaffNotifTokensForRegion as jest.MockedFunction<
typeof getStaffNotifTokensForRegion
>
const mockedAllTokens = getAllStaffNotifTokens as jest.MockedFunction<
typeof getAllStaffNotifTokens
>
const mockedPush = queueDataPusher.pushNotifQueue as jest.MockedFunction<
typeof queueDataPusher.pushNotifQueue
>
function tokens(count: number) {
return Array.from({ length: count }, (_, index) => ({
staffUserId: index + 1,
token: `tok-${index + 1}`,
}))
}
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(console, 'warn').mockImplementation(() => undefined)
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('notifyRegionAdmins', () => {
it('refuses to broadcast when the event has no region', async () => {
await expect(notifyRegionAdmins(null, { title: 't', body: 'b' })).resolves.toBe(0)
expect(mockedTokens).not.toHaveBeenCalled()
expect(mockedPush).not.toHaveBeenCalled()
})
it('only reads the devices of the given region', async () => {
mockedTokens.mockResolvedValue(tokens(1))
await notifyRegionAdmins(42, { title: 't', body: 'b', orderId: 1 })
expect(mockedTokens).toHaveBeenCalledWith(42)
})
it('chunks tokens five per message and forwards order ids', async () => {
mockedTokens.mockResolvedValue(tokens(7))
await expect(
notifyRegionAdmins(42, { title: 'Order Cancelled', body: '#12', orderId: 12 })
).resolves.toBe(7)
expect(mockedPush).toHaveBeenCalledTimes(2)
expect(mockedPush).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
name: 'send-admin-notification',
jobData: expect.objectContaining({
tokens: ['tok-1', 'tok-2', 'tok-3', 'tok-4', 'tok-5'],
title: 'Order Cancelled',
body: '#12',
orderId: 12,
complaintId: null,
}),
})
)
expect(mockedPush).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
jobData: expect.objectContaining({ tokens: ['tok-6', 'tok-7'] }),
})
)
})
it('forwards complaint ids and defaults the order id to null', async () => {
mockedTokens.mockResolvedValue(tokens(1))
await notifyRegionAdmins(42, {
title: 'New Complaint',
body: 'Missing item',
complaintId: 9,
})
expect(mockedPush).toHaveBeenCalledWith(
expect.objectContaining({
jobData: expect.objectContaining({ orderId: null, complaintId: 9 }),
})
)
})
it('keeps queueing after one chunk fails', async () => {
mockedTokens.mockResolvedValue(tokens(6))
mockedPush.mockRejectedValueOnce(new Error('queue down'))
jest.spyOn(console, 'error').mockImplementation(() => undefined)
await expect(
notifyRegionAdmins(42, { title: 't', body: 'b', orderId: 1 })
).resolves.toBe(1)
expect(mockedPush).toHaveBeenCalledTimes(2)
})
})
describe('notifyAllAdmins', () => {
it('broadcasts to every device for manual notifications', async () => {
mockedAllTokens.mockResolvedValue(tokens(2))
await expect(notifyAllAdmins({ title: 't', body: 'b' })).resolves.toBe(2)
expect(mockedAllTokens).toHaveBeenCalledTimes(1)
expect(mockedTokens).not.toHaveBeenCalled()
expect(mockedPush).toHaveBeenCalledTimes(1)
})
})