|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Simple test for memoizePromise helper function |
| 4 | +describe('memoizePromise helper', () => { |
| 5 | + it('should cache promise results correctly', async () => { |
| 6 | + // Create a simple memoized function for testing |
| 7 | + const memoizePromise = <T extends any[], R>( |
| 8 | + fn: (...args: T) => Promise<R> |
| 9 | + ): ((...args: T) => Promise<R>) => { |
| 10 | + const cache = new Map<string, Promise<R>>() |
| 11 | + |
| 12 | + return async (...args: T): Promise<R> => { |
| 13 | + const key = JSON.stringify(args) |
| 14 | + |
| 15 | + if (cache.has(key)) { |
| 16 | + return cache.get(key)! |
| 17 | + } |
| 18 | + |
| 19 | + const promise = fn(...args) |
| 20 | + cache.set(key, promise) |
| 21 | + return promise |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Test function that returns different values |
| 26 | + let callCount = 0 |
| 27 | + const testFunction = async (arg: string): Promise<string> => { |
| 28 | + callCount++ |
| 29 | + return `result-${arg}-${callCount}` |
| 30 | + } |
| 31 | + |
| 32 | + const memoizedFunction = memoizePromise(testFunction) |
| 33 | + |
| 34 | + // First call |
| 35 | + const result1 = await memoizedFunction('test') |
| 36 | + expect(result1).toBe('result-test-1') |
| 37 | + expect(callCount).toBe(1) |
| 38 | + |
| 39 | + // Second call with same argument should return cached result |
| 40 | + const result2 = await memoizedFunction('test') |
| 41 | + expect(result2).toBe('result-test-1') // Same as first call |
| 42 | + expect(callCount).toBe(1) // Function not called again |
| 43 | + |
| 44 | + // Different argument should call function again |
| 45 | + const result3 = await memoizedFunction('different') |
| 46 | + expect(result3).toBe('result-different-2') |
| 47 | + expect(callCount).toBe(2) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should handle different argument combinations', async () => { |
| 51 | + const memoizePromise = <T extends any[], R>( |
| 52 | + fn: (...args: T) => Promise<R> |
| 53 | + ): ((...args: T) => Promise<R>) => { |
| 54 | + const cache = new Map<string, Promise<R>>() |
| 55 | + |
| 56 | + return async (...args: T): Promise<R> => { |
| 57 | + const key = JSON.stringify(args) |
| 58 | + |
| 59 | + if (cache.has(key)) { |
| 60 | + return cache.get(key)! |
| 61 | + } |
| 62 | + |
| 63 | + const promise = fn(...args) |
| 64 | + cache.set(key, promise) |
| 65 | + return promise |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + let callCount = 0 |
| 70 | + const testFunction = async (arg1: string, arg2: number): Promise<string> => { |
| 71 | + callCount++ |
| 72 | + return `${arg1}-${arg2}-${callCount}` |
| 73 | + } |
| 74 | + |
| 75 | + const memoizedFunction = memoizePromise(testFunction) |
| 76 | + |
| 77 | + // Different argument combinations should not use cache |
| 78 | + const result1 = await memoizedFunction('a', 1) |
| 79 | + const result2 = await memoizedFunction('b', 2) |
| 80 | + const result3 = await memoizedFunction('a', 1) // Same as first call |
| 81 | + |
| 82 | + expect(result1).toBe('a-1-1') |
| 83 | + expect(result2).toBe('b-2-2') |
| 84 | + expect(result3).toBe('a-1-1') // Cached result |
| 85 | + expect(callCount).toBe(2) // Only called twice |
| 86 | + }) |
| 87 | + |
| 88 | + it('should generate keys for objects and primitives', async () => { |
| 89 | + const memoizePromise = <T extends any[], R>( |
| 90 | + fn: (...args: T) => Promise<R> |
| 91 | + ): ((...args: T) => Promise<R>) => { |
| 92 | + const cache = new Map<string, Promise<R>>() |
| 93 | + |
| 94 | + return async (...args: T): Promise<R> => { |
| 95 | + const key = JSON.stringify(args) |
| 96 | + |
| 97 | + if (cache.has(key)) { |
| 98 | + return cache.get(key)! |
| 99 | + } |
| 100 | + |
| 101 | + const promise = fn(...args) |
| 102 | + cache.set(key, promise) |
| 103 | + return promise |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + let callCount = 0 |
| 108 | + const testFunction = async (obj: { name: string }, num: number): Promise<string> => { |
| 109 | + callCount++ |
| 110 | + return `${obj.name}-${num}-${callCount}` |
| 111 | + } |
| 112 | + |
| 113 | + const memoizedFunction = memoizePromise(testFunction) |
| 114 | + |
| 115 | + const obj1 = { name: 'test' } |
| 116 | + const obj2 = { name: 'test' } |
| 117 | + |
| 118 | + // Same object content should use cache |
| 119 | + const result1 = await memoizedFunction(obj1, 1) |
| 120 | + const result2 = await memoizedFunction(obj2, 1) |
| 121 | + |
| 122 | + expect(result1).toBe('test-1-1') |
| 123 | + expect(result2).toBe('test-1-1') // Cached result |
| 124 | + expect(callCount).toBe(1) // Only called once |
| 125 | + }) |
| 126 | + |
| 127 | + it('should handle promise rejections correctly', async () => { |
| 128 | + const memoizePromise = <T extends any[], R>( |
| 129 | + fn: (...args: T) => Promise<R> |
| 130 | + ): ((...args: T) => Promise<R>) => { |
| 131 | + const cache = new Map<string, Promise<R>>() |
| 132 | + |
| 133 | + return async (...args: T): Promise<R> => { |
| 134 | + const key = JSON.stringify(args) |
| 135 | + |
| 136 | + if (cache.has(key)) { |
| 137 | + return cache.get(key)! |
| 138 | + } |
| 139 | + |
| 140 | + const promise = fn(...args) |
| 141 | + cache.set(key, promise) |
| 142 | + return promise |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + let callCount = 0 |
| 147 | + const testFunction = async (shouldFail: boolean): Promise<string> => { |
| 148 | + callCount++ |
| 149 | + if (shouldFail) { |
| 150 | + throw new Error('Test error') |
| 151 | + } |
| 152 | + return `success-${callCount}` |
| 153 | + } |
| 154 | + |
| 155 | + const memoizedFunction = memoizePromise(testFunction) |
| 156 | + |
| 157 | + // First call should fail |
| 158 | + await expect(memoizedFunction(true)).rejects.toThrow('Test error') |
| 159 | + expect(callCount).toBe(1) |
| 160 | + |
| 161 | + // Second call with same argument should fail again (cached error) |
| 162 | + await expect(memoizedFunction(true)).rejects.toThrow('Test error') |
| 163 | + expect(callCount).toBe(1) // Still 1 because error was cached |
| 164 | + |
| 165 | + // Different argument should work |
| 166 | + const result = await memoizedFunction(false) |
| 167 | + expect(result).toBe('success-2') |
| 168 | + expect(callCount).toBe(2) // Only called twice (once for true, once for false) |
| 169 | + }) |
| 170 | +}) |
0 commit comments