@@ -11,28 +11,33 @@ import { ROOT_DIR } from "../../accuracy/sdk/constants.js";
11
11
import { timeout } from "../../integration/helpers.js" ;
12
12
import { EJSON , EJSONOptions } from "bson" ;
13
13
14
- const dummySessionId = "1FOO" ;
15
- const dummyExportsPath = path . join ( ROOT_DIR , "tests" , "tmp" , "exports" ) ;
16
- const dummySessionExportPath = path . join ( dummyExportsPath , dummySessionId ) ;
14
+ const exportsPath = path . join ( ROOT_DIR , "tests" , "tmp" , "exports" ) ;
17
15
const exportsManagerConfig : SessionExportsManagerConfig = {
18
- exportPath : dummyExportsPath ,
16
+ exportsPath ,
19
17
exportTimeoutMs : config . exportTimeoutMs ,
20
18
exportCleanupIntervalMs : config . exportCleanupIntervalMs ,
21
19
} as const ;
22
- function getDummyExportName ( timestamp : number ) {
23
- return `foo.bar.${ timestamp } .json` ;
24
- }
25
- function getDummyExportPath ( timestamp : number ) {
26
- return path . join ( dummySessionExportPath , getDummyExportName ( timestamp ) ) ;
20
+
21
+ function getExportNameAndPath ( sessionId : string , timestamp : number ) {
22
+ const exportName = `foo.bar.${ timestamp } .json` ;
23
+ const sessionExportsPath = path . join ( exportsPath , sessionId ) ;
24
+ const exportPath = path . join ( sessionExportsPath , exportName ) ;
25
+ return {
26
+ sessionExportsPath,
27
+ exportName,
28
+ exportPath,
29
+ exportURI : `exported-data://${ exportName } ` ,
30
+ } ;
27
31
}
28
32
29
- async function createDummyExport ( timestamp : number ) {
33
+ async function createDummyExport ( sessionId : string , timestamp : number ) {
30
34
const content = "[]" ;
31
- await fs . mkdir ( dummySessionExportPath , { recursive : true } ) ;
32
- await fs . writeFile ( getDummyExportPath ( timestamp ) , content ) ;
35
+ const { exportName, exportPath, sessionExportsPath } = getExportNameAndPath ( sessionId , timestamp ) ;
36
+ await fs . mkdir ( sessionExportsPath , { recursive : true } ) ;
37
+ await fs . writeFile ( exportPath , content ) ;
33
38
return {
34
- name : getDummyExportName ( timestamp ) ,
35
- path : getDummyExportPath ( timestamp ) ,
39
+ exportName ,
40
+ exportPath ,
36
41
content,
37
42
} ;
38
43
}
@@ -75,8 +80,8 @@ describe("SessionExportsManager integration test", () => {
75
80
76
81
beforeEach ( async ( ) => {
77
82
await manager ?. close ( ) ;
78
- await fs . rm ( exportsManagerConfig . exportPath , { recursive : true , force : true } ) ;
79
- await fs . mkdir ( exportsManagerConfig . exportPath , { recursive : true } ) ;
83
+ await fs . rm ( exportsManagerConfig . exportsPath , { recursive : true , force : true } ) ;
84
+ await fs . mkdir ( exportsManagerConfig . exportsPath , { recursive : true } ) ;
80
85
session = new Session ( { apiBaseUrl : "" } ) ;
81
86
manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
82
87
} ) ;
@@ -92,25 +97,22 @@ describe("SessionExportsManager integration test", () => {
92
97
} ) ;
93
98
94
99
describe ( "#exportsDirectoryPath" , ( ) => {
95
- it ( "should throw when session is not initialized" , ( ) => {
96
- expect ( ( ) => manager . exportsDirectoryPath ( ) ) . toThrow ( ) ;
97
- } ) ;
98
-
99
100
it ( "should return a session path when session is initialized" , ( ) => {
100
- session . sessionId = dummySessionId ;
101
101
manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
102
- expect ( manager . exportsDirectoryPath ( ) ) . toEqual ( path . join ( exportsManagerConfig . exportPath , dummySessionId ) ) ;
102
+ expect ( manager . exportsDirectoryPath ( ) ) . toEqual (
103
+ path . join ( exportsManagerConfig . exportsPath , session . sessionId )
104
+ ) ;
103
105
} ) ;
104
106
} ) ;
105
107
106
108
describe ( "#exportFilePath" , ( ) => {
107
109
it ( "should throw when export name has no extension" , ( ) => {
108
- expect ( ( ) => manager . exportFilePath ( dummySessionExportPath , "name" ) ) . toThrow ( ) ;
110
+ expect ( ( ) => manager . exportFilePath ( "session-path" , "name" ) ) . toThrow ( ) ;
109
111
} ) ;
110
112
111
113
it ( "should return path to provided export file" , ( ) => {
112
- expect ( manager . exportFilePath ( dummySessionExportPath , "mflix.movies.json" ) ) . toEqual (
113
- path . join ( dummySessionExportPath , "mflix.movies.json" )
114
+ expect ( manager . exportFilePath ( "session-path" , "mflix.movies.json" ) ) . toEqual (
115
+ path . join ( "session-path" , "mflix.movies.json" )
114
116
) ;
115
117
} ) ;
116
118
} ) ;
@@ -121,15 +123,16 @@ describe("SessionExportsManager integration test", () => {
121
123
} ) ;
122
124
123
125
it ( "should return the resource content" , async ( ) => {
124
- const { name, content } = await createDummyExport ( Date . now ( ) ) ;
125
- session . sessionId = dummySessionId ;
126
- manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
127
- expect ( await manager . readExport ( name ) ) . toEqual ( content ) ;
126
+ const { exportName, content } = await createDummyExport ( session . sessionId , Date . now ( ) ) ;
127
+ expect ( await manager . readExport ( exportName ) ) . toEqual ( content ) ;
128
128
} ) ;
129
129
} ) ;
130
130
131
131
describe ( "#createJSONExport" , ( ) => {
132
132
let inputCursor : FindCursor ;
133
+ let exportName : string ;
134
+ let exportPath : string ;
135
+ let exportURI : string ;
133
136
beforeEach ( ( ) => {
134
137
void inputCursor ?. close ( ) ;
135
138
inputCursor = createDummyFindCursor ( [
@@ -142,19 +145,17 @@ describe("SessionExportsManager integration test", () => {
142
145
longNumber : Long . fromNumber ( 123456 ) ,
143
146
} ,
144
147
] ) ;
148
+ ( { exportName, exportPath, exportURI } = getExportNameAndPath ( session . sessionId , Date . now ( ) ) ) ;
145
149
} ) ;
146
150
147
151
describe ( "when cursor is empty" , ( ) => {
148
152
it ( "should create an empty export" , async ( ) => {
149
153
inputCursor = createDummyFindCursor ( [ ] ) ;
150
154
151
155
const emitSpy = vi . spyOn ( session , "emit" ) ;
152
- session . sessionId = dummySessionId ;
153
- manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
154
- const timestamp = Date . now ( ) ;
155
156
await manager . createJSONExport ( {
156
157
input : inputCursor ,
157
- exportName : getDummyExportName ( timestamp ) ,
158
+ exportName,
158
159
jsonExportFormat : "relaxed" ,
159
160
} ) ;
160
161
@@ -163,19 +164,16 @@ describe("SessionExportsManager integration test", () => {
163
164
expect ( availableExports ) . toHaveLength ( 1 ) ;
164
165
expect ( availableExports ) . toContainEqual (
165
166
expect . objectContaining ( {
166
- name : getDummyExportName ( timestamp ) ,
167
- uri : `exported-data:// ${ getDummyExportName ( timestamp ) } ` ,
167
+ name : exportName ,
168
+ uri : exportURI ,
168
169
} )
169
170
) ;
170
171
171
172
// Emit event
172
- expect ( emitSpy ) . toHaveBeenCalledWith (
173
- "export-available" ,
174
- `exported-data://${ getDummyExportName ( timestamp ) } `
175
- ) ;
173
+ expect ( emitSpy ) . toHaveBeenCalledWith ( "export-available" , exportURI ) ;
176
174
177
175
// Exports relaxed json
178
- const jsonData = JSON . parse ( await manager . readExport ( getDummyExportName ( timestamp ) ) ) as unknown [ ] ;
176
+ const jsonData = JSON . parse ( await manager . readExport ( exportName ) ) as unknown [ ] ;
179
177
expect ( jsonData ) . toEqual ( [ ] ) ;
180
178
} ) ;
181
179
} ) ;
@@ -186,8 +184,6 @@ describe("SessionExportsManager integration test", () => {
186
184
] ) ( "$cond" , ( { exportName } ) => {
187
185
it ( "should export relaxed json, update available exports and emit export-available event" , async ( ) => {
188
186
const emitSpy = vi . spyOn ( session , "emit" ) ;
189
- session . sessionId = dummySessionId ;
190
- manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
191
187
await manager . createJSONExport ( {
192
188
input : inputCursor ,
193
189
exportName,
@@ -221,8 +217,6 @@ describe("SessionExportsManager integration test", () => {
221
217
] ) ( "$cond" , ( { exportName } ) => {
222
218
it ( "should export canonical json, update available exports and emit export-available event" , async ( ) => {
223
219
const emitSpy = vi . spyOn ( session , "emit" ) ;
224
- session . sessionId = dummySessionId ;
225
- manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
226
220
await manager . createJSONExport ( {
227
221
input : inputCursor ,
228
222
exportName,
@@ -257,8 +251,6 @@ describe("SessionExportsManager integration test", () => {
257
251
describe ( "when transform stream throws an error" , ( ) => {
258
252
it ( "should remove the partial export and never make it available" , async ( ) => {
259
253
const emitSpy = vi . spyOn ( session , "emit" ) ;
260
- session . sessionId = dummySessionId ;
261
- manager = new SessionExportsManager ( session , exportsManagerConfig ) ;
262
254
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
263
255
( manager as any ) . docToEJSONStream = function ( ejsonOptions : EJSONOptions | undefined ) {
264
256
let docsTransformed = 0 ;
@@ -285,9 +277,6 @@ describe("SessionExportsManager integration test", () => {
285
277
} ) ;
286
278
} ;
287
279
288
- const timestamp = Date . now ( ) ;
289
- const exportName = getDummyExportName ( timestamp ) ;
290
- const exportPath = getDummyExportPath ( timestamp ) ;
291
280
await expect ( ( ) =>
292
281
manager . createJSONExport ( {
293
282
input : inputCursor ,
@@ -319,24 +308,8 @@ describe("SessionExportsManager integration test", () => {
319
308
] ) ;
320
309
} ) ;
321
310
322
- it ( "should do nothing if session is not initialized" , async ( ) => {
323
- const { path } = await createDummyExport ( Date . now ( ) ) ;
324
- new SessionExportsManager ( session , {
325
- ...exportsManagerConfig ,
326
- exportTimeoutMs : 100 ,
327
- exportCleanupIntervalMs : 50 ,
328
- } ) ;
329
-
330
- expect ( await fileExists ( path ) ) . toEqual ( true ) ;
331
- await timeout ( 200 ) ;
332
- expect ( await fileExists ( path ) ) . toEqual ( true ) ;
333
- } ) ;
334
-
335
311
it ( "should cleanup expired exports if session is initialized" , async ( ) => {
336
- session . sessionId = dummySessionId ;
337
- const timestamp = Date . now ( ) ;
338
- const exportName = getDummyExportName ( timestamp ) ;
339
- const exportPath = getDummyExportPath ( timestamp ) ;
312
+ const { exportName, exportPath, exportURI } = getExportNameAndPath ( session . sessionId , Date . now ( ) ) ;
340
313
const manager = new SessionExportsManager ( session , {
341
314
...exportsManagerConfig ,
342
315
exportTimeoutMs : 100 ,
@@ -351,7 +324,7 @@ describe("SessionExportsManager integration test", () => {
351
324
expect ( manager . listAvailableExports ( ) ) . toContainEqual (
352
325
expect . objectContaining ( {
353
326
name : exportName ,
354
- uri : `exported-data:// ${ exportName } ` ,
327
+ uri : exportURI ,
355
328
} )
356
329
) ;
357
330
expect ( await fileExists ( exportPath ) ) . toEqual ( true ) ;
0 commit comments