File size: 2,131 Bytes
f91a684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export const COMPILER_API_BASE = '/api/compiler';

export interface TestCase {
  input: string;
  expectedOutput: string;
  isHidden: boolean;
}

export interface Problem {
  id: string;
  title: string;
  description: string;
  difficulty: string;
  testCases: TestCase[];
}

export interface ExecutionResult {
  stdout: string;
  stderr: string;
  exitCode: number;
  timeLimitExceeded: boolean;
  durationMs: number;
}

export interface RunRequest {
  language: string;
  code: string;
  customInput: string;
}

export interface TestCaseResult {
  passed: boolean;
  input: string;
  expectedOutput: string;
  actualOutput: string;
  error?: string;
  durationMs: number;
  timeLimitExceeded: boolean;
}

export interface SubmitResult {
  verdict: string;
  passed: number;
  total: number;
  results: TestCaseResult[];
}

async function readError(res: Response) {
  const text = await res.text();
  return text.trim() || res.statusText;
}

export async function runCode(req: RunRequest): Promise<ExecutionResult> {
  const res = await fetch(`${COMPILER_API_BASE}/run`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(req),
  });

  if (!res.ok) {
    throw new Error(`Execution failed: ${await readError(res)}`);
  }

  return res.json();
}

export async function submitCode(problemId: string, language: string, code: string): Promise<SubmitResult> {
  const res = await fetch(`${COMPILER_API_BASE}/submit`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ problemId, language, code }),
  });

  if (!res.ok) {
    throw new Error(`Submit failed: ${await readError(res)}`);
  }

  return res.json();
}

export async function fetchProblems(): Promise<Problem[]> {
  const res = await fetch(`${COMPILER_API_BASE}/problems`);
  if (!res.ok) throw new Error('Failed to fetch problems');
  return res.json();
}

export async function fetchProblem(id: string): Promise<Problem> {
  const res = await fetch(`${COMPILER_API_BASE}/problems/${id}`);
  if (!res.ok) throw new Error('Failed to fetch problem');
  return res.json();
}