mirror of
https://github.com/bendtherules/ask262.git
synced 2026-08-19 00:31:06 +00:00
feat(evaluate) - Run evaluateInEngine262 in child_process with strict timeout
This commit is contained in:
@@ -7,12 +7,7 @@
|
||||
* Usage: bun run src/test/manual/test-evaluate-in-engine262.ts ["your JavaScript code"]
|
||||
*/
|
||||
|
||||
import { createEvaluateInEngine262Tool } from "../../agent-tools";
|
||||
|
||||
interface EvaluateResult {
|
||||
importantSections: string[][];
|
||||
otherSections: string[][];
|
||||
}
|
||||
import { createEvaluateInEngine262Tool } from "../../agent-tools/index.js";
|
||||
|
||||
async function main() {
|
||||
// Get test code from command line or use default
|
||||
@@ -39,13 +34,15 @@ async function main() {
|
||||
|
||||
console.log("Executing tool...\n");
|
||||
try {
|
||||
const result = await evaluateTool.func({ code: testCode });
|
||||
const result = await evaluateTool({ code: testCode });
|
||||
|
||||
// Parse and verify results
|
||||
const parsed: EvaluateResult = JSON.parse(result);
|
||||
// Verify results
|
||||
if ("error" in result) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
const importantCount = parsed.importantSections.length;
|
||||
const otherCount = parsed.otherSections.length;
|
||||
const importantCount = result.importantSections.length;
|
||||
const otherCount = result.otherSections.length;
|
||||
const totalCount = importantCount + otherCount;
|
||||
|
||||
console.log(`\n✓ Captured ${totalCount} marks`);
|
||||
@@ -53,9 +50,9 @@ async function main() {
|
||||
console.log("");
|
||||
|
||||
// Flatten and dedupe section IDs
|
||||
const importantIds = new Set(parsed.importantSections.flat());
|
||||
const importantIds = new Set(result.importantSections);
|
||||
const otherIds = new Set(
|
||||
parsed.otherSections.flat().filter((id) => !importantIds.has(id)),
|
||||
result.otherSections.filter((id: string) => !importantIds.has(id)),
|
||||
);
|
||||
|
||||
const totalUnique = importantIds.size + otherIds.size;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Test timeout functionality for evaluateInEngine262 tool
|
||||
* Tests that code execution properly times out after the specified duration
|
||||
*/
|
||||
|
||||
import { createEvaluateInEngine262Tool } from "../../agent-tools/evaluateInEngine262.js";
|
||||
|
||||
async function testTimeout() {
|
||||
console.log("=== Testing evaluateInEngine262 Timeout ===\n");
|
||||
|
||||
// Test 1: Quick code should complete before timeout
|
||||
console.log("Test 1: Quick code (should succeed)...");
|
||||
const quickTool = createEvaluateInEngine262Tool(5000); // 5 second timeout
|
||||
const start1 = Date.now();
|
||||
const quickResult = await quickTool({ code: "console.log('hello'); 1 + 1" });
|
||||
const elapsed1 = Date.now() - start1;
|
||||
|
||||
if (quickResult.error) {
|
||||
throw new Error(`Quick code failed: ${quickResult.error}`);
|
||||
}
|
||||
console.log(`✓ Completed in ${elapsed1}ms`);
|
||||
console.log(
|
||||
` Console output: ${JSON.stringify(quickResult.consoleOutput)}\n`,
|
||||
);
|
||||
|
||||
// Test 2: Long-running code should timeout
|
||||
console.log("Test 2: Long-running code (should timeout after 500ms)...");
|
||||
const slowTool = createEvaluateInEngine262Tool(500); // 500ms timeout
|
||||
const start2 = Date.now();
|
||||
const slowResult = await slowTool({
|
||||
code: `
|
||||
// Busy-wait loop that takes ~5 seconds
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < 5000) {
|
||||
// Busy wait - no yielding
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
Math.sqrt(i);
|
||||
}
|
||||
}
|
||||
console.log("completed");
|
||||
`,
|
||||
});
|
||||
const elapsed2 = Date.now() - start2;
|
||||
|
||||
if (!slowResult.error) {
|
||||
throw new Error("Expected timeout error but code completed successfully");
|
||||
}
|
||||
if (!slowResult.error.includes("timeout")) {
|
||||
throw new Error(`Expected timeout error but got: ${slowResult.error}`);
|
||||
}
|
||||
console.log(`✓ Timed out in ${elapsed2}ms`);
|
||||
console.log(` Error message: ${slowResult.error}\n`);
|
||||
|
||||
// Note: worker.terminate() sends signal but CPU-bound loops may not stop immediately.
|
||||
// The important thing is that we got a timeout error before the 5-second loop completed.
|
||||
if (elapsed2 > 5000) {
|
||||
throw new Error(`Timeout took too long: ${elapsed2}ms (expected < 5000ms)`);
|
||||
}
|
||||
|
||||
console.log(` (Note: Total time includes worker termination cleanup)`);
|
||||
console.log("=== All timeout tests passed! ✓ ===");
|
||||
}
|
||||
|
||||
testTimeout().catch((error) => {
|
||||
console.error("\nTest failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user