-
Notifications
You must be signed in to change notification settings - Fork 0
[OA/CRM][Fullstack]: Test Runner #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import judge0Connector, { | ||
| type JudgeSubmissionRequestBody, | ||
| } from '@/lib/connectors/judge0.connector'; | ||
| import { SubmissionSchema } from '@/lib/schemas/submission.schema'; | ||
| import TaskTemplateService from '@/lib/services/task-template.service'; | ||
| // import { getSession } from '@/lib/utils/auth.utils'; | ||
| import { handleError } from '@/lib/utils/errors.utils'; | ||
| import { mapLanguageToJudge } from '@/lib/utils/language.utils'; | ||
| import { type NextRequest } from 'next/server'; | ||
|
|
||
| export async function POST( | ||
| request: NextRequest, | ||
| { params }: { params: Promise<{ taskTemplateId: string }> } | ||
| ) { | ||
| try { | ||
| const body = await request.json(); | ||
| const { taskTemplateId } = await params; | ||
| const parsed = SubmissionSchema.parse(body); | ||
| const languageId = mapLanguageToJudge(parsed.language); | ||
| const taskTemplate = await TaskTemplateService.getTaskTemplate( | ||
| taskTemplateId, | ||
| 'TODO: REPLACE THIS LATER LAITH WITH COOKIE' | ||
| ); | ||
| const tests = [...taskTemplate.privateTestCases, ...taskTemplate.publicTestCases]; | ||
| const formatted: JudgeSubmissionRequestBody[] = tests.map((test) => ({ | ||
| source_code: parsed.code, | ||
| language_id: languageId, | ||
| stdin: test.input, | ||
| expected_output: test.output, | ||
| })); | ||
|
|
||
| const result = await judge0Connector.executeSubmissions(formatted); | ||
| return Response.json({ | ||
| data: result, | ||
| status: 200, | ||
| }); | ||
| } catch (err) { | ||
| return handleError(err); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { handleError } from '@/lib/utils/errors.utils'; | ||
| import { assertRecruiterOrAbove } from '@/lib/utils/permissions.utils'; | ||
| import { type NextRequest } from 'next/server'; | ||
| import judge0Connector, { | ||
| type JudgeSubmissionRequestBody, | ||
| } from '@/lib/connectors/judge0.connector'; | ||
| import { TestSubmissionSchema } from '@/lib/schemas/submission.schema'; | ||
| import { mapLanguageToJudge } from '@/lib/utils/language.utils'; | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| await assertRecruiterOrAbove(request.headers); | ||
| const body = await request.json(); | ||
| const parsed = TestSubmissionSchema.parse(body); | ||
| const languageId = mapLanguageToJudge(parsed.language); | ||
| const formatted: JudgeSubmissionRequestBody[] = parsed.tests.map((test) => ({ | ||
| source_code: parsed.code, | ||
| language_id: languageId, | ||
| stdin: test.input, | ||
| expected_output: test.output, | ||
| })); | ||
|
|
||
| const result = await judge0Connector.executeSubmissions(formatted); | ||
|
|
||
| return Response.json({ | ||
| data: result, | ||
| status: 200, | ||
| }); | ||
| } catch (err) { | ||
| return handleError(err); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| type SubmissionSchemaDTO, | ||
| type TestSubmissionSchemaDTO, | ||
| } from '@/lib/schemas/submission.schema'; | ||
| import { type JudgeResultRequestBody } from '@/lib/connectors/judge0.connector'; | ||
|
|
||
| export async function runEditorSubmission( | ||
| submission: TestSubmissionSchemaDTO | ||
| ): Promise<JudgeResultRequestBody> { | ||
| const result = await fetch(`/api/runner`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(submission), | ||
| }); | ||
|
|
||
| const json = await result.json(); | ||
|
|
||
| if (!result.ok) { | ||
| throw new Error(json.message); | ||
| } | ||
|
|
||
| return json.data; | ||
| } | ||
|
|
||
| export async function runAssessmentSubmission( | ||
| taskTemplateId: string, | ||
cherman23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| submission: SubmissionSchemaDTO | ||
| ): Promise<JudgeResultRequestBody> { | ||
| const result = await fetch(`/api/runner/${taskTemplateId}`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(submission), | ||
| }); | ||
|
|
||
| const json = await result.json(); | ||
|
|
||
| if (!result.ok) { | ||
| throw new Error(json.message); | ||
| } | ||
|
|
||
| return json.data; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useState } from 'react'; | ||
| import { runEditorSubmission, runAssessmentSubmission } from '@/lib/api/runner'; | ||
| import { type JudgeResultRequestBody } from '@/lib/connectors/judge0.connector'; | ||
| import { type ProgrammingLanguage } from '@/generated/prisma'; | ||
| import { type TestCaseDTO } from '@/lib/schemas/task-template.schema'; | ||
|
|
||
| export default function useTestRunner(code: string, language: ProgrammingLanguage) { | ||
| const [error, setError] = useState<Error>(); | ||
| const [loading, setLoading] = useState<boolean>(false); | ||
| const [output, setOutput] = useState<JudgeResultRequestBody>(); | ||
|
|
||
| async function runEditPageTests(tests: TestCaseDTO[]) { | ||
| try { | ||
| setLoading(true); | ||
| const result = await runEditorSubmission({ | ||
| code, | ||
| language, | ||
| tests, | ||
| }); | ||
| setOutput(result); | ||
| console.warn(result); | ||
| } catch (err) { | ||
| setError(err as Error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| async function runAssessmentTests(taskTemplateId: string) { | ||
| try { | ||
| setLoading(true); | ||
| const result = await runAssessmentSubmission(taskTemplateId, { | ||
| code, | ||
| language, | ||
| }); | ||
| setOutput(result); | ||
| } catch (err) { | ||
| setError(err as Error); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| runAssessmentTests, | ||
| runEditPageTests, | ||
| error, | ||
| loading, | ||
| output, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { ProgrammingLanguage } from '@/generated/prisma'; | ||
| import { testCaseSchema } from '@/lib/schemas/task-template.schema'; | ||
| import { z } from 'zod'; | ||
|
|
||
| export const SubmissionSchema = z.object({ | ||
| code: z.string(), | ||
| language: z.enum(ProgrammingLanguage), | ||
| additionalTests: z.array(testCaseSchema).optional(), // May be useful for letting users create their own test cases in the future | ||
| }); | ||
|
|
||
| export const TestSubmissionSchema = SubmissionSchema.omit({ | ||
| additionalTests: true, | ||
| }).extend({ | ||
| tests: z.array(testCaseSchema), | ||
| }); | ||
|
|
||
| export type SubmissionSchemaDTO = z.infer<typeof SubmissionSchema>; | ||
| export type TestSubmissionSchemaDTO = z.infer<typeof TestSubmissionSchema>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.