feat:add test
This commit is contained in:
parent
2375707515
commit
7ea49fb495
4902
package-lock.json
generated
4902
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -17,8 +17,10 @@
|
|||||||
"react-dom": "^19.0.0"
|
"react-dom": "^19.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@babel/preset-typescript": "^7.26.0",
|
||||||
"@eslint/eslintrc": "^3",
|
"@eslint/eslintrc": "^3",
|
||||||
"@types/dockerode": "^3.3.32",
|
"@types/dockerode": "^3.3.32",
|
||||||
|
"@types/jest": "^29.5.14",
|
||||||
"@types/node": "^20",
|
"@types/node": "^20",
|
||||||
"@types/react": "^19",
|
"@types/react": "^19",
|
||||||
"@types/react-dom": "^19",
|
"@types/react-dom": "^19",
|
||||||
@ -26,9 +28,11 @@
|
|||||||
"eslint": "^9",
|
"eslint": "^9",
|
||||||
"eslint-config-next": "15.1.3",
|
"eslint-config-next": "15.1.3",
|
||||||
"html-webpack-plugin": "^5.6.3",
|
"html-webpack-plugin": "^5.6.3",
|
||||||
|
"jest": "^29.7.0",
|
||||||
"node-loader": "^2.1.0",
|
"node-loader": "^2.1.0",
|
||||||
"postcss": "^8",
|
"postcss": "^8",
|
||||||
"tailwindcss": "^3.4.1",
|
"tailwindcss": "^3.4.1",
|
||||||
|
"ts-jest": "^29.2.5",
|
||||||
"ts-loader": "^9.5.1",
|
"ts-loader": "^9.5.1",
|
||||||
"typescript": "^5.7.2",
|
"typescript": "^5.7.2",
|
||||||
"webpack": "^5.97.1",
|
"webpack": "^5.97.1",
|
||||||
|
115
src/actions/enhancedmultiFileRunner.test.ts
Normal file
115
src/actions/enhancedmultiFileRunner.test.ts
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
const { runMultiFileCodeWithOptionalTestData, setGiteaRepoUrl, setGiteaToken, setTestDataPath } = require('./enhancedmultiFileRunner');
|
||||||
|
const Docker = require('dockerode');
|
||||||
|
const fs = require('fs/promises');
|
||||||
|
const path = require('path');
|
||||||
|
const axios = require('axios');
|
||||||
|
|
||||||
|
jest.mock('dockerode');
|
||||||
|
jest.mock('fs/promises');
|
||||||
|
jest.mock('axios');
|
||||||
|
|
||||||
|
const mockDocker = new Docker();
|
||||||
|
const mockContainer = {
|
||||||
|
start: jest.fn(),
|
||||||
|
logs: jest.fn(),
|
||||||
|
wait: jest.fn(),
|
||||||
|
remove: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
mockDocker.createContainer = jest.fn().mockResolvedValue(mockContainer);
|
||||||
|
mockContainer.logs.mockResolvedValue({ on: jest.fn() });
|
||||||
|
mockContainer.wait.mockResolvedValue({ StatusCode: 0 });
|
||||||
|
|
||||||
|
describe('runMultiFileCodeWithOptionalTestData', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if files are not provided', async () => {
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({ files: [], language: 'c' }))
|
||||||
|
.rejects.toThrow('Files and language are required.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if language is not provided', async () => {
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({ files: [{ name: 'main.c', content: '' }], language: '' }))
|
||||||
|
.rejects.toThrow('Files and language are required.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if an unsupported language is provided', async () => {
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({ files: [{ name: 'main.c', content: '' }], language: 'ruby' }))
|
||||||
|
.rejects.toThrow("Language 'ruby' is not supported.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully compile and run C code', async () => {
|
||||||
|
const result = await runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'main.c', content: 'int main() { return 0; }' }],
|
||||||
|
language: 'c',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toHaveProperty('output');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully compile and run Java code', async () => {
|
||||||
|
const result = await runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'Main.java', content: 'public class Main { public static void main(String[] args) {} }' }],
|
||||||
|
language: 'java',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toHaveProperty('output');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should successfully compile and run Python code', async () => {
|
||||||
|
const result = await runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'script.py', content: 'print("Hello, World!")' }],
|
||||||
|
language: 'python',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toHaveProperty('output');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch test data from Gitea if configured', async () => {
|
||||||
|
await setGiteaRepoUrl('http://example.com');
|
||||||
|
await setGiteaToken('token');
|
||||||
|
await setTestDataPath('testData');
|
||||||
|
|
||||||
|
axios.get.mockResolvedValue({ data: [{ type: 'file', name: 'testData.txt', download_url: 'http://example.com/testData.txt' }] });
|
||||||
|
|
||||||
|
const result = await runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'main.c', content: '' }],
|
||||||
|
language: 'c',
|
||||||
|
testDataFiles: ['testData.txt'],
|
||||||
|
expectedAnswerFiles: ['expected.txt'],
|
||||||
|
resultOutputFile: 'result.txt',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toHaveProperty('output');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle errors during file operations', async () => {
|
||||||
|
fs.writeFile.mockRejectedValue(new Error('File write error'));
|
||||||
|
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'main.c', content: '' }],
|
||||||
|
language: 'c',
|
||||||
|
})).rejects.toThrow('File write error');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle errors during Docker container operations', async () => {
|
||||||
|
mockDocker.createContainer.mockRejectedValue(new Error('Docker error'));
|
||||||
|
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'main.c', content: '' }],
|
||||||
|
language: 'c',
|
||||||
|
})).rejects.toThrow('Docker error');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle errors during HTTP requests', async () => {
|
||||||
|
axios.get.mockRejectedValue(new Error('HTTP error'));
|
||||||
|
|
||||||
|
await expect(runMultiFileCodeWithOptionalTestData({
|
||||||
|
files: [{ name: 'main.c', content: '' }],
|
||||||
|
language: 'c',
|
||||||
|
testDataFiles: ['testData.txt'],
|
||||||
|
})).rejects.toThrow('HTTP error');
|
||||||
|
});
|
||||||
|
});
|
86
src/app/page-old.tsx
Normal file
86
src/app/page-old.tsx
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
import { runCode } from '@/actions'; // 导入 Server Action
|
||||||
|
|
||||||
|
// import './styles.css'; // 引入样式
|
||||||
|
//
|
||||||
|
// const root = document.getElementById('root');
|
||||||
|
// if (root) {
|
||||||
|
// root.innerHTML = 'Hello, world!';
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /// <reference types="webpack-env" />
|
||||||
|
//
|
||||||
|
// if (module.hot) {
|
||||||
|
// module.hot.accept(() => {
|
||||||
|
// console.log('HMR is working!');
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 启用 HMR
|
||||||
|
// if (module.hot) {
|
||||||
|
// module.hot.accept('./styles.css', () => {
|
||||||
|
// console.log('CSS updated!');
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
const CodeRunner = () => {
|
||||||
|
const [code, setCode] = useState('');
|
||||||
|
const [language, setLanguage] = useState('c'); // 默认语言为 C
|
||||||
|
const [output, setOutput] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
|
const runCodeHandler = async () => {
|
||||||
|
try {
|
||||||
|
// 调用 Server Action 执行代码
|
||||||
|
const result = await runCode({ code, language });
|
||||||
|
|
||||||
|
setOutput(result.output || ''); // 如果没有输出,设置为空字符串
|
||||||
|
setError(result.error || ''); // 如果没有错误,设置为空字符串
|
||||||
|
} catch (err) {
|
||||||
|
setError((err as Error).message);
|
||||||
|
setOutput('');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Code Runner</h1>
|
||||||
|
<textarea
|
||||||
|
value={code}
|
||||||
|
onChange={(e) => setCode(e.target.value)}
|
||||||
|
placeholder="Write your code here"
|
||||||
|
rows={10}
|
||||||
|
cols={50}
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<label>
|
||||||
|
Language:
|
||||||
|
<select value={language} onChange={(e) => setLanguage(e.target.value)}>
|
||||||
|
<option value="c">C</option>
|
||||||
|
<option value="java">Java</option>
|
||||||
|
<option value="python">Python</option> {/* 添加 Python */}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<button onClick={runCodeHandler}>Run Code</button>
|
||||||
|
|
||||||
|
{output && (
|
||||||
|
<div>
|
||||||
|
<h3>Output:</h3>
|
||||||
|
<pre>{output}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{error && (
|
||||||
|
<div>
|
||||||
|
<h3>Error:</h3>
|
||||||
|
<pre>{error}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CodeRunner;
|
152
src/app/page.tsx
152
src/app/page.tsx
@ -1,86 +1,110 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { runCode } from '@/actions'; // 导入 Server Action
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
import { runMultiFileCodeWithOptionalTestData } from '@/actions/enhancedmultiFileRunner';
|
||||||
|
|
||||||
// import './styles.css'; // 引入样式
|
const TestPage = () => {
|
||||||
//
|
const [code, setCode] = useState<string>('');
|
||||||
// const root = document.getElementById('root');
|
const [language, setLanguage] = useState<string>('c');
|
||||||
// if (root) {
|
const [result, setResult] = useState<string>('');
|
||||||
// root.innerHTML = 'Hello, world!';
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /// <reference types="webpack-env" />
|
|
||||||
//
|
|
||||||
// if (module.hot) {
|
|
||||||
// module.hot.accept(() => {
|
|
||||||
// console.log('HMR is working!');
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // 启用 HMR
|
|
||||||
// if (module.hot) {
|
|
||||||
// module.hot.accept('./styles.css', () => {
|
|
||||||
// console.log('CSS updated!');
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
const handleRun = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setResult('');
|
||||||
|
|
||||||
const CodeRunner = () => {
|
const testFiles = [
|
||||||
const [code, setCode] = useState('');
|
{ name: 'input1.txt', content: '1 2 3' },
|
||||||
const [language, setLanguage] = useState('c'); // 默认语言为 C
|
{ name: 'input2.txt', content: '4 5 6' },
|
||||||
const [output, setOutput] = useState('');
|
];
|
||||||
const [error, setError] = useState('');
|
|
||||||
|
const expectedFiles = [
|
||||||
|
{ name: 'output1.txt', content: '6' },
|
||||||
|
{ name: 'output2.txt', content: '15' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const codeFile = {
|
||||||
|
name: language === 'c' ? 'main.c' : language === 'python' ? 'script.py' : 'Main.java',
|
||||||
|
content: code,
|
||||||
|
};
|
||||||
|
|
||||||
const runCodeHandler = async () => {
|
|
||||||
try {
|
try {
|
||||||
// 调用 Server Action 执行代码
|
const result = await runMultiFileCodeWithOptionalTestData({
|
||||||
const result = await runCode({ code, language });
|
files: [codeFile],
|
||||||
|
language,
|
||||||
|
testDataFiles: testFiles.map((file) => file.name),
|
||||||
|
expectedAnswerFiles: expectedFiles.map((file) => file.name),
|
||||||
|
resultOutputFile: 'result.txt',
|
||||||
|
});
|
||||||
|
|
||||||
setOutput(result.output || ''); // 如果没有输出,设置为空字符串
|
setResult(JSON.stringify(result, null, 2));
|
||||||
setError(result.error || ''); // 如果没有错误,设置为空字符串
|
} catch (error) {
|
||||||
} catch (err) {
|
// 使用类型断言确保 error 是 Error 类型
|
||||||
setError((err as Error).message);
|
setResult(`Error: ${(error as Error).message}`);
|
||||||
setOutput('');
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
|
||||||
<h1>Code Runner</h1>
|
<h1>Test Page</h1>
|
||||||
<textarea
|
<div style={{ marginBottom: '20px' }}>
|
||||||
value={code}
|
|
||||||
onChange={(e) => setCode(e.target.value)}
|
|
||||||
placeholder="Write your code here"
|
|
||||||
rows={10}
|
|
||||||
cols={50}
|
|
||||||
/>
|
|
||||||
<div>
|
|
||||||
<label>
|
<label>
|
||||||
Language:
|
<strong>Language:</strong>
|
||||||
<select value={language} onChange={(e) => setLanguage(e.target.value)}>
|
<select
|
||||||
|
value={language}
|
||||||
|
onChange={(e) => setLanguage(e.target.value)}
|
||||||
|
style={{ marginLeft: '10px' }}
|
||||||
|
>
|
||||||
<option value="c">C</option>
|
<option value="c">C</option>
|
||||||
<option value="java">Java</option>
|
<option value="java">Java</option>
|
||||||
<option value="python">Python</option> {/* 添加 Python */}
|
<option value="python">Python</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<button onClick={runCodeHandler}>Run Code</button>
|
<div style={{ marginBottom: '20px' }}>
|
||||||
|
<label>
|
||||||
{output && (
|
<strong>Code:</strong>
|
||||||
<div>
|
<textarea
|
||||||
<h3>Output:</h3>
|
value={code}
|
||||||
<pre>{output}</pre>
|
onChange={(e) => setCode(e.target.value)}
|
||||||
</div>
|
rows={10}
|
||||||
)}
|
style={{ width: '100%', fontFamily: 'monospace', fontSize: '14px' }}
|
||||||
{error && (
|
/>
|
||||||
<div>
|
</label>
|
||||||
<h3>Error:</h3>
|
</div>
|
||||||
<pre>{error}</pre>
|
<button
|
||||||
</div>
|
onClick={handleRun}
|
||||||
)}
|
style={{
|
||||||
|
padding: '10px 20px',
|
||||||
|
background: 'blue',
|
||||||
|
color: 'white',
|
||||||
|
border: 'none',
|
||||||
|
borderRadius: '5px',
|
||||||
|
cursor: 'pointer',
|
||||||
|
}}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{loading ? 'Running...' : 'Run Code'}
|
||||||
|
</button>
|
||||||
|
<div style={{ marginTop: '20px' }}>
|
||||||
|
<h2>Result</h2>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
background: '#f4f4f4',
|
||||||
|
padding: '10px',
|
||||||
|
border: '1px solid #ddd',
|
||||||
|
borderRadius: '5px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{result || 'No results yet.'}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default CodeRunner;
|
export default TestPage;
|
||||||
|
Loading…
Reference in New Issue
Block a user