Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion packages/bot/src/utils/music/queueManipulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,7 @@ describe('queueManipulation — multi-user VC blend', () => {

const addedTrack = queue.addTrack.mock.calls[0]?.[0] as Track
expect(addedTrack?.metadata?.recommendationReason).toContain(
'spotify mood match',
'spotify preferred',
)
})

Expand Down Expand Up @@ -3236,3 +3236,85 @@ describe('queueManipulation — within-cycle dedup via extractSongCore', () => {
)
})
})

describe('queueManipulation — Spotify priority', () => {
beforeEach(() => {
likedTrackWeightsMock.mockResolvedValue(new Map())
dislikedTrackWeightsMock.mockResolvedValue(new Map())
getPreferredArtistKeysMock.mockResolvedValue(new Set())
getBlockedArtistKeysMock.mockResolvedValue(new Set())
getImplicitDislikeKeysMock.mockResolvedValue(new Set())
getImplicitLikeKeysMock.mockResolvedValue(new Set())
consumeLastFmSeedSliceMock.mockResolvedValue([])
getSimilarTracksMock.mockResolvedValue([])
getTrackHistoryMock.mockResolvedValue([])
getTagTopTracksMock.mockResolvedValue([])
getGuildSettingsMock.mockResolvedValue({ autoplayMode: 'similar' })
})

it('uses song-core query for Spotify engine when seed has artist-song format', async () => {
const searchMock = jest
.fn()
.mockResolvedValue({ tracks: [{ title: 'Shape of You', author: 'Ed Sheeran', url: 'https://open.spotify.com/track/abc', source: 'spotify', durationMS: 234000 }] })

const queue = createQueueMock({
currentTrack: {
title: 'Ed Sheeran - Shape of You',
author: 'Ed SheeranVEVO',
url: 'https://youtube.com/watch?v=seed001',
requestedBy: { id: 'user-1' },
} as unknown as Track,
metadata: { requestedBy: { id: 'user-1' } },
tracks: { size: 0, toArray: jest.fn().mockReturnValue([]) },
player: { search: searchMock },
})

await replenishQueue(queue as unknown as GuildQueue)

const firstCallQuery: string = searchMock.mock.calls[0]?.[0] ?? ''
expect(firstCallQuery).not.toContain('Ed Sheeran - Shape of You Ed Sheeran')
expect(firstCallQuery).toContain('Shape of You')
expect(firstCallQuery).toContain('Ed Sheeran')
})

it('prefers Spotify candidate over YouTube candidate of the same song', async () => {
const addedTracks: unknown[] = []
const ytTrack = {
title: 'Halo',
author: 'Beyoncé',
url: 'https://youtube.com/watch?v=haloyt',
source: 'youtube',
durationMS: 241000,
}
const spotifyTrack = {
title: 'Halo',
author: 'Beyoncé',
url: 'https://open.spotify.com/track/halo001',
source: 'spotify',
durationMS: 241000,
}

const queue = createQueueMock({
currentTrack: {
title: 'Crazy In Love',
author: 'Beyoncé',
url: 'https://youtube.com/watch?v=seed001',
source: 'youtube',
requestedBy: { id: 'user-1' },
} as unknown as Track,
metadata: { requestedBy: { id: 'user-1' } },
tracks: { size: 7, toArray: jest.fn().mockReturnValue([]) },
addTrack: jest.fn((t: unknown) => addedTracks.push(t)),
player: {
search: jest.fn().mockResolvedValue({
tracks: [ytTrack, spotifyTrack],
}),
},
})

await replenishQueue(queue as unknown as GuildQueue)

const selected = addedTracks[0] as { source?: string }
expect(selected?.source).toBe('spotify')
})
})
30 changes: 22 additions & 8 deletions packages/bot/src/utils/music/queueManipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,26 @@ async function searchSeedCandidates(
const modifier = QUERY_MODIFIERS[replenishCount % QUERY_MODIFIERS.length]
const query = modifier ? `${baseQuery} ${modifier}` : baseQuery

// Build a cleaner query for Spotify by separating song from artist,
// avoiding duplicates like "Beyoncé - Halo Beyoncé" → "Halo Beyoncé".
const songCore = extractSongCore(seed.title, seed.author)
const cleanedAuthor = cleanAuthor(seed.author)
const spotifyBase = songCore
? `${songCore} ${cleanedAuthor}`.trim()
: baseQuery
const spotifyQuery = modifier ? `${spotifyBase} ${modifier}` : spotifyBase

const engines: QueryType[] = [
QueryType.SPOTIFY_SEARCH,
QueryType.YOUTUBE_SEARCH,
QueryType.AUTO,
]

for (const [idx, engine] of engines.entries()) {
const engineQuery =
engine === QueryType.SPOTIFY_SEARCH ? spotifyQuery : query
try {
const searchResult = await queue.player.search(query, {
const searchResult = await queue.player.search(engineQuery, {
requestedBy: requestedBy ?? undefined,
searchEngine: engine,
})
Expand All @@ -805,15 +816,15 @@ async function searchSeedCandidates(
warnLog({
message:
'Autoplay: primary search returned no results, using fallback engine',
data: { engine, query },
data: { engine, query: engineQuery },
})
}
return tracks
}
} catch (error) {
debugLog({
message: 'Search failed for seed, trying next engine',
data: { query, engine, error: String(error) },
data: { query: engineQuery, engine, error: String(error) },
})
}
}
Expand Down Expand Up @@ -1556,9 +1567,12 @@ function calculateRecommendationScore(
score += 0.15
reasons.push('session novelty')
}
if (candidate.source === currentTrack.source) {
if (
candidate.source === currentTrack.source &&
candidate.source !== 'spotify'
) {
score -= 0.25
} else if (candidate.source) {
} else if (candidate.source && candidate.source !== currentTrack.source) {
reasons.push('source variety')
}
const tokenScore = sharedTitleTokenScore(
Expand Down Expand Up @@ -1613,9 +1627,9 @@ function calculateRecommendationScore(
}
}

if (candidate.source === 'spotify' && currentTrack.source === 'spotify') {
score += 0.08
reasons.push('spotify mood match')
if (candidate.source === 'spotify') {
score += 0.15
reasons.push('spotify preferred')
}

if (autoplayMode === 'discover') {
Expand Down
Loading