From 4c2cc71a76da65a5e0ce1f4894bace6757711c63 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Tue, 5 May 2026 15:26:30 -0300 Subject: [PATCH 1/8] feat(bot): add Spotify 429 retry with Retry-After header --- packages/bot/src/spotify/spotifyApi.ts | 413 ++++++++++-------- .../bot/src/spotify/spotifyApiRetry.spec.ts | 126 ++++++ 2 files changed, 365 insertions(+), 174 deletions(-) create mode 100644 packages/bot/src/spotify/spotifyApiRetry.spec.ts diff --git a/packages/bot/src/spotify/spotifyApi.ts b/packages/bot/src/spotify/spotifyApi.ts index 48b0e8341..0c2a94e9d 100644 --- a/packages/bot/src/spotify/spotifyApi.ts +++ b/packages/bot/src/spotify/spotifyApi.ts @@ -1,5 +1,5 @@ import { logAndSwallow } from '@lucky/shared/utils/error' -import { debugLog } from '@lucky/shared/utils/general/log' +import { debugLog, warnLog } from '@lucky/shared/utils/general/log' export interface SpotifyRecommendationTrack { id: string @@ -14,6 +14,60 @@ export type SpotifyAudioFeatureConstraints = { danceability?: number } +async function sleep(ms: number): Promise { + return new Promise((resolve) => { + setTimeout(resolve, ms) + }) +} + +async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promise { + for (let attempt = 0; attempt <= maxRetries; attempt++) { + try { + return await fn() + } catch (error) { + const isResponse = error instanceof Response + const status = isResponse ? error.status : null + + if (status === 429) { + const retryAfterHeader = isResponse + ? error.headers.get('Retry-After') + : null + const retryAfterSeconds = retryAfterHeader + ? parseInt(retryAfterHeader, 10) + : 1 + const delayMs = retryAfterSeconds * 1000 + + if (attempt < maxRetries) { + debugLog({ + message: 'Spotify 429 rate limit, retrying', + data: { + attempt, + maxRetries, + delayMs, + retryAfter: retryAfterSeconds, + }, + }) + await sleep(delayMs) + continue + } else { + warnLog({ + message: 'Spotify 429 retry exhausted', + data: { + attempt, + maxRetries, + }, + }) + throw error + } + } + + throw error + } + } + + throw new Error('Unreachable: withSpotifyRetry loop should always return or throw') +} + export async function getSpotifyRecommendations( accessToken: string, seedTrackIds: string[], @@ -22,95 +76,98 @@ export async function getSpotifyRecommendations( ): Promise { if (seedTrackIds.length === 0) return [] try { - const params = new URLSearchParams({ - seed_tracks: seedTrackIds.slice(0, 5).join(','), - limit: String(Math.min(limit, 100)), - }) + const result = await withSpotifyRetry(async () => { + const params = new URLSearchParams({ + seed_tracks: seedTrackIds.slice(0, 5).join(','), + limit: String(Math.min(limit, 100)), + }) - if (audioConstraints) { - const TOLERANCE = 0.25 - if (audioConstraints.energy !== undefined) { - params.set( - 'min_energy', - String( - Math.max( - 0, - audioConstraints.energy - TOLERANCE, - ).toFixed(2), - ), - ) - params.set( - 'max_energy', - String( - Math.min( - 1, - audioConstraints.energy + TOLERANCE, - ).toFixed(2), - ), - ) - } - if (audioConstraints.valence !== undefined) { - params.set( - 'min_valence', - String( - Math.max( - 0, - audioConstraints.valence - TOLERANCE, - ).toFixed(2), - ), - ) - params.set( - 'max_valence', - String( - Math.min( - 1, - audioConstraints.valence + TOLERANCE, - ).toFixed(2), - ), - ) + if (audioConstraints) { + const TOLERANCE = 0.25 + if (audioConstraints.energy !== undefined) { + params.set( + 'min_energy', + String( + Math.max( + 0, + audioConstraints.energy - TOLERANCE, + ).toFixed(2), + ), + ) + params.set( + 'max_energy', + String( + Math.min( + 1, + audioConstraints.energy + TOLERANCE, + ).toFixed(2), + ), + ) + } + if (audioConstraints.valence !== undefined) { + params.set( + 'min_valence', + String( + Math.max( + 0, + audioConstraints.valence - TOLERANCE, + ).toFixed(2), + ), + ) + params.set( + 'max_valence', + String( + Math.min( + 1, + audioConstraints.valence + TOLERANCE, + ).toFixed(2), + ), + ) + } + if (audioConstraints.danceability !== undefined) { + params.set( + 'min_danceability', + String( + Math.max( + 0, + audioConstraints.danceability - TOLERANCE, + ).toFixed(2), + ), + ) + params.set( + 'max_danceability', + String( + Math.min( + 1, + audioConstraints.danceability + TOLERANCE, + ).toFixed(2), + ), + ) + } } - if (audioConstraints.danceability !== undefined) { - params.set( - 'min_danceability', - String( - Math.max( - 0, - audioConstraints.danceability - TOLERANCE, - ).toFixed(2), - ), - ) - params.set( - 'max_danceability', - String( - Math.min( - 1, - audioConstraints.danceability + TOLERANCE, - ).toFixed(2), - ), - ) + const res = await fetch( + `https://api.spotify.com/v1/recommendations?${params.toString()}`, + { headers: { Authorization: `Bearer ${accessToken}` } }, + ) + if (!res.ok) return [] + const data = (await res.json().catch(() => null)) as { + tracks?: Array<{ + id?: string + name?: string + artists?: Array<{ name?: string }> + duration_ms?: number + }> } - } - const res = await fetch( - `https://api.spotify.com/v1/recommendations?${params.toString()}`, - { headers: { Authorization: `Bearer ${accessToken}` } }, - ) - if (!res.ok) return [] - const data = (await res.json().catch(() => null)) as { - tracks?: Array<{ - id?: string - name?: string - artists?: Array<{ name?: string }> - duration_ms?: number - }> - } - return (data?.tracks ?? []) - .filter((t) => t.id && t.name) - .map((t) => ({ - id: t.id!, - name: t.name!, - artists: (t.artists ?? []).map((a) => ({ name: a.name ?? '' })), - duration_ms: t.duration_ms ?? 0, - })) + return (data?.tracks ?? []) + .filter((t) => t.id && t.name) + .map((t) => ({ + id: t.id!, + name: t.name!, + artists: (t.artists ?? []).map((a) => ({ name: a.name ?? '' })), + duration_ms: t.duration_ms ?? 0, + })) + }) + return result } catch (err) { logAndSwallow(err, 'spotify.getRecommendations', { seedTrackIds: seedTrackIds.length }) return [] @@ -150,27 +207,29 @@ export async function getAudioFeatures( spotifyTrackId: string, ): Promise { try { - const res = await fetch( - `https://api.spotify.com/v1/audio-features/${spotifyTrackId}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + const data = await withSpotifyRetry(async () => { + const res = await fetch( + `https://api.spotify.com/v1/audio-features/${spotifyTrackId}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) + ) - if (!res.ok) { - return null - } + if (!res.ok) { + return null + } - const data = (await res.json().catch(() => null)) as { - energy?: number - valence?: number - danceability?: number - tempo?: number - acousticness?: number - } + return (await res.json().catch(() => null)) as { + energy?: number + valence?: number + danceability?: number + tempo?: number + acousticness?: number + } + }) if (!data?.energy || typeof data.valence !== 'number') { return null @@ -196,34 +255,36 @@ export async function getBatchAudioFeatures( if (spotifyIds.length === 0) return new Map() try { - const ids = spotifyIds.slice(0, 100).join(',') - const res = await fetch( - `https://api.spotify.com/v1/audio-features?ids=${ids}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + const result = await withSpotifyRetry(async () => { + const ids = spotifyIds.slice(0, 100).join(',') + const res = await fetch( + `https://api.spotify.com/v1/audio-features?ids=${ids}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) - - if (!res.ok) return new Map() - - const data = (await res.json().catch(() => null)) as { - audio_features?: Array<{ - id?: string - energy?: number - valence?: number - danceability?: number - tempo?: number - acousticness?: number - } | null> - } + ) + + if (!res.ok) return null + + return (await res.json().catch(() => null)) as { + audio_features?: Array<{ + id?: string + energy?: number + valence?: number + danceability?: number + tempo?: number + acousticness?: number + } | null> + } + }) - const result = new Map() - if (!data?.audio_features) return result + const resultMap = new Map() + if (!result?.audio_features) return resultMap - for (const feature of data.audio_features) { + for (const feature of result.audio_features) { if ( !feature?.id || typeof feature.energy !== 'number' || @@ -232,7 +293,7 @@ export async function getBatchAudioFeatures( continue } - result.set(feature.id, { + resultMap.set(feature.id, { energy: feature.energy, valence: feature.valence, danceability: feature.danceability ?? 0, @@ -241,7 +302,7 @@ export async function getBatchAudioFeatures( }) } - return result + return resultMap } catch (err) { logAndSwallow(err, 'spotify.getBatchAudioFeatures', { count: spotifyIds.length }) return new Map() @@ -267,32 +328,34 @@ export async function getArtistPopularity( }) try { - const params = new URLSearchParams({ - q: artistName, - type: 'artist', - limit: '1', - }) + const popularity = await withSpotifyRetry(async () => { + const params = new URLSearchParams({ + q: artistName, + type: 'artist', + limit: '1', + }) - const res = await fetch( - `https://api.spotify.com/v1/search?${params.toString()}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + const res = await fetch( + `https://api.spotify.com/v1/search?${params.toString()}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) + ) - if (!res.ok) { - artistPopularityCache.set(artistName, { value: null }) - return null - } + if (!res.ok) { + return null + } - const data = (await res.json().catch(() => null)) as { - artists?: { items?: Array<{ popularity?: number }> } - } + const data = (await res.json().catch(() => null)) as { + artists?: { items?: Array<{ popularity?: number }> } + } + + return data?.artists?.items?.[0]?.popularity ?? null + }) - const popularity = data?.artists?.items?.[0]?.popularity ?? null artistPopularityCache.set(artistName, { value: popularity }) return popularity } catch { @@ -320,32 +383,34 @@ export async function getArtistGenres( }) try { - const params = new URLSearchParams({ - q: artistName, - type: 'artist', - limit: '1', - }) + const genres = await withSpotifyRetry(async () => { + const params = new URLSearchParams({ + q: artistName, + type: 'artist', + limit: '1', + }) - const res = await fetch( - `https://api.spotify.com/v1/search?${params.toString()}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + const res = await fetch( + `https://api.spotify.com/v1/search?${params.toString()}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) + ) - if (!res.ok) { - artistGenresCache.set(artistName, { value: null }) - return [] - } + if (!res.ok) { + return [] + } - const data = (await res.json().catch(() => null)) as { - artists?: { items?: Array<{ genres?: string[] }> } - } + const data = (await res.json().catch(() => null)) as { + artists?: { items?: Array<{ genres?: string[] }> } + } + + return data?.artists?.items?.[0]?.genres ?? [] + }) - const genres = data?.artists?.items?.[0]?.genres ?? [] artistGenresCache.set(artistName, { value: genres }) return genres } catch { diff --git a/packages/bot/src/spotify/spotifyApiRetry.spec.ts b/packages/bot/src/spotify/spotifyApiRetry.spec.ts new file mode 100644 index 000000000..d561d176b --- /dev/null +++ b/packages/bot/src/spotify/spotifyApiRetry.spec.ts @@ -0,0 +1,126 @@ +import * as spotifyApi from './spotifyApi' +import { debugLog, warnLog } from '@lucky/shared/utils/general/log' +import { logAndSwallow } from '@lucky/shared/utils/error' + +jest.mock('@lucky/shared/utils/general/log') +jest.mock('@lucky/shared/utils/error') + +describe('Spotify API 429 Retry Logic', () => { + let fetchMock: jest.Mock + + beforeEach(() => { + jest.clearAllMocks() + fetchMock = jest.fn() + global.fetch = fetchMock + ;(logAndSwallow as jest.Mock).mockImplementation(() => {}) + }) + + afterEach(() => { + jest.restoreAllMocks() + }) + + describe('getAudioFeatures with 429 retry', () => { + it('should retry on 429 and succeed on second attempt', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + const error = new Response(null, { status: 429 }) + throw error + } + // Second attempt succeeds + return new Response( + JSON.stringify({ + id: 'track1', + energy: 0.8, + valence: 0.7, + danceability: 0.6, + tempo: 120, + acousticness: 0.2, + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getAudioFeatures('test-token', 'track1') + + expect(attemptCount).toBe(2) + expect(result).not.toBeNull() + expect(result?.energy).toBe(0.8) + expect(result?.valence).toBe(0.7) + expect(debugLog).toHaveBeenCalled() + }) + + it('should swallow error and return null after exhausting retries on consecutive 429 responses', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + const error = new Response(null, { status: 429 }) + throw error + }) + + const result = await spotifyApi.getAudioFeatures('test-token', 'track1') + + // maxRetries default is 2, so we expect: initial attempt + 2 retries = 3 total + expect(attemptCount).toBe(3) + expect(warnLog).toHaveBeenCalled() + expect(logAndSwallow).toHaveBeenCalled() + expect(result).toBeNull() + }) + }) + + describe('getSpotifyRecommendations with 429 retry', () => { + it('should retry on 429 and return recommendations on second attempt', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + const error = new Response(null, { status: 429 }) + throw error + } + return new Response( + JSON.stringify({ + tracks: [ + { + id: 'rec1', + name: 'Recommended Song', + artists: [{ name: 'Artist' }], + duration_ms: 180000, + }, + ], + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getSpotifyRecommendations('test-token', [ + 'seed1', + ]) + + expect(attemptCount).toBe(2) + expect(result).toHaveLength(1) + expect(result[0].name).toBe('Recommended Song') + expect(debugLog).toHaveBeenCalled() + }) + + it('should swallow error and return empty array after exhausting retries on multiple 429 responses', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + const error = new Response(null, { status: 429 }) + throw error + }) + + const result = await spotifyApi.getSpotifyRecommendations('test-token', ['seed1']) + + expect(attemptCount).toBe(3) // initial + 2 retries + expect(warnLog).toHaveBeenCalled() + expect(logAndSwallow).toHaveBeenCalled() + expect(result).toEqual([]) + }) + }) +}) From a37c02b9416195b87717fe39ed6bcf7418064f69 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 8 May 2026 15:38:02 -0300 Subject: [PATCH 2/8] test(spotify): add non-429 and Retry-After header coverage Co-Authored-By: Claude Sonnet 4.6 --- .../bot/src/spotify/spotifyApiRetry.spec.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/bot/src/spotify/spotifyApiRetry.spec.ts b/packages/bot/src/spotify/spotifyApiRetry.spec.ts index d561d176b..950a850f2 100644 --- a/packages/bot/src/spotify/spotifyApiRetry.spec.ts +++ b/packages/bot/src/spotify/spotifyApiRetry.spec.ts @@ -69,6 +69,54 @@ describe('Spotify API 429 Retry Logic', () => { expect(logAndSwallow).toHaveBeenCalled() expect(result).toBeNull() }) + + it('should throw immediately on non-429 errors without retrying', async () => { + fetchMock.mockImplementation(async () => { + throw new Response(null, { status: 401 }) + }) + + const result = await spotifyApi.getAudioFeatures('test-token', 'track1') + + // No retry — only 1 attempt + expect(fetchMock).toHaveBeenCalledTimes(1) + expect(result).toBeNull() + expect(logAndSwallow).toHaveBeenCalled() + }) + + it('should use Retry-After header when present', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + throw new Response(null, { + status: 429, + headers: { 'Retry-After': '0' }, + }) + } + return new Response( + JSON.stringify({ + id: 'track1', + energy: 0.8, + valence: 0.7, + danceability: 0.6, + tempo: 120, + acousticness: 0.2, + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getAudioFeatures('test-token', 'track1') + + expect(attemptCount).toBe(2) + expect(result).not.toBeNull() + expect(debugLog).toHaveBeenCalledWith( + expect.objectContaining({ + data: expect.objectContaining({ retryAfter: 0 }), + }), + ) + }) }) describe('getSpotifyRecommendations with 429 retry', () => { From b956dd84f9d0e4b5525a51bbc6c0719b9de26048 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 8 May 2026 21:28:41 -0300 Subject: [PATCH 3/8] fix(spotify): remove unreachable throw after withSpotifyRetry loop The for loop always returns or throws on every path, making the post-loop throw dead code. Removing it improves SonarCloud coverage metrics and eliminates the unreachable branch warning. Co-Authored-By: Claude Sonnet 4.6 --- packages/bot/src/spotify/spotifyApi.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/bot/src/spotify/spotifyApi.ts b/packages/bot/src/spotify/spotifyApi.ts index 37569f6f1..113ba0c16 100644 --- a/packages/bot/src/spotify/spotifyApi.ts +++ b/packages/bot/src/spotify/spotifyApi.ts @@ -64,8 +64,6 @@ async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promis throw error } } - - throw new Error('Unreachable: withSpotifyRetry loop should always return or throw') } export async function getSpotifyRecommendations( From d5bef5ee056e0b102a28303f548c0f5691f4673a Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 8 May 2026 21:40:51 -0300 Subject: [PATCH 4/8] refactor(spotify): use while(true) in withSpotifyRetry for TypeScript correctness The for-loop exit was unreachable but TypeScript required a throw/return after it (TS2366). Using while(true) makes TypeScript's flow analysis see that the loop never exits normally, satisfying the return-type constraint without adding dead code that SonarCloud would flag. Co-Authored-By: Claude Sonnet 4.6 --- packages/bot/src/spotify/spotifyApi.ts | 45 ++++++++++++-------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/bot/src/spotify/spotifyApi.ts b/packages/bot/src/spotify/spotifyApi.ts index 113ba0c16..d03546502 100644 --- a/packages/bot/src/spotify/spotifyApi.ts +++ b/packages/bot/src/spotify/spotifyApi.ts @@ -21,14 +21,15 @@ async function sleep(ms: number): Promise { } async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promise { - for (let attempt = 0; attempt <= maxRetries; attempt++) { + let attempt = 0 + while (true) { try { return await fn() } catch (error) { const isResponse = error instanceof Response const status = isResponse ? error.status : null - if (status === 429) { + if (status === 429 && attempt < maxRetries) { const retryAfterHeader = isResponse ? error.headers.get('Retry-After') : null @@ -36,29 +37,25 @@ async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promis ? parseInt(retryAfterHeader, 10) : 1 const delayMs = retryAfterSeconds * 1000 + debugLog({ + message: 'Spotify 429 rate limit, retrying', + data: { + attempt, + maxRetries, + delayMs, + retryAfter: retryAfterSeconds, + }, + }) + await sleep(delayMs) + attempt++ + continue + } - if (attempt < maxRetries) { - debugLog({ - message: 'Spotify 429 rate limit, retrying', - data: { - attempt, - maxRetries, - delayMs, - retryAfter: retryAfterSeconds, - }, - }) - await sleep(delayMs) - continue - } else { - warnLog({ - message: 'Spotify 429 retry exhausted', - data: { - attempt, - maxRetries, - }, - }) - throw error - } + if (status === 429) { + warnLog({ + message: 'Spotify 429 retry exhausted', + data: { attempt, maxRetries }, + }) } throw error From 64a7e870e5e2bcd796cae464f8a70dddd2b31567 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 8 May 2026 22:02:00 -0300 Subject: [PATCH 5/8] test(spotify): add 429 retry tests for getBatchAudioFeatures, getArtistPopularity, and getArtistGenres Covers new withSpotifyRetry wrapper paths in all three functions to push new code coverage above the 80% SonarCloud quality gate threshold. Co-Authored-By: Claude Sonnet 4.6 --- .../bot/src/spotify/spotifyApiRetry.spec.ts | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/packages/bot/src/spotify/spotifyApiRetry.spec.ts b/packages/bot/src/spotify/spotifyApiRetry.spec.ts index 950a850f2..28950e3e5 100644 --- a/packages/bot/src/spotify/spotifyApiRetry.spec.ts +++ b/packages/bot/src/spotify/spotifyApiRetry.spec.ts @@ -171,4 +171,113 @@ describe('Spotify API 429 Retry Logic', () => { expect(result).toEqual([]) }) }) + + describe('getBatchAudioFeatures with 429 retry', () => { + it('should retry on 429 and return features on second attempt', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + throw new Response(null, { status: 429 }) + } + return new Response( + JSON.stringify({ + audio_features: [ + { + id: 'batch1', + energy: 0.7, + valence: 0.6, + danceability: 0.5, + tempo: 130, + acousticness: 0.1, + }, + ], + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getBatchAudioFeatures('test-token', ['batch1']) + + expect(attemptCount).toBe(2) + expect(result.size).toBe(1) + expect(result.get('batch1')?.energy).toBe(0.7) + }) + + it('should return empty map after exhausting retries', async () => { + fetchMock.mockImplementation(async () => { + throw new Response(null, { status: 429 }) + }) + + const result = await spotifyApi.getBatchAudioFeatures('test-token', ['batch1']) + + expect(result.size).toBe(0) + expect(logAndSwallow).toHaveBeenCalled() + }) + }) + + describe('getArtistPopularity with 429 retry', () => { + it('should retry on 429 and return popularity on second attempt', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + throw new Response(null, { status: 429 }) + } + return new Response( + JSON.stringify({ + artists: { items: [{ popularity: 82 }] }, + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getArtistPopularity('test-token', 'Retry Artist Popularity') + + expect(attemptCount).toBe(2) + expect(result).toBe(82) + }) + + it('should return null after network error', async () => { + fetchMock.mockRejectedValue(new Error('network failure')) + + const result = await spotifyApi.getArtistPopularity('test-token', 'Failing Artist Popularity') + + expect(result).toBeNull() + }) + }) + + describe('getArtistGenres with 429 retry', () => { + it('should retry on 429 and return genres on second attempt', async () => { + let attemptCount = 0 + + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + throw new Response(null, { status: 429 }) + } + return new Response( + JSON.stringify({ + artists: { items: [{ genres: ['pop', 'rock'] }] }, + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getArtistGenres('test-token', 'Retry Artist Genres') + + expect(attemptCount).toBe(2) + expect(result).toEqual(['pop', 'rock']) + }) + + it('should return empty array after network error', async () => { + fetchMock.mockRejectedValue(new Error('network failure')) + + const result = await spotifyApi.getArtistGenres('test-token', 'Failing Artist Genres') + + expect(result).toEqual([]) + }) + }) }) From 25fe79180842042737836fb0e54b4c469ab58064 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Sat, 9 May 2026 22:06:06 -0300 Subject: [PATCH 6/8] fix(spotify): retry actually fires on 429 + handle Retry-After HTTP-date MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous retry implementation never triggered in production because fetch() does NOT throw on HTTP error statuses — it resolves with a Response where ok=false. Each wrapped callback's 'if (!res.ok) return null' short-circuited before withSpotifyRetry's catch could see anything, so 429 responses silently became null/[]. Fix: - Add throwIfRetryable(res) helper that throws the Response on 429. - Call it inside every wrapped callback BEFORE the !res.ok early return, so a resolved 429 becomes a thrown Response that the retry catch can intercept. - 5 sites covered: getSpotifyRecommendations, getAudioFeatures, getBatchAudioFeatures, getArtistPopularity, getArtistGenres. Also fix Retry-After parsing (Greptile concern 2): - parseInt of an HTTP-date returns NaN, which becomes NaN ms sleep (0ms in practice — busy-loop retry, not the requested back-off). - New parseRetryAfterMs helper handles both delta-seconds and RFC 7231 IMF-fixdate, returns null when unparseable so we fall back to a sane default (1s) instead of NaN. - Cap the delay at MAX_RETRY_AFTER_MS (60s) to prevent abuse. Tests: - New 'fetch resolves (not throws)' suite covering the realistic path. - Retry-After delta-seconds via fake timers. - Retry-After HTTP-date doesn't produce NaN. Addresses Greptile feedback on PR #808. Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/bot/src/spotify/spotifyApi.ts | 56 +++++++++++- .../bot/src/spotify/spotifyApiRetry.spec.ts | 91 +++++++++++++++++++ 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/packages/bot/src/spotify/spotifyApi.ts b/packages/bot/src/spotify/spotifyApi.ts index d03546502..b26e32fad 100644 --- a/packages/bot/src/spotify/spotifyApi.ts +++ b/packages/bot/src/spotify/spotifyApi.ts @@ -20,6 +20,46 @@ async function sleep(ms: number): Promise { }) } +const DEFAULT_RETRY_AFTER_MS = 1000 +const MAX_RETRY_AFTER_MS = 60 * 1000 + +/** + * Parse a Retry-After header value into a millisecond delay. + * + * Per RFC 7231 the header may be either: + * - delta-seconds: a non-negative integer ("120") + * - HTTP-date: an RFC 7231 IMF-fixdate ("Sat, 10 May 2026 12:00:00 GMT") + * + * Returns null when the header is missing or unparseable so callers can fall + * back to a default delay (rather than NaN milliseconds, which becomes a busy + * loop). + */ +function parseRetryAfterMs(header: string | null): number | null { + if (!header) return null + const trimmed = header.trim() + if (!trimmed) return null + // delta-seconds form first — bare integer + if (/^\d+$/.test(trimmed)) { + const seconds = Number.parseInt(trimmed, 10) + return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : null + } + // HTTP-date form + const targetMs = Date.parse(trimmed) + if (!Number.isFinite(targetMs)) return null + const delta = targetMs - Date.now() + return delta > 0 ? delta : 0 +} + +/** + * Throw the Response when its status is retryable so withSpotifyRetry's + * catch block can intercept it. fetch() does not throw on HTTP error + * statuses, so wrapped callbacks must signal retry-eligible failures + * explicitly. + */ +function throwIfRetryable(res: Response): void { + if (res.status === 429) throw res +} + async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promise { let attempt = 0 while (true) { @@ -33,17 +73,18 @@ async function withSpotifyRetry(fn: () => Promise, maxRetries = 2): Promis const retryAfterHeader = isResponse ? error.headers.get('Retry-After') : null - const retryAfterSeconds = retryAfterHeader - ? parseInt(retryAfterHeader, 10) - : 1 - const delayMs = retryAfterSeconds * 1000 + const parsedDelayMs = parseRetryAfterMs(retryAfterHeader) + const delayMs = Math.min( + parsedDelayMs ?? DEFAULT_RETRY_AFTER_MS, + MAX_RETRY_AFTER_MS, + ) debugLog({ message: 'Spotify 429 rate limit, retrying', data: { attempt, maxRetries, delayMs, - retryAfter: retryAfterSeconds, + retryAfterHeader, }, }) await sleep(delayMs) @@ -144,6 +185,7 @@ export async function getSpotifyRecommendations( `https://api.spotify.com/v1/recommendations?${params.toString()}`, { headers: { Authorization: `Bearer ${accessToken}` } }, ) + throwIfRetryable(res) if (!res.ok) return [] const data = (await res.json().catch(() => null)) as { tracks?: Array<{ @@ -213,6 +255,7 @@ export async function getAudioFeatures( }, ) + throwIfRetryable(res) if (!res.ok) { return null } @@ -262,6 +305,7 @@ export async function getBatchAudioFeatures( }, ) + throwIfRetryable(res) if (!res.ok) return null return (await res.json().catch(() => null)) as { @@ -340,6 +384,7 @@ export async function getArtistPopularity( }, ) + throwIfRetryable(res) if (!res.ok) { return null } @@ -395,6 +440,7 @@ export async function getArtistGenres( }, ) + throwIfRetryable(res) if (!res.ok) { return [] } diff --git a/packages/bot/src/spotify/spotifyApiRetry.spec.ts b/packages/bot/src/spotify/spotifyApiRetry.spec.ts index 28950e3e5..2b4e627de 100644 --- a/packages/bot/src/spotify/spotifyApiRetry.spec.ts +++ b/packages/bot/src/spotify/spotifyApiRetry.spec.ts @@ -280,4 +280,95 @@ describe('Spotify API 429 Retry Logic', () => { expect(result).toEqual([]) }) }) + + // Regression: real fetch() RESOLVES with a 429 Response (it does not throw + // on HTTP error statuses). Verify retry fires when fetch resolves rather + // than rejects — Greptile feedback on PR #808. + describe('429 retry when fetch resolves (not throws) the Response', () => { + it('retries on resolved 429 then succeeds', async () => { + let attemptCount = 0 + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + return new Response(null, { status: 429 }) + } + return new Response( + JSON.stringify({ + id: 't1', + energy: 0.5, + valence: 0.5, + danceability: 0.5, + tempo: 100, + acousticness: 0.5, + }), + { status: 200 }, + ) + }) + + const result = await spotifyApi.getAudioFeatures('tok', 't1') + + expect(attemptCount).toBe(2) + expect(result).not.toBeNull() + }) + + it('honours Retry-After delta-seconds header from resolved Response', async () => { + jest.useFakeTimers() + try { + let attemptCount = 0 + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + return new Response(null, { + status: 429, + headers: { 'Retry-After': '2' }, + }) + } + return new Response( + JSON.stringify({ id: 't', energy: 0.5, valence: 0.5 }), + { status: 200 }, + ) + }) + + const promise = spotifyApi.getAudioFeatures('tok', 't') + await jest.advanceTimersByTimeAsync(2000) + await promise + + expect(attemptCount).toBe(2) + } finally { + jest.useRealTimers() + } + }) + + it('parses Retry-After HTTP-date header without producing NaN delay', async () => { + jest.useFakeTimers() + try { + const baseTime = new Date('2026-05-10T12:00:00Z').getTime() + jest.setSystemTime(baseTime) + const futureDate = new Date(baseTime + 3000).toUTCString() + + let attemptCount = 0 + fetchMock.mockImplementation(async () => { + attemptCount++ + if (attemptCount === 1) { + return new Response(null, { + status: 429, + headers: { 'Retry-After': futureDate }, + }) + } + return new Response( + JSON.stringify({ id: 't', energy: 0.5, valence: 0.5 }), + { status: 200 }, + ) + }) + + const promise = spotifyApi.getAudioFeatures('tok', 't') + await jest.advanceTimersByTimeAsync(3000) + await promise + + expect(attemptCount).toBe(2) + } finally { + jest.useRealTimers() + } + }) + }) }) From 041254e9e01b2f3753b2ccf23960fa5056d27b3c Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Sun, 10 May 2026 11:00:53 -0300 Subject: [PATCH 7/8] test(spotify): align Retry-After log assertion with new field name The retry refactor renamed the debug-log field from 'retryAfter' to 'retryAfterHeader' (storing the raw header string for diagnostics rather than the parsed seconds value). Update the legacy spec assertion to match. Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/bot/src/spotify/spotifyApiRetry.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bot/src/spotify/spotifyApiRetry.spec.ts b/packages/bot/src/spotify/spotifyApiRetry.spec.ts index 2b4e627de..e1b0bed29 100644 --- a/packages/bot/src/spotify/spotifyApiRetry.spec.ts +++ b/packages/bot/src/spotify/spotifyApiRetry.spec.ts @@ -113,7 +113,7 @@ describe('Spotify API 429 Retry Logic', () => { expect(result).not.toBeNull() expect(debugLog).toHaveBeenCalledWith( expect.objectContaining({ - data: expect.objectContaining({ retryAfter: 0 }), + data: expect.objectContaining({ retryAfterHeader: '0' }), }), ) }) From dbc37ff1bce274a61307791be6627ca35cbef7dc Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Sun, 10 May 2026 11:10:40 -0300 Subject: [PATCH 8/8] fix(spotify): apply withSpotifyRetry to remaining fetch sites MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per Greptile feedback (PR #808): three Spotify endpoints still bypassed the 429 retry path because they called fetch directly. Wrap them in withSpotifyRetry + throwIfRetryable for consistency: - searchSpotifyTrack - getUserTopArtistsAndTracks (parallel artists + tracks fetches) - getUserSavedTracks (per-page wrap inside the pagination loop — particularly exposed since it can issue up to 4 sequential requests) Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/bot/src/spotify/spotifyApi.ts | 91 ++++++++++++++------------ 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/packages/bot/src/spotify/spotifyApi.ts b/packages/bot/src/spotify/spotifyApi.ts index b26e32fad..47f3cb135 100644 --- a/packages/bot/src/spotify/spotifyApi.ts +++ b/packages/bot/src/spotify/spotifyApi.ts @@ -473,25 +473,28 @@ export async function searchSpotifyTrack( limit: '1', }) - const res = await fetch( - `https://api.spotify.com/v1/search?${params.toString()}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + return await withSpotifyRetry(async () => { + const res = await fetch( + `https://api.spotify.com/v1/search?${params.toString()}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) + ) - if (!res.ok) { - return null - } + throwIfRetryable(res) + if (!res.ok) { + return null + } - const data = (await res.json().catch(() => null)) as { - tracks?: { items?: Array<{ id?: string }> } - } + const data = (await res.json().catch(() => null)) as { + tracks?: { items?: Array<{ id?: string }> } + } - return data?.tracks?.items?.[0]?.id ?? null + return data?.tracks?.items?.[0]?.id ?? null + }) } catch { return null } @@ -513,25 +516,25 @@ export async function getUserTopArtistsAndTracks( accessToken: string, ): Promise<{ artists: SpotifyTopArtist[]; tracks: SpotifyTopTrack[] } | null> { try { - const topArtistsRes = await fetch( - 'https://api.spotify.com/v1/me/top/artists?limit=20&time_range=medium_term', - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }, - ) - - const topTracksRes = await fetch( - 'https://api.spotify.com/v1/me/top/tracks?limit=20&time_range=medium_term', - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }, - ) + const headers = { Authorization: `Bearer ${accessToken}` } + const [topArtistsRes, topTracksRes] = await Promise.all([ + withSpotifyRetry(async () => { + const r = await fetch( + 'https://api.spotify.com/v1/me/top/artists?limit=20&time_range=medium_term', + { method: 'GET', headers }, + ) + throwIfRetryable(r) + return r + }), + withSpotifyRetry(async () => { + const r = await fetch( + 'https://api.spotify.com/v1/me/top/tracks?limit=20&time_range=medium_term', + { method: 'GET', headers }, + ) + throwIfRetryable(r) + return r + }), + ]) if (!topArtistsRes.ok || !topTracksRes.ok) { return null @@ -586,15 +589,19 @@ export async function getUserSavedTracks( limit: String(pageLimit), offset: String(offset), }) - const res = await fetch( - `https://api.spotify.com/v1/me/tracks?${params.toString()}`, - { - method: 'GET', - headers: { - Authorization: `Bearer ${accessToken}`, + const res = await withSpotifyRetry(async () => { + const r = await fetch( + `https://api.spotify.com/v1/me/tracks?${params.toString()}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${accessToken}`, + }, }, - }, - ) + ) + throwIfRetryable(r) + return r + }) if (!res.ok) { logAndSwallow(