-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS test with Mocha (DRAFT) #2255
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
000f8fa
test(autonomi): add js mocha test
b-zee bca9503
fix(autonomi): gen secret key in the rigth way
b-zee 7ff98aa
docs(autonomi): add wasm README
b-zee b6bbb24
refactor(global): remove patch from Cargo.toml
b-zee 68060f9
Merge remote-tracking branch 'origin/main' into test-js-wasm
b-zee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should be using the new API for Archives here: