@@ -42,6 +42,28 @@ function setUpMockResponses(mockResponses) {
4242 } ) ;
4343}
4444
45+ // Checks that a set of messages contains the correct data format for the database
46+ function checkPlaylistsUploadedToDB ( messages , input ) {
47+ messages . forEach ( ( message ) => {
48+ const data = message [ 0 ] . data ;
49+
50+ expect ( message . length ) . to . be ( 1 ) ;
51+
52+ expect ( data . key ) . to . be ( input . playlistId ) ;
53+ expect ( Object . keys ( data . val ) ) . to . contain ( 'lastUpdatedDBAt' ) ;
54+ expect ( data . val . lastUpdatedDBAt . length ) . to . be ( 24 ) ;
55+ expect ( Object . keys ( data . val ) ) . to . contain ( 'lastVideoPublishedAt' ) ;
56+ expect ( data . val . lastVideoPublishedAt . length ) . to . be ( 20 ) ;
57+ expect ( Object . keys ( data . val ) ) . to . contain ( 'videos' ) ;
58+ expect ( Object . keys ( data . val . videos ) . length ) . to . be . greaterThan ( 0 ) ;
59+ // Check the format of the videos
60+ for ( const [ videoId , publishTime ] of Object . entries ( data . val . videos ) ) {
61+ expect ( videoId . length ) . to . be ( 11 ) ;
62+ expect ( publishTime . length ) . to . be ( 10 ) ;
63+ }
64+ } ) ;
65+ }
66+
4567describe ( 'shuffleVideo' , function ( ) {
4668
4769 beforeEach ( function ( ) {
@@ -481,7 +503,8 @@ describe('shuffleVideo', function () {
481503 if ( videoId . startsWith ( 'DEL_LOC' ) ) {
482504 delete allVideos [ videoId ] ;
483505 } else {
484- allVideos [ videoId ] = publishTime ;
506+ // The YT API returns the publishTime in ISO 8601 format, so we need to convert it, as the localStorage and DB use a different format
507+ allVideos [ videoId ] = new Date ( publishTime ) . toISOString ( ) . slice ( 0 , 19 ) + 'Z' ;
485508 }
486509 }
487510 allVideos = Object . fromEntries ( Object . entries ( allVideos ) . sort ( ( a , b ) => b [ 1 ] . localeCompare ( a [ 1 ] ) ) ) ;
@@ -856,7 +879,8 @@ describe('shuffleVideo', function () {
856879 if ( videoId . startsWith ( 'DEL_LOC' ) ) {
857880 delete allVideos [ videoId ] ;
858881 } else {
859- allVideos [ videoId ] = publishTime ;
882+ // The YT API returns the publishTime in ISO 8601 format, so we need to convert it, as the localStorage and DB use a different format
883+ allVideos [ videoId ] = new Date ( publishTime ) . toISOString ( ) . slice ( 0 , 19 ) + 'Z' ;
860884 }
861885 }
862886 allVideos = Object . fromEntries ( Object . entries ( allVideos ) . sort ( ( a , b ) => b [ 1 ] . localeCompare ( a [ 1 ] ) ) ) ;
@@ -963,7 +987,8 @@ describe('shuffleVideo', function () {
963987 it ( 'should only interact with the database to remove deleted videos' , async function ( ) {
964988 await chooseRandomVideo ( input . channelId , false , domElement ) ;
965989
966- const commands = chrome . runtime . sendMessage . args . map ( arg => arg [ 0 ] . command ) ;
990+ const messages = chrome . runtime . sendMessage . args ;
991+ const commands = messages . map ( arg => arg [ 0 ] . command ) ;
967992
968993 // callCount is 3 if we didn't choose a deleted video, 4 else
969994 expect ( chrome . runtime . sendMessage . callCount ) . to . be . within ( 3 , 4 ) ;
@@ -974,6 +999,9 @@ describe('shuffleVideo', function () {
974999
9751000 if ( chrome . runtime . sendMessage . callCount === 4 ) {
9761001 expect ( commands ) . to . contain ( 'overwritePlaylistInfoInDB' ) ;
1002+ // One entry should contain a valid DB update
1003+ const overwriteMessages = messages . filter ( arg => arg [ 0 ] . command === 'overwritePlaylistInfoInDB' ) ;
1004+ checkPlaylistsUploadedToDB ( overwriteMessages , input ) ;
9771005 }
9781006
9791007 } ) ;
@@ -982,7 +1010,8 @@ describe('shuffleVideo', function () {
9821010 await chooseRandomVideo ( input . channelId , false , domElement ) ;
9831011 const playlistInfoAfter = await getKeyFromLocalStorage ( input . playlistId ) ;
9841012
985- const commands = chrome . runtime . sendMessage . args . map ( arg => arg [ 0 ] . command ) ;
1013+ const messages = chrome . runtime . sendMessage . args ;
1014+ const commands = messages . map ( arg => arg [ 0 ] . command ) ;
9861015
9871016 if ( needsYTAPIInteraction ( input ) ) {
9881017 expect ( chrome . runtime . sendMessage . callCount ) . to . be ( 6 ) ;
@@ -1008,14 +1037,21 @@ describe('shuffleVideo', function () {
10081037 break ;
10091038 case 5 :
10101039 expect ( commands ) . to . contain ( 'overwritePlaylistInfoInDB' ) ;
1040+ const overwriteMessages = messages . filter ( arg => arg [ 0 ] . command === 'overwritePlaylistInfoInDB' ) ;
1041+ checkPlaylistsUploadedToDB ( overwriteMessages , input ) ;
1042+
10111043 expect ( numDeletedVideosBefore ) . to . be . greaterThan ( numDeletedVideosAfter ) ;
10121044 break ;
10131045 case 6 :
10141046 expect ( commands ) . to . contain ( 'getAPIKey' ) ;
10151047 if ( numDeletedVideosBefore > numDeletedVideosAfter ) {
10161048 expect ( commands ) . to . contain ( 'overwritePlaylistInfoInDB' ) ;
1049+ const overwriteMessages = messages . filter ( arg => arg [ 0 ] . command === 'overwritePlaylistInfoInDB' ) ;
1050+ checkPlaylistsUploadedToDB ( overwriteMessages , input ) ;
10171051 } else {
10181052 expect ( commands ) . to . contain ( 'updatePlaylistInfoInDB' ) ;
1053+ const updateMessages = messages . filter ( arg => arg [ 0 ] . command === 'updatePlaylistInfoInDB' ) ;
1054+ checkPlaylistsUploadedToDB ( updateMessages , input ) ;
10191055 }
10201056 break ;
10211057 default :
@@ -1027,7 +1063,8 @@ describe('shuffleVideo', function () {
10271063 await chooseRandomVideo ( input . channelId , false , domElement ) ;
10281064 const playlistInfoAfter = await getKeyFromLocalStorage ( input . playlistId ) ;
10291065
1030- const commands = chrome . runtime . sendMessage . args . map ( arg => arg [ 0 ] . command ) ;
1066+ const messages = chrome . runtime . sendMessage . args ;
1067+ const commands = messages . map ( arg => arg [ 0 ] . command ) ;
10311068
10321069 expect ( commands ) . to . contain ( 'connectionTest' ) ;
10331070 expect ( commands ) . to . contain ( 'getPlaylistFromDB' ) ;
@@ -1041,6 +1078,8 @@ describe('shuffleVideo', function () {
10411078 expect ( chrome . runtime . sendMessage . callCount ) . to . be ( 6 ) ;
10421079 expect ( commands ) . to . contain ( 'getAPIKey' ) ;
10431080 expect ( commands ) . to . contain ( 'updatePlaylistInfoInDB' ) ;
1081+ const updateMessages = messages . filter ( arg => arg [ 0 ] . command === 'updatePlaylistInfoInDB' ) ;
1082+ checkPlaylistsUploadedToDB ( updateMessages , input ) ;
10441083 } else {
10451084 expect ( chrome . runtime . sendMessage . callCount ) . to . be ( 4 ) ;
10461085 }
@@ -1053,7 +1092,8 @@ describe('shuffleVideo', function () {
10531092 it ( 'should only fetch data from the database' , async function ( ) {
10541093 await chooseRandomVideo ( input . channelId , false , domElement ) ;
10551094
1056- const commands = chrome . runtime . sendMessage . args . map ( arg => arg [ 0 ] . command ) ;
1095+ const messages = chrome . runtime . sendMessage . args ;
1096+ const commands = messages . map ( arg => arg [ 0 ] . command ) ;
10571097
10581098 // 4 because we only need to fetch from the DB
10591099 expect ( chrome . runtime . sendMessage . callCount ) . to . be ( 4 ) ;
@@ -1068,7 +1108,8 @@ describe('shuffleVideo', function () {
10681108 it ( 'should update the database' , async function ( ) {
10691109 await chooseRandomVideo ( input . channelId , false , domElement ) ;
10701110
1071- const commands = chrome . runtime . sendMessage . args . map ( arg => arg [ 0 ] . command ) ;
1111+ const messages = chrome . runtime . sendMessage . args ;
1112+ const commands = messages . map ( arg => arg [ 0 ] . command ) ;
10721113
10731114 // 6 because we need to fetch from the DB, fetch from the YT API, and update the DB
10741115 expect ( chrome . runtime . sendMessage . callCount ) . to . be ( 6 ) ;
@@ -1079,6 +1120,8 @@ describe('shuffleVideo', function () {
10791120 expect ( commands ) . to . contain ( 'getCurrentTabId' ) ;
10801121 expect ( commands ) . to . contain ( 'getAPIKey' ) ;
10811122 expect ( commands ) . to . contain ( 'updatePlaylistInfoInDB' ) ;
1123+ const updateMessages = messages . filter ( arg => arg [ 0 ] . command === 'updatePlaylistInfoInDB' ) ;
1124+ checkPlaylistsUploadedToDB ( updateMessages , input ) ;
10821125 } ) ;
10831126 }
10841127 } ) ;
0 commit comments