Repository navigation
Expand file tree
/
Copy pathtest-browser.mjs
More file actions
67 lines (53 loc) · 3.55 KB
/
Copy pathtest-browser.mjs
File metadata and controls
67 lines (53 loc) · 3.55 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
// Copyright 2026 Quantova Inc
// SPDX-License-Identifier: Apache-2.0 OR MIT
import { readFileSync } from 'node:fs';
function fail(msg) {
console.error('FAIL ' + msg);
process.exit(1);
}
function resolveExport(node, active) {
if (typeof node === 'string') return node;
for (const key of Object.keys(node)) {
if (key === 'default' || active.has(key)) {
const hit = resolveExport(node[key], active);
if (hit) return hit;
}
}
return null;
}
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)));
const root = pkg.exports['.'];
const browserTarget = resolveExport(root, new Set(['browser', 'import', 'default']));
const esmTarget = resolveExport(root, new Set(['import', 'default']));
const nodeImportTarget = resolveExport(root, new Set(['node', 'import', 'default']));
const nodeRequireTarget = resolveExport(root, new Set(['node', 'require', 'default']));
if (browserTarget !== './browser.mjs') fail('browser condition must resolve to the guarded browser entry, got ' + browserTarget);
if (esmTarget !== './browser.mjs') fail('bundler import must resolve to the guarded browser entry, got ' + esmTarget);
if (/pkg\//.test(browserTarget) || /pkg\//.test(esmTarget)) fail('browser or import must never resolve to the raw wasm build');
if (nodeImportTarget !== './index.js') fail('node import must resolve to the node entry, got ' + nodeImportTarget);
if (nodeRequireTarget !== './index.js') fail('node require must resolve to the node entry, got ' + nodeRequireTarget);
const mod = await import(new URL(browserTarget, import.meta.url));
if (typeof mod.Client !== 'function') fail('the browser entry must export a Client constructor, got ' + typeof mod.Client);
if (!mod.Client.prototype || typeof mod.Client.prototype.transfer !== 'function') fail('the browser Client is missing the guarded transfer');
if (typeof mod.generateSeed !== 'function') fail('the browser entry must export generateSeed');
if (typeof mod.core !== 'object' || typeof mod.core.valid_address !== 'function') fail('the browser entry must expose the core');
if (typeof mod.vmCallFee !== 'function' || mod.vmCallFee('500', 21000n) !== 500n) fail('the browser entry must export the call fee helper');
let coercedThrew = false;
try { mod.core.address('00'.repeat(32), -1n); } catch { coercedThrew = true; }
if (!coercedThrew) fail('the browser core must refuse a negative index rather than wrap it');
const seed = mod.generateSeed();
if (!(seed instanceof Uint8Array) || seed.length !== 32) fail('generateSeed must return a thirty two byte Uint8Array');
const client = new mod.Client('http://127.0.0.1:1');
const to = client.address(seed, 1);
if (!mod.core.valid_address(to)) fail('the browser core did not derive a valid address');
let amtThrew = false;
try { await client.transfer(seed, 0, to, 1000, '1000'); }
catch (e) { amtThrew = true; if (!/decimal string or a BigInt/.test(e.message)) fail('browser amount error unclear: ' + e.message); }
if (!amtThrew) fail('the browser Client must refuse a number amount');
let ceilThrew = false;
try { await client.transfer(seed, 0, to, '1000', 1000); }
catch (e) { ceilThrew = true; if (!/maximum fee/.test(e.message) || !/JavaScript number/.test(e.message)) fail('browser ceiling error unclear: ' + e.message); }
if (!ceilThrew) fail('the browser Client must refuse a number fee ceiling');
const raw = await import(new URL('./pkg/qcore_js.js', import.meta.url));
if (raw.Client !== undefined) fail('the raw wasm build was expected to have no Client');
console.log('browser entry: guarded Client resolved, number amount and number ceiling both refused');