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