|
1 | 1 | const EXECUTION_SERVICE_URL = process.env.NEXT_PUBLIC_EXECUTION_SERVICE_URL;
|
2 | 2 |
|
3 | 3 | export interface TestData {
|
4 |
| - input: string |
5 |
| - expected: string |
| 4 | + input: string; |
| 5 | + expected: string; |
6 | 6 | }
|
7 | 7 |
|
8 | 8 | export interface TestResult {
|
9 |
| - input: string |
10 |
| - expected: string |
11 |
| - actual: string |
12 |
| - passed: boolean |
13 |
| - error: string |
| 9 | + input: string; |
| 10 | + expected: string; |
| 11 | + actual: string; |
| 12 | + passed: boolean; |
| 13 | + error: string; |
14 | 14 | }
|
15 | 15 |
|
16 |
| -export type Test = TestResult | TestData |
| 16 | +export type Test = TestResult | TestData; |
17 | 17 |
|
18 | 18 | export const isTestResult = (test: Test): test is TestResult => {
|
19 |
| - return 'actual' in test && 'passed' in test && 'error' in test; |
| 19 | + return "actual" in test && "passed" in test && "error" in test; |
20 | 20 | };
|
21 | 21 |
|
22 | 22 | export interface GeneralTestResults {
|
23 |
| - passed: number; |
24 |
| - total: number; |
| 23 | + passed: number; |
| 24 | + total: number; |
25 | 25 | }
|
26 | 26 |
|
27 | 27 | export interface SubmissionHiddenTestResultsAndStatus {
|
28 |
| - hiddenTestResults: GeneralTestResults; |
29 |
| - status: string; |
| 28 | + hiddenTestResults: GeneralTestResults; |
| 29 | + status: string; |
30 | 30 | }
|
31 | 31 |
|
32 |
| -export interface SubmissionResults extends SubmissionHiddenTestResultsAndStatus { |
33 |
| - visibleTestResults: TestResult[]; |
| 32 | +export interface SubmissionResults |
| 33 | + extends SubmissionHiddenTestResultsAndStatus { |
| 34 | + visibleTestResults: TestResult[]; |
34 | 35 | }
|
35 | 36 |
|
36 | 37 | export interface ExecutionResults {
|
37 |
| - visibleTestResults: TestResult[]; |
38 |
| - customTestResults: TestResult[]; |
| 38 | + visibleTestResults: TestResult[]; |
| 39 | + customTestResults: TestResult[]; |
39 | 40 | }
|
40 | 41 |
|
41 | 42 | export interface Code {
|
42 |
| - code: string; |
43 |
| - language: string; |
44 |
| - customTestCases: string; |
| 43 | + code: string; |
| 44 | + language: string; |
| 45 | + customTestCases: string; |
45 | 46 | }
|
46 | 47 |
|
47 | 48 | export interface Submission {
|
48 |
| - title: string |
49 |
| - code: string |
50 |
| - language: string |
51 |
| - user: string |
52 |
| - matchedUser: string |
53 |
| - matchedTopics: string[] |
54 |
| - questionDifficulty: string |
55 |
| - questionTopics: string[] |
| 49 | + title: string; |
| 50 | + code: string; |
| 51 | + language: string; |
| 52 | + user: string; |
| 53 | + matchedUser: string; |
| 54 | + matchedTopics: string[]; |
| 55 | + questionDifficulty: string; |
| 56 | + questionTopics: string[]; |
56 | 57 | }
|
57 | 58 |
|
58 | 59 | export const GetVisibleTests = async (
|
59 |
| - questionDocRefId: string, |
| 60 | + questionDocRefId: string |
60 | 61 | ): Promise<TestData[]> => {
|
61 |
| - const response = await fetch( |
62 |
| - `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`, |
63 |
| - { |
64 |
| - method: "GET", |
65 |
| - headers: { |
66 |
| - "Content-Type": "application/json", |
67 |
| - }, |
68 |
| - } |
69 |
| - ); |
70 |
| - |
71 |
| - if (response.status === 200) { |
72 |
| - return response.json(); |
73 |
| - } else { |
74 |
| - throw new Error( |
75 |
| - `Error fetching test cases: ${response.status} ${response.statusText}` |
76 |
| - ); |
| 62 | + const response = await fetch( |
| 63 | + `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`, |
| 64 | + { |
| 65 | + method: "GET", |
| 66 | + headers: { |
| 67 | + "Content-Type": "application/json", |
| 68 | + }, |
77 | 69 | }
|
78 |
| -} |
| 70 | + ); |
| 71 | + |
| 72 | + if (response.status === 200) { |
| 73 | + return response.json(); |
| 74 | + } else { |
| 75 | + throw new Error( |
| 76 | + `Error fetching test cases: ${response.status} ${response.statusText}` |
| 77 | + ); |
| 78 | + } |
| 79 | +}; |
79 | 80 |
|
80 | 81 | export const ExecuteVisibleAndCustomTests = async (
|
81 |
| - questionDocRefId: string, |
82 |
| - code: Code, |
| 82 | + questionDocRefId: string, |
| 83 | + code: Code |
83 | 84 | ): Promise<ExecutionResults> => {
|
84 |
| - const response = await fetch( |
85 |
| - `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/execute`, |
86 |
| - { |
87 |
| - method: "POST", |
88 |
| - headers: { |
89 |
| - "Content-Type": "application/json", |
90 |
| - }, |
91 |
| - body: JSON.stringify(code), |
92 |
| - } |
| 85 | + const response = await fetch( |
| 86 | + `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/execute`, |
| 87 | + { |
| 88 | + method: "POST", |
| 89 | + headers: { |
| 90 | + "Content-Type": "application/json", |
| 91 | + }, |
| 92 | + body: JSON.stringify(code), |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + if (response.status === 200) { |
| 97 | + return response.json(); |
| 98 | + } else { |
| 99 | + throw new Error( |
| 100 | + `Error executing code: ${response.status} ${response.statusText}` |
93 | 101 | );
|
| 102 | + } |
| 103 | +}; |
94 | 104 |
|
95 |
| - if (response.status === 200) { |
96 |
| - return response.json(); |
97 |
| - } else { |
98 |
| - throw new Error( |
99 |
| - `Error executing code: ${response.status} ${response.statusText}` |
100 |
| - ); |
| 105 | +export const ExecuteVisibleAndHiddenTestsAndSubmit = async ( |
| 106 | + questionDocRefId: string, |
| 107 | + collaboration: Submission |
| 108 | +): Promise<SubmissionResults> => { |
| 109 | + const response = await fetch( |
| 110 | + `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/submit`, |
| 111 | + { |
| 112 | + method: "POST", |
| 113 | + headers: { |
| 114 | + "Content-Type": "application/json", |
| 115 | + }, |
| 116 | + body: JSON.stringify(collaboration), |
101 | 117 | }
|
102 |
| -} |
| 118 | + ); |
103 | 119 |
|
104 |
| -export const ExecuteVisibleAndHiddenTestsAndSubmit = async ( |
105 |
| - questionDocRefId: string, |
106 |
| - collaboration: Submission, |
| 120 | + if (response.status === 200) { |
| 121 | + return response.json(); |
| 122 | + } else { |
| 123 | + throw new Error( |
| 124 | + `Error submitting code: ${response.status} ${response.statusText}` |
| 125 | + ); |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +export const CreateTestcases = async ( |
| 130 | + questionDocRefId: string, |
| 131 | + questionTitle: string, |
| 132 | + visibleTestcases: TestData[], |
| 133 | + hiddenTestcases: TestData[] |
107 | 134 | ): Promise<SubmissionResults> => {
|
108 |
| - const response = await fetch( |
109 |
| - `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}/submit`, |
110 |
| - { |
111 |
| - method: "POST", |
112 |
| - headers: { |
113 |
| - "Content-Type": "application/json", |
114 |
| - }, |
115 |
| - body: JSON.stringify(collaboration), |
116 |
| - } |
| 135 | + const response = await fetch(`${EXECUTION_SERVICE_URL}tests`, { |
| 136 | + method: "POST", |
| 137 | + headers: { |
| 138 | + "Content-Type": "application/json", |
| 139 | + }, |
| 140 | + body: JSON.stringify({ |
| 141 | + questionDocRefId: questionDocRefId, |
| 142 | + questionTitle: questionTitle, |
| 143 | + visibleTestCases: visibleTestcases, |
| 144 | + hiddenTestCases: hiddenTestcases, |
| 145 | + }), |
| 146 | + }); |
| 147 | + |
| 148 | + if (response.ok) { |
| 149 | + return response.json(); |
| 150 | + } else { |
| 151 | + throw new Error( |
| 152 | + `Error creating testcases: ${response.status} ${response.statusText}` |
117 | 153 | );
|
| 154 | + } |
| 155 | +}; |
118 | 156 |
|
119 |
| - if (response.status === 200) { |
120 |
| - return response.json(); |
121 |
| - } else { |
122 |
| - throw new Error( |
123 |
| - `Error submitting code: ${response.status} ${response.statusText}` |
124 |
| - ); |
| 157 | +export const UpdateTestcases = async ( |
| 158 | + questionDocRefId: string, |
| 159 | + visibleTestcases: TestData[], |
| 160 | + hiddenTestcases: TestData[] |
| 161 | +): Promise<SubmissionResults> => { |
| 162 | + const response = await fetch( |
| 163 | + `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`, |
| 164 | + { |
| 165 | + method: "PUT", |
| 166 | + headers: { |
| 167 | + "Content-Type": "application/json", |
| 168 | + }, |
| 169 | + body: JSON.stringify({ |
| 170 | + visibleTestCases: visibleTestcases, |
| 171 | + hiddenTestCases: hiddenTestcases, |
| 172 | + }), |
125 | 173 | }
|
126 |
| -} |
| 174 | + ); |
| 175 | + |
| 176 | + if (response.ok) { |
| 177 | + return response.json(); |
| 178 | + } else { |
| 179 | + throw new Error( |
| 180 | + `Error updating testcases: ${response.status} ${response.statusText}` |
| 181 | + ); |
| 182 | + } |
| 183 | +}; |
| 184 | + |
| 185 | +export const DeleteTestcases = async ( |
| 186 | + questionDocRefId: string |
| 187 | +): Promise<void> => { |
| 188 | + const response = await fetch( |
| 189 | + `${EXECUTION_SERVICE_URL}tests/${questionDocRefId}`, |
| 190 | + { |
| 191 | + method: "DELETE", |
| 192 | + headers: { |
| 193 | + "Content-Type": "application/json", |
| 194 | + }, |
| 195 | + } |
| 196 | + ); |
| 197 | + |
| 198 | + if (!response.ok) { |
| 199 | + throw new Error( |
| 200 | + `Error deleting testcases: ${response.status} ${response.statusText}` |
| 201 | + ); |
| 202 | + } |
| 203 | +}; |
0 commit comments