-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
228 lines (197 loc) · 9.41 KB
/
test.html
File metadata and controls
228 lines (197 loc) · 9.41 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
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tests - Albert Heijn Zelfscanner</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 2rem;
background-color: #f5f5f5;
}
.test-container {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.test-result {
padding: 0.5rem;
margin: 0.5rem 0;
border-radius: 4px;
}
.test-pass {
background-color: #d4edda;
color: #155724;
}
.test-fail {
background-color: #f8d7da;
color: #721c24;
}
.test-summary {
margin-top: 2rem;
padding: 1rem;
background-color: #e9ecef;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="test-container">
<h1>Albert Heijn Zelfscanner - Automated Tests</h1>
<div id="test-results"></div>
<div id="test-summary" class="test-summary"></div>
</div>
<script src="products.js"></script>
<script>
// Simple test framework
class TestRunner {
constructor() {
this.tests = [];
this.passed = 0;
this.failed = 0;
}
test(name, fn) {
this.tests.push({ name, fn });
}
async run() {
const resultsEl = document.getElementById('test-results');
const summaryEl = document.getElementById('test-summary');
for (const test of this.tests) {
try {
await test.fn();
this.passed++;
resultsEl.innerHTML += `<div class="test-result test-pass">✓ ${test.name}</div>`;
} catch (error) {
this.failed++;
resultsEl.innerHTML += `<div class="test-result test-fail">✗ ${test.name}: ${error.message}</div>`;
}
}
summaryEl.innerHTML = `
<h3>Test Summary</h3>
<p>Total: ${this.tests.length} | Passed: ${this.passed} | Failed: ${this.failed}</p>
<p>Success Rate: ${((this.passed / this.tests.length) * 100).toFixed(1)}%</p>
`;
}
assert(condition, message) {
if (!condition) {
throw new Error(message || 'Assertion failed');
}
}
assertEqual(actual, expected, message) {
if (actual !== expected) {
throw new Error(message || `Expected ${expected}, got ${actual}`);
}
}
assertNotNull(value, message) {
if (value === null || value === undefined) {
throw new Error(message || 'Value should not be null/undefined');
}
}
}
// Initialize test runner
const runner = new TestRunner();
// Product Database Tests
runner.test('Products database should be defined', () => {
runner.assertNotNull(PRODUCTS_DATABASE, 'PRODUCTS_DATABASE should be defined');
});
runner.test('Products database should contain products', () => {
const productCount = Object.keys(PRODUCTS_DATABASE).length;
runner.assert(productCount > 0, 'Should have at least one product');
});
runner.test('All products should have required fields', () => {
for (const [barcode, product] of Object.entries(PRODUCTS_DATABASE)) {
runner.assertNotNull(product.name, `Product ${barcode} should have a name`);
runner.assertNotNull(product.price, `Product ${barcode} should have a price`);
runner.assertNotNull(product.description, `Product ${barcode} should have a description`);
runner.assertNotNull(product.category, `Product ${barcode} should have a category`);
runner.assert(typeof product.price === 'number', `Product ${barcode} price should be a number`);
runner.assert(product.price > 0, `Product ${barcode} price should be positive`);
}
});
runner.test('findProductByBarcode should work correctly', () => {
const firstBarcode = Object.keys(PRODUCTS_DATABASE)[0];
const product = findProductByBarcode(firstBarcode);
runner.assertNotNull(product, 'Should find product with valid barcode');
const invalidProduct = findProductByBarcode('invalid-barcode');
runner.assertEqual(invalidProduct, null, 'Should return null for invalid barcode');
});
runner.test('getRandomProducts should return valid products', () => {
const randomProducts = getRandomProducts(5);
runner.assertEqual(randomProducts.length, 5, 'Should return requested number of products');
randomProducts.forEach((product, index) => {
runner.assertNotNull(product.name, `Random product ${index} should have a name`);
runner.assertNotNull(product.price, `Random product ${index} should have a price`);
runner.assertNotNull(product.quantity, `Random product ${index} should have a quantity`);
runner.assert(product.quantity >= 1 && product.quantity <= 3, `Random product ${index} quantity should be 1-3`);
});
});
runner.test('searchProductsByName should work correctly', () => {
const results = searchProductsByName('melk');
runner.assert(results.length > 0, 'Should find products containing "melk"');
const noResults = searchProductsByName('xyz123nonexistent');
runner.assertEqual(noResults.length, 0, 'Should return empty array for non-existent search');
});
runner.test('getAllCategories should return unique categories', () => {
const categories = getAllCategories();
runner.assert(categories.length > 0, 'Should return at least one category');
const uniqueCategories = [...new Set(categories)];
runner.assertEqual(categories.length, uniqueCategories.length, 'Categories should be unique');
});
runner.test('getProductsByCategory should filter correctly', () => {
const categories = getAllCategories();
if (categories.length > 0) {
const firstCategory = categories[0];
const products = getProductsByCategory(firstCategory);
runner.assert(products.length > 0, `Should find products in category ${firstCategory}`);
products.forEach(product => {
runner.assertEqual(product.category, firstCategory, 'All returned products should be in the requested category');
});
}
});
// Format Testing
runner.test('Product prices should be reasonable', () => {
for (const [barcode, product] of Object.entries(PRODUCTS_DATABASE)) {
runner.assert(product.price >= 0.01 && product.price <= 100,
`Product ${product.name} price ${product.price} should be reasonable (0.01-100 EUR)`);
}
});
runner.test('Product names should be in Dutch', () => {
const dutchWords = ['AH', 'melk', 'brood', 'kaas', 'koffie', 'thee'];
let dutchProductsFound = 0;
for (const [barcode, product] of Object.entries(PRODUCTS_DATABASE)) {
const hasAH = product.name.includes('AH');
const hasDutchWord = dutchWords.some(word =>
product.name.toLowerCase().includes(word) ||
product.description.toLowerCase().includes(word)
);
if (hasAH || hasDutchWord) {
dutchProductsFound++;
}
}
runner.assert(dutchProductsFound > 0, 'Should have products with Dutch names/descriptions');
});
// Camera functionality tests
runner.test('Camera button should exist in HTML', () => {
const html = document.documentElement.outerHTML;
runner.assert(html.includes('cameraButton'), 'Should have camera button element');
runner.assert(html.includes('📷'), 'Should have camera icon');
});
runner.test('Camera modal should exist in HTML', () => {
const html = document.documentElement.outerHTML;
runner.assert(html.includes('cameraModal'), 'Should have camera modal element');
runner.assert(html.includes('Camera Scanner'), 'Should have camera scanner text');
runner.assert(html.includes('Start Camera'), 'Should have start camera button');
});
runner.test('QuaggaJS library should be included', () => {
const html = document.documentElement.outerHTML;
runner.assert(html.includes('quagga'), 'Should include QuaggaJS library');
});
// Run all tests when page loads
document.addEventListener('DOMContentLoaded', () => {
runner.run();
});
</script>
</body>
</html>