@@ -584,6 +584,148 @@ describe('GitRepository', () => {
584584 } ) ;
585585 } ) ;
586586
587+ describe ( '#findRecent' , ( ) => {
588+ const OTHER_SERVICE = 'other_service' ;
589+ const OTHER_TERMS = 'Privacy Policy' ;
590+
591+ before ( async function ( ) {
592+ this . timeout ( 5000 ) ;
593+
594+ await subject . save ( new Version ( {
595+ serviceId : SERVICE_PROVIDER_ID ,
596+ termsType : TERMS_TYPE ,
597+ content : CONTENT ,
598+ fetchDate : FETCH_DATE_EARLIER ,
599+ snapshotIds : [ SNAPSHOT_ID ] ,
600+ } ) ) ;
601+ await subject . save ( new Version ( {
602+ serviceId : SERVICE_PROVIDER_ID ,
603+ termsType : TERMS_TYPE ,
604+ content : `${ CONTENT } - updated` ,
605+ fetchDate : FETCH_DATE ,
606+ snapshotIds : [ SNAPSHOT_ID ] ,
607+ } ) ) ;
608+ await subject . save ( new Version ( {
609+ serviceId : SERVICE_PROVIDER_ID ,
610+ termsType : OTHER_TERMS ,
611+ content : CONTENT ,
612+ fetchDate : FETCH_DATE_LATER ,
613+ snapshotIds : [ SNAPSHOT_ID ] ,
614+ } ) ) ;
615+ await subject . save ( new Version ( {
616+ serviceId : OTHER_SERVICE ,
617+ termsType : TERMS_TYPE ,
618+ content : CONTENT ,
619+ fetchDate : FETCH_DATE_LATER ,
620+ snapshotIds : [ SNAPSHOT_ID ] ,
621+ } ) ) ;
622+ } ) ;
623+
624+ after ( ( ) => subject . removeAll ( ) ) ;
625+
626+ context ( 'without filters' , ( ) => {
627+ let records ;
628+
629+ before ( async ( ) => {
630+ records = await subject . findRecent ( 10 ) ;
631+ } ) ;
632+
633+ it ( 'returns records in descending chronological order' , ( ) => {
634+ const dates = records . map ( record => record . fetchDate . getTime ( ) ) ;
635+
636+ expect ( dates ) . to . deep . equal ( [ ...dates ] . sort ( ( a , b ) => b - a ) ) ;
637+ } ) ;
638+
639+ it ( 'returns all matching records' , ( ) => {
640+ expect ( records ) . to . have . length ( 4 ) ;
641+ } ) ;
642+
643+ it ( 'does not load content eagerly' , ( ) => {
644+ for ( const record of records ) {
645+ expect ( ( ) => record . content ) . to . throw ( 'Content not defined' ) ;
646+ }
647+ } ) ;
648+
649+ it ( 'exposes the metadata needed for feed entries' , ( ) => {
650+ const [ record ] = records ;
651+
652+ expect ( record . id ) . to . be . a ( 'string' ) ;
653+ expect ( record . serviceId ) . to . be . a ( 'string' ) ;
654+ expect ( record . termsType ) . to . be . a ( 'string' ) ;
655+ expect ( record . fetchDate ) . to . be . an . instanceof ( Date ) ;
656+ expect ( record . isFirstRecord ) . to . be . a ( 'boolean' ) ;
657+ expect ( record . isTechnicalUpgrade ) . to . be . a ( 'boolean' ) ;
658+ } ) ;
659+ } ) ;
660+
661+ context ( 'when limit is smaller than the number of matching records' , ( ) => {
662+ let records ;
663+
664+ before ( async ( ) => {
665+ records = await subject . findRecent ( 2 ) ;
666+ } ) ;
667+
668+ it ( 'returns at most limit records' , ( ) => {
669+ expect ( records ) . to . have . length ( 2 ) ;
670+ } ) ;
671+
672+ it ( 'returns the most recent records' , ( ) => {
673+ for ( const record of records ) {
674+ expect ( record . fetchDate . getTime ( ) ) . to . be . at . least ( FETCH_DATE . getTime ( ) ) ;
675+ }
676+ } ) ;
677+ } ) ;
678+
679+ context ( 'when a serviceId filter is given' , ( ) => {
680+ let records ;
681+
682+ before ( async ( ) => {
683+ records = await subject . findRecent ( 10 , { serviceId : SERVICE_PROVIDER_ID } ) ;
684+ } ) ;
685+
686+ it ( 'returns only records for that service' , ( ) => {
687+ for ( const record of records ) {
688+ expect ( record . serviceId ) . to . equal ( SERVICE_PROVIDER_ID ) ;
689+ }
690+ } ) ;
691+
692+ it ( 'returns all records that match' , ( ) => {
693+ expect ( records ) . to . have . length ( 3 ) ;
694+ } ) ;
695+ } ) ;
696+
697+ context ( 'when both serviceId and termsType filters are given' , ( ) => {
698+ let records ;
699+
700+ before ( async ( ) => {
701+ records = await subject . findRecent ( 10 , { serviceId : SERVICE_PROVIDER_ID , termsType : TERMS_TYPE } ) ;
702+ } ) ;
703+
704+ it ( 'returns only records for that service and terms type' , ( ) => {
705+ for ( const record of records ) {
706+ expect ( record . serviceId ) . to . equal ( SERVICE_PROVIDER_ID ) ;
707+ expect ( record . termsType ) . to . equal ( TERMS_TYPE ) ;
708+ }
709+ } ) ;
710+
711+ it ( 'returns all records that match' , ( ) => {
712+ expect ( records ) . to . have . length ( 2 ) ;
713+ } ) ;
714+ } ) ;
715+
716+ context ( 'when filters match no record' , ( ) => {
717+ let records ;
718+
719+ before ( async ( ) => {
720+ records = await subject . findRecent ( 10 , { serviceId : 'unknown' } ) ;
721+ } ) ;
722+
723+ it ( 'returns an empty array' , ( ) => {
724+ expect ( records ) . to . deep . equal ( [ ] ) ;
725+ } ) ;
726+ } ) ;
727+ } ) ;
728+
587729 describe ( '#findLatest' , ( ) => {
588730 context ( 'when there are records for the given service' , ( ) => {
589731 let lastSnapshotId ;
0 commit comments