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
43 changes: 43 additions & 0 deletions packages/bot/src/handlers/player/trackHandlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
lastPlayedTracks,
recentlyPlayedTracks,
setupTrackHandlers,
getRecentSkipCount,
} from './trackHandlers'

const QueueRepeatMode = {
Expand Down Expand Up @@ -569,6 +570,48 @@ describe('trackHandlers autoplay replenishment', () => {
)
})

it('increments getRecentSkipCount on early skip and resets on track completion', async () => {
jest.useFakeTimers()
const handlers = setupHandlers()
const queue = {
...createQueue(QueueRepeatMode.AUTOPLAY),
guild: { id: 'guild-skip-count', name: 'Skip Count Guild' },
} as unknown as GuildQueue
const track = { ...createTrack('skip-count-user'), durationMS: 100000 }

await handlers.playerStart(queue, track)
jest.advanceTimersByTime(10000) // 10% through — early skip
await handlers.playerSkip(queue, track)
expect(getRecentSkipCount(queue.guild.id)).toBe(1)

// Another early skip increments further
await handlers.playerStart(queue, track)
jest.advanceTimersByTime(10000)
await handlers.playerSkip(queue, track)
expect(getRecentSkipCount(queue.guild.id)).toBe(2)

// Completing a track (>80%) resets the counter
await handlers.playerStart(queue, track)
jest.advanceTimersByTime(90000)
await handlers.playerFinish(queue, track)
expect(getRecentSkipCount(queue.guild.id)).toBe(0)
})

it('does not increment getRecentSkipCount when skip is after 30% of track', async () => {
jest.useFakeTimers()
const handlers = setupHandlers()
const queue = {
...createQueue(QueueRepeatMode.AUTOPLAY),
guild: { id: 'guild-skip-late', name: 'Skip Late Guild' },
} as unknown as GuildQueue
const track = { ...createTrack('skip-late-user'), durationMS: 100000 }

await handlers.playerStart(queue, track)
jest.advanceTimersByTime(50000) // 50% through — late skip
await handlers.playerSkip(queue, track)
expect(getRecentSkipCount(queue.guild.id)).toBe(0)
})

it('does not record feedback on playerSkip when track < 20 seconds duration', async () => {
jest.useFakeTimers()
const handlers = setupHandlers()
Expand Down
13 changes: 13 additions & 0 deletions packages/bot/src/handlers/player/trackHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const guildTrackStartTimes = new LRUCache<string, number>({
updateAgeOnGet: true,
})

const guildRecentSkipCounts = new LRUCache<string, number>({
max: MAX_GUILD_ENTRIES,
ttl: TRACK_STATE_TTL_MS,
updateAgeOnGet: true,
})
Comment thread
greptile-apps[bot] marked this conversation as resolved.

export function getRecentSkipCount(guildId: string): number {
return guildRecentSkipCounts.get(guildId) ?? 0
}

export type TrackHistoryEntry = {
url: string
title: string
Expand Down Expand Up @@ -278,6 +288,7 @@ const handlePlayerFinish = async (
(Date.now() - startTime) / track.durationMS
if (completionRatio > 0.8) {
await recordImplicitTrackFeedback(track, 'implicit_like')
guildRecentSkipCounts.delete(queue.guild.id)
}
}
guildTrackStartTimes.delete(queue.guild.id)
Expand Down Expand Up @@ -317,6 +328,8 @@ const handlePlayerSkip = async (
const skipRatio = (Date.now() - startTime) / track.durationMS
if (skipRatio < 0.3) {
await recordImplicitTrackFeedback(track, 'implicit_dislike')
const current = guildRecentSkipCounts.get(queue.guild.id) ?? 0
guildRecentSkipCounts.set(queue.guild.id, current + 1)
}
}
guildTrackStartTimes.delete(queue.guild.id)
Expand Down
3 changes: 2 additions & 1 deletion packages/bot/src/utils/music/autoplay/replenisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getArtistPopularity,
} from '../../../spotify/spotifyApi'
import { detectSessionMood, type SessionMood } from './sessionMood'
import { getRecentSkipCount } from '../../../handlers/player/trackHandlers'
Comment thread
greptile-apps[bot] marked this conversation as resolved.
import {
collectRecommendationCandidates,
SERTANEJO_TAGS,
Expand Down Expand Up @@ -116,7 +117,7 @@ async function _replenishQueue(
Math.abs(allHistoryTracks.length - guildMoodCache.historyLen) < 3
? guildMoodCache.mood
: (() => {
const mood = detectSessionMood(allHistoryTracks, 0 /* TODO: wire recentSkipCount from skip tracking */)
const mood = detectSessionMood(allHistoryTracks, getRecentSkipCount(replenishGuildId))
sessionMoodCache.set(replenishGuildId, {
mood,
historyLen: allHistoryTracks.length,
Expand Down
Loading