@@ -7,7 +7,10 @@ import {
7
7
ERROR_FAILED_TO_EXECUTE_MESSAGE ,
8
8
ERROR_NOT_SAME_LENGTH_MESSAGE ,
9
9
SUCCESS_MESSAGE ,
10
+ ERROR_INVALID_TEST_CASES_MESSAGE ,
10
11
} from "../utils/constants" ;
12
+ import { questionService } from "../utils/questionApi" ;
13
+ import { testCasesApi } from "../utils/testCasesApi" ;
11
14
12
15
interface CompilerResult {
13
16
status : string ;
@@ -19,14 +22,9 @@ interface CompilerResult {
19
22
}
20
23
21
24
export const executeCode = async ( req : Request , res : Response ) => {
22
- const {
23
- language,
24
- code,
25
- stdinList,
26
- stdoutList : expectedStdoutList ,
27
- } = req . body ;
25
+ const { questionId, language, code } = req . body ;
28
26
29
- if ( ! language || ! code || ! stdinList || ! expectedStdoutList ) {
27
+ if ( ! language || ! code || ! questionId ) {
30
28
res . status ( 400 ) . json ( {
31
29
message : ERROR_MISSING_REQUIRED_FIELDS_MESSAGE ,
32
30
} ) ;
@@ -40,48 +38,71 @@ export const executeCode = async (req: Request, res: Response) => {
40
38
return ;
41
39
}
42
40
43
- if ( stdinList . length !== expectedStdoutList . length ) {
44
- res . status ( 400 ) . json ( {
45
- message : ERROR_NOT_SAME_LENGTH_MESSAGE ,
46
- } ) ;
47
- return ;
48
- }
49
-
50
41
try {
51
- const response = await oneCompilerApi ( language , stdinList , code ) ;
42
+ // Get question test case files
43
+ const qnsResponse = await questionService . get ( `/${ questionId } ` ) ;
44
+ const { testcaseInputFileUrl, testcaseOutputFileUrl } =
45
+ qnsResponse . data . question ;
52
46
53
- const data = ( response . data as CompilerResult [ ] ) . map ( ( result , index ) => {
54
- const {
55
- status,
56
- exception,
57
- stdout : actualStdout ,
58
- stderr,
59
- stdin,
60
- executionTime,
61
- } = result ;
62
- const expectedStdout = expectedStdoutList [ index ] ;
47
+ // Extract test cases from input and output files
48
+ const testCases = await testCasesApi (
49
+ testcaseInputFileUrl ,
50
+ testcaseOutputFileUrl
51
+ ) ;
63
52
64
- return {
65
- status,
66
- exception,
67
- expectedStdout,
68
- actualStdout,
69
- stderr,
70
- stdin,
71
- executionTime,
72
- isMatch :
73
- stderr !== null
74
- ? false
75
- : actualStdout . trim ( ) === expectedStdout . trim ( ) ,
76
- } ;
77
- } ) ;
53
+ const stdinList : string [ ] = testCases . input ;
54
+ const expectedResultList : string [ ] = testCases . output ;
55
+
56
+ if ( stdinList . length !== expectedResultList . length ) {
57
+ res . status ( 400 ) . json ( {
58
+ message : ERROR_NOT_SAME_LENGTH_MESSAGE ,
59
+ } ) ;
60
+ return ;
61
+ }
62
+
63
+ if ( stdinList . length === 0 ) {
64
+ res . status ( 400 ) . json ( {
65
+ message : ERROR_INVALID_TEST_CASES_MESSAGE ,
66
+ } ) ;
67
+ return ;
68
+ }
69
+
70
+ // Execute code for each test case
71
+ const compilerResponse = await oneCompilerApi ( language , stdinList , code ) ;
72
+
73
+ const compilerData = ( compilerResponse . data as CompilerResult [ ] ) . map (
74
+ ( result , index ) => {
75
+ let { stdout, ...restofResult } = result ; // eslint-disable-line
76
+ const expectedResultValue = expectedResultList [ index ] . trim ( ) ;
77
+
78
+ if ( ! stdout ) {
79
+ stdout = "" ;
80
+ }
81
+
82
+ // Extract the last line as the result value
83
+ // and the rest as stdout
84
+ const lines = stdout . trim ( ) . split ( "\n" ) ;
85
+ const resultValue = lines . pop ( ) || "" ;
86
+ stdout = lines . join ( "\n" ) ;
87
+
88
+ return {
89
+ ...restofResult ,
90
+ stdout,
91
+ actualResult : resultValue ,
92
+ expectedResult : expectedResultValue ,
93
+ isMatch :
94
+ result . stderr !== null
95
+ ? false
96
+ : resultValue === expectedResultValue ,
97
+ } ;
98
+ }
99
+ ) ;
78
100
79
101
res . status ( 200 ) . json ( {
80
102
message : SUCCESS_MESSAGE ,
81
- data,
103
+ data : compilerData ,
82
104
} ) ;
83
- } catch ( err ) {
84
- console . log ( err ) ;
105
+ } catch {
85
106
res . status ( 500 ) . json ( { message : ERROR_FAILED_TO_EXECUTE_MESSAGE } ) ;
86
107
}
87
108
} ;
0 commit comments