|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { splitIntoBatches, processBatches, DEFAULT_BATCH_SIZE } from './batchProcessor' |
| 3 | + |
| 4 | +describe('batchProcessor', () => { |
| 5 | + describe('splitIntoBatches', () => { |
| 6 | + it('returns empty array when given empty array', () => { |
| 7 | + const result = splitIntoBatches([]) |
| 8 | + expect(result).toEqual([]) |
| 9 | + }) |
| 10 | + |
| 11 | + it('returns single batch when items length is less than batch size', () => { |
| 12 | + const items = Array.from({ length: 50 }, (_, i) => i) |
| 13 | + const result = splitIntoBatches(items) |
| 14 | + |
| 15 | + expect(result).toHaveLength(1) |
| 16 | + expect(result[0]).toHaveLength(50) |
| 17 | + }) |
| 18 | + |
| 19 | + it('returns single batch when items length equals batch size', () => { |
| 20 | + const items = Array.from({ length: 100 }, (_, i) => i) |
| 21 | + const result = splitIntoBatches(items) |
| 22 | + |
| 23 | + expect(result).toHaveLength(1) |
| 24 | + expect(result[0]).toHaveLength(100) |
| 25 | + }) |
| 26 | + |
| 27 | + it('splits items into multiple batches when length exceeds batch size', () => { |
| 28 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 29 | + const result = splitIntoBatches(items) |
| 30 | + |
| 31 | + expect(result).toHaveLength(2) |
| 32 | + expect(result[0]).toHaveLength(100) |
| 33 | + expect(result[1]).toHaveLength(50) |
| 34 | + }) |
| 35 | + |
| 36 | + it('splits items into exact batches when length is multiple of batch size', () => { |
| 37 | + const items = Array.from({ length: 200 }, (_, i) => i) |
| 38 | + const result = splitIntoBatches(items) |
| 39 | + |
| 40 | + expect(result).toHaveLength(2) |
| 41 | + expect(result[0]).toHaveLength(100) |
| 42 | + expect(result[1]).toHaveLength(100) |
| 43 | + }) |
| 44 | + |
| 45 | + it('handles custom batch sizes', () => { |
| 46 | + const items = Array.from({ length: 75 }, (_, i) => i) |
| 47 | + const result = splitIntoBatches(items, 25) |
| 48 | + |
| 49 | + expect(result).toHaveLength(3) |
| 50 | + expect(result[0]).toHaveLength(25) |
| 51 | + expect(result[1]).toHaveLength(25) |
| 52 | + expect(result[2]).toHaveLength(25) |
| 53 | + }) |
| 54 | + |
| 55 | + it('throws error when batch size is zero', () => { |
| 56 | + expect(() => splitIntoBatches([1, 2, 3], 0)).toThrow('Batch size must be greater than 0') |
| 57 | + }) |
| 58 | + |
| 59 | + it('throws error when batch size is negative', () => { |
| 60 | + expect(() => splitIntoBatches([1, 2, 3], -1)).toThrow('Batch size must be greater than 0') |
| 61 | + }) |
| 62 | + |
| 63 | + it('preserves item order in batches', () => { |
| 64 | + const items = [1, 2, 3, 4, 5] |
| 65 | + const result = splitIntoBatches(items, 2) |
| 66 | + |
| 67 | + expect(result).toEqual([[1, 2], [3, 4], [5]]) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('processBatches', () => { |
| 72 | + it('processes empty array', async () => { |
| 73 | + const processFn = vi.fn().mockResolvedValue('result') |
| 74 | + const result = await processBatches([], processFn) |
| 75 | + |
| 76 | + expect(result).toEqual([]) |
| 77 | + expect(processFn).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + it('processes single batch when items length is less than batch size', async () => { |
| 81 | + const items = Array.from({ length: 50 }, (_, i) => i) |
| 82 | + const processFn = vi.fn().mockResolvedValue('result') |
| 83 | + |
| 84 | + await processBatches(items, processFn) |
| 85 | + |
| 86 | + expect(processFn).toHaveBeenCalledTimes(1) |
| 87 | + expect(processFn).toHaveBeenCalledWith(items) |
| 88 | + }) |
| 89 | + |
| 90 | + it('processes single batch when items length equals batch size', async () => { |
| 91 | + const items = Array.from({ length: 100 }, (_, i) => i) |
| 92 | + const processFn = vi.fn().mockResolvedValue('result') |
| 93 | + |
| 94 | + await processBatches(items, processFn) |
| 95 | + |
| 96 | + expect(processFn).toHaveBeenCalledTimes(1) |
| 97 | + expect(processFn).toHaveBeenCalledWith(items) |
| 98 | + }) |
| 99 | + |
| 100 | + it('processes multiple batches when items length exceeds batch size', async () => { |
| 101 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 102 | + const processFn = vi.fn().mockResolvedValue('result') |
| 103 | + |
| 104 | + await processBatches(items, processFn) |
| 105 | + |
| 106 | + expect(processFn).toHaveBeenCalledTimes(2) |
| 107 | + expect(processFn).toHaveBeenNthCalledWith(1, items.slice(0, 100)) |
| 108 | + expect(processFn).toHaveBeenNthCalledWith(2, items.slice(100, 150)) |
| 109 | + }) |
| 110 | + |
| 111 | + it('returns results from all batch processing calls', async () => { |
| 112 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 113 | + const processFn = vi |
| 114 | + .fn() |
| 115 | + .mockResolvedValueOnce({ batch: 1 }) |
| 116 | + .mockResolvedValueOnce({ batch: 2 }) |
| 117 | + |
| 118 | + const result = await processBatches(items, processFn) |
| 119 | + |
| 120 | + expect(result).toEqual([{ batch: 1 }, { batch: 2 }]) |
| 121 | + }) |
| 122 | + |
| 123 | + it('processes batches sequentially', async () => { |
| 124 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 125 | + const callOrder: number[] = [] |
| 126 | + |
| 127 | + const processFn = vi.fn().mockImplementation(async (batch: number[]) => { |
| 128 | + const firstItem = batch[0] |
| 129 | + if (firstItem !== undefined) { |
| 130 | + callOrder.push(firstItem) |
| 131 | + } |
| 132 | + return `batch-${firstItem}` |
| 133 | + }) |
| 134 | + |
| 135 | + await processBatches(items, processFn) |
| 136 | + |
| 137 | + expect(callOrder).toEqual([0, 100]) |
| 138 | + }) |
| 139 | + |
| 140 | + it('uses custom batch size', async () => { |
| 141 | + const items = Array.from({ length: 75 }, (_, i) => i) |
| 142 | + const processFn = vi.fn().mockResolvedValue('result') |
| 143 | + |
| 144 | + await processBatches(items, processFn, 25) |
| 145 | + |
| 146 | + expect(processFn).toHaveBeenCalledTimes(3) |
| 147 | + }) |
| 148 | + |
| 149 | + it('propagates errors from processing function', async () => { |
| 150 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 151 | + const processFn = vi.fn().mockRejectedValue(new Error('Processing failed')) |
| 152 | + |
| 153 | + await expect(processBatches(items, processFn)).rejects.toThrow('Processing failed') |
| 154 | + }) |
| 155 | + |
| 156 | + it('uses DEFAULT_BATCH_SIZE when batch size is not specified', async () => { |
| 157 | + const items = Array.from({ length: 150 }, (_, i) => i) |
| 158 | + const processFn = vi.fn().mockResolvedValue('result') |
| 159 | + |
| 160 | + await processBatches(items, processFn) |
| 161 | + |
| 162 | + expect(processFn).toHaveBeenCalledTimes(2) |
| 163 | + const firstBatch = items.slice(0, DEFAULT_BATCH_SIZE) |
| 164 | + expect(processFn).toHaveBeenNthCalledWith(1, expect.arrayContaining(firstBatch)) |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments