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
2 changes: 1 addition & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class MediaSession {
private mediaSessionStartTimestamp = Date.now(); //Timestamp created on logMediaSessionStart event
private mediaSessionEndTimestamp = Date.now(); //Timestamp updated when any event is loggged
private mediaTimeSpent() {
return this.mediaSessionEndTimestamp - this.mediaSessionStartTimestamp;
return Date.now() - this.mediaSessionStartTimestamp;
}
private currentPlaybackStartTimestamp?: number; //Timestamp for beginning of current playback
private storedPlaybackTime = 0; //On Pause calculate playback time and clear currentPlaybackTime
Expand Down
25 changes: 25 additions & 0 deletions test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,4 +1463,29 @@ describe('MediaSession', () => {
});
});
});

describe('Media Session Attributes', () => {
it('should get mediaTimeSpent based on current time', async () => {
const options = {
customAttributes: {
content_rating: 'epic',
},
currentPlayheadPosition: 0,
};

// logPlay is triggered to start media content time tracking.
mpMedia.logPlay(options);
// 100ms delay added to account for the time spent on media content.
await new Promise(f => setTimeout(f, 100));
mpMedia.logPause(options);
// Another 100ms delay added after logPause is triggered to account for time spent on media session (total = +200ms).
await new Promise(f => setTimeout(f, 100));

const mpMediaTimeSpent = mpMedia['mediaTimeSpent']();
// the mediaTimeSpent varies in value each test run by a millisecond or two (i,e value is could be 200ms, 201ms, 202ms)
// and we can't determine the exact value, hence the greaterThanOrEqual and lessThanOrEqual tests.
expect(mpMediaTimeSpent).to.greaterThanOrEqual(200);
expect(mpMediaTimeSpent).to.lessThanOrEqual(300);
Comment on lines +1487 to +1488
Copy link
Member

Choose a reason for hiding this comment

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

This is fine for now. But I made a ticket to use jest timers in the future.

});
});
});
Loading