From 0dca49ab4364962eec68e577880508f1ec802dab Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Mon, 17 Mar 2025 14:46:21 +0000 Subject: [PATCH] fix(player): Check for 0 duration before finalizing listen session with duration-based calculation --- .../PlayerState/PositionalPlayerState.ts | 2 +- src/backend/tests/player/player.test.ts | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/backend/sources/PlayerState/PositionalPlayerState.ts b/src/backend/sources/PlayerState/PositionalPlayerState.ts index 17967e3c..25159df1 100644 --- a/src/backend/sources/PlayerState/PositionalPlayerState.ts +++ b/src/backend/sources/PlayerState/PositionalPlayerState.ts @@ -90,7 +90,7 @@ export class PositionalPlayerState extends AbstractPlayerState { duration, } = {} } = this.currentPlay; - if(duration !== undefined && (duration - this.currentListenRange.end.position) < this.gracefulEndBuffer) { + if(duration !== undefined && duration !== 0 && (duration - this.currentListenRange.end.position) < this.gracefulEndBuffer) { // likely the track was listened to until it ended // but polling interval or network delays caused MS to not get data on the very end // also...within 3 seconds of ending is close enough to call this complete IMO diff --git a/src/backend/tests/player/player.test.ts b/src/backend/tests/player/player.test.ts index ad3dcdd3..609cad21 100644 --- a/src/backend/tests/player/player.test.ts +++ b/src/backend/tests/player/player.test.ts @@ -257,6 +257,29 @@ describe('Player listen ranges', function () { assert.equal(player.getListenDuration(), 7); }); + it('Listened for is track duration invariant', function () { + const player = new TestPositionalPlayerState(logger, [NO_DEVICE, NO_USER]); + + const positioned = clone(newPlay); + positioned.data.duration = 0; + + player.update(testState({play: positioned, position: 3, status: REPORTED_PLAYER_STATUSES.playing})); + + player.currentListenRange.rtPlayer.setPosition(10000); + player.update(testState({play: positioned, position: 10, status: REPORTED_PLAYER_STATUSES.playing}), dayjs().add(10, 'seconds')); + + player.currentListenRange.rtPlayer.setPosition(20000); + player.update(testState({play: positioned, position: 20, status: REPORTED_PLAYER_STATUSES.playing}), dayjs().add(20, 'seconds')); + + const otherPlay = clone(positioned); + otherPlay.data.track = "A New Track"; + player.currentListenRange.rtPlayer.setPosition(30000); + const [currPlay, prevPlay] = player.update(testState({play: otherPlay, position: 2, status: REPORTED_PLAYER_STATUSES.playing}), dayjs().add(30, 'seconds')); + + assert.isDefined(prevPlay); + assert.equal(prevPlay.data.listenedFor, 17); + }); + it('Range ends if position over drifts', function () { const player = new TestPositionalPlayerState(logger, [NO_DEVICE, NO_USER]); -- 2.51.2