|
| 1 | +import express from 'express'; |
| 2 | +import request from 'supertest'; |
| 3 | + |
| 4 | +// Mock the requireAuth middleware to pass a developerId |
| 5 | +jest.mock('../middleware/requireAuth.js', () => ({ |
| 6 | + requireAuth: (req: express.Request, _res: express.Response, next: express.NextFunction) => { |
| 7 | + (req as any).developerId = 'dev-user-123'; |
| 8 | + next(); |
| 9 | + } |
| 10 | +})); |
| 11 | + |
| 12 | +import { createInvoicesRouter } from './invoices.js'; |
| 13 | +import { errorHandler } from '../middleware/errorHandler.js'; |
| 14 | +import prisma from '../lib/prisma.js'; |
| 15 | + |
| 16 | +// Mock prisma.invoice.findMany |
| 17 | +jest.mock('../lib/prisma.js', () => ({ |
| 18 | + __esModule: true, |
| 19 | + default: { |
| 20 | + invoice: { |
| 21 | + findMany: jest.fn(), |
| 22 | + } |
| 23 | + } |
| 24 | +})); |
| 25 | + |
| 26 | +describe('GET /api/invoices cursor pagination', () => { |
| 27 | + let app: express.Express; |
| 28 | + let findManyMock: jest.Mock; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + findManyMock = prisma.invoice.findMany as jest.Mock; |
| 32 | + findManyMock.mockReset(); |
| 33 | + |
| 34 | + app = express(); |
| 35 | + app.use(express.json()); |
| 36 | + app.use('/api/invoices', createInvoicesRouter()); |
| 37 | + app.use(errorHandler); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('returns paginated data without cursor', async () => { |
| 45 | + const mockInvoices = [ |
| 46 | + { id: 'uuid-1', created_at: new Date('2026-07-28T10:00:00Z') }, |
| 47 | + { id: 'uuid-2', created_at: new Date('2026-07-28T09:00:00Z') }, |
| 48 | + ]; |
| 49 | + findManyMock.mockResolvedValue(mockInvoices); |
| 50 | + |
| 51 | + const res = await request(app).get('/api/invoices?limit=2'); |
| 52 | + |
| 53 | + expect(res.status).toBe(200); |
| 54 | + expect(res.body.data).toHaveLength(2); |
| 55 | + expect(res.body.meta.hasMore).toBe(false); |
| 56 | + expect(res.body.meta.nextCursor).toBeNull(); |
| 57 | + |
| 58 | + expect(findManyMock).toHaveBeenCalledWith(expect.objectContaining({ |
| 59 | + take: 3, // limit + 1 |
| 60 | + where: { user_id: 'dev-user-123' }, |
| 61 | + orderBy: [{ created_at: 'desc' }, { id: 'desc' }], |
| 62 | + cursor: undefined, |
| 63 | + })); |
| 64 | + }); |
| 65 | + |
| 66 | + it('generates nextCursor when hasMore is true', async () => { |
| 67 | + const mockInvoices = [ |
| 68 | + { id: 'uuid-1', created_at: new Date('2026-07-28T10:00:00Z') }, |
| 69 | + { id: 'uuid-2', created_at: new Date('2026-07-28T09:00:00Z') }, |
| 70 | + { id: 'uuid-3', created_at: new Date('2026-07-28T08:00:00Z') }, |
| 71 | + ]; |
| 72 | + findManyMock.mockResolvedValue(mockInvoices); |
| 73 | + |
| 74 | + const res = await request(app).get('/api/invoices?limit=2'); |
| 75 | + |
| 76 | + expect(res.status).toBe(200); |
| 77 | + expect(res.body.data).toHaveLength(2); |
| 78 | + expect(res.body.meta.hasMore).toBe(true); |
| 79 | + |
| 80 | + // decoded cursor should contain the last item of the current page (uuid-2) |
| 81 | + const decodedCursor = JSON.parse(Buffer.from(res.body.meta.nextCursor, 'base64').toString('utf-8')); |
| 82 | + expect(decodedCursor.id).toBe('uuid-2'); |
| 83 | + expect(decodedCursor.created_at).toBe('2026-07-28T09:00:00.000Z'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('queries using cursor when provided', async () => { |
| 87 | + const cursorData = { |
| 88 | + id: 'uuid-2', |
| 89 | + created_at: '2026-07-28T09:00:00.000Z' |
| 90 | + }; |
| 91 | + const cursorBase64 = Buffer.from(JSON.stringify(cursorData)).toString('base64'); |
| 92 | + findManyMock.mockResolvedValue([]); |
| 93 | + |
| 94 | + const res = await request(app).get(`/api/invoices?limit=10&cursor=${cursorBase64}`); |
| 95 | + |
| 96 | + expect(res.status).toBe(200); |
| 97 | + expect(findManyMock).toHaveBeenCalledWith(expect.objectContaining({ |
| 98 | + cursor: { id: 'uuid-2' } // Should pass only id to prisma cursor |
| 99 | + })); |
| 100 | + }); |
| 101 | + |
| 102 | + it('rejects invalid cursor format (not base64 json)', async () => { |
| 103 | + const res = await request(app).get(`/api/invoices?limit=10&cursor=invalid_base64`); |
| 104 | + expect(res.status).toBe(400); |
| 105 | + expect(res.body.message).toContain('Invalid cursor format'); |
| 106 | + expect(findManyMock).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('rejects valid base64 but invalid schema payload', async () => { |
| 110 | + const cursorData = { id: 'not-a-uuid' }; // missing created_at and invalid uuid |
| 111 | + const cursorBase64 = Buffer.from(JSON.stringify(cursorData)).toString('base64'); |
| 112 | + |
| 113 | + const res = await request(app).get(`/api/invoices?cursor=${cursorBase64}`); |
| 114 | + expect(res.status).toBe(400); |
| 115 | + expect(res.body.message).toContain('Invalid cursor format'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('rejects invalid limit parameter', async () => { |
| 119 | + const res = await request(app).get('/api/invoices?limit=500'); |
| 120 | + expect(res.status).toBe(400); |
| 121 | + expect(res.body.message).toContain('limit must be between 1 and 100'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles database errors gracefully', async () => { |
| 125 | + findManyMock.mockRejectedValue(new Error('DB connection failed')); |
| 126 | + const res = await request(app).get('/api/invoices'); |
| 127 | + expect(res.status).toBe(500); // Standard error handler should catch it |
| 128 | + }); |
| 129 | +}); |
0 commit comments