Skip to content

Commit 439b410

Browse files
authored
Merge pull request #68 from CS3219-AY2425S1/titus/add-test-cases-to-questions
feat: add test cases to question service
2 parents ee60055 + b4e499d commit 439b410

File tree

7 files changed

+256
-234
lines changed

7 files changed

+256
-234
lines changed

apps/frontend/src/app/collaboration/[id]/page.tsx

Lines changed: 68 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ import {
2222
ClockCircleOutlined,
2323
CodeOutlined,
2424
InfoCircleFilled,
25-
MessageOutlined,
2625
SendOutlined,
2726
VideoCameraOutlined,
2827
} from "@ant-design/icons";
2928
import CollaborativeEditor, {
3029
CollaborativeEditorHandle,
3130
} from "@/components/CollaborativeEditor/CollaborativeEditor";
32-
import { CreateHistory } from "@/app/services/history";
3331
import { WebrtcProvider } from "y-webrtc";
34-
import { ExecuteVisibleAndCustomTests, ExecuteVisibleAndHiddenTestsAndSubmit, ExecutionResults, GetVisibleTests, isTestResult, SubmissionHiddenTestResultsAndStatus, SubmissionResults, Test, TestData, TestResult } from "@/app/services/execute";
32+
import {
33+
ExecuteVisibleAndCustomTests,
34+
ExecuteVisibleAndHiddenTestsAndSubmit,
35+
ExecutionResults,
36+
GetVisibleTests,
37+
SubmissionHiddenTestResultsAndStatus,
38+
SubmissionResults,
39+
Test,
40+
} from "@/app/services/execute";
3541
import { QuestionDetailFull } from "@/components/question/QuestionDetailFull/QuestionDetailFull";
3642
import VideoPanel from "@/components/VideoPanel/VideoPanel";
3743

@@ -89,8 +95,12 @@ export default function CollaborationPage(props: CollaborationProps) {
8995
);
9096
const [visibleTestCases, setVisibleTestCases] = useState<Test[]>([]);
9197
const [isLoadingTestCase, setIsLoadingTestCase] = useState<boolean>(false);
92-
const [isLoadingSubmission, setIsLoadingSubmission] = useState<boolean>(false);
93-
const [submissionHiddenTestResultsAndStatus, setSubmissionHiddenTestResultsAndStatus] = useState<SubmissionHiddenTestResultsAndStatus | undefined>(undefined);
98+
const [isLoadingSubmission, setIsLoadingSubmission] =
99+
useState<boolean>(false);
100+
const [
101+
submissionHiddenTestResultsAndStatus,
102+
setSubmissionHiddenTestResultsAndStatus,
103+
] = useState<SubmissionHiddenTestResultsAndStatus | undefined>(undefined);
94104

95105
// End Button Modal state
96106
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
@@ -151,7 +161,7 @@ export default function CollaborationPage(props: CollaborationProps) {
151161
type: "info",
152162
content: message,
153163
});
154-
}
164+
};
155165

156166
const sendSubmissionResultsToMatchedUser = (data: SubmissionResults) => {
157167
if (!providerRef.current) {
@@ -161,7 +171,7 @@ export default function CollaborationPage(props: CollaborationProps) {
161171
submissionResults: data,
162172
id: Date.now(),
163173
});
164-
}
174+
};
165175

166176
const sendExecutingStateToMatchedUser = (executing: boolean) => {
167177
if (!providerRef.current) {
@@ -171,7 +181,7 @@ export default function CollaborationPage(props: CollaborationProps) {
171181
executing: executing,
172182
id: Date.now(),
173183
});
174-
}
184+
};
175185

176186
const sendSubmittingStateToMatchedUser = (submitting: boolean) => {
177187
if (!providerRef.current) {
@@ -181,7 +191,7 @@ export default function CollaborationPage(props: CollaborationProps) {
181191
submitting: submitting,
182192
id: Date.now(),
183193
});
184-
}
194+
};
185195

