Skip to content

Commit

Permalink
add encode and serialise tests, use btreeset for determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Mar 21, 2024
1 parent 8513e00 commit a6e9e68
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 12 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/kitsune-wasm-mrf/tests/load_and_run.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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::<HashSet<_, _>>(),
[Cow::Borrowed("*")].into_iter().collect::<BTreeSet<_>>(),
),
config_schema: None,
}
Expand Down
1 change: 0 additions & 1 deletion lib/mrf-manifest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 7 additions & 7 deletions lib/mrf-manifest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use schemars::{schema::RootSchema, JsonSchema};
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
collections::BTreeSet,
ops::{Deref, DerefMut},
};

Expand Down Expand Up @@ -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<Cow<'a, str>>);
pub struct ActivitySet<'a>(#[serde(borrow)] pub BTreeSet<Cow<'a, str>>);

impl ActivitySet<'_> {
/// Does the set of requested activity types contain `*`?
Expand All @@ -58,13 +59,13 @@ impl ActivitySet<'_> {
.iter()
.cloned()
.map(cow_to_static)
.collect::<ahash::HashSet<Cow<'static, str>>>()
.collect::<BTreeSet<Cow<'static, str>>>()

Check warning on line 62 in lib/mrf-manifest/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-manifest/src/lib.rs#L62

Added line #L62 was not covered by tests
.into()
}
}

impl<'a> Deref for ActivitySet<'a> {
type Target = ahash::HashSet<Cow<'a, str>>;
type Target = BTreeSet<Cow<'a, str>>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -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<ActivitySet<'a>> for ahash::HashSet<Cow<'a, str>> {
impl<'a> From<ActivitySet<'a>> for BTreeSet<Cow<'a, str>> {
fn from(value: ActivitySet<'a>) -> Self {
value.0
}
}

impl<'a> From<ahash::HashSet<Cow<'a, str>>> for ActivitySet<'a> {
fn from(value: ahash::HashSet<Cow<'a, str>>) -> Self {
impl<'a> From<BTreeSet<Cow<'a, str>>> for ActivitySet<'a> {
fn from(value: BTreeSet<Cow<'a, str>>) -> Self {
Self(value)
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/mrf-manifest/tests/encode.rs
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 11 additions & 0 deletions lib/mrf-manifest/tests/serialise.rs
Original file line number Diff line number Diff line change
@@ -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);
}
190 changes: 190 additions & 0 deletions lib/mrf-manifest/tests/snapshots/encode__encode_works.snap
Original file line number Diff line number Diff line change
@@ -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
]
5 changes: 5 additions & 0 deletions lib/mrf-manifest/tests/snapshots/serialise__encode_works.snap
Original file line number Diff line number Diff line change
@@ -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"}

0 comments on commit a6e9e68

Please sign in to comment.