Skip to content

Commit

Permalink
w3sper: Align tests with actual library usage
Browse files Browse the repository at this point in the history
 - Add `@dusk/w3sper` to import map for integration-like testing
 - Add `useAsProtocolDriver` to avoid exposing full `ProtocolDriver`
 - Fix `ProtocolDriver` leftover to use cryptographically secure `rng`
 - Change tests to avoid automagically WASM loading
  • Loading branch information
ZER0 committed Nov 24, 2024
1 parent ad0ef94 commit b3b5946
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 170 deletions.
4 changes: 4 additions & 0 deletions w3sper.js/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "@dusk/w3sper",
"version": "0.1.0-beta.1",
"exports": "./src/mod.js",
"imports": {
"@dusk/exu": "jsr:@dusk/[email protected]",
"@dusk/w3sper": "./src/mod.js"
},
"tasks": {
"test": "deno test --allow-net --allow-read --allow-write --allow-run --allow-import",
"wasm": "cd ../wallet-core && cargo wasm",
Expand Down
1 change: 1 addition & 0 deletions w3sper.js/src/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./network/mod.js";
export * from "./profile.js";
export * from "./bookkeeper.js";
export * from "./transaction.js";
export { useAsProtocolDriver } from "./protocol-driver/mod.js";
33 changes: 24 additions & 9 deletions w3sper.js/src/protocol-driver/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

import * as exu from "jsr:@dusk/exu@0.1.2";
import * as exu from "@dusk/exu";
import { none } from "./none.js";

import { DriverError } from "./error.js";
import * as DataBuffer from "./buffer.js";
import { withAllocator } from "./alloc.js";

const rng = () => new Uint8Array(32); //crypto.getRandomValues(new Uint8Array(32));
const rng = () => crypto.getRandomValues(new Uint8Array(32));

const uninit = Object.freeze([
none`No Protocol Driver loaded yet. Call "load" first.`,
Expand All @@ -36,7 +36,6 @@ export function load(source, importsURL) {
}

// Parse known globals once.

driverGlobals = protocolDriverModule.task(
withAllocator(async function (_exports, allocator) {
const { ptr, u32, u64 } = allocator.types;
Expand All @@ -51,14 +50,30 @@ export function load(source, importsURL) {
)();
}

export function unload() {
export async function unload() {
if (protocolDriverModule instanceof none || driverGlobals instanceof none) {
return Promise.resolve();
} else {
return Promise.all([protocolDriverModule, driverGlobals]).then(() => {
[protocolDriverModule, driverGlobals] = uninit;
});
return;
}

await Promise.all([protocolDriverModule, driverGlobals]);

[protocolDriverModule, driverGlobals] = uninit;
}

export function useAsProtocolDriver(source, importsURL) {
load(source, importsURL);

return {
cleanup() {
return unload();
},
then(onFulfilled, onRejected) {
return driverGlobals
.then(() => onFulfilled())
.catch(onRejected)
.finally(unload);
},
};
}

export async function opening(bytes) {
Expand Down
8 changes: 4 additions & 4 deletions w3sper.js/tests/balances_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import {
Bookkeeper,
AddressSyncer,
AccountSyncer,
} from "../src/mod.js";
} from "@dusk/w3sper";

import { test, assert, seeder, Treasury } from "./harness.js";

test.withLocalWasm = "release";

test("Account Balance", async () => {
const network = new Network("http://localhost:8080/");
const network = await Network.connect("http://localhost:8080/");

const user =
"oCqYsUMRqpRn2kSabH52Gt6FQCwH5JXj5MtRdYVtjMSJ73AFvdbPf98p3gz98fQwNy9ZBiDem6m9BivzURKFSKLYWP3N9JahSPZs9PnZ996P18rTGAjQTNFsxtbrKx79yWu";
Expand All @@ -28,6 +26,8 @@ test("Account Balance", async () => {
nonce: 0n,
value: 1_001_000_000_000_000n,
});

await network.disconnect();
});

test("Balances synchronization", async () => {
Expand Down
38 changes: 9 additions & 29 deletions w3sper.js/tests/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,21 @@ const mergeMap = (dest, source, lookup) => {
return;
};

import * as ProtocolDriver from "../src/protocol-driver/mod.js";
import {
test as harnessTest,
export {
test,
assert,
} from "http://rawcdn.githack.com/mio-mini/test-harness/0.1.0/mod.js";

import { Bookmark } from "../src/mod.js";
import { Bookmark } from "@dusk/w3sper";

export { assert };
const WASM_RELEASE_PATH =
"../target/wasm32-unknown-unknown/release/wallet_core.wasm";

export async function test(name, fn) {
let path = "";
switch (test.withLocalWasm) {
case "debug":
path = "../target/wasm32-unknown-unknown/debug/wallet_core.wasm";
break;
case "release":
path = "../target/wasm32-unknown-unknown/release/wallet_core.wasm";
break;
}

if (path.length > 0 && typeof Deno !== "undefined") {
const testFn = async (...args) => {
const wasm = await Deno.readFile(path);

ProtocolDriver.load(
wasm,
new URL("./assets/debug-imports.js", import.meta.url),
);

await Promise.resolve(fn(...args)).finally(ProtocolDriver.unload);
};

return harnessTest(name, testFn);
export function getLocalWasmBuffer() {
if (typeof Deno !== "undefined") {
return Deno.readFile(WASM_RELEASE_PATH);
}
return Promise.reject("Can't accesso to file system");
}

// Define a seed for deterministic profile generation
Expand Down
3 changes: 2 additions & 1 deletion w3sper.js/tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<script type="importmap">
{
"imports": {
"jsr:@dusk/[email protected]": "https://rawcdn.githack.com/dusk-network/exu/v0.1.2/src/mod.js"
"@dusk/exu": "https://rawcdn.githack.com/dusk-network/exu/v0.1.2/src/mod.js",
"@dusk/w3sper": "../src/mod.js"
}
}
</script>
Expand Down
12 changes: 2 additions & 10 deletions w3sper.js/tests/network_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

import {
Network,
ProfileGenerator,
Bookkeeper,
AddressSyncer,
AccountSyncer,
} from "../src/mod.js";
import { Network } from "@dusk/w3sper";

import { test, assert, seeder, Treasury } from "./harness.js";

test.withLocalWasm = "release";
import { test, assert } from "./harness.js";

test("Network connection", async () => {
const network = new Network("http://localhost:8080/");
Expand Down
11 changes: 7 additions & 4 deletions w3sper.js/tests/notes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

import { test, assert } from "./harness.js";
import { ProfileGenerator, Bookkeeper } from "../src/mod.js";
import { test, assert, getLocalWasmBuffer } from "./harness.js";
import { ProfileGenerator, Bookkeeper } from "@dusk/w3sper";

const hex = (bytes) =>
Array.from(bytes)
Expand All @@ -18,12 +18,13 @@ const seeder = async () => SEED;
const NOTES_RKYV = "./tests/assets/notes.rkyv";
const notesBuffer = await Deno.readFile(NOTES_RKYV);

// NOTE: This tests helps to check some of the protocol driver internals
import * as ProtocolDriver from "../src/protocol-driver/mod.js";

test.withLocalWasm = "release";

// Test case for default profile
test("owened notes balance", async () => {
ProtocolDriver.load(await getLocalWasmBuffer());

const profiles = new ProfileGenerator(seeder);

const owner1 = await Promise.all([
Expand Down Expand Up @@ -166,4 +167,6 @@ test("owened notes balance", async () => {

picked = await ProtocolDriver.pickNotes(owner4, notes4[0], 1n);
assert.equal(picked.size, 0);

await ProtocolDriver.unload();
});
138 changes: 64 additions & 74 deletions w3sper.js/tests/profile_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,87 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

import {
test,
assert,
} from "http://rawcdn.githack.com/mio-mini/test-harness/0.1.0/mod.js";
import { test, assert, getLocalWasmBuffer } from "./harness.js";

import { Network, ProfileGenerator } from "../src/mod.js";
import { ProfileGenerator, useAsProtocolDriver } from "@dusk/w3sper";

// Define a seed for deterministic profile generation
const SEED = new Uint8Array(64);
const seeder = async () => SEED;

// fetch local wasm
const wasmBuffer = await getLocalWasmBuffer();

// Test case for initial profile generation
test("Initial Profile Generation", async () => {
const network = await Network.connect("http://localhost:8080/");

const profiles = new ProfileGenerator(seeder);

// Verify that the profile list is initially empty
assert.equal(profiles.length, 0);

await network.disconnect();
});

// Test case for default profile
test("Default Profile", async () => {
const network = await Network.connect("http://localhost:8080/");

const profiles = new ProfileGenerator(seeder);
const defaultProfile = await profiles.default;

// Validate the default profile's address and account details
assert.equal(
defaultProfile.address.toString(),
"ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN",
);

assert.equal(
defaultProfile.account.toString(),
"qe1FbZxf6YaCAeFNSvL1G82cBhG4Q4gBf4vKYo527Vws3b23jdbBuzKSFsdUHnZeBgsTnyNJLkApEpRyJw87sdzR9g9iESJrG5ZgpCs9jq88m6d4qMY5txGpaXskRQmkzE3",
);

// Ensure the default profile is indexed correctly
assert.equal(+defaultProfile, 0);
assert.equal(profiles.indexOf(defaultProfile), 0);
assert.equal(await profiles.at(0), defaultProfile);
assert.equal(await profiles.at(1), undefined);

// Ensure the default profile's keys (address and account) are indexed correctly
assert.equal(+defaultProfile, +defaultProfile.address);
assert.equal(+defaultProfile, +defaultProfile.account);

// Verify that the profile list has been updated to include the default profile
assert.equal(profiles.length, 1);

await network.disconnect();
});
test("Default Profile", () =>
useAsProtocolDriver(wasmBuffer).then(async () => {
const profiles = new ProfileGenerator(seeder);
const defaultProfile = await profiles.default;

// Validate the default profile's address and account details
assert.equal(
defaultProfile.address.toString(),
"ivmscertKgRyX8wNMJJsQcSVEyPsfSMUQXSAgeAPQXsndqFq9Pmknzhm61QvcEEdxPaGgxDS4RHpb6KKccrnSKN",
);

assert.equal(
defaultProfile.account.toString(),
"qe1FbZxf6YaCAeFNSvL1G82cBhG4Q4gBf4vKYo527Vws3b23jdbBuzKSFsdUHnZeBgsTnyNJLkApEpRyJw87sdzR9g9iESJrG5ZgpCs9jq88m6d4qMY5txGpaXskRQmkzE3",
);

// Ensure the default profile is indexed correctly
assert.equal(+defaultProfile, 0);
assert.equal(profiles.indexOf(defaultProfile), 0);
assert.equal(await profiles.at(0), defaultProfile);
assert.equal(await profiles.at(1), undefined);

// Ensure the default profile's keys (address and account) are indexed correctly
assert.equal(+defaultProfile, +defaultProfile.address);
assert.equal(+defaultProfile, +defaultProfile.account);

// Verify that the profile list has been updated to include the default profile
assert.equal(profiles.length, 1);
}));

// Test case for generating the next profile
test("Next Profile Generation", async () => {
const network = await Network.connect("http://localhost:8080/");

const profiles = new ProfileGenerator(seeder);

// Generate the next profile
const profile = await profiles.next();

// Validate the next profile's address and account details
assert.equal(
profile.address.toString(),
"3MoVQ6VfGNu8fJ5GeHPRDVUfxcsDEmGXpWhvKhXY7F2dKCp7QWRw8RqPcbuJGdRqeTtxpuiwETnGAJLnhT4Kq4e8",
);

assert.equal(
profile.account.toString(),
"25omWWRyfcMjYNbQZVyc3rypYLi8UqZuthoJHqbCEriRX3z2EmnBaXWZLFL2NvzvDnkoYoHLGiSYQpmupNJj1sSdWNstqzfFEpiqvSNYw7gqvoEiU9FsEHUMG1ZyG3XgL8Rv",
);

// Ensure the next profile is indexed correctly
assert.equal(+profile, 1);
assert.equal(profiles.indexOf(profile), 1);
assert.equal(await profiles.at(1), profile);

// Ensure the next profile's keys (address and account) are indexed correctly
assert.equal(+profile, +profile.address);
assert.equal(+profile, +profile.account);

// Verify that the profile list has been updated to include the next profile
assert.equal(profiles.length, 2);

await network.disconnect();
});
test("Next Profile Generation", () =>
useAsProtocolDriver(wasmBuffer).then(async () => {
const profiles = new ProfileGenerator(seeder);

// Generate the next profile
const profile = await profiles.next();

// Validate the next profile's address and account details
assert.equal(
profile.address.toString(),
"3MoVQ6VfGNu8fJ5GeHPRDVUfxcsDEmGXpWhvKhXY7F2dKCp7QWRw8RqPcbuJGdRqeTtxpuiwETnGAJLnhT4Kq4e8",
);

assert.equal(
profile.account.toString(),
"25omWWRyfcMjYNbQZVyc3rypYLi8UqZuthoJHqbCEriRX3z2EmnBaXWZLFL2NvzvDnkoYoHLGiSYQpmupNJj1sSdWNstqzfFEpiqvSNYw7gqvoEiU9FsEHUMG1ZyG3XgL8Rv",
);

// Ensure the next profile is indexed correctly
assert.equal(+profile, 1);
assert.equal(profiles.indexOf(profile), 1);
assert.equal(await profiles.at(1), profile);

// Ensure the next profile's keys (address and account) are indexed correctly
assert.equal(+profile, +profile.address);
assert.equal(+profile, +profile.account);

// Verify that the profile list has been updated to include the next profile
assert.equal(profiles.length, 2);
}));

// Test case for ProfileGenerator type checking
test("ProfileGenerator Type Checking", () => {
Expand Down
Loading

0 comments on commit b3b5946

Please sign in to comment.