1
1
import { accountCopyIndex } from '@algolia/client-account' ;
2
+ import { createRetryablePromise } from '@algolia/client-common' ;
2
3
import { Rule , Synonym } from '@algolia/client-search' ;
3
4
4
5
import { waitResponses } from '../../../../client-common/src/__tests__/helpers' ;
5
6
import { TestSuite } from '../../../../client-common/src/__tests__/TestSuite' ;
6
7
7
8
const testSuite = new TestSuite ( 'account_copy_index' ) ;
8
9
9
- test ( testSuite . testName , async ( ) => {
10
- const source = testSuite . makeIndex ( ) ;
11
- const destination = testSuite
12
- . makeSearchClient ( 'ALGOLIA_APPLICATION_ID_2' , 'ALGOLIA_ADMIN_KEY_2' )
13
- . initIndex ( testSuite . makeIndexName ( ) ) ;
14
-
15
- await expect ( accountCopyIndex ( source , source ) ) . rejects . toEqual ( {
16
- name : 'IndicesInTheSameAppError' ,
17
- message : 'Indices are in the same application. Use SearchClient.copyIndex instead.' ,
18
- appId : source . appId ,
19
- } ) ;
20
-
10
+ describe ( testSuite . testName , ( ) => {
21
11
const rule : Rule = {
22
12
objectID : 'one' ,
23
13
condition : { anchoring : 'is' , pattern : 'pattern' } ,
@@ -32,29 +22,101 @@ test(testSuite.testName, async () => {
32
22
33
23
const synomy : Synonym = { objectID : 'one' , type : 'synonym' , synonyms : [ 'one' , 'two' ] } ;
34
24
35
- await waitResponses ( [
36
- source . saveObject ( { objectID : 'one' } ) ,
37
- source . setSettings ( { searchableAttributes : [ 'objectID' ] } ) ,
38
- source . saveRule ( rule ) ,
39
- source . saveSynonym ( synomy ) ,
40
- ] ) ;
25
+ const setUpSource = async ( source : ReturnType < typeof testSuite . makeIndex > ) => {
26
+ await waitResponses ( [
27
+ source . saveObject ( { objectID : 'one' } ) ,
28
+ source . setSettings ( { searchableAttributes : [ 'objectID' ] } ) ,
29
+ source . saveRule ( rule ) ,
30
+ source . saveSynonym ( synomy ) ,
31
+ ] ) ;
32
+ } ;
33
+
34
+ const waitForIndexCreated = ( destination : ReturnType < typeof testSuite . makeIndex > ) =>
35
+ createRetryablePromise ( async retry => {
36
+ const exists = await destination . exists ( ) ;
37
+
38
+ return exists ? Promise . resolve ( ) : retry ( ) ;
39
+ } ) ;
40
+
41
+ it ( 'copies indices between accounts' , async ( ) => {
42
+ const source = testSuite . makeIndex ( ) ;
43
+ const destination = testSuite
44
+ . makeSearchClient ( 'ALGOLIA_APPLICATION_ID_2' , 'ALGOLIA_ADMIN_KEY_2' )
45
+ . initIndex ( testSuite . makeIndexName ( ) ) ;
46
+
47
+ await setUpSource ( source ) ;
48
+
49
+ await expect ( accountCopyIndex ( source , source ) ) . rejects . toEqual ( {
50
+ name : 'IndicesInTheSameAppError' ,
51
+ message : 'Indices are in the same application. Use SearchClient.copyIndex instead.' ,
52
+ appId : source . appId ,
53
+ } ) ;
54
+
55
+ await expect ( accountCopyIndex ( source , destination ) . wait ( ) ) . resolves . toBeUndefined ( ) ;
56
+
57
+ await expect ( destination . search ( '' ) ) . resolves . toMatchObject ( {
58
+ hits : [ { objectID : 'one' } ] ,
59
+ } ) ;
41
60
42
- await expect ( accountCopyIndex ( source , destination ) . wait ( ) ) . resolves . toBeUndefined ( ) ;
61
+ await expect ( destination . getSettings ( ) ) . resolves . toMatchObject ( {
62
+ searchableAttributes : [ 'objectID' ] ,
63
+ } ) ;
43
64
44
- await expect ( destination . search ( '' ) ) . resolves . toMatchObject ( {
45
- hits : [ { objectID : 'one' } ] ,
65
+ await expect ( destination . getRule ( 'one' ) ) . resolves . toMatchObject ( rule ) ;
66
+
67
+ await expect ( destination . getSynonym ( 'one' ) ) . resolves . toEqual ( synomy ) ;
68
+
69
+ await expect ( accountCopyIndex ( source , destination ) ) . rejects . toEqual ( {
70
+ name : 'DestinationIndiceAlreadyExistsError' ,
71
+ message : 'Destination indice already exists.' ,
72
+ } ) ;
46
73
} ) ;
47
74
48
- await expect ( destination . getSettings ( ) ) . resolves . toMatchObject ( {
49
- searchableAttributes : [ 'objectID' ] ,
75
+ it ( 'it does not need to wait' , async ( ) => {
76
+ const source = testSuite . makeIndex ( ) ;
77
+ const destination = testSuite
78
+ . makeSearchClient ( 'ALGOLIA_APPLICATION_ID_2' , 'ALGOLIA_ADMIN_KEY_2' )
79
+ . initIndex ( testSuite . makeIndexName ( ) ) ;
80
+
81
+ await setUpSource ( source ) ;
82
+
83
+ await expect ( accountCopyIndex ( source , destination ) ) . resolves . toBeUndefined ( ) ;
84
+
85
+ await waitForIndexCreated ( destination ) ;
86
+ await expect ( destination . exists ( ) ) . resolves . toEqual ( true ) ;
50
87
} ) ;
51
88
52
- await expect ( destination . getRule ( 'one' ) ) . resolves . toEqual ( rule ) ;
89
+ it ( 'it bubbles up errors' , async ( ) => {
90
+ const source = testSuite . makeIndex ( ) ;
91
+
92
+ await setUpSource ( source ) ;
93
+
94
+ const indexName = testSuite . makeIndexName ( ) ;
95
+
96
+ const addApiKeyResponse = await testSuite
97
+ . makeSearchClient ( 'ALGOLIA_APPLICATION_ID_2' , 'ALGOLIA_ADMIN_KEY_2' )
98
+ . addApiKey ( [ 'settings' , 'editSettings' , 'search' ] , {
99
+ indexes : [ indexName ] ,
100
+ } )
101
+ . wait ( ) ;
102
+
103
+ const destination = testSuite
104
+ . algoliasearch ( `${ process . env . ALGOLIA_APPLICATION_ID_2 } ` , addApiKeyResponse . key )
105
+ . initIndex ( indexName ) ;
106
+
107
+ await expect ( accountCopyIndex ( source , destination ) . wait ( ) ) . rejects . toMatchObject ( {
108
+ name : 'ApiError' ,
109
+ message : 'Not enough rights to update an object near line:1 column:64' ,
110
+ status : 400 ,
111
+ } ) ;
53
112
54
- await expect ( destination . getSynonym ( 'one' ) ) . resolves . toEqual ( synomy ) ;
113
+ // At this point, we should have created the index. But it should
114
+ // be empty because we only have set settings on it.
115
+ await waitForIndexCreated ( destination ) ;
55
116
56
- await expect ( accountCopyIndex ( source , destination ) ) . rejects . toEqual ( {
57
- name : 'DestinationIndiceAlreadyExistsError' ,
58
- message : 'Destination indice already exists.' ,
117
+ await expect ( destination . search ( '' ) ) . resolves . toMatchObject ( {
118
+ nbHits : 0 ,
119
+ hits : [ ] ,
120
+ } ) ;
59
121
} ) ;
60
122
} ) ;
0 commit comments