|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { queryBounties, type BountyFilters } from '@/backend/services/bounty.service'; |
| 3 | + |
| 4 | +// Mock Prisma |
| 5 | +vi.mock('@/lib/prisma', () => ({ |
| 6 | + prisma: { |
| 7 | + bounty: { |
| 8 | + findMany: vi.fn(), |
| 9 | + }, |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +import { prisma } from '@/lib/prisma'; |
| 14 | +const mockPrisma = vi.mocked(prisma); |
| 15 | + |
| 16 | +describe('Bounty Service - Cursor Pagination', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('Cursor-Based Pagination', () => { |
| 22 | + it('should return first page with hasNextPage true when more items exist', async () => { |
| 23 | + const mockBounties = Array.from({ length: 11 }, (_, i) => ({ |
| 24 | + id: `bounty-${String(i).padStart(2, '0')}`, |
| 25 | + title: `Bounty ${i}`, |
| 26 | + description: `Description ${i}`, |
| 27 | + budget: 1000 + i * 100, |
| 28 | + deadline: new Date('2025-12-31'), |
| 29 | + status: 'OPEN' as const, |
| 30 | + category: 'Design', |
| 31 | + tags: ['ui'], |
| 32 | + createdAt: new Date('2025-06-01'), |
| 33 | + })); |
| 34 | + |
| 35 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 36 | + |
| 37 | + const result = await queryBounties(10, undefined, {}); |
| 38 | + |
| 39 | + expect(result.items).toHaveLength(10); |
| 40 | + expect(result.hasNextPage).toBe(true); |
| 41 | + expect(result.nextCursor).toBe('bounty-09'); |
| 42 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 43 | + take: 11, |
| 44 | + where: {}, |
| 45 | + select: expect.any(Object), |
| 46 | + orderBy: { createdAt: 'desc' }, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return last page with hasNextPage false', async () => { |
| 51 | + const mockBounties = Array.from({ length: 5 }, (_, i) => ({ |
| 52 | + id: `bounty-${i}`, |
| 53 | + title: `Bounty ${i}`, |
| 54 | + description: `Description ${i}`, |
| 55 | + budget: 1000, |
| 56 | + deadline: new Date('2025-12-31'), |
| 57 | + status: 'OPEN' as const, |
| 58 | + category: 'Design', |
| 59 | + tags: [], |
| 60 | + createdAt: new Date('2025-06-01'), |
| 61 | + })); |
| 62 | + |
| 63 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 64 | + |
| 65 | + const result = await queryBounties(10, undefined, {}); |
| 66 | + |
| 67 | + expect(result.items).toHaveLength(5); |
| 68 | + expect(result.hasNextPage).toBe(false); |
| 69 | + expect(result.nextCursor).toBe('bounty-4'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should use cursor for pagination skip', async () => { |
| 73 | + const mockBounties = Array.from({ length: 11 }, (_, i) => ({ |
| 74 | + id: `bounty-${i + 10}`, |
| 75 | + title: `Bounty ${i + 10}`, |
| 76 | + description: `Description ${i}`, |
| 77 | + budget: 1000, |
| 78 | + deadline: new Date('2025-12-31'), |
| 79 | + status: 'OPEN' as const, |
| 80 | + category: 'Design', |
| 81 | + tags: [], |
| 82 | + createdAt: new Date('2025-06-01'), |
| 83 | + })); |
| 84 | + |
| 85 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 86 | + |
| 87 | + const result = await queryBounties(10, 'bounty-09', {}); |
| 88 | + |
| 89 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 90 | + take: 11, |
| 91 | + cursor: { id: 'bounty-09' }, |
| 92 | + skip: 1, |
| 93 | + where: {}, |
| 94 | + select: expect.any(Object), |
| 95 | + orderBy: { createdAt: 'desc' }, |
| 96 | + }); |
| 97 | + expect(result.items).toHaveLength(10); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return empty array with no nextCursor when no results', async () => { |
| 101 | + mockPrisma.bounty.findMany.mockResolvedValue([]); |
| 102 | + |
| 103 | + const result = await queryBounties(10, undefined, {}); |
| 104 | + |
| 105 | + expect(result.items).toHaveLength(0); |
| 106 | + expect(result.hasNextPage).toBe(false); |
| 107 | + expect(result.nextCursor).toBeNull(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('Status Filter', () => { |
| 112 | + it('should filter bounties by status', async () => { |
| 113 | + const mockBounties = Array.from({ length: 3 }, (_, i) => ({ |
| 114 | + id: `bounty-${i}`, |
| 115 | + title: `Completed Bounty ${i}`, |
| 116 | + description: `Description ${i}`, |
| 117 | + budget: 1000, |
| 118 | + deadline: new Date('2025-12-31'), |
| 119 | + status: 'COMPLETED' as const, |
| 120 | + category: 'Design', |
| 121 | + tags: [], |
| 122 | + createdAt: new Date('2025-06-01'), |
| 123 | + })); |
| 124 | + |
| 125 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 126 | + |
| 127 | + const result = await queryBounties(10, undefined, { status: 'COMPLETED' }); |
| 128 | + |
| 129 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 130 | + take: 11, |
| 131 | + where: { status: 'COMPLETED' }, |
| 132 | + select: expect.any(Object), |
| 133 | + orderBy: { createdAt: 'desc' }, |
| 134 | + }); |
| 135 | + expect(result.items).toHaveLength(3); |
| 136 | + expect(result.items.every(b => b.status === 'COMPLETED')).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should support filtering by IN_PROGRESS status', async () => { |
| 140 | + const mockBounties = [ |
| 141 | + { |
| 142 | + id: 'bounty-1', |
| 143 | + title: 'In Progress Bounty', |
| 144 | + description: 'Description', |
| 145 | + budget: 5000, |
| 146 | + deadline: new Date('2025-12-31'), |
| 147 | + status: 'IN_PROGRESS' as const, |
| 148 | + category: 'Development', |
| 149 | + tags: ['backend'], |
| 150 | + createdAt: new Date('2025-06-01'), |
| 151 | + }, |
| 152 | + ]; |
| 153 | + |
| 154 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 155 | + |
| 156 | + const result = await queryBounties(10, undefined, { status: 'IN_PROGRESS' }); |
| 157 | + |
| 158 | + expect(result.items[0].status).toBe('IN_PROGRESS'); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('Budget Filter', () => { |
| 163 | + it('should filter by minimum budget', async () => { |
| 164 | + const mockBounties = Array.from({ length: 5 }, (_, i) => ({ |
| 165 | + id: `bounty-${i}`, |
| 166 | + title: `Bounty ${i}`, |
| 167 | + description: `Description ${i}`, |
| 168 | + budget: 5000 + i * 1000, |
| 169 | + deadline: new Date('2025-12-31'), |
| 170 | + status: 'OPEN' as const, |
| 171 | + category: 'Design', |
| 172 | + tags: [], |
| 173 | + createdAt: new Date('2025-06-01'), |
| 174 | + })); |
| 175 | + |
| 176 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 177 | + |
| 178 | + const result = await queryBounties(10, undefined, { |
| 179 | + budget: { min: 5000 }, |
| 180 | + }); |
| 181 | + |
| 182 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 183 | + take: 11, |
| 184 | + where: { budget: { gte: 5000 } }, |
| 185 | + select: expect.any(Object), |
| 186 | + orderBy: { createdAt: 'desc' }, |
| 187 | + }); |
| 188 | + expect(result.items.every(b => b.budget >= 5000)).toBe(true); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should filter by maximum budget', async () => { |
| 192 | + const mockBounties = Array.from({ length: 3 }, (_, i) => ({ |
| 193 | + id: `bounty-${i}`, |
| 194 | + title: `Bounty ${i}`, |
| 195 | + description: `Description ${i}`, |
| 196 | + budget: 1000 + i * 500, |
| 197 | + deadline: new Date('2025-12-31'), |
| 198 | + status: 'OPEN' as const, |
| 199 | + category: 'Design', |
| 200 | + tags: [], |
| 201 | + createdAt: new Date('2025-06-01'), |
| 202 | + })); |
| 203 | + |
| 204 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 205 | + |
| 206 | + const result = await queryBounties(10, undefined, { |
| 207 | + budget: { max: 3000 }, |
| 208 | + }); |
| 209 | + |
| 210 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 211 | + take: 11, |
| 212 | + where: { budget: { lte: 3000 } }, |
| 213 | + select: expect.any(Object), |
| 214 | + orderBy: { createdAt: 'desc' }, |
| 215 | + }); |
| 216 | + expect(result.items.every(b => b.budget <= 3000)).toBe(true); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should filter by budget range (min and max)', async () => { |
| 220 | + const mockBounties = Array.from({ length: 4 }, (_, i) => ({ |
| 221 | + id: `bounty-${i}`, |
| 222 | + title: `Bounty ${i}`, |
| 223 | + description: `Description ${i}`, |
| 224 | + budget: 2000 + i * 1000, |
| 225 | + deadline: new Date('2025-12-31'), |
| 226 | + status: 'OPEN' as const, |
| 227 | + category: 'Design', |
| 228 | + tags: [], |
| 229 | + createdAt: new Date('2025-06-01'), |
| 230 | + })); |
| 231 | + |
| 232 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 233 | + |
| 234 | + const result = await queryBounties(10, undefined, { |
| 235 | + budget: { min: 2000, max: 5000 }, |
| 236 | + }); |
| 237 | + |
| 238 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 239 | + take: 11, |
| 240 | + where: { |
| 241 | + budget: { |
| 242 | + gte: 2000, |
| 243 | + lte: 5000, |
| 244 | + }, |
| 245 | + }, |
| 246 | + select: expect.any(Object), |
| 247 | + orderBy: { createdAt: 'desc' }, |
| 248 | + }); |
| 249 | + expect(result.items.every(b => b.budget >= 2000 && b.budget <= 5000)).toBe(true); |
| 250 | + }); |
| 251 | + }); |
| 252 | + |
| 253 | + describe('Combined Filters', () => { |
| 254 | + it('should apply both status and budget filters', async () => { |
| 255 | + const mockBounties = [ |
| 256 | + { |
| 257 | + id: 'bounty-1', |
| 258 | + title: 'Open High Budget Bounty', |
| 259 | + description: 'Description', |
| 260 | + budget: 10000, |
| 261 | + deadline: new Date('2025-12-31'), |
| 262 | + status: 'OPEN' as const, |
| 263 | + category: 'Design', |
| 264 | + tags: [], |
| 265 | + createdAt: new Date('2025-06-01'), |
| 266 | + }, |
| 267 | + ]; |
| 268 | + |
| 269 | + mockPrisma.bounty.findMany.mockResolvedValue(mockBounties); |
| 270 | + |
| 271 | + const result = await queryBounties(10, undefined, { |
| 272 | + status: 'OPEN', |
| 273 | + budget: { min: 5000 }, |
| 274 | + }); |
| 275 | + |
| 276 | + expect(mockPrisma.bounty.findMany).toHaveBeenCalledWith({ |
| 277 | + take: 11, |
| 278 | + where: { |
| 279 | + status: 'OPEN', |
| 280 | + budget: { gte: 5000 }, |
| 281 | + }, |
| 282 | + select: expect.any(Object), |
| 283 | + orderBy: { createdAt: 'desc' }, |
| 284 | + }); |
| 285 | + expect(result.items[0].status).toBe('OPEN'); |
| 286 | + expect(result.items[0].budget).toBeGreaterThanOrEqual(5000); |
| 287 | + }); |
| 288 | + }); |
| 289 | +}); |
0 commit comments