@@ -10,6 +10,9 @@ import {
1010 getUpdatedDownloadCenterConfig ,
1111 createAndPublishDownloadCenterConfig ,
1212 createJsonFeedEntry ,
13+ updateJsonFeedCTA ,
14+ UpdateCTAConfig ,
15+ JsonFeed ,
1316} from './config' ;
1417import { promises as fs } from 'fs' ;
1518import path from 'path' ;
@@ -529,4 +532,225 @@ describe('DownloadCenter config', function () {
529532 expect ( serverTargets ) . to . include ( target ) ;
530533 } ) ;
531534 } ) ;
535+
536+ describe ( 'updateJsonFeedCTA' , function ( ) {
537+ let dlCenter : any ;
538+ let uploadConfig : sinon . SinonStub ;
539+ let downloadConfig : sinon . SinonStub ;
540+ let uploadAsset : sinon . SinonStub ;
541+ let downloadAsset : sinon . SinonStub ;
542+
543+ const existingUploadedJsonFeed = require ( path . resolve (
544+ __dirname ,
545+ '..' ,
546+ '..' ,
547+ 'test' ,
548+ 'fixtures' ,
549+ 'cta-versions.json'
550+ ) ) as JsonFeed ;
551+
552+ const getConfig = ( ctas : UpdateCTAConfig [ 'ctas' ] ) : UpdateCTAConfig => {
553+ return {
554+ ctas,
555+ isDryRun : false ,
556+ awsAccessKeyId : 'accessKey' ,
557+ awsSecretAccessKey : 'secretKey' ,
558+ } ;
559+ } ;
560+
561+ const getUploadedJsonFeed = ( ) : JsonFeed => {
562+ return JSON . parse ( uploadAsset . lastCall . args [ 1 ] ) as JsonFeed ;
563+ } ;
564+
565+ beforeEach ( function ( ) {
566+ uploadConfig = sinon . stub ( ) ;
567+ downloadConfig = sinon . stub ( ) ;
568+ uploadAsset = sinon . stub ( ) ;
569+ downloadAsset = sinon . stub ( ) ;
570+ dlCenter = sinon . stub ( ) ;
571+
572+ downloadAsset . returns ( JSON . stringify ( existingUploadedJsonFeed ) ) ;
573+
574+ dlCenter . returns ( {
575+ downloadConfig,
576+ uploadConfig,
577+ uploadAsset,
578+ downloadAsset,
579+ } ) ;
580+ } ) ;
581+
582+ for ( let dryRun of [ false , true ] ) {
583+ it ( `when dryRun is ${ dryRun } , does ${
584+ dryRun ? 'not ' : ''
585+ } upload the updated json feed`, async function ( ) {
586+ const config = getConfig ( {
587+ '1.10.3' : {
588+ chunks : [ { text : 'Foo' } ] ,
589+ } ,
590+ '*' : {
591+ chunks : [ { text : 'Bar' } ] ,
592+ } ,
593+ } ) ;
594+
595+ config . isDryRun = dryRun ;
596+
597+ await updateJsonFeedCTA ( config , dlCenter ) ;
598+ if ( dryRun ) {
599+ expect ( uploadAsset ) . to . not . have . been . called ;
600+ } else {
601+ expect ( uploadAsset ) . to . have . been . called ;
602+
603+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
604+ expect ( updatedJsonFeed . cta ?. chunks ) . to . deep . equal ( [ { text : 'Bar' } ] ) ;
605+ expect (
606+ updatedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.3' ) [ 0 ]
607+ . cta ?. chunks
608+ ) . to . deep . equal ( [ { text : 'Foo' } ] ) ;
609+ expect (
610+ updatedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.4' ) [ 0 ]
611+ . cta
612+ ) . to . be . undefined ;
613+ }
614+ } ) ;
615+ }
616+
617+ it ( 'cannot add new versions' , async function ( ) {
618+ expect (
619+ existingUploadedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.5' )
620+ ) . to . have . lengthOf ( 0 ) ;
621+
622+ const config = getConfig ( {
623+ '1.10.5' : {
624+ chunks : [ { text : 'Foo' } ] ,
625+ } ,
626+ } ) ;
627+
628+ await updateJsonFeedCTA ( config , dlCenter ) ;
629+
630+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
631+
632+ expect (
633+ updatedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.5' )
634+ ) . to . have . lengthOf ( 0 ) ;
635+ } ) ;
636+
637+ it ( 'can remove global cta' , async function ( ) {
638+ // Preserve existing CTAs, but omit the global one
639+ const ctas = ( existingUploadedJsonFeed . versions as any [ ] ) . reduce (
640+ ( acc , current ) => {
641+ acc [ current . version ] = current . cta ;
642+ return acc ;
643+ } ,
644+ { }
645+ ) ;
646+ const config = getConfig ( ctas ) ;
647+
648+ expect ( config . ctas [ '*' ] ) . to . be . undefined ;
649+ await updateJsonFeedCTA ( config , dlCenter ) ;
650+
651+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
652+
653+ expect ( updatedJsonFeed . cta ) . to . be . undefined ;
654+ } ) ;
655+
656+ it ( 'can remove version specific cta' , async function ( ) {
657+ expect (
658+ existingUploadedJsonFeed . versions . map ( ( v ) => v . cta ) . filter ( ( cta ) => cta )
659+ ) . to . have . length . greaterThan ( 0 ) ;
660+
661+ const config = getConfig ( {
662+ '*' : existingUploadedJsonFeed . cta ! ,
663+ } ) ;
664+
665+ await updateJsonFeedCTA ( config , dlCenter ) ;
666+
667+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
668+ expect ( updatedJsonFeed . cta ) . to . not . be . undefined ;
669+ expect (
670+ updatedJsonFeed . versions . map ( ( v ) => v . cta ) . filter ( ( cta ) => cta )
671+ ) . to . have . lengthOf ( 0 ) ;
672+ } ) ;
673+
674+ it ( 'can update global cta' , async function ( ) {
675+ const config = getConfig ( {
676+ '*' : {
677+ chunks : [ { text : "It's a beautiful day" , style : 'imagePositive' } ] ,
678+ } ,
679+ } ) ;
680+
681+ await updateJsonFeedCTA ( config , dlCenter ) ;
682+
683+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
684+
685+ expect ( updatedJsonFeed . cta ) . to . deep . equal ( {
686+ chunks : [ { text : "It's a beautiful day" , style : 'imagePositive' } ] ,
687+ } ) ;
688+ } ) ;
689+
690+ it ( 'can update version-specific cta' , async function ( ) {
691+ const config = getConfig ( {
692+ '1.10.3' : {
693+ chunks : [ { text : "It's a beautiful day" , style : 'imagePositive' } ] ,
694+ } ,
695+ } ) ;
696+
697+ await updateJsonFeedCTA ( config , dlCenter ) ;
698+
699+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
700+
701+ expect (
702+ updatedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.3' ) [ 0 ] . cta
703+ ) . to . deep . equal ( {
704+ chunks : [ { text : "It's a beautiful day" , style : 'imagePositive' } ] ,
705+ } ) ;
706+ } ) ;
707+
708+ it ( 'can add global cta' , async function ( ) {
709+ // Remove the existing cta
710+ existingUploadedJsonFeed . cta = undefined ;
711+
712+ const config = getConfig ( {
713+ '*' : {
714+ chunks : [
715+ { text : 'Go outside and enjoy the sun' , style : 'imagePositive' } ,
716+ ] ,
717+ } ,
718+ } ) ;
719+
720+ await updateJsonFeedCTA ( config , dlCenter ) ;
721+
722+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
723+
724+ expect ( updatedJsonFeed . cta ) . to . deep . equal ( {
725+ chunks : [
726+ { text : 'Go outside and enjoy the sun' , style : 'imagePositive' } ,
727+ ] ,
728+ } ) ;
729+ } ) ;
730+
731+ it ( 'can add version-specific cta' , async function ( ) {
732+ // Remove the existing cta
733+ existingUploadedJsonFeed . cta = undefined ;
734+
735+ const config = getConfig ( {
736+ '1.10.4' : {
737+ chunks : [
738+ { text : 'Go outside and enjoy the sun' , style : 'imagePositive' } ,
739+ ] ,
740+ } ,
741+ } ) ;
742+
743+ await updateJsonFeedCTA ( config , dlCenter ) ;
744+
745+ const updatedJsonFeed = getUploadedJsonFeed ( ) ;
746+
747+ expect (
748+ updatedJsonFeed . versions . filter ( ( v ) => v . version === '1.10.4' ) [ 0 ] . cta
749+ ) . to . deep . equal ( {
750+ chunks : [
751+ { text : 'Go outside and enjoy the sun' , style : 'imagePositive' } ,
752+ ] ,
753+ } ) ;
754+ } ) ;
755+ } ) ;
532756} ) ;
0 commit comments