Skip to content

fix(shared): update getSpotifyRelatedArtists tests for the #648 Last.fm fallback - #904

Closed
LucasSantana-Dev wants to merge 1 commit into
release/v2.12.0from
fix/spotify-related-artists-test-update
Closed

LucasSantana-Dev wants to merge 1 commit into
release/v2.12.0from
fix/spotify-related-artists-test-update

Conversation

@LucasSantana-Dev

@LucasSantana-Dev LucasSantana-Dev commented May 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Three tests in packages/shared/src/__tests__/utils/spotify/artistApi.test.ts have been red since PR #648 (Spotify→Last.fm fallback rewrite). The impl was updated; the tests weren't. This PR brings the tests back into alignment with current behavior.

Diagnostic trail

Followed /diagnose discipline. Phase 1 loop = the spec itself (2-sec deterministic). Phase 2 reproduced: all three failing tests early-return [] because the impl's first fetch (fetchSpotifyArtistName) isn't mocked by the existing old-shape mock. Phase 3 ranked four hypotheses; H1 (stale tests) confirmed against H2 (impl regressed) and H3 (dead code — backend/src/routes/artists.ts:404 is a live consumer).

What changed

  • Three response-builder helpers (makeSpotifyArtistNameResponse, makeLastFmSimilarResponse, makeSpotifySearchResponse) make each test's fetch sequence legible.
  • process.env.LASTFM_API_KEY is set in beforeEach and restored in afterEach; without it fetchLastFmSimilarArtists early-returns [].
  • Test 1 (should return unique artists from recommendations) → mocks the full Spotify → Last.fm → Spotify search chain.
  • Test 2 (should deduplicate duplicate artist ids in recommendations) → mocks two similar names that resolve to the same Spotify id; asserts the dedupe by id still holds.
  • Test 3 (should limit results to 12 artists) → replaced. The pre-fix(artists): related-artists fallback + responsive panel + batch save endpoint #648 impl had a 12-cap; the post-fix(artists): related-artists fallback + responsive panel + batch save endpoint #648 impl returns all unique resolved artists up to the Math.max(limit, 30) lookup bound. The new test name and body assert the actual contract and guard against a silent cap regression.

The two already-passing tests (403 graceful, empty response) are kept; they happen to pass because the impl's first fetch returns null in those cases, which is a real behavior worth keeping under test.

Verification

  • npx jest --config packages/shared/jest.config.cjs --ci packages/shared/src/__tests__/utils/spotify/artistApi.test.ts: 9/9 passing.
  • npm run test:ci --workspace=packages/shared: 417/417 tests passing on this branch. The single remaining failed suite (FeatureToggleService.spec.ts) is fixed by in-flight PR fix(shared,backend,db): retire orphan per-guild feature toggle substrate #903 and out of scope here.

Notes

  • No impl change. getSpotifyRelatedArtists is correct; only the tests were lying about its contract.
  • No regression-test seam needed (we are aligning existing tests with existing correct behavior; nothing to add or move).

Test plan

  • Local shared test:ci — green for ours
  • CI Quality Gates on this PR

Refs: PR #648 (introduced the chain), #903 (sibling test-cleanup PR open against same base).

Summary by CodeRabbit

  • Tests
    • Enhanced Spotify artist API test suite to validate multi-source related artists lookup (Spotify + Last.fm integration)
    • Added comprehensive test coverage for deduplication, error handling, and increased result limits
    • Improved test fixtures and environment variable management

Review Change Stack

…fm fallback

PR #648 ("fix(artists): related-artists fallback + responsive panel + batch
save endpoint") rewired `getSpotifyRelatedArtists` after Spotify deprecated
`/v1/recommendations` and `/v1/artists/{id}/related-artists` for new apps
(2024). The new impl chains:

  1. GET https://api.spotify.com/v1/artists/{id}      → seed artist name
  2. GET https://ws.audioscrobbler.com/2.0/?...       → similar names (Last.fm)
  3. GET https://api.spotify.com/v1/search?...        → per-name lookup

The tests stayed mocking the old `/v1/recommendations` payload shape
(`{ tracks: [{ artists: [...] }] }`) with a single fetch. The new impl's
first call is `fetchSpotifyArtistName`, which the old mocks don't satisfy;
the impl early-returns `[]` before any of the assertions can match. Three
tests have been red since #648:

