@@ -53,6 +53,31 @@ describe('Archivist', function () {
5353
5454 const services = [ 'service·A' , 'Service B!' ] ;
5555
56+ function setupNockForServices ( { serviceA = true , serviceB = true } = { } ) {
57+ nock . cleanAll ( ) ;
58+ if ( serviceA ) {
59+ nock ( 'https://www.servicea.example' )
60+ . get ( '/tos' )
61+ . reply ( 200 , serviceASnapshotExpectedContent , { 'Content-Type' : 'text/html' } ) ;
62+ }
63+ if ( serviceB ) {
64+ nock ( 'https://www.serviceb.example' )
65+ . get ( '/privacy' )
66+ . reply ( 200 , serviceBSnapshotExpectedContent , { 'Content-Type' : 'application/pdf' } ) ;
67+ }
68+ }
69+
70+ async function createAndInitializeArchivist ( ) {
71+ const archivist = new Archivist ( {
72+ recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
73+ fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
74+ } ) ;
75+
76+ await archivist . initialize ( ) ;
77+
78+ return archivist ;
79+ }
80+
5681 before ( async ( ) => {
5782 gitVersion = new Git ( {
5883 path : VERSIONS_PATH ,
@@ -71,13 +96,8 @@ describe('Archivist', function () {
7196
7297 describe ( '#track' , ( ) => {
7398 before ( async ( ) => {
74- nock ( 'https://www.servicea.example' ) . get ( '/tos' ) . reply ( 200 , serviceASnapshotExpectedContent , { 'Content-Type' : 'text/html' } ) ;
75- nock ( 'https://www.serviceb.example' ) . get ( '/privacy' ) . reply ( 200 , serviceBSnapshotExpectedContent , { 'Content-Type' : 'application/pdf' } ) ;
76- app = new Archivist ( {
77- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
78- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
79- } ) ;
80- await app . initialize ( ) ;
99+ setupNockForServices ( ) ;
100+ app = await createAndInitializeArchivist ( ) ;
81101 } ) ;
82102
83103 context ( 'when everything works fine' , ( ) => {
@@ -113,8 +133,7 @@ describe('Archivist', function () {
113133 context ( 'when there is an operational error with service A' , ( ) => {
114134 before ( async ( ) => {
115135 // as there is no more HTTP request mocks for service A, it should throw an `ENOTFOUND` error which is considered as an expected error in our workflow
116- nock . cleanAll ( ) ;
117- nock ( 'https://www.serviceb.example' ) . get ( '/privacy' ) . reply ( 200 , serviceBSnapshotExpectedContent , { 'Content-Type' : 'application/pdf' } ) ;
136+ setupNockForServices ( { serviceA : false , serviceB : true } ) ;
118137 await app . track ( { services } ) ;
119138 } ) ;
120139
@@ -152,14 +171,8 @@ describe('Archivist', function () {
152171 let serviceBCommits ;
153172
154173 before ( async ( ) => {
155- nock ( 'https://www.servicea.example' ) . get ( '/tos' ) . reply ( 200 , serviceASnapshotExpectedContent , { 'Content-Type' : 'text/html' } ) ;
156- nock ( 'https://www.serviceb.example' ) . get ( '/privacy' ) . reply ( 200 , serviceBSnapshotExpectedContent , { 'Content-Type' : 'application/pdf' } ) ;
157- app = new Archivist ( {
158- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
159- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
160- } ) ;
161-
162- await app . initialize ( ) ;
174+ setupNockForServices ( ) ;
175+ app = await createAndInitializeArchivist ( ) ;
163176 await app . track ( { services } ) ;
164177
165178 ( { id : originalSnapshotId } = await app . recorder . snapshotsRepository . findLatest ( SERVICE_A_ID , SERVICE_A_TYPE ) ) ;
@@ -212,14 +225,8 @@ describe('Archivist', function () {
212225 let versionB ;
213226
214227 before ( async ( ) => {
215- nock ( 'https://www.servicea.example' ) . get ( '/tos' ) . reply ( 200 , serviceASnapshotExpectedContent , { 'Content-Type' : 'text/html' } ) ;
216- nock ( 'https://www.serviceb.example' ) . get ( '/privacy' ) . reply ( 200 , serviceBSnapshotExpectedContent , { 'Content-Type' : 'application/pdf' } ) ;
217- app = new Archivist ( {
218- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
219- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
220- } ) ;
221-
222- await app . initialize ( ) ;
228+ setupNockForServices ( ) ;
229+ app = await createAndInitializeArchivist ( ) ;
223230 await app . track ( { services } ) ;
224231 app . services [ SERVICE_A_ID ] . getTerms ( { type : SERVICE_A_TYPE } ) . sourceDocuments [ 0 ] . contentSelectors = 'inexistant-selector' ;
225232 inaccessibleContentSpy = sinon . spy ( ) ;
@@ -279,11 +286,7 @@ describe('Archivist', function () {
279286 . get ( '/community-standards/new-policy/' )
280287 . reply ( 200 , MOCK_CONTENT_4 , { 'Content-Type' : 'text/html' } ) ;
281288
282- app = new Archivist ( {
283- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
284- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
285- } ) ;
286- await app . initialize ( ) ;
289+ app = await createAndInitializeArchivist ( ) ;
287290
288291 let terms = app . services [ SERVICE_ID ] . getTerms ( { type : TERMS_TYPE } ) ;
289292
@@ -362,11 +365,7 @@ describe('Archivist', function () {
362365 . get ( '/community-standards/violence-incitement/' )
363366 . reply ( 200 , MOCK_CONTENT_3 , { 'Content-Type' : 'text/html' } ) ;
364367
365- app = new Archivist ( {
366- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
367- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
368- } ) ;
369- await app . initialize ( ) ;
368+ app = await createAndInitializeArchivist ( ) ;
370369
371370 // Disable executeClientScripts for testing since nock doesn't work with headless browser
372371 let terms = app . services [ SERVICE_ID ] . getTerms ( { type : TERMS_TYPE } ) ;
@@ -438,11 +437,7 @@ describe('Archivist', function () {
438437 . get ( '/community-standards/violence-incitement/' )
439438 . reply ( 200 , MOCK_CONTENT_3 , { 'Content-Type' : 'text/html' } ) ;
440439
441- app = new Archivist ( {
442- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
443- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
444- } ) ;
445- await app . initialize ( ) ;
440+ app = await createAndInitializeArchivist ( ) ;
446441
447442 // Disable executeClientScripts for testing since nock doesn't work with headless browser
448443 let terms = app . services [ SERVICE_ID ] . getTerms ( { type : TERMS_TYPE } ) ;
@@ -502,11 +497,7 @@ describe('Archivist', function () {
502497 . get ( '/community-standards/new-policy/' )
503498 . reply ( 200 , MOCK_CONTENT_4 , { 'Content-Type' : 'text/html' } ) ;
504499
505- app = new Archivist ( {
506- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
507- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
508- } ) ;
509- await app . initialize ( ) ;
500+ app = await createAndInitializeArchivist ( ) ;
510501
511502 // Modify declaration before any tracking
512503 const terms = app . services [ SERVICE_ID ] . getTerms ( { type : TERMS_TYPE } ) ;
@@ -552,11 +543,7 @@ describe('Archivist', function () {
552543 const retryableError = new FetchDocumentError ( FetchDocumentError . LIKELY_TRANSIENT_ERRORS [ 0 ] ) ;
553544
554545 before ( async ( ) => {
555- app = new Archivist ( {
556- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
557- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
558- } ) ;
559- await app . initialize ( ) ;
546+ app = await createAndInitializeArchivist ( ) ;
560547 } ) ;
561548
562549 beforeEach ( ( ) => {
@@ -641,11 +628,7 @@ describe('Archivist', function () {
641628
642629 describe ( '#attach' , ( ) => {
643630 before ( async ( ) => {
644- app = new Archivist ( {
645- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
646- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
647- } ) ;
648- await app . initialize ( ) ;
631+ app = await createAndInitializeArchivist ( ) ;
649632
650633 EVENTS . forEach ( event => {
651634 const handlerName = `on${ event [ 0 ] . toUpperCase ( ) } ${ event . substring ( 1 ) } ` ;
@@ -674,14 +657,9 @@ describe('Archivist', function () {
674657 let plugin ;
675658
676659 before ( async ( ) => {
677- nock . cleanAll ( ) ;
678- nock ( 'https://www.servicea.example' ) . get ( '/tos' ) . reply ( 200 , serviceASnapshotExpectedContent , { 'Content-Type' : 'text/html' } ) ;
660+ setupNockForServices ( { serviceA : true , serviceB : false } ) ;
679661
680- app = new Archivist ( {
681- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
682- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
683- } ) ;
684- await app . initialize ( ) ;
662+ app = await createAndInitializeArchivist ( ) ;
685663
686664 plugin = { onFirstVersionRecorded : ( ) => { throw new Error ( 'Plugin error' ) ; } } ;
687665
@@ -728,11 +706,7 @@ describe('Archivist', function () {
728706 }
729707
730708 before ( async ( ) => {
731- app = new Archivist ( {
732- recorderConfig : config . get ( '@opentermsarchive/engine.recorder' ) ,
733- fetcherConfig : config . get ( '@opentermsarchive/engine.fetcher' ) ,
734- } ) ;
735- await app . initialize ( ) ;
709+ app = await createAndInitializeArchivist ( ) ;
736710
737711 EVENTS . forEach ( event => {
738712 const handlerName = `on${ event [ 0 ] . toUpperCase ( ) } ${ event . substr ( 1 ) } ` ;
0 commit comments