1
1
import { MainModel , getTestModelInstance , valid } from '../../test/fixtures/TestModel.js' ;
2
+ import { NotFoundEngineError , NotImplementedError } from './Engine.js' ;
2
3
import FileEngine from './FileEngine.js' ;
3
- import { NotFoundEngineError } from './Engine.js' ;
4
4
import assertions from '../../test/assertions.js' ;
5
5
import fs from 'node:fs/promises' ;
6
6
import stubFs from '../../test/mocks/fs.js' ;
@@ -67,6 +67,21 @@ test('FileEngine.put(model)', async t => {
67
67
} ,
68
68
} ) ) ;
69
69
70
+ assertions . calledWith ( t , filesystem . readFile , '/tmp/fileEngine/MainModel/_search_index_raw.json' ) ;
71
+ assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/MainModel/_search_index_raw.json' , JSON . stringify ( {
72
+ 'MainModel/000000000000' : {
73
+ id : 'MainModel/000000000000' ,
74
+ string : 'String' ,
75
+ } ,
76
+ } ) ) ;
77
+ assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/MainModel/_search_index.json' , JSON . stringify ( {
78
+ version : '2.3.9' ,
79
+ fields : [ 'string' ] ,
80
+ fieldVectors : [ [ 'string/MainModel/000000000000' , [ 0 , 0.288 ] ] ] ,
81
+ invertedIndex : [ [ 'string' , { _index : 0 , string : { 'MainModel/000000000000' : { } } } ] ] ,
82
+ pipeline : [ 'stemmer' ] ,
83
+ } ) ) ;
84
+
70
85
assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/LinkedModel/000000000000.json' , JSON . stringify ( model . linked . toData ( ) ) ) ;
71
86
assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/LinkedModel/111111111111.json' , JSON . stringify ( model . requiredLinked . toData ( ) ) ) ;
72
87
assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/LinkedModel/_index.json' , JSON . stringify ( {
@@ -103,6 +118,45 @@ test('FileEngine.put(model)', async t => {
103
118
} ) ) ;
104
119
} ) ;
105
120
121
+ test ( 'FileEngine.put(model) updates existing search indexes' , async t => {
122
+ const filesystem = stubFs ( {
123
+ 'MainModel/_search_index_raw.json' : {
124
+ 'MainModel/111111111111' : {
125
+ id : 'MainModel/111111111111' ,
126
+ string : 'String' ,
127
+ } ,
128
+ } ,
129
+ } ) ;
130
+
131
+ const model = getTestModelInstance ( valid ) ;
132
+ await t . notThrowsAsync ( ( ) => FileEngine . configure ( {
133
+ path : '/tmp/fileEngine' ,
134
+ filesystem,
135
+ } ) . put ( model ) ) ;
136
+
137
+ assertions . calledWith ( t , filesystem . readFile , '/tmp/fileEngine/MainModel/_search_index_raw.json' ) ;
138
+ assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/MainModel/_search_index_raw.json' , JSON . stringify ( {
139
+ 'MainModel/111111111111' : {
140
+ id : 'MainModel/111111111111' ,
141
+ string : 'String' ,
142
+ } ,
143
+ 'MainModel/000000000000' : {
144
+ id : 'MainModel/000000000000' ,
145
+ string : 'String' ,
146
+ } ,
147
+ } ) ) ;
148
+ assertions . calledWith ( t , filesystem . writeFile , '/tmp/fileEngine/MainModel/_search_index.json' , JSON . stringify ( {
149
+ version : '2.3.9' ,
150
+ fields : [ 'string' ] ,
151
+ fieldVectors : [ [ 'string/MainModel/111111111111' , [ 0 , 0.182 ] ] , [ 'string/MainModel/000000000000' , [ 0 , 0.182 ] ] ] ,
152
+ invertedIndex : [ [ 'string' , {
153
+ _index : 0 ,
154
+ string : { 'MainModel/111111111111' : { } , 'MainModel/000000000000' : { } } ,
155
+ } ] ] ,
156
+ pipeline : [ 'stemmer' ] ,
157
+ } ) ) ;
158
+ } ) ;
159
+
106
160
test ( 'FileEngine.put(model) updates existing indexes' , async t => {
107
161
const filesystem = stubFs ( {
108
162
'MainModel/_index.json' : {
@@ -194,7 +248,7 @@ test('FileEngine.find(MainModel, {string: "test"}) when a matching model does no
194
248
} ) ;
195
249
196
250
test ( 'FileEngine.find(MainModel, {string: "test"}) when no index exists' , async t => {
197
- const filesystem = stubFs ( ) ;
251
+ const filesystem = stubFs ( { } , [ ] ) ;
198
252
199
253
const models = await FileEngine . configure ( {
200
254
path : '/tmp/fileEngine' ,
@@ -204,6 +258,70 @@ test('FileEngine.find(MainModel, {string: "test"}) when no index exists', async
204
258
t . deepEqual ( models , [ ] ) ;
205
259
} ) ;
206
260
261
+ test ( 'FileEngine.search(MainModel, "String") when a matching model exists' , async t => {
262
+ const filesystem = stubFs ( { } , [
263
+ getTestModelInstance ( valid ) ,
264
+ getTestModelInstance ( {
265
+ id : 'MainModel/1111111111111' ,
266
+ string : 'another string' ,
267
+ } ) ,
268
+ ] ) ;
269
+
270
+ const configuration = {
271
+ path : '/tmp/fileEngine' ,
272
+ filesystem,
273
+ } ;
274
+
275
+ const model0 = await FileEngine . configure ( configuration ) . get ( MainModel , 'MainModel/000000000000' ) ;
276
+
277
+ const model1 = await FileEngine . configure ( configuration ) . get ( MainModel , 'MainModel/1111111111111' ) ;
278
+
279
+ const models = await FileEngine . configure ( configuration ) . search ( MainModel , 'String' ) ;
280
+
281
+ t . like ( models , [ {
282
+ ref : 'MainModel/000000000000' ,
283
+ score : 0.211 ,
284
+ model : model0 ,
285
+ } , {
286
+ ref : 'MainModel/1111111111111' ,
287
+ score : 0.16 ,
288
+ model : model1 ,
289
+ } ] ) ;
290
+ } ) ;
291
+
292
+ test ( 'FileEngine.search(MainModel, "not-even-close-to-a-match") when a matching model exists' , async t => {
293
+ const filesystem = stubFs ( { } , [
294
+ getTestModelInstance ( valid ) ,
295
+ getTestModelInstance ( {
296
+ id : 'MainModel/1111111111111' ,
297
+ string : 'another string' ,
298
+ } ) ,
299
+ ] ) ;
300
+
301
+ const configuration = {
302
+ path : '/tmp/fileEngine' ,
303
+ filesystem,
304
+ } ;
305
+
306
+ const models = await FileEngine . configure ( configuration ) . search ( MainModel , 'not-even-close-to-a-match' ) ;
307
+
308
+ t . deepEqual ( models , [ ] ) ;
309
+ } ) ;
310
+
311
+ test ( 'FileEngine.search(MainModel, "String") when no index exists for the model' , async t => {
312
+ const filesystem = stubFs ( { } , [ ] ) ;
313
+
314
+ const configuration = {
315
+ path : '/tmp/fileEngine' ,
316
+ filesystem,
317
+ } ;
318
+
319
+ await t . throwsAsync ( async ( ) => await FileEngine . configure ( configuration ) . search ( MainModel , 'String' ) , {
320
+ instanceOf : NotImplementedError ,
321
+ message : 'The model MainModel does not have a search index available.' ,
322
+ } ) ;
323
+ } ) ;
324
+
207
325
test ( 'FileEngine.hydrate(model)' , async t => {
208
326
const model = getTestModelInstance ( valid ) ;
209
327
0 commit comments