diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c9deb0f..e9c26b2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,6 +57,10 @@ jobs: cargo install wasm-pack wasm-pack build + - name: Test wasm + run: | + wasm-pack test --node + - name: Upload to codecov.io uses: codecov/codecov-action@v3 with: diff --git a/Makefile b/Makefile index 6bcb3d2..8390e4c 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ setup: clean: cargo clean + rm wasm/pkg/* fix: cargo fix @@ -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 @@ -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" diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index bba109a..cb1c810 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -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" @@ -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] diff --git a/wasm/README.md b/wasm/README.md index 6aff470..dc87520 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -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] diff --git a/wasm/tests/README.md b/wasm/tests/README.md new file mode 100644 index 0000000..085829f --- /dev/null +++ b/wasm/tests/README.md @@ -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. diff --git a/wasm/tests/test_wasm.rs b/wasm/tests/test_wasm.rs new file mode 100644 index 0000000..02a5469 --- /dev/null +++ b/wasm/tests/test_wasm.rs @@ -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); +}