feat: added syntax check
This commit is contained in:
@@ -134,6 +134,47 @@ describe('PythonRuntimeManager', () => {
|
||||
await expect(inspectPromise).resolves.toEqual(['render', 'helper']);
|
||||
});
|
||||
|
||||
it('checks syntax and returns structured syntax errors', async () => {
|
||||
const worker = new MockWorker();
|
||||
const manager = new PythonRuntimeManager(() => worker as unknown as Worker);
|
||||
|
||||
const initPromise = manager.initialize();
|
||||
worker.emitMessage({ type: 'ready' });
|
||||
await initPromise;
|
||||
|
||||
const syntaxPromise = manager.syntaxCheck('def broken(:\n pass');
|
||||
await Promise.resolve();
|
||||
|
||||
const request = worker.postedMessages[0] as { type: string; requestId: string; code: string };
|
||||
expect(request.type).toBe('syntaxCheck');
|
||||
|
||||
worker.emitMessage({
|
||||
type: 'syntaxResult',
|
||||
requestId: request.requestId,
|
||||
errors: [
|
||||
{
|
||||
line: 1,
|
||||
column: 11,
|
||||
endLine: 1,
|
||||
endColumn: 12,
|
||||
message: 'invalid syntax',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await expect(syntaxPromise).resolves.toEqual({
|
||||
errors: [
|
||||
{
|
||||
line: 1,
|
||||
column: 11,
|
||||
endLine: 1,
|
||||
endColumn: 12,
|
||||
message: 'invalid syntax',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects when runtime returns run error', async () => {
|
||||
const worker = new MockWorker();
|
||||
const manager = new PythonRuntimeManager(() => worker as unknown as Worker);
|
||||
|
||||
43
tests/renderer/python/pythonSyntaxCheck.integration.test.ts
Normal file
43
tests/renderer/python/pythonSyntaxCheck.integration.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { beforeAll, describe, expect, it } from 'vitest';
|
||||
import { loadPyodide, type PyodideInterface } from 'pyodide';
|
||||
import { runPythonSyntaxCheck } from '../../../src/renderer/python/pythonSyntaxCheck';
|
||||
|
||||
describe('pythonSyntaxCheck integration (real pyodide)', () => {
|
||||
let runtime: PyodideInterface;
|
||||
|
||||
beforeAll(async () => {
|
||||
runtime = await loadPyodide();
|
||||
}, 60000);
|
||||
|
||||
it('returns no errors for syntactically valid python', async () => {
|
||||
const source = [
|
||||
'def normalize_blogmark(post):',
|
||||
' title = (post.get("title") or "").strip()',
|
||||
' if title:',
|
||||
' post["title"] = title',
|
||||
' return post',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
const errors = await runPythonSyntaxCheck(runtime, source);
|
||||
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns structured syntax diagnostics for invalid python', async () => {
|
||||
const source = [
|
||||
'def normalize_blogmark(post):',
|
||||
' if post.get("title")',
|
||||
' return post',
|
||||
'',
|
||||
].join('\n');
|
||||
|
||||
const errors = await runPythonSyntaxCheck(runtime, source);
|
||||
|
||||
expect(errors.length).toBeGreaterThan(0);
|
||||
expect(errors[0]).toEqual(expect.objectContaining({
|
||||
line: 2,
|
||||
message: expect.any(String),
|
||||
}));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user