1
1
const fs = require ( 'fs' ) ;
2
- const path = require ( 'path' ) ;
3
2
const {
4
3
EXPORTED_TESTS_DIR ,
5
- EXPORTED_TESTS_DATA_DIR ,
6
4
PYTHAGORA_METADATA_DIR ,
7
- METADATA_FILENAME ,
8
5
EXPORT_METADATA_FILENAME ,
9
- SRC_TO_ROOT
10
6
} = require ( '../const/common' ) ;
11
- const { getAllGeneratedTests, updateMetadata } = require ( "../utils/common" ) ;
12
- const { convertOldTestForGPT} = require ( "../utils/legacy" ) ;
13
- const { setUpPythagoraDirs} = require ( "../helpers/starting" ) ;
7
+ const { getAllGeneratedTests } = require ( "../utils/common" ) ;
8
+ const { convertOldTestForGPT } = require ( "../utils/legacy" ) ;
9
+ const { setUpPythagoraDirs } = require ( "../helpers/starting" ) ;
14
10
const {
15
11
logAndExit,
16
- testExported,
17
- pleaseCaptureLoginTestLog,
18
- enterLoginRouteLog,
19
12
testEligibleForExportLog,
20
- testExportStartedLog
21
13
} = require ( "../utils/cmdPrint" ) ;
22
14
const {
23
- getJestAuthFunction,
24
- getJestTest,
25
- getJestTestName,
26
15
isEligibleForExport,
27
16
checkForAPIKey
28
17
} = require ( "../helpers/api" ) ;
29
- const _ = require ( 'lodash' ) ;
30
18
const args = require ( '../utils/getArgs.js' ) ;
19
+ const {
20
+ createDefaultFiles,
21
+ cleanupDataFolder,
22
+ exportTest,
23
+ testExists,
24
+ } = require ( '../helpers/exports' ) ;
31
25
32
- async function createDefaultFiles ( generatedTests ) {
33
- if ( ! fs . existsSync ( 'jest.config.js' ) ) {
34
- fs . copyFileSync ( path . join ( __dirname , '../templates/jest-config.js' ) , './jest.config.js' ) ;
35
- }
36
-
37
- if ( ! fs . existsSync ( `./${ EXPORTED_TESTS_DIR } /jest-global-setup.js` ) ) {
38
- fs . copyFileSync ( path . join ( __dirname , '../templates/jest-global-setup.js' ) , `./${ EXPORTED_TESTS_DIR } /global-setup.js` ) ;
39
- }
40
-
41
- if ( ! fs . existsSync ( `./${ EXPORTED_TESTS_DIR } /auth.js` ) ) {
42
- await configureAuthFile ( generatedTests ) ;
43
- }
44
- }
45
-
46
- async function configureAuthFile ( generatedTests ) {
47
- // TODO make require path better
48
- let pythagoraMetadata = require ( `../${ SRC_TO_ROOT } .pythagora/${ METADATA_FILENAME } ` ) ;
49
- let loginPath = _ . get ( pythagoraMetadata , 'exportRequirements.login.endpointPath' ) ;
50
- let loginRequestBody = _ . get ( pythagoraMetadata , 'exportRequirements.login.requestBody' ) ;
51
- let loginMongoQueries = _ . get ( pythagoraMetadata , 'exportRequirements.login.mongoQueriesArray' ) ;
52
-
53
- if ( ! loginPath ) {
54
- enterLoginRouteLog ( ) ;
55
- process . exit ( 1 ) ;
56
- }
57
-
58
- if ( ! loginRequestBody || ! loginMongoQueries ) {
59
- let loginTest = generatedTests . find ( t => t . endpoint === loginPath && t . method !== 'OPTIONS' ) ;
60
- if ( loginTest ) {
61
- _ . set ( pythagoraMetadata , 'exportRequirements.login.mongoQueriesArray' , loginTest . intermediateData . filter ( d => d . type === 'mongodb' ) ) ;
62
- _ . set ( pythagoraMetadata , 'exportRequirements.login.requestBody' , loginTest . body ) ;
63
- updateMetadata ( pythagoraMetadata ) ;
64
- } else {
65
- pleaseCaptureLoginTestLog ( loginPath ) ;
66
- process . exit ( 1 ) ;
67
- }
68
- }
69
-
70
- let loginData = pythagoraMetadata . exportRequirements . login ;
71
- let code = await getJestAuthFunction ( loginData . mongoQueriesArray , loginData . requestBody , loginData . endpointPath ) ;
72
-
73
- fs . writeFileSync ( `./${ EXPORTED_TESTS_DIR } /auth.js` , code ) ;
74
- }
75
-
76
- function configurePrepareDbFile ( ) {
77
- // TODO
78
- }
79
-
80
- function cleanupDataFolder ( ) {
81
- const pythagoraTestsFolderPath = `./${ EXPORTED_TESTS_DIR } ` ;
82
- const dataFolderPath = `./${ EXPORTED_TESTS_DATA_DIR } ` ;
83
-
84
- try {
85
- // Read the files in the ./pythagora_tests/data folder
86
- const files = fs . readdirSync ( dataFolderPath ) ;
87
-
88
- files . forEach ( ( file ) => {
89
- const filePathInPythagoraTests = path . join ( pythagoraTestsFolderPath , file . replace ( '.json' , '.test.js' ) ) ;
90
-
91
- // Check if the file exists in the pythagora_tests folder
92
- if ( ! fs . existsSync ( filePathInPythagoraTests ) ) {
93
- // File doesn't exist in the pythagora_tests folder, so delete it from the data folder
94
- const filePathInData = path . join ( dataFolderPath , file ) ;
95
- fs . unlinkSync ( filePathInData ) ;
96
- }
97
- } ) ;
98
- } catch ( err ) {
99
- // console.error('Error deleting files from the data folder:', err);
100
- }
101
- }
102
-
103
- async function exportTest ( originalTest , exportsMetadata ) {
104
- testExportStartedLog ( ) ;
105
- let test = convertOldTestForGPT ( originalTest ) ;
106
- let jestTest = await getJestTest ( test ) ;
107
- let testName = await getJestTestName ( jestTest , Object . values ( exportsMetadata ) . map ( obj => obj . testName ) ) ;
108
- if ( ! jestTest && ! testName ) return console . error ( 'There was issue with getting GPT response. Make sure you have access to GPT4 with your API key.' ) ;
109
-
110
- fs . writeFileSync ( `./${ EXPORTED_TESTS_DATA_DIR } /${ testName . replace ( '.test.js' , '.json' ) } ` , JSON . stringify ( test . mongoQueries , null , 2 ) ) ;
111
- fs . writeFileSync ( `./${ EXPORTED_TESTS_DIR } /${ testName } ` , jestTest . replace ( test . testId , testName ) ) ;
112
-
113
- testExported ( testName ) ;
114
- saveExportJson ( exportsMetadata , originalTest , testName ) ;
115
-
116
- }
117
-
118
- function testExists ( exportsMetadata , testId ) {
119
- return ! ! exportsMetadata [ testId ] && exportsMetadata [ testId ] . testName && fs . existsSync ( `./${ EXPORTED_TESTS_DIR } /${ exportsMetadata [ testId ] . testName } ` )
120
- }
121
-
122
- function saveExportJson ( exportsMetadata , test , testName ) {
123
- exportsMetadata [ test . id ] = {
124
- endpoint : test . endpoint ,
125
- testName
126
- } ;
127
- fs . writeFileSync ( `./${ PYTHAGORA_METADATA_DIR } /${ EXPORT_METADATA_FILENAME } ` , JSON . stringify ( exportsMetadata ) ) ;
128
- }
129
-
130
- ( async ( ) => {
26
+ async function runExport ( ) {
131
27
checkForAPIKey ( ) ;
132
28
setUpPythagoraDirs ( ) ;
133
29
cleanupDataFolder ( ) ;
@@ -162,4 +58,8 @@ function saveExportJson(exportsMetadata, test, testName) {
162
58
}
163
59
164
60
process . exit ( 0 ) ;
165
- } ) ( )
61
+ } ;
62
+
63
+ module . exports = {
64
+ runExport
65
+ } ;
0 commit comments