|
| 1 | +import { logger } from '../../common/logging' |
| 2 | + |
| 3 | +jest.mock('../../common/fetch', () => ({ |
| 4 | + request: { post: jest.fn() }, |
| 5 | + isFetchError: (e: any) => !!e?.__isFetchError, |
| 6 | +})) |
| 7 | + |
| 8 | +import { request as mockedRequest } from '../../common/fetch' |
| 9 | +import { upload } from '../upload' |
| 10 | + |
| 11 | +const post = (mockedRequest as any).post as jest.Mock |
| 12 | + |
| 13 | +function makeFetchError(status: number, retryAfter?: string) { |
| 14 | + const headerMap = new Map<string, string>() |
| 15 | + if (retryAfter) headerMap.set('Retry-After', retryAfter) |
| 16 | + return { |
| 17 | + __isFetchError: true, |
| 18 | + response: { |
| 19 | + status, |
| 20 | + headers: { get: (k: string) => headerMap.get(k) ?? null }, |
| 21 | + }, |
| 22 | + data: { message: 'err' }, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function makeOkResponse() { |
| 27 | + return { |
| 28 | + data: { |
| 29 | + meta: { |
| 30 | + success: true, |
| 31 | + published_count: 0, |
| 32 | + failed_count: 0, |
| 33 | + published_nodes: [], |
| 34 | + failed_nodes: [], |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function makeDocs(n: number) { |
| 41 | + return Array.from({ length: n }, (_, i) => ({ |
| 42 | + figmaNode: `https://www.figma.com/design/abc123/Test?node-id=${i + 1}-0`, |
| 43 | + label: `L${i + 1}`, |
| 44 | + component: `C${i + 1}`, |
| 45 | + })) as any[] |
| 46 | +} |
| 47 | + |
| 48 | +describe('upload — retry behavior (via postWithRetry)', () => { |
| 49 | + let recordedDelaysMs: number[] |
| 50 | + let originalSetTimeout: typeof setTimeout |
| 51 | + let exitSpy: jest.SpyInstance |
| 52 | + let stderrSpy: jest.SpyInstance |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + post.mockReset() |
| 56 | + recordedDelaysMs = [] |
| 57 | + originalSetTimeout = global.setTimeout |
| 58 | + |
| 59 | + global.setTimeout = ((fn: any, ms: number) => { |
| 60 | + recordedDelaysMs.push(ms) |
| 61 | + fn() |
| 62 | + return 0 as any |
| 63 | + }) as any |
| 64 | + |
| 65 | + exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { |
| 66 | + throw new Error(`process.exit(${code})`) |
| 67 | + }) as any) |
| 68 | + |
| 69 | + stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true) |
| 70 | + |
| 71 | + logger.warn = jest.fn() |
| 72 | + logger.info = jest.fn() |
| 73 | + logger.debug = jest.fn() |
| 74 | + logger.error = jest.fn() |
| 75 | + }) |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + global.setTimeout = originalSetTimeout |
| 79 | + exitSpy.mockRestore() |
| 80 | + stderrSpy.mockRestore() |
| 81 | + jest.restoreAllMocks() |
| 82 | + }) |
| 83 | + |
| 84 | + describe('on 429 (rate limit)', () => { |
| 85 | + it('uses the extended schedule 5/15/30/45/60/75s and ignores Retry-After', async () => { |
| 86 | + // Even with a Retry-After header, the 429 path should use the local schedule. |
| 87 | + post |
| 88 | + .mockRejectedValueOnce(makeFetchError(429, '999')) |
| 89 | + .mockRejectedValueOnce(makeFetchError(429)) |
| 90 | + .mockResolvedValueOnce(makeOkResponse()) |
| 91 | + |
| 92 | + await upload({ |
| 93 | + accessToken: 'tok', |
| 94 | + docs: makeDocs(1), |
| 95 | + verbose: false, |
| 96 | + }) |
| 97 | + |
| 98 | + expect(post).toHaveBeenCalledTimes(3) |
| 99 | + expect(recordedDelaysMs).toEqual([5_000, 15_000]) |
| 100 | + }) |
| 101 | + |
| 102 | + it('gives up after 6 retries (7 attempts total) and exits', async () => { |
| 103 | + for (let i = 0; i < 7; i++) post.mockRejectedValueOnce(makeFetchError(429)) |
| 104 | + |
| 105 | + await expect( |
| 106 | + upload({ |
| 107 | + accessToken: 'tok', |
| 108 | + docs: makeDocs(1), |
| 109 | + verbose: false, |
| 110 | + }), |
| 111 | + ).rejects.toThrow('process.exit(1)') |
| 112 | + |
| 113 | + expect(post).toHaveBeenCalledTimes(7) |
| 114 | + expect(recordedDelaysMs).toEqual([5_000, 15_000, 30_000, 45_000, 60_000, 75_000]) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('on 5xx (server error)', () => { |
| 119 | + it('uses the short schedule 5/15/30s', async () => { |
| 120 | + post |
| 121 | + .mockRejectedValueOnce(makeFetchError(500)) |
| 122 | + .mockRejectedValueOnce(makeFetchError(503)) |
| 123 | + .mockResolvedValueOnce(makeOkResponse()) |
| 124 | + |
| 125 | + await upload({ |
| 126 | + accessToken: 'tok', |
| 127 | + docs: makeDocs(1), |
| 128 | + verbose: false, |
| 129 | + }) |
| 130 | + |
| 131 | + expect(post).toHaveBeenCalledTimes(3) |
| 132 | + expect(recordedDelaysMs).toEqual([5_000, 15_000]) |
| 133 | + }) |
| 134 | + |
| 135 | + it('honors Retry-After header instead of the fixed delay', async () => { |
| 136 | + post.mockRejectedValueOnce(makeFetchError(503, '7')).mockResolvedValueOnce(makeOkResponse()) |
| 137 | + |
| 138 | + await upload({ |
| 139 | + accessToken: 'tok', |
| 140 | + docs: makeDocs(1), |
| 141 | + verbose: false, |
| 142 | + }) |
| 143 | + |
| 144 | + expect(recordedDelaysMs).toEqual([7_000]) |
| 145 | + }) |
| 146 | + |
| 147 | + it('gives up after 3 retries (4 attempts total) and exits', async () => { |
| 148 | + for (let i = 0; i < 4; i++) post.mockRejectedValueOnce(makeFetchError(500)) |
| 149 | + |
| 150 | + await expect( |
| 151 | + upload({ |
| 152 | + accessToken: 'tok', |
| 153 | + docs: makeDocs(1), |
| 154 | + verbose: false, |
| 155 | + }), |
| 156 | + ).rejects.toThrow('process.exit(1)') |
| 157 | + |
| 158 | + expect(post).toHaveBeenCalledTimes(4) |
| 159 | + expect(recordedDelaysMs).toEqual([5_000, 15_000, 30_000]) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + it('tracks 429 and 5xx retry counters independently', async () => { |
| 164 | + // Alternate: 500, 429, 500, 429, 500, 429, success |
| 165 | + // After this sequence: 3 5xx retries used, 3 429 retries used → both still |
| 166 | + // within budget, request succeeds. |
| 167 | + post |
| 168 | + .mockRejectedValueOnce(makeFetchError(500)) |
| 169 | + .mockRejectedValueOnce(makeFetchError(429)) |
| 170 | + .mockRejectedValueOnce(makeFetchError(500)) |
| 171 | + .mockRejectedValueOnce(makeFetchError(429)) |
| 172 | + .mockRejectedValueOnce(makeFetchError(500)) |
| 173 | + .mockRejectedValueOnce(makeFetchError(429)) |
| 174 | + .mockResolvedValueOnce(makeOkResponse()) |
| 175 | + |
| 176 | + await upload({ |
| 177 | + accessToken: 'tok', |
| 178 | + docs: makeDocs(1), |
| 179 | + verbose: false, |
| 180 | + }) |
| 181 | + |
| 182 | + expect(post).toHaveBeenCalledTimes(7) |
| 183 | + // 500s consume 5xx schedule indices 0,1,2 → 5s,15s,30s |
| 184 | + // 429s consume 429 schedule indices 0,1,2 → 5s,15s,30s |
| 185 | + // Interleaved in encounter order: |
| 186 | + expect(recordedDelaysMs).toEqual([5_000, 5_000, 15_000, 15_000, 30_000, 30_000]) |
| 187 | + }) |
| 188 | + |
| 189 | + it('does not retry on a non-retryable status (e.g. 400)', async () => { |
| 190 | + post.mockRejectedValueOnce(makeFetchError(400)) |
| 191 | + |
| 192 | + await expect( |
| 193 | + upload({ |
| 194 | + accessToken: 'tok', |
| 195 | + docs: makeDocs(1), |
| 196 | + verbose: false, |
| 197 | + }), |
| 198 | + ).rejects.toThrow('process.exit(1)') |
| 199 | + |
| 200 | + expect(post).toHaveBeenCalledTimes(1) |
| 201 | + expect(recordedDelaysMs).toEqual([]) |
| 202 | + }) |
| 203 | +}) |
| 204 | + |
| 205 | +describe('upload — batch concurrency', () => { |
| 206 | + let stderrSpy: jest.SpyInstance |
| 207 | + |
| 208 | + beforeEach(() => { |
| 209 | + post.mockReset() |
| 210 | + logger.warn = jest.fn() |
| 211 | + logger.info = jest.fn() |
| 212 | + logger.debug = jest.fn() |
| 213 | + logger.error = jest.fn() |
| 214 | + stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => true) |
| 215 | + }) |
| 216 | + |
| 217 | + afterEach(() => { |
| 218 | + stderrSpy.mockRestore() |
| 219 | + }) |
| 220 | + |
| 221 | + it('processes batches sequentially (one in flight at a time), not in parallel', async () => { |
| 222 | + let inFlight = 0 |
| 223 | + let maxInFlight = 0 |
| 224 | + const callOrder: number[] = [] |
| 225 | + let callIndex = 0 |
| 226 | + |
| 227 | + post.mockImplementation(async () => { |
| 228 | + const id = ++callIndex |
| 229 | + callOrder.push(id) |
| 230 | + inFlight++ |
| 231 | + maxInFlight = Math.max(maxInFlight, inFlight) |
| 232 | + await new Promise((r) => setImmediate(r)) |
| 233 | + await new Promise((r) => setImmediate(r)) |
| 234 | + inFlight-- |
| 235 | + return makeOkResponse() |
| 236 | + }) |
| 237 | + |
| 238 | + await upload({ |
| 239 | + accessToken: 'tok', |
| 240 | + docs: makeDocs(4), |
| 241 | + batchSize: 1, |
| 242 | + verbose: false, |
| 243 | + }) |
| 244 | + |
| 245 | + expect(post).toHaveBeenCalledTimes(4) |
| 246 | + expect(maxInFlight).toBe(1) |
| 247 | + expect(callOrder).toEqual([1, 2, 3, 4]) |
| 248 | + }) |
| 249 | +}) |
0 commit comments