-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-browser.html
More file actions
150 lines (133 loc) · 5.28 KB
/
test-browser.html
File metadata and controls
150 lines (133 loc) · 5.28 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
<!DOCTYPE html>
<html>
<head>
<title>blake3-bao Browser Test</title>
<style>
body { font-family: monospace; padding: 20px; background: #1a1a2e; color: #eee; }
h1 { color: #7c3aed; }
pre { background: #16213e; padding: 15px; border-radius: 5px; overflow-x: auto; }
.pass { color: #10b981; }
.fail { color: #ef4444; }
</style>
</head>
<body>
<h1>blake3-bao Browser Test</h1>
<pre id="output">Loading...</pre>
<script src="dist/blake3-bao.min.js"></script>
<script>
const output = document.getElementById('output');
let tests = 0, passed = 0;
function log(msg) {
output.textContent += msg + '\n';
}
function test(name, fn) {
tests++;
try {
fn();
log('<span class="pass">PASS</span>: ' + name);
passed++;
} catch (e) {
log('<span class="fail">FAIL</span>: ' + name + ' - ' + e.message);
}
}
function assertEqual(actual, expected, msg) {
if (actual !== expected) {
throw new Error(msg + ': expected ' + expected + ', got ' + actual);
}
}
output.textContent = 'Running browser tests...\n\n';
try {
// Test BLAKE3 hashing
test('BLAKE3 hash', () => {
const hash = blake3Bao.blake3.hash('hello world');
const hex = blake3Bao.blake3.toHex(hash);
assertEqual(hex, 'd74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24', 'Hash mismatch');
});
// Test BLAKE3 hashHex
test('BLAKE3 hashHex', () => {
const hex = blake3Bao.blake3.hashHex('hello world');
assertEqual(hex, 'd74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24', 'Hash mismatch');
});
// Test BLAKE3 Hasher
test('BLAKE3 Hasher streaming', () => {
const hasher = blake3Bao.blake3.createHasher();
hasher.update('hello ');
hasher.update('world');
const hex = hasher.finalizeHex();
assertEqual(hex, 'd74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24', 'Hash mismatch');
});
// Test Bao encoding
test('Bao encode', () => {
const data = new Uint8Array(1024);
for (let i = 0; i < data.length; i++) data[i] = i & 0xff;
const { encoded, hash } = blake3Bao.bao.baoEncode(data);
assertEqual(encoded.length, 1032, 'Encoded size mismatch');
assertEqual(hash.length, 32, 'Hash size mismatch');
});
// Test Bao decode
test('Bao encode/decode round-trip', () => {
const data = new Uint8Array(2049);
for (let i = 0; i < data.length; i++) data[i] = i & 0xff;
const { encoded, hash } = blake3Bao.bao.baoEncode(data);
const decoded = blake3Bao.bao.baoDecode(encoded, hash);
assertEqual(decoded.length, data.length, 'Decoded size mismatch');
for (let i = 0; i < data.length; i++) {
if (decoded[i] !== data[i]) {
throw new Error('Data mismatch at index ' + i);
}
}
});
// Test Bao slicing
test('Bao slice', () => {
const data = new Uint8Array(4096);
for (let i = 0; i < data.length; i++) data[i] = i & 0xff;
const { encoded, hash } = blake3Bao.bao.baoEncode(data);
const slice = blake3Bao.bao.baoSlice(encoded, 1024, 1024);
const sliceData = blake3Bao.bao.baoDecodeSlice(slice, hash, 1024, 1024);
assertEqual(sliceData.length, 1024, 'Slice size mismatch');
});
// Test HashSequence
test('HashSequence', () => {
const seq = new blake3Bao.bao.HashSequence();
const hash1 = blake3Bao.blake3.hash('file1');
const hash2 = blake3Bao.blake3.hash('file2');
seq.addHash(hash1);
seq.addHash(hash2);
assertEqual(seq.length, 2, 'Sequence length mismatch');
assertEqual(seq.hasHash(hash1), true, 'Should have hash1');
});
// Test PartialBao
test('PartialBao', () => {
const data = new Uint8Array(32 * 1024); // 32 KB = 2 chunk groups
for (let i = 0; i < data.length; i++) data[i] = i & 0xff;
const { hash } = blake3Bao.bao.baoEncodeIroh(data, true);
const partial = new blake3Bao.bao.PartialBao(hash, data.length);
assertEqual(partial.numGroups, 2, 'Should have 2 groups');
assertEqual(partial.isComplete(), false, 'Should not be complete');
});
// Test Iroh encoding
test('Bao Iroh encode', () => {
const data = new Uint8Array(64 * 1024); // 64 KB
const { encoded, hash } = blake3Bao.bao.baoEncodeIroh(data, true);
// Iroh outboard for 64KB should be small (tree nodes only)
assertEqual(hash.length, 32, 'Hash size mismatch');
// 64KB = 4 chunk groups = 3 parent nodes = header + 3*64 bytes
assertEqual(encoded.length, 8 + 3 * 64, 'Outboard size mismatch');
});
log('\n========================================');
log('Results: ' + passed + '/' + tests + ' tests passed');
if (passed === tests) {
log('\n<span class="pass">All browser tests passed!</span>');
} else {
log('\n<span class="fail">Some tests failed.</span>');
}
// Re-render with HTML spans
output.innerHTML = output.textContent;
} catch (e) {
log('\nFATAL ERROR: ' + e.message);
console.error(e);
output.innerHTML = output.textContent;
}
</script>
</body>
</html>