Skip to content

Commit

Permalink
Added wasm-bindgen-test with docs and ci/cd.
Browse files Browse the repository at this point in the history
Added a wasm-bindgen-test that runs for the DaterWrapper.

Also added wasm-tests to github actions as well as an updated section to
the READMEs detailing how to test the wasm crate.

Addresses cesride issue WebOfTrust#162
  • Loading branch information
daidoji committed Dec 18, 2023
1 parent 5840093 commit c8674c2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ jobs:
cargo install wasm-pack
wasm-pack build
- name: Test wasm
run: |
cd wasm
wasm-pack test --node
- name: Upload to codecov.io
uses: codecov/codecov-action@v3
with:
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ setup:

clean:
cargo clean
rm wasm/pkg/*

fix:
cargo fix
Expand All @@ -11,7 +12,11 @@ fix:
clippy:
cargo clippy --all-targets -- -D warnings

preflight:
wasm: .
cd wasm && wasm-pack build && wasm-pack build --target=nodejs # sanity builds
cd wasm && wasm-pack test --node # Node tests (only ones that signify-ts uses right now)

base-cesride-crate:
cargo generate-lockfile
cargo fmt --check
cargo outdated -R --exit-code 1
Expand All @@ -21,4 +26,6 @@ preflight:
cargo build --release
cargo test --release
cargo tarpaulin
cd wasm && wasm-pack build && wasm-pack build --target=nodejs

preflight: base-cesride-crate wasm
printf "Preflight check complete"
4 changes: 2 additions & 2 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["cdylib", "rlib"]
tmp_dev = []

[dependencies]
wasm-bindgen = "0.2.88"
wasm-bindgen = "0.2"
js-sys = '0.3'
serde_json = "1.0"

Expand All @@ -27,7 +27,7 @@ path = '..'
package = "cesride"

[dev-dependencies]
wasm-bindgen-test = "0.3.38"
wasm-bindgen-test = "0.3.39"

# wasm-opt is used instead
#[profile.release]
Expand Down
7 changes: 7 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ Currently only the CJS artifact works when integrating with signify-ts (maybe so
4. The cesride-wasm artifact should now be callable by tests and signify-ts.

Its also nice to note that wasm-pack produces a types file in pkg/ that describes the complete interface. Something like `cesride_wasm.d.ts`. Useful for debugging when making changes to this crate.

## Testing
You can test the Rust interface wrappers using rstest as long as they're vanilla Rust but as soon as you start having dependencies on anything that requires wasm-pack it'll be better to use wasm-bindgen-test crate to test. Those tests are in the `tests` directory and must remain in the base wasm crate for wasm-bindgen-test to work.

That crate is actually just the runner, `wasm-pack test --target=<{node,chrome,firefox,safari}>` is how the tests are run and by default they run in --headless browsers. Some of the targets may not work if you don't have the correct browser drivers installed on your testing instance or docker container. Installing those is out of scope for this README.

To add a test, import from the cesride\_wasm crate and test it by using the the macro #[wasm\_bindgen\_test] on all the things you'd like to test. Async tests are also available. More info can be found (here)[https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html]
1 change: 1 addition & 0 deletions wasm/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
These tests have to be at the root of the wasm crate in this tests directory for wasm-bindgen-test to work.
40 changes: 40 additions & 0 deletions wasm/tests/test_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use wasm_bindgen_test::*;

use cesride_wasm::DaterWrapper;
use cesride_wasm::CesrideMatterCodex;

/*
These dater tests are transcriptions from the first two test_dater tests in
test_coring from keripy.
*/
#[wasm_bindgen_test]
fn test_dater_default_now() {
// Default constructor should be equivalent to something like now()
// We just check the structure of this one.
let dater = DaterWrapper::new(None, None, None, None, None, None).unwrap();
assert_eq!(dater.code(), CesrideMatterCodex::DateTime.code());
assert_eq!(dater.raw().len(), 24);
assert_eq!(dater.qb64().unwrap().len(), 36);
assert_eq!(dater.qb2().unwrap().len(), 27);
assert_eq!(dater.dts().unwrap().len(), 32);
}

#[wasm_bindgen_test]
fn test_dater_dts1_construction() {
let dts1 = "2020-08-22T17:50:09.988921+00:00";
let dts1b = b"2020-08-22T17:50:09.988921+00:00";
let dt1raw = b"\xdbM\xb4\xfbO>\xdbd\xf5\xed\xcetsO]\xf7\xcf=\xdbZt\xd1\xcd4";
let dt1qb64 = "1AAG2020-08-22T17c50c09d988921p00c00";
let dt1qb64b = b"1AAG2020-08-22T17c50c09d988921p00c00";
let dt1qb2 = b"\xd4\x00\x06\xdbM\xb4\xfbO>\xdbd\xf5\xed\xcetsO]\xf7\xcf=\xdbZt\xd1\xcd4";

let dater = DaterWrapper::new(Some(dts1.to_string()), None, None, None, None, None).unwrap();
assert_eq!(dater.raw(), b"\xdbM\xb4\xfbO>\xdbd\xf5\xed\xcetsO]\xf7\xcf=\xdbZt\xd1\xcd4");
assert_eq!(dater.code(), CesrideMatterCodex::DateTime.code());
assert_eq!(dater.dts().unwrap(), dts1);
assert_eq!(dater.dtsb().unwrap(), dts1b);
assert_eq!(dater.raw(), dt1raw);
assert_eq!(dater.qb64().unwrap(), dt1qb64);
assert_eq!(dater.qb64b().unwrap(), dt1qb64b);
assert_eq!(dater.qb2().unwrap(), dt1qb2);
}

0 comments on commit c8674c2

Please sign in to comment.