Skip to content

Commit a02a228

Browse files
committed
Add a 'test' button to each test in Puter.js's test suite
1 parent 6da8ebf commit a02a228

File tree

1 file changed

+88
-6
lines changed

1 file changed

+88
-6
lines changed

src/puter-js/test/run.html

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
.test-container{
3939
font-family: monospace;
4040
}
41+
.test-checkbox-container{
42+
display: flex;
43+
align-items: center;
44+
}
4145
.test-container:hover{
4246
background-color: #f0f0f0;
4347
}
@@ -47,6 +51,35 @@
4751
.test-container input{
4852
float: left;
4953
}
54+
.test-run-button {
55+
margin-left: 10px;
56+
background-color: #4c84af;
57+
border: none;
58+
color: white;
59+
padding: 5px 10px;
60+
text-align: center;
61+
text-decoration: none;
62+
display: inline-block;
63+
font-size: 12px;
64+
cursor: pointer;
65+
border-radius: 3px;
66+
opacity: 0;
67+
transition: opacity 0.2s ease;
68+
}
69+
.test-container:hover .test-run-button {
70+
opacity: 1;
71+
}
72+
.test-run-button:hover {
73+
background-color: #3a6a8a;
74+
}
75+
.test-run-button:disabled {
76+
background-color: #999;
77+
cursor: not-allowed;
78+
opacity: 1;
79+
}
80+
.test-run-button:disabled:hover {
81+
background-color: #999;
82+
}
5083
</style>
5184
<script>
5285
document.addEventListener("DOMContentLoaded", () => {
@@ -77,24 +110,33 @@
77110
$('#tests').append('<h2>File System Tests</h2>');
78111
for (let i = 0; i < fsTests.length; i++) {
79112
$('#tests').append(`<div class="test-container" id="fsTests-container-${i}">
80-
<input type="checkbox" class="test-checkbox" id="fsTests${i}" checked>
81-
<label for="fsTests${i}">${fsTests[i].name}</label><br>
113+
<div class="test-checkbox-container">
114+
<input type="checkbox" class="test-checkbox" id="fsTests${i}" checked>
115+
<label for="fsTests${i}">${fsTests[i].name}</label><br>
116+
<button class="test-run-button" onclick="runSingleTest('fs', ${i})">Run Test</button>
117+
</div>
82118
</div>`);
83119
}
84120

85121
$('#tests').append('<h2>Key Value Tests</h2>');
86122
for (let i = 0; i < kvTests.length; i++) {
87123
$('#tests').append(`<div class="test-container" id="kvTests-container-${i}">
88-
<input type="checkbox" class="test-checkbox" id="kvTests${i}" checked>
89-
<label for="kvTests${i}">${kvTests[i].name}</label><br>
124+
<div class="test-checkbox-container">
125+
<input type="checkbox" class="test-checkbox" id="kvTests${i}" checked>
126+
<label for="kvTests${i}">${kvTests[i].name}</label><br>
127+
<button class="test-run-button" onclick="runSingleTest('kv', ${i})">Run Test</button>
128+
</div>
90129
</div>`);
91130
}
92131

93132
$('#tests').append('<h2>AI Tests</h2>');
94133
for (let i = 0; i < aiTests.length; i++) {
95134
$('#tests').append(`<div class="test-container" id="aiTests-container-${i}">
96-
<input type="checkbox" class="test-checkbox" id="aiTests${i}" checked>
97-
<label for="aiTests${i}">${aiTests[i].name}</label><br>
135+
<div class="test-checkbox-container">
136+
<input type="checkbox" class="test-checkbox" id="aiTests${i}" checked>
137+
<label for="aiTests${i}">${aiTests[i].name}</label><br>
138+
<button class="test-run-button" onclick="runSingleTest('ai', ${i})">Run Test</button>
139+
</div>
98140
</div>`);
99141
}
100142

@@ -104,6 +146,46 @@
104146
}
105147
}
106148

149+
async function runSingleTest(testType, index) {
150+
const testSuites = {
151+
'fs': fsTests,
152+
'kv': kvTests,
153+
'ai': aiTests
154+
};
155+
156+
const tests = testSuites[testType];
157+
const containerId = `${testType}Tests-container-${index}`;
158+
const buttonSelector = `#${containerId} .test-run-button`;
159+
160+
// Disable the button and show it while running
161+
$(buttonSelector).prop('disabled', true).text('Running...');
162+
163+
// Clear previous results
164+
$(`#${containerId}`).css('background-color', '');
165+
$(`#${containerId} pre`).remove();
166+
167+
try {
168+
await tests[index]();
169+
// make this test's container green
170+
$(`#${containerId}`).css('background-color', '#85e085');
171+
} catch (e) {
172+
console.error(`${testType.toUpperCase()} Test failed:`, tests[index].name, e);
173+
// make this test's container red
174+
$(`#${containerId}`).css('background-color', '#ffbfbf');
175+
// message - show full error information including JSON details
176+
let errorMessage = e.message || e.toString();
177+
if (e.originalError) {
178+
errorMessage += '\n\nOriginal Error:\n' + JSON.stringify(e.originalError, null, 2);
179+
}
180+
$(`#${containerId}`).append(`<pre style="color:#c00000; white-space: pre-wrap; font-size: 12px; margin: 5px 0; padding: 10px; background-color: #f8f8f8; border-radius: 3px;">${errorMessage}</pre>`);
181+
} finally {
182+
// Re-enable the button
183+
$(buttonSelector).prop('disabled', false).text('Run Test');
184+
}
185+
}
186+
187+
window.runSingleTest = runSingleTest;
188+
107189
async function runTests() {
108190
// go through fsTests and run each test
109191
for (let i = 0; i < fsTests.length; i++) {

0 commit comments

Comments
 (0)