Repository navigation
Expand file tree
/
Copy pathtests.html
More file actions
420 lines (357 loc) · 14.8 KB
/
Copy pathtests.html
File metadata and controls
420 lines (357 loc) · 14.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Configurator Tests - Simple Test Runner</title>
<style>
body {
font-family: 'Courier New', monospace;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background-color: #1e1e1e;
color: #d4d4d4;
}
.header {
background-color: #2d2d30;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
border-left: 4px solid #007acc;
}
.header h1 {
margin: 0 0 10px 0;
color: #4ec9b0;
}
.header p {
margin: 0;
color: #858585;
}
.test-suite {
background-color: #2d2d30;
padding: 15px;
margin-bottom: 15px;
border-radius: 5px;
}
.test-suite h2 {
margin: 0 0 15px 0;
color: #4ec9b0;
font-size: 18px;
border-bottom: 1px solid #3e3e42;
padding-bottom: 10px;
}
.test {
padding: 10px;
margin-bottom: 8px;
border-radius: 3px;
background-color: #252526;
border-left: 3px solid #858585;
}
.test.pass {
border-left-color: #4ec9b0;
background-color: #1a3a2d;
}
.test.fail {
border-left-color: #f48771;
background-color: #3a1e1e;
}
.test-name {
font-weight: bold;
margin-bottom: 5px;
}
.test.pass .test-name::before {
content: "✓ ";
color: #4ec9b0;
}
.test.fail .test-name::before {
content: "✗ ";
color: #f48771;
}
.test-message {
font-size: 12px;
color: #858585;
margin-left: 20px;
}
.test.fail .test-message {
color: #f48771;
}
.summary {
background-color: #2d2d30;
padding: 20px;
border-radius: 5px;
margin-top: 20px;
text-align: center;
}
.summary h2 {
margin: 0 0 15px 0;
color: #4ec9b0;
}
.summary.all-pass {
border: 2px solid #4ec9b0;
}
.summary.has-failures {
border: 2px solid #f48771;
}
.stat {
display: inline-block;
margin: 0 15px;
font-size: 18px;
}
.stat-pass {
color: #4ec9b0;
}
.stat-fail {
color: #f48771;
}
</style>
</head>
<body>
<div class="header">
<h1>Configurator Test Suite</h1>
<p>Simple browser-based test runner (2007-era approach)</p>
</div>
<div id="test-results"></div>
<div id="summary" class="summary">
<h2>Test Summary</h2>
<div id="summary-stats"></div>
</div>
<script src="src/system-configurator.js"></script>
<script>
// Simple test framework (2007-era style)
const testResults = [];
let currentSuite = null;
function describe(suiteName, fn) {
currentSuite = { name: suiteName, tests: [] };
fn();
testResults.push(currentSuite);
currentSuite = null;
}
function it(testName, fn) {
try {
fn();
currentSuite.tests.push({ name: testName, passed: true, message: 'Passed' });
} catch (error) {
currentSuite.tests.push({
name: testName,
passed: false,
message: error.message
});
}
}
function assertEqual(actual, expected, message) {
if (actual !== expected) {
throw new Error(message || 'Expected ' + expected + ' but got ' + actual);
}
}
function assertTrue(value, message) {
if (!value) {
throw new Error(message || 'Expected true but got ' + value);
}
}
function assertGreaterThan(actual, threshold, message) {
if (actual <= threshold) {
throw new Error(message || 'Expected ' + actual + ' to be greater than ' + threshold);
}
}
// TEST SUITES
describe('SystemConfigurator - Basic Functionality', function() {
it('should create a configurator with base price', function() {
const config = new SystemConfigurator({ basePrice: 1000 });
assertEqual(config.basePrice, 1000, 'Base price should be 1000');
});
it('should return base price when no options selected', function() {
const config = new SystemConfigurator({ basePrice: 1000 });
assertEqual(config.getPrice(), 1000, 'Price should equal base price');
});
it('should calculate price with selected options', function() {
const config = new SystemConfigurator({
basePrice: 1000,
options: [[{
choices: [
{ price: 0, name: "None", warrantyEligible: false, selected: true },
{ price: 200, name: "Option A", warrantyEligible: true, selected: false }
],
quantity: 1
}]]
});
config.selectOption(0, 0, 1);
assertEqual(config.getPrice(), 1200, 'Price should be 1000 + 200');
});
it('should respect option quantity', function() {
const config = new SystemConfigurator({
basePrice: 1000,
options: [[{
choices: [
{ price: 0, name: "None", warrantyEligible: false, selected: true },
{ price: 100, name: "Option A", warrantyEligible: true, selected: false }
],
quantity: 3
}]]
});
config.selectOption(0, 0, 1);
assertEqual(config.getPrice(), 1300, 'Price should be 1000 + (100 * 3)');
});
});
describe('SystemConfigurator - Recursive Subitems', function() {
it('should add subitem price to total', function() {
const config = new SystemConfigurator({ basePrice: 1000 });
const subitem = new SimpleItem({ name: 'sub', price: 500, quantity: 1 });
config.addSubitem(subitem);
assertEqual(config.getPrice(), 1500, 'Price should include subitem');
});
it('should recursively calculate nested configurator prices', function() {
const mainConfig = new SystemConfigurator({ basePrice: 1000 });
const subConfig = new SystemConfigurator({
basePrice: 500,
options: [[{
choices: [
{ price: 0, name: "None", warrantyEligible: false, selected: true },
{ price: 200, name: "Option A", warrantyEligible: true, selected: false }
],
quantity: 1
}]]
});
subConfig.selectOption(0, 0, 1); // Select 200 option
mainConfig.addSubitem(subConfig);
assertEqual(mainConfig.getPrice(), 1700, 'Price should recursively include subconfig');
});
it('should support multiple levels of nesting', function() {
const level1 = new SystemConfigurator({ basePrice: 1000 });
const level2 = new SystemConfigurator({ basePrice: 500 });
const level3 = new SystemConfigurator({ basePrice: 250 });
level2.addSubitem(level3);
level1.addSubitem(level2);
assertEqual(level1.getPrice(), 1750, 'Price should include all nested levels');
});
it('should respect subitem quantity', function() {
const config = new SystemConfigurator({ basePrice: 1000 });
const subitem = new SimpleItem({ name: 'sub', price: 500, quantity: 3 });
config.addSubitem(subitem);
assertEqual(config.getPrice(), 2500, 'Price should be 1000 + (500 * 3)');
});
});
describe('SimpleItem - Polymorphism', function() {
it('should return fixed price', function() {
const item = new SimpleItem({ name: 'simple', price: 800, quantity: 1 });
assertEqual(item.getPrice(), 800, 'Should return fixed price');
});
it('should work polymorphically with SystemConfigurator', function() {
const items = [
new SystemConfigurator({ basePrice: 1000 }),
new SimpleItem({ name: 'simple', price: 500, quantity: 1 }),
new SystemConfigurator({ basePrice: 750 })
];
let total = 0;
items.forEach(function(item) {
total += item.getPrice();
});
assertEqual(total, 2250, 'Polymorphic total should be correct');
});
});
describe('Warranty Calculations', function() {
it('should calculate warranty for base price', function() {
const config = new SystemConfigurator({
basePrice: 8000,
warranties: [{
name: "Standard",
tiers: [
{ minPrice: 0, price: 0 },
{ minPrice: 5000, price: 100 }
]
}]
});
config.setWarranty("Standard");
const warrantyPrice = config.getWarrantyPrice();
assertEqual(warrantyPrice, 100, 'Warranty should be 100 for price > 5000');
});
it('should include warranty in total price', function() {
const config = new SystemConfigurator({
basePrice: 8000,
warranties: [{
name: "Standard",
tiers: [
{ minPrice: 0, price: 0 },
{ minPrice: 5000, price: 100 }
]
}]
});
config.setWarranty("Standard");
assertEqual(config.getPrice(), 8100, 'Total should include warranty');
});
});
describe('Complex Integration Test', function() {
it('should handle complex nested configuration correctly', function() {
// Create a cluster with multiple nodes
const cluster = new SystemConfigurator({
basePrice: 5000,
options: [[{
choices: [
{ price: 0, name: "None", warrantyEligible: false, selected: true },
{ price: 500, name: "Management", warrantyEligible: true, selected: false }
],
quantity: 1
}]]
});
const node1 = new SystemConfigurator({
basePrice: 2000,
options: [[{
choices: [
{ price: 0, name: "None", warrantyEligible: false, selected: true },
{ price: 400, name: "CPU", warrantyEligible: true, selected: false }
],
quantity: 1
}]]
});
const node2 = new SystemConfigurator({ basePrice: 1500 });
const switches = new SimpleItem({ name: 'switch', price: 800, quantity: 2 });
cluster.selectOption(0, 0, 1); // Select management
node1.selectOption(0, 0, 1); // Select CPU
cluster.addSubitem(node1);
cluster.addSubitem(node2);
cluster.addSubitem(switches);
// 5000 + 500 + (2000 + 400) + 1500 + (800 * 2) = 11000
assertEqual(cluster.getPrice(), 11000, 'Complex configuration should calculate correctly');
});
});
// RENDER RESULTS
function renderResults() {
const resultsDiv = document.getElementById('test-results');
const summaryDiv = document.getElementById('summary');
const summaryStats = document.getElementById('summary-stats');
let totalTests = 0;
let passedTests = 0;
let failedTests = 0;
let html = '';
testResults.forEach(function(suite) {
html += '<div class="test-suite">';
html += '<h2>' + suite.name + '</h2>';
suite.tests.forEach(function(test) {
totalTests++;
if (test.passed) {
passedTests++;
} else {
failedTests++;
}
const testClass = test.passed ? 'test pass' : 'test fail';
html += '<div class="' + testClass + '">';
html += '<div class="test-name">' + test.name + '</div>';
html += '<div class="test-message">' + test.message + '</div>';
html += '</div>';
});
html += '</div>';
});
resultsDiv.innerHTML = html;
// Update summary
const summaryClass = failedTests === 0 ? 'summary all-pass' : 'summary has-failures';
summaryDiv.className = summaryClass;
summaryStats.innerHTML =
'<div class="stat">Total: <strong>' + totalTests + '</strong></div>' +
'<div class="stat stat-pass">Passed: <strong>' + passedTests + '</strong></div>' +
'<div class="stat stat-fail">Failed: <strong>' + failedTests + '</strong></div>';
}
// Run tests when page loads
renderResults();
</script>
</body>
</html>