-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-browser.html
More file actions
208 lines (179 loc) · 7.9 KB
/
test-browser.html
File metadata and controls
208 lines (179 loc) · 7.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scionic Merkle Tree - Browser Test</title>
<style>
body {
font-family: monospace;
max-width: 1200px;
margin: 20px auto;
padding: 20px;
background: #1e1e1e;
color: #d4d4d4;
}
h1 { color: #4ec9b0; }
h2 { color: #569cd6; }
.test { margin: 10px 0; padding: 10px; background: #252526; border-radius: 4px; }
.pass { color: #4ec9b0; }
.fail { color: #f48771; }
.info { color: #ce9178; }
pre { background: #1e1e1e; padding: 10px; overflow-x: auto; border-left: 3px solid #569cd6; }
button {
background: #0e639c;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 4px;
margin: 5px;
}
button:hover { background: #1177bb; }
#results { margin-top: 20px; }
</style>
</head>
<body>
<h1>🌐 Scionic Merkle Tree - Browser Tests</h1>
<p>Testing browser-compatible Merkle DAG implementation</p>
<div>
<button onclick="runAllTests()">▶ Run All Tests</button>
<button onclick="document.getElementById('results').innerHTML = ''">Clear Results</button>
</div>
<div id="results"></div>
<script type="module">
import * as ScionicMerkleTree from './dist/browser/scionic-merkle-tree.es.js';
window.ScionicMerkleTree = ScionicMerkleTree;
window.log = (msg, type = 'info') => {
const div = document.createElement('div');
div.className = 'test ' + type;
div.innerHTML = msg;
document.getElementById('results').appendChild(div);
};
window.runAllTests = async () => {
document.getElementById('results').innerHTML = '';
log('<h2>Running Browser Tests...</h2>');
let passed = 0;
let failed = 0;
try {
// Test 1: Create DAG from small file
log('<b>Test 1:</b> Create DAG from small file');
const content1 = new TextEncoder().encode('Hello from browser!');
const dag1 = await ScionicMerkleTree.createDagFromFile('test.txt', content1);
if (dag1.Root && dag1.Root.startsWith('bafi')) {
log(`✓ DAG created: ${dag1.Root}`, 'pass');
log(` Leaves: ${Object.keys(dag1.Leafs).length}`, 'info');
passed++;
} else {
log('✗ Invalid root CID', 'fail');
failed++;
}
// Test 2: Verify DAG
log('<b>Test 2:</b> Verify DAG integrity');
await ScionicMerkleTree.verifyDag(dag1);
log('✓ DAG verification passed', 'pass');
passed++;
// Test 3: Reconstruct file
log('<b>Test 3:</b> Reconstruct file from DAG');
const reconstructed = ScionicMerkleTree.reconstructFile(dag1);
const text = new TextDecoder().decode(reconstructed);
if (text === 'Hello from browser!') {
log('✓ File reconstructed correctly', 'pass');
passed++;
} else {
log(`✗ Reconstruction failed: got "${text}"`, 'fail');
failed++;
}
// Test 4: Large file with chunking
log('<b>Test 4:</b> Large file with chunking');
const CHUNK_SIZE = 2 * 1024 * 1024;
const largeContent = new Uint8Array(CHUNK_SIZE + 500);
for (let i = 0; i < largeContent.length; i++) {
largeContent[i] = i % 256;
}
const dag2 = await ScionicMerkleTree.createDagFromFile('large.bin', largeContent);
const chunks = Object.values(dag2.Leafs).filter(leaf => leaf.Type === 'chunk');
if (chunks.length > 0) {
log(`✓ Chunking works: ${chunks.length} chunks created`, 'pass');
passed++;
} else {
log('✗ No chunks created', 'fail');
failed++;
}
// Test 5: Merkle tree
log('<b>Test 5:</b> Browser Merkle tree');
const leaves = [
await ScionicMerkleTree.hashData(new TextEncoder().encode('a')),
await ScionicMerkleTree.hashData(new TextEncoder().encode('b')),
await ScionicMerkleTree.hashData(new TextEncoder().encode('c'))
];
const tree = new ScionicMerkleTree.BrowserMerkleTree(leaves);
await tree.build();
const root = tree.getRoot();
// Verify proofs
let allValid = true;
for (let i = 0; i < leaves.length; i++) {
const proof = tree.getProof(i);
const valid = await ScionicMerkleTree.BrowserMerkleTree.verify(leaves[i], proof, root);
if (!valid) allValid = false;
}
if (allValid) {
log('✓ Merkle tree proofs verified', 'pass');
passed++;
} else {
log('✗ Merkle proof verification failed', 'fail');
failed++;
}
// Test 6: CID determinism
log('<b>Test 6:</b> CID determinism');
const content6 = new TextEncoder().encode('deterministic');
const dag6a = await ScionicMerkleTree.createDagFromFile('det.txt', content6);
const dag6b = await ScionicMerkleTree.createDagFromFile('det.txt', content6);
if (dag6a.Root === dag6b.Root) {
log(`✓ Deterministic CIDs: ${dag6a.Root}`, 'pass');
passed++;
} else {
log('✗ Non-deterministic CIDs', 'fail');
failed++;
}
// Test 7: Serialization
log('<b>Test 7:</b> CBOR serialization');
const cbor = ScionicMerkleTree.toCBOR(dag1);
const restored = ScionicMerkleTree.fromCBOR(cbor);
if (restored.Root === dag1.Root) {
log(`✓ CBOR round-trip successful (${cbor.length} bytes)`, 'pass');
passed++;
} else {
log('✗ CBOR round-trip failed', 'fail');
failed++;
}
// Test 8: Blob input
log('<b>Test 8:</b> Blob input support');
const blob = new Blob(['Blob test content'], { type: 'text/plain' });
const dag8 = await ScionicMerkleTree.createDagFromFile('blob.txt', blob);
if (dag8.Root && dag8.Root.startsWith('bafi')) {
log('✓ Blob input handled correctly', 'pass');
passed++;
} else {
log('✗ Blob handling failed', 'fail');
failed++;
}
// Summary
log(`<h2>Results: ${passed}/${passed + failed} tests passed</h2>`,
passed === passed + failed ? 'pass' : 'fail');
if (passed === passed + failed) {
log('🎉 All browser tests passed!', 'pass');
}
} catch (error) {
log(`<b>Error:</b> ${error.message}`, 'fail');
log(`<pre>${error.stack}</pre>`, 'fail');
failed++;
}
};
// Run tests automatically
window.addEventListener('load', () => {
setTimeout(runAllTests, 100);
});
</script>
</body>
</html>