186196
const sendExecutionResultsToMatchedUser = (data: ExecutionResults) => {
187197
if (!providerRef.current) {
@@ -191,60 +201,54 @@ export default function CollaborationPage(props: CollaborationProps) {
191201
executionResults: data,
192202
id: Date.now(),
193203
});
194-
}
204+
};
195205

196206
const updateSubmissionResults = (data: SubmissionResults) => {
197207
setSubmissionHiddenTestResultsAndStatus({
198208
hiddenTestResults: data.hiddenTestResults,
199209
status: data.status,
200210
});
201211
setVisibleTestCases(data.visibleTestResults);
202-
}
212+
};
203213

204214
const updateExecutionResults = (data: ExecutionResults) => {
205215
setVisibleTestCases(data.visibleTestResults);
206-
}
216+
};
207217

208218
const handleRunTestCases = async () => {
209219
if (!questionDocRefId) {
210220
throw new Error("Question ID not found");
211221
}
212222
setIsLoadingTestCase(true);
213223
sendExecutingStateToMatchedUser(true);
214-
const data = await ExecuteVisibleAndCustomTests(
215-
questionDocRefId,
216-
{
217-
code: code,
218-
language: selectedLanguage,
219-
customTestCases: "",
220-
}
221-
);
224+
const data = await ExecuteVisibleAndCustomTests(questionDocRefId, {
225+
code: code,
226+
language: selectedLanguage,
227+
customTestCases: "",
228+
});
222229
setVisibleTestCases(data.visibleTestResults);
223-
infoMessage("Test cases executed. Review the results below.")
230+
infoMessage("Test cases executed. Review the results below.");
224231
sendExecutionResultsToMatchedUser(data);
225232
setIsLoadingTestCase(false);
226233
sendExecutingStateToMatchedUser(false);
227-
}
234+
};
228235