- `should return unique artists from recommendations`
- `should deduplicate duplicate artist ids in recommendations`
- `should limit results to 12 artists`

This commit:

- Adds three response-builder helpers (`makeSpotifyArtistNameResponse`,
  `makeLastFmSimilarResponse`, `makeSpotifySearchResponse`) so the chain's
  fetch sequence is readable per test.
- Sets `process.env.LASTFM_API_KEY` in `beforeEach` and restores it in
  `afterEach`; without it, `fetchLastFmSimilarArtists` early-returns `[]`.
- Rewrites the first two failing tests to mock the full Spotify →
  Last.fm → Spotify search chain instead of the dead `/v1/recommendations`
  payload.
- Replaces the `should limit results to 12 artists` test with one that
  asserts the actual current contract: the impl returns all unique
  resolved artists from Last.fm (no artificial cap below the
  `Math.max(limit, 30)` lookup bound). The pre-#648 12-cap is gone; a
  regression that re-introduces a silent cap would fail this test.

Verification:

- `npx jest --config packages/shared/jest.config.cjs --ci packages/shared/src/__tests__/utils/spotify/artistApi.test.ts`:
  9/9 passing.
- `npm run test:ci --workspace=packages/shared` against this branch:
  417/417 tests passing. The one remaining failed suite
  (`FeatureToggleService.spec.ts`) is fixed by the in-flight PR #903 and
  out of scope here.

No impl change. Phase 1 loop = the spec itself (deterministic, 2-second
run). H1 confirmed at the start of the diagnostic session: tests stale
vs new impl. No regression-test seam was needed (we are bringing existing
tests back into alignment with existing correct behavior).

Refs: PR #648 (introduced the chain), this PR (#904).
@vercel

vercel Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lucky Ready Ready Preview, Comment May 20, 2026 2:45am

Request Review

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LucasSantana-Dev has reached the 50-review limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@github-actions

Copy link
Copy Markdown

Failed to generate code suggestions for PR

@coderabbitai

coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f31b4b72-afc5-4f4a-9b0f-ab95241b932c

📥 Commits

Reviewing files that changed from the base of the PR and between 748a5da and dffc0ea.

📒 Files selected for processing (1)
  • packages/shared/src/__tests__/utils/spotify/artistApi.test.ts

📝 Walkthrough

Walkthrough

The test suite for getSpotifyRelatedArtists is refactored to match a new implementation that chains Spotify artist seed lookup, Last.fm similar-artist lookup, and per-name Spotify searches. Response factory helpers now generate consistent mock payloads, environment variables are managed across test lifecycle, and assertions verify deduplication and removal of result caps. Tests for searchSpotifyArtists remain functionally unchanged.

Changes

Spotify Artist API Test Suite

Layer / File(s) Summary
Test infrastructure and fixtures
packages/shared/src/__tests__/utils/spotify/artistApi.test.ts
Jest imports updated to include lifecycle helpers; response factory functions added for Spotify artist seed, Last.fm similar-artist, and Spotify search mocks; beforeEach/afterEach blocks introduced to preserve and restore LASTFM_API_KEY environment variable.
getSpotifyRelatedArtists test cases
packages/shared/src/__tests__/utils/spotify/artistApi.test.ts
Test cases reworked from single-request "recommendations" shape to sequential three-step fetch chain; new assertions validate successful multi-step resolution, deduplication when different similar-artist names resolve to the same Spotify ID, 403 error graceful handling, empty-result handling, and removal of previous result cap (now expecting 15 unique resolved artists).
searchSpotifyArtists test cases
packages/shared/src/__tests__/utils/spotify/artistApi.test.ts
Existing test behavior for successful searches, empty-query scenarios, 401 error handling, and network rejection handling preserved with only local formatting and wiring adjustments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • LucasSantana-Dev/Lucky#699: Implements the new getSpotifyRelatedArtists multi-call chain logic that these tests validate, including seed lookup, Last.fm integration, and per-name search behavior.

Suggested labels

shared, size/m

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: updating tests for getSpotifyRelatedArtists to accommodate a new Last.fm fallback implementation from PR #648.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/spotify-related-artists-test-update

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

@LucasSantana-Dev
LucasSantana-Dev deleted the branch release/v2.12.0 May 21, 2026 16:27

This branch was successfully deployed

1 active deployment
Preview — dffc0ea6 Deployed May 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant