From a6e9e689cdfe83be94ce5b7b6dcdd025364b7eee Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Thu, 21 Mar 2024 23:53:03 +0100 Subject: [PATCH] add encode and serialise tests, use btreeset for determinism --- Cargo.lock | 2 - crates/kitsune-wasm-mrf/tests/load_and_run.rs | 4 +- lib/mrf-manifest/Cargo.toml | 1 - lib/mrf-manifest/src/lib.rs | 14 +- lib/mrf-manifest/tests/encode.rs | 10 + lib/mrf-manifest/tests/serialise.rs | 11 + .../tests/snapshots/encode__encode_works.snap | 190 ++++++++++++++++++ .../snapshots/serialise__encode_works.snap | 5 + 8 files changed, 225 insertions(+), 12 deletions(-) create mode 100644 lib/mrf-manifest/tests/encode.rs create mode 100644 lib/mrf-manifest/tests/serialise.rs create mode 100644 lib/mrf-manifest/tests/snapshots/encode__encode_works.snap create mode 100644 lib/mrf-manifest/tests/snapshots/serialise__encode_works.snap diff --git a/Cargo.lock b/Cargo.lock index 6830cc3c0..03d63aaf4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,7 +47,6 @@ dependencies = [ "cfg-if", "getrandom 0.2.12", "once_cell", - "serde", "version_check", "zerocopy", ] @@ -4846,7 +4845,6 @@ dependencies = [ name = "mrf-manifest" version = "0.0.1-pre.5" dependencies = [ - "ahash 0.8.11", "insta", "leb128", "miette", diff --git a/crates/kitsune-wasm-mrf/tests/load_and_run.rs b/crates/kitsune-wasm-mrf/tests/load_and_run.rs index 535279518..2d2499cf9 100644 --- a/crates/kitsune-wasm-mrf/tests/load_and_run.rs +++ b/crates/kitsune-wasm-mrf/tests/load_and_run.rs @@ -1,7 +1,7 @@ use kitsune_wasm_mrf::{MrfModule, MrfService, Outcome}; use mrf_manifest::{ActivitySet, ApiVersion, ManifestV1}; use smol_str::SmolStr; -use std::{borrow::Cow, collections::HashSet}; +use std::{borrow::Cow, collections::BTreeSet}; use wasmtime::{component::Component, Config, Engine}; const WASM_COMPONENT: &[u8] = include_bytes!("example_mrf.component.wasm"); @@ -12,7 +12,7 @@ fn dummy_manifest() -> ManifestV1<'static> { name: "dummy".into(), version: "1.0.0".parse().unwrap(), activity_types: ActivitySet::from( - [Cow::Borrowed("*")].into_iter().collect::>(), + [Cow::Borrowed("*")].into_iter().collect::>(), ), config_schema: None, } diff --git a/lib/mrf-manifest/Cargo.toml b/lib/mrf-manifest/Cargo.toml index 59525e515..9c56a38b5 100644 --- a/lib/mrf-manifest/Cargo.toml +++ b/lib/mrf-manifest/Cargo.toml @@ -6,7 +6,6 @@ version.workspace = true license = "MIT OR Apache-2.0" [dependencies] -ahash = { version = "0.8.11", features = ["serde"] } leb128 = { version = "0.2.5", optional = true } miette = { version = "7.2.0", optional = true } olpc-cjson = { version = "0.1.3", optional = true } diff --git a/lib/mrf-manifest/src/lib.rs b/lib/mrf-manifest/src/lib.rs index da7e58f14..c26d32a96 100644 --- a/lib/mrf-manifest/src/lib.rs +++ b/lib/mrf-manifest/src/lib.rs @@ -10,6 +10,7 @@ use schemars::{schema::RootSchema, JsonSchema}; use serde::{Deserialize, Serialize}; use std::{ borrow::Cow, + collections::BTreeSet, ops::{Deref, DerefMut}, }; @@ -41,7 +42,7 @@ where /// Wrapper around a hash set intended for use with the `activityTypes` field #[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] #[serde(transparent)] -pub struct ActivitySet<'a>(#[serde(borrow)] pub ahash::HashSet>); +pub struct ActivitySet<'a>(#[serde(borrow)] pub BTreeSet>); impl ActivitySet<'_> { /// Does the set of requested activity types contain `*`? @@ -58,13 +59,13 @@ impl ActivitySet<'_> { .iter() .cloned() .map(cow_to_static) - .collect::>>() + .collect::>>() .into() } } impl<'a> Deref for ActivitySet<'a> { - type Target = ahash::HashSet>; + type Target = BTreeSet>; fn deref(&self) -> &Self::Target { &self.0 @@ -77,15 +78,14 @@ impl DerefMut for ActivitySet<'_> { } } -#[allow(clippy::implicit_hasher)] // Literally not applicable here. False positive. Otherwise we would need to have the whole thing generic over the hasher -impl<'a> From> for ahash::HashSet> { +impl<'a> From> for BTreeSet> { fn from(value: ActivitySet<'a>) -> Self { value.0 } } -impl<'a> From>> for ActivitySet<'a> { - fn from(value: ahash::HashSet>) -> Self { +impl<'a> From>> for ActivitySet<'a> { + fn from(value: BTreeSet>) -> Self { Self(value) } } diff --git a/lib/mrf-manifest/tests/encode.rs b/lib/mrf-manifest/tests/encode.rs new file mode 100644 index 000000000..539417bfb --- /dev/null +++ b/lib/mrf-manifest/tests/encode.rs @@ -0,0 +1,10 @@ +#![cfg(feature = "encode")] + +const MANIFEST: &str = include_str!("test-manifest.json"); + +#[test] +fn encode_works() { + let manifest = serde_json::from_str(MANIFEST).unwrap(); + let encoded_manifest = mrf_manifest::encode(&manifest).unwrap(); + insta::assert_json_snapshot!(encoded_manifest); +} diff --git a/lib/mrf-manifest/tests/serialise.rs b/lib/mrf-manifest/tests/serialise.rs new file mode 100644 index 000000000..5df5e3c4f --- /dev/null +++ b/lib/mrf-manifest/tests/serialise.rs @@ -0,0 +1,11 @@ +#![cfg(feature = "serialise")] + +const MANIFEST: &str = include_str!("test-manifest.json"); + +#[test] +fn encode_works() { + let manifest = serde_json::from_str(MANIFEST).unwrap(); + let encoded_manifest = mrf_manifest::serialise(&manifest).unwrap(); + let encoded_manifest_str = String::from_utf8(encoded_manifest).unwrap(); + insta::assert_snapshot!(encoded_manifest_str); +} diff --git a/lib/mrf-manifest/tests/snapshots/encode__encode_works.snap b/lib/mrf-manifest/tests/snapshots/encode__encode_works.snap new file mode 100644 index 000000000..ac59090e6 --- /dev/null +++ b/lib/mrf-manifest/tests/snapshots/encode__encode_works.snap @@ -0,0 +1,190 @@ +--- +source: lib/mrf-manifest/tests/encode.rs +expression: encoded_manifest +--- +[ + 0, + 181, + 1, + 11, + 109, + 97, + 110, + 105, + 102, + 101, + 115, + 116, + 45, + 118, + 48, + 123, + 34, + 97, + 99, + 116, + 105, + 118, + 105, + 116, + 121, + 84, + 121, + 112, + 101, + 115, + 34, + 58, + 91, + 34, + 65, + 110, + 110, + 111, + 117, + 110, + 99, + 101, + 34, + 44, + 34, + 67, + 114, + 101, + 97, + 116, + 101, + 34, + 44, + 34, + 68, + 101, + 108, + 101, + 116, + 101, + 34, + 44, + 34, + 76, + 105, + 107, + 101, + 34, + 93, + 44, + 34, + 97, + 112, + 105, + 86, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 34, + 118, + 49, + 34, + 44, + 34, + 99, + 111, + 110, + 102, + 105, + 103, + 83, + 99, + 104, + 101, + 109, + 97, + 34, + 58, + 110, + 117, + 108, + 108, + 44, + 34, + 109, + 97, + 110, + 105, + 102, + 101, + 115, + 116, + 86, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 34, + 118, + 49, + 34, + 44, + 34, + 110, + 97, + 109, + 101, + 34, + 58, + 34, + 116, + 101, + 115, + 116, + 45, + 109, + 97, + 110, + 105, + 102, + 101, + 115, + 116, + 34, + 44, + 34, + 118, + 101, + 114, + 115, + 105, + 111, + 110, + 34, + 58, + 34, + 48, + 46, + 48, + 46, + 49, + 45, + 99, + 97, + 116, + 116, + 121, + 119, + 97, + 109, + 112, + 117, + 115, + 34, + 125 +] diff --git a/lib/mrf-manifest/tests/snapshots/serialise__encode_works.snap b/lib/mrf-manifest/tests/snapshots/serialise__encode_works.snap new file mode 100644 index 000000000..1e892dea2 --- /dev/null +++ b/lib/mrf-manifest/tests/snapshots/serialise__encode_works.snap @@ -0,0 +1,5 @@ +--- +source: lib/mrf-manifest/tests/serialise.rs +expression: encoded_manifest_str +--- +{"activityTypes":["Announce","Create","Delete","Like"],"apiVersion":"v1","configSchema":null,"manifestVersion":"v1","name":"test-manifest","version":"0.0.1-cattywampus"}