229236
const handleSubmitCode = async () => {
230237
if (!questionDocRefId) {
231238
throw new Error("Question ID not found");
232239
}
233240
setIsLoadingSubmission(true);
234241
sendSubmittingStateToMatchedUser(true);
235-
const data = await ExecuteVisibleAndHiddenTestsAndSubmit(
236-
questionDocRefId,
237-
{
238-
code: code,
239-
language: selectedLanguage,
240-
user: currentUser ?? "",
241-
matchedUser: matchedUser ?? "",
242-
matchedTopics: matchedTopics ?? [],
243-
title: questionTitle ?? "",
244-
questionDifficulty: complexity ?? "",
245-
questionTopics: categories,
246-
}
247-
);
242+
const data = await ExecuteVisibleAndHiddenTestsAndSubmit(questionDocRefId, {
243+
code: code,
244+
language: selectedLanguage,
245+
user: currentUser ?? "",
246+
matchedUser: matchedUser ?? "",
247+
matchedTopics: matchedTopics ?? [],
248+
title: questionTitle ?? "",
249+
questionDifficulty: complexity ?? "",
250+
questionTopics: categories,
251+
});
248252
setVisibleTestCases(data.visibleTestResults);
249253
setSubmissionHiddenTestResultsAndStatus({
250254
hiddenTestResults: data.hiddenTestResults,
@@ -254,7 +258,7 @@ export default function CollaborationPage(props: CollaborationProps) {
254258
successMessage("Code saved successfully!");
255259
setIsLoadingSubmission(false);
256260
sendSubmittingStateToMatchedUser(false);
257-
}
261+
};
258262

259263
const handleCodeChange = (code: string) => {
260264
setCode(code);
@@ -310,55 +314,6 @@ export default function CollaborationPage(props: CollaborationProps) {
310314
}
311315
}, [isSessionEndModalOpen, countDown]);
312316

313-
// Tabs component items for visibleTestCases
314-
var items: TabsProps["items"] = visibleTestCases.map((item, index) => {
315-
return {
316-
key: index.toString(),
317-
label: (
318-
<span
319-
style={{
320-
color: !isTestResult(item)
321-
? ""
322-
: (item.passed ? "green" : "red"),
323-
}}
324-
>
325-
Case {index + 1}
326-
</span>
327-
),
328-
children: (
329-
<div>
330-
<Input.TextArea
331-
disabled
332-
placeholder={`Stdin: ${item.input}\nStdout: ${item.expected}`}
333-
rows={4}
334-
/>
335-
{isTestResult(item) && (
336-
<div className="test-result-container">
337-
<InfoCircleFilled className="hidden-test-icon" />
338-
<Typography.Text
339-
strong
340-
style={{ color: item.passed ? "green" : "red" }}
341-
>
342-
{item.passed ? "Passed" : "Failed"}
343-
</Typography.Text>
344-
<br />
345-
<Typography.Text strong>Actual Output:</Typography.Text> {item.actual}
346-
<br />
347-
{item.error && (
348-
<>
349-
<Typography.Text strong>Error:</Typography.Text>
350-
<div className="error-message">
351-
{item.error}
352-
</div>
353-
</>
354-
)}
355-
</div>
356-
)}
357-
</div>
358-
),
359-
};
360-
});
361-
362317
// Handles the cleaning of localstorage variables, stopping the timer & signalling collab user on webrtc
363318
// type: "initiator" | "peer"
364319
const handleCloseCollaboration = (type: string) => {
@@ -438,7 +393,7 @@ export default function CollaborationPage(props: CollaborationProps) {
438393
complexity={complexity}
439394
categories={categories}
440395
description={description}
441-
testcaseItems={items}
396+
visibleTestcases={visibleTestCases}
442397
shouldShowSubmitButton
443398
handleRunTestCases={handleRunTestCases}
444399
isLoadingTestCase={isLoadingTestCase}
@@ -487,31 +442,50 @@ export default function CollaborationPage(props: CollaborationProps) {
487442
)}
488443
<div className="hidden-test-results">
489444
<InfoCircleFilled className="hidden-test-icon" />
490-
<Typography.Text
445+
<Typography.Text
491446
strong
492447
style={{
493448
color: submissionHiddenTestResultsAndStatus
494-
? submissionHiddenTestResultsAndStatus.status === "Accepted"
449+
? submissionHiddenTestResultsAndStatus.status ===
450+
"Accepted"
495451
? "green"
496-
: submissionHiddenTestResultsAndStatus.status === "Attempted"
452+
: submissionHiddenTestResultsAndStatus.status ===
453+
"Attempted"
497454
? "orange"
498455
: "black" // default color for any other status
499456
: "gray", // color for "Not Attempted"
500457
}}
501458
>
502-
Session Status: {submissionHiddenTestResultsAndStatus ? submissionHiddenTestResultsAndStatus.status : "Not Attempted"}
459+
Session Status:{" "}
460+
{submissionHiddenTestResultsAndStatus
461+
? submissionHiddenTestResultsAndStatus.status
462+
: "Not Attempted"}
503463
</Typography.Text>
504464
<br />
505465
{submissionHiddenTestResultsAndStatus && (
506466
<Typography.Text
507467
strong
508468
style={{
509-
color: submissionHiddenTestResultsAndStatus.hiddenTestResults.passed === submissionHiddenTestResultsAndStatus.hiddenTestResults.total
510-
? "green" // All test cases passed
511-
: "red" // Some test cases failed
469+
color:
470+
submissionHiddenTestResultsAndStatus.hiddenTestResults
471+
.passed ===
472+
submissionHiddenTestResultsAndStatus.hiddenTestResults
473+
.total
474+
? "green" // All test cases passed
475+
: "red", // Some test cases failed
512476
}}
513477
>
514-
Passed {submissionHiddenTestResultsAndStatus.hiddenTestResults.passed} / {submissionHiddenTestResultsAndStatus.hiddenTestResults.total} hidden test cases
478+
Passed{" "}
479+
{
480+
submissionHiddenTestResultsAndStatus.hiddenTestResults
481+
.passed
482+
}{" "}
483+
/{" "}
484+
{
485+
submissionHiddenTestResultsAndStatus.hiddenTestResults
486+
.total
487+
}{" "}
488+
hidden test cases
515489
</Typography.Text>
516490
)}
517491
</div>

0 commit comments

Comments
 (0)