-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sandbox.html
More file actions
141 lines (123 loc) · 5.23 KB
/
test-sandbox.html
File metadata and controls
141 lines (123 loc) · 5.23 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<title>Sandbox Test</title>
<style>
body { font-family: monospace; padding: 20px; background: #1a1a2e; color: #fff; }
pre { background: #0a0a0f; padding: 10px; border-radius: 5px; }
.success { color: #4ade80; }
.error { color: #f87171; }
button { padding: 10px 20px; margin: 10px 0; cursor: pointer; }
</style>
</head>
<body>
<h1>Sandbox Execution Test</h1>
<button onclick="runTest()">Run Test</button>
<pre id="output">Click "Run Test" to test the sandbox...</pre>
<script>
async function runTest() {
const output = document.getElementById('output');
output.textContent = 'Testing...\n';
// Test 1: Simple console.log
output.textContent += '\n=== Test 1: console.log ===\n';
try {
const result1 = await executeJavaScript('console.log("Hello from sandbox!")');
output.textContent += 'Result: ' + JSON.stringify(result1, null, 2) + '\n';
output.textContent += result1.success ? '✓ PASSED\n' : '✗ FAILED\n';
} catch (e) {
output.textContent += '✗ ERROR: ' + e.message + '\n';
}
// Test 2: Return value
output.textContent += '\n=== Test 2: Return value ===\n';
try {
const result2 = await executeJavaScript('return 2 + 2');
output.textContent += 'Result: ' + JSON.stringify(result2, null, 2) + '\n';
output.textContent += result2.output === '4' ? '✓ PASSED\n' : '✗ FAILED (expected "4")\n';
} catch (e) {
output.textContent += '✗ ERROR: ' + e.message + '\n';
}
// Test 3: Error handling
output.textContent += '\n=== Test 3: Error handling ===\n';
try {
const result3 = await executeJavaScript('throw new Error("test error")');
output.textContent += 'Result: ' + JSON.stringify(result3, null, 2) + '\n';
output.textContent += (!result3.success && result3.error) ? '✓ PASSED\n' : '✗ FAILED\n';
} catch (e) {
output.textContent += '✗ ERROR: ' + e.message + '\n';
}
// Test 4: Multiple console.logs
output.textContent += '\n=== Test 4: Multiple logs ===\n';
try {
const result4 = await executeJavaScript('console.log("line 1"); console.log("line 2"); console.log("line 3");');
output.textContent += 'Result: ' + JSON.stringify(result4, null, 2) + '\n';
output.textContent += result4.output.includes('line 1') && result4.output.includes('line 3') ? '✓ PASSED\n' : '✗ FAILED\n';
} catch (e) {
output.textContent += '✗ ERROR: ' + e.message + '\n';
}
output.textContent += '\n=== Tests Complete ===\n';
}
// Copy of the sandbox code for testing
async function executeJavaScript(code) {
const startTime = performance.now();
return new Promise((resolve) => {
let settled = false;
let iframe = null;
let blobUrl = null;
const cleanup = () => {
if (iframe?.parentNode) iframe.parentNode.removeChild(iframe);
if (blobUrl) URL.revokeObjectURL(blobUrl);
};
const timeout = setTimeout(() => {
if (!settled) {
settled = true;
cleanup();
resolve({ success: false, output: '', error: 'Timeout', duration: 10000 });
}
}, 10000);
const handleMessage = (event) => {
if (!settled && event.data && (event.data.type === 'result' || event.data.type === 'error')) {
settled = true;
clearTimeout(timeout);
window.removeEventListener('message', handleMessage);
cleanup();
const duration = performance.now() - startTime;
if (event.data.type === 'result') {
resolve({ success: true, output: event.data.output || '', duration });
} else {
resolve({ success: false, output: event.data.output || '', error: event.data.error, duration });
}
}
};
window.addEventListener('message', handleMessage);
const html = `<!DOCTYPE html>
<html><head><meta charset="utf-8"></head><body><script>
const output = [];
console.log = (...args) => {
output.push(args.map(a => typeof a === 'object' ? JSON.stringify(a, null, 2) : String(a)).join(' '));
};
console.error = console.log;
console.warn = console.log;
(async () => {
try {
const result = await (async () => { ${code} })();
if (result !== undefined) {
output.push(typeof result === 'object' ? JSON.stringify(result, null, 2) : String(result));
}
parent.postMessage({ type: 'result', output: output.join('\\n') }, '*');
} catch (e) {
parent.postMessage({ type: 'error', error: e.message || String(e), output: output.join('\\n') }, '*');
}
})();
</script></body></html>`;
const blob = new Blob([html], { type: 'text/html' });
blobUrl = URL.createObjectURL(blob);
iframe = document.createElement('iframe');
iframe.style.cssText = 'position:fixed;width:0;height:0;border:0;opacity:0;';
iframe.setAttribute('sandbox', 'allow-scripts');
iframe.src = blobUrl;
document.body.appendChild(iframe);
});
}
</script>
</body>
</html>