forked from maidsafe/autonomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request maidsafe#2255 from b-zee/test-js-wasm
JS test with Mocha (DRAFT)
- Loading branch information
Showing
8 changed files
with
1,683 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,75 @@ | ||
<html> | ||
|
||
<head> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/> | ||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" /> | ||
</head> | ||
|
||
<body> | ||
<!-- credits: https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html --> | ||
<script type="module"> | ||
<!-- credits: https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html --> | ||
<script type="module"> | ||
import init, * as aut from './pkg/autonomi.js'; | ||
|
||
async function run() { | ||
document.getElementById("btn-run").disabled = true; | ||
const peer_addr = document.getElementById('peer_id').value; | ||
document.getElementById("btn-run").disabled = true; | ||
const peer_addr = document.getElementById('peer_id').value; | ||
|
||
await init(); | ||
await init(); | ||
|
||
aut.logInit("sn_networking=warn,autonomi=trace"); | ||
aut.logInit("sn_networking=warn,autonomi=trace"); | ||
|
||
updateProgress("Connecting..."); | ||
const client = await new aut.Client([peer_addr]); | ||
updateProgress("Connected"); | ||
updateProgress("Connecting..."); | ||
const client = await aut.Client.connect([peer_addr]); | ||
updateProgress("Connected"); | ||
|
||
updateProgress("Getting wallet..."); | ||
const wallet = aut.getFundedWallet(); | ||
updateProgress("Wallet retrieved"); | ||
updateProgress("Getting wallet..."); | ||
const wallet = aut.getFundedWallet(); | ||
updateProgress("Wallet retrieved"); | ||
|
||
// Random 32 byte data | ||
const data = [...Array(16)].map(() => Math.floor(Math.random() * 9)); | ||
updateProgress("Our data: " + data); | ||
// Random 32 byte data | ||
const data = [...Array(16)].map(() => Math.floor(Math.random() * 9)); | ||
updateProgress("Our data: " + data); | ||
|
||
updateProgress("Calculating cost..."); | ||
let result = await client.dataCost(data, wallet); | ||
updateProgress("Calculated cost: " + result.toString()); | ||
updateProgress("Calculating cost..."); | ||
let result = await client.dataCost(data, wallet); | ||
updateProgress("Calculated cost: " + result.toString()); | ||
|
||
updateProgress("Putting data..."); | ||
const dataAddr = await client.dataPut(data, wallet); | ||
updateProgress("Put done: " + dataAddr.toString()); | ||
updateProgress("Putting data..."); | ||
const dataAddr = await client.dataPut(data, wallet); | ||
updateProgress("Put done: " + dataAddr.toString()); | ||
|
||
let archive = new Map([["README.md", dataAddr]]); | ||
updateProgress("Putting data as archive... " + archive); | ||
let archiveAddr = await client.archivePut(archive, wallet); | ||
updateProgress("Archive put done: " + archiveAddr); | ||
let archive = new Map([["README.md", dataAddr]]); | ||
updateProgress("Putting data as archive... " + archive); | ||
let archiveAddr = await client.archivePut(archive, wallet); | ||
updateProgress("Archive put done: " + archiveAddr); | ||
|
||
updateProgress("Fetching archive..."); | ||
let archiveFetched = await client.archiveGet(archiveAddr); | ||
updateProgress("Archive fetched: " + archiveFetched); | ||
updateProgress("Fetching archive..."); | ||
let archiveFetched = await client.archiveGet(archiveAddr); | ||
updateProgress("Archive fetched: " + archiveFetched); | ||
|
||
for (const [path, addr] of archiveFetched) { | ||
updateProgress("Fetching data: " + path + ", addr: " + addr); | ||
const dataFetched = await client.dataGet(addr); | ||
updateProgress("Data fetched: " + dataFetched); | ||
} | ||
for (const [path, addr] of archiveFetched) { | ||
updateProgress("Fetching data: " + path + ", addr: " + addr); | ||
const dataFetched = await client.dataGet(addr); | ||
updateProgress("Data fetched: " + dataFetched); | ||
} | ||
|
||
// Generate random secret key | ||
const secretKey = [...Array(32)].map(() => Math.floor(Math.random() * 9)); | ||
await client.writeBytesToVault(data, wallet, secretKey); | ||
// Generate random secret key | ||
const secretKey = [...Array(32)].map(() => Math.floor(Math.random() * 9)); | ||
await client.writeBytesToVault(data, wallet, secretKey); | ||
|
||
const vault = await client.fetchAndDecryptVault(secretKey); | ||
updateProgress("Vault: " + vault); | ||
const vault = await client.fetchAndDecryptVault(secretKey); | ||
updateProgress("Vault: " + vault); | ||
} | ||
|
||
function updateProgress(message) { | ||
document.getElementById('progress').textContent = message; | ||
document.getElementById('progress').textContent = message; | ||
} | ||
|
||
document.getElementById("btn-run").addEventListener("click", run, false); | ||
</script> | ||
</script> | ||
|
||
<label for="peer_id">Peer MultiAddr: <input type="text" id="peer_id"/></label> | ||
<button id="btn-run">Run</button> | ||
<span id="progress"></span> | ||
<label for="peer_id">Peer MultiAddr: <input type="text" id="peer_id" /></label> | ||
<button id="btn-run">Run</button> | ||
<span id="progress"></span> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Mocha Tests</title> | ||
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"> | ||
<link href="./node_modules/mocha/mocha.css" rel="stylesheet" /> | ||
</head> | ||
|
||
<body> | ||
|
||
<div id="mocha"></div> | ||
|
||
<script type="module" class="mocha-init"> | ||
import './node_modules/mocha/mocha.js'; | ||
mocha.setup('bdd'); | ||
mocha.checkLeaks(); | ||
</script> | ||
<script type="module" src="index.js"></script> | ||
<script type="module" class="mocha-exec"> | ||
document.getElementById("btn-run").addEventListener("click", run, false); | ||
|
||
function run() { | ||
document.getElementById("btn-run").disabled = true; | ||
window.peer_addr = document.getElementById('peer_addr').value; | ||
mocha.run(); | ||
} | ||
</script> | ||
|
||
<label for="peer_addr">Peer multiaddr: <input type="text" id="peer_addr" style="width: 75%;" | ||
placeholder="/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3Koo..." /></label> | ||
<button id="btn-run">Run</button> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import init, * as atnm from '../pkg/autonomi.js'; | ||
import { assert } from './node_modules/chai/chai.js'; | ||
|
||
function randomData(len) { | ||
const array = new Uint8Array(len); | ||
window.crypto.getRandomValues(array); | ||
return array; | ||
} | ||
|
||
describe('autonomi', function () { | ||
this.timeout(180 * 1000); | ||
|
||
let client; | ||
let wallet; | ||
before(async () => { | ||
await init(); | ||
atnm.logInit("sn_networking=warn,autonomi=trace"); | ||
client = await atnm.Client.connect([window.peer_addr]); | ||
wallet = atnm.getFundedWallet(); | ||
}); | ||
|
||
it('calculates cost', async () => { | ||
const data = randomData(32); | ||
const cost = await client.dataCost(data); | ||
|
||
assert.typeOf(Number.parseFloat(cost.toString()), 'number'); | ||
}); | ||
|
||
it('puts data (32 bytes)', async () => { | ||
const data = randomData(32); | ||
const addr = await client.dataPut(data, wallet); | ||
|
||
assert.typeOf(addr, 'string'); | ||
}); | ||
|
||
it('puts data and gets it (32 bytes)', async () => { | ||
const data = randomData(32); | ||
const addr = await client.dataPut(data, wallet); | ||
const fetchedData = await client.dataGet(addr); | ||
|
||
assert.deepEqual(Array.from(data), Array.from(fetchedData)); | ||
}); | ||
|
||
it('puts data, creates archive and retrieves it', async () => { | ||
const data = randomData(32); | ||
const addr = await client.dataPut(data, wallet); | ||
const archive = new Map([["foo", addr]]); | ||
const archiveAddr = await client.archivePut(archive, wallet); | ||
|
||
const archiveFetched = await client.archiveGet(archiveAddr); | ||
|
||
assert.deepEqual(archive, archiveFetched); | ||
}); | ||
|
||
it('writes bytes to vault and fetches it', async () => { | ||
const data = randomData(32); | ||
const secretKey = atnm.genSecretKey(); | ||
|
||
await client.writeBytesToVault(data, wallet, secretKey); | ||
const dataFetched = await client.fetchAndDecryptVault(secretKey); | ||
|
||
assert.deepEqual(data, dataFetched); | ||
}); | ||
}); |
Oops, something went wrong.