diff --git a/packages/backend/native-utils/crates/model/Cargo.toml b/packages/backend/native-utils/crates/model/Cargo.toml index c2002443d6..4925fb702f 100644 --- a/packages/backend/native-utils/crates/model/Cargo.toml +++ b/packages/backend/native-utils/crates/model/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = [] +default = ["napi"] noarray = [] napi = ["dep:napi", "dep:napi-derive"] @@ -30,3 +30,6 @@ utoipa = "3.3.0" # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix napi = { version = "2.12.0", default-features = false, features = ["napi4"], optional = true } napi-derive = { version = "2.12.0", optional = true } + +[dev-dependencies] +pretty_assertions = "1.3.0" diff --git a/packages/backend/native-utils/crates/model/src/schema.rs b/packages/backend/native-utils/crates/model/src/schema.rs index 78a0887fe4..71f9348b32 100644 --- a/packages/backend/native-utils/crates/model/src/schema.rs +++ b/packages/backend/native-utils/crates/model/src/schema.rs @@ -8,11 +8,13 @@ use schemars::{schema_for, JsonSchema}; cfg_if! { if #[cfg(feature = "napi")] { mod napi; - pub use napi::antenna::Antenna; - pub use napi::antenna::AntennaSrc; + pub use self::napi::antenna::Antenna; + pub use self::napi::antenna::AntennaSrc; } else { pub use antenna::Antenna; pub use antenna::AntennaSrc; + pub use app::App; + pub use app::AppPermission; } } diff --git a/packages/backend/native-utils/crates/model/src/schema/antenna.rs b/packages/backend/native-utils/crates/model/src/schema/antenna.rs index 2f13a563d8..338998019c 100644 --- a/packages/backend/native-utils/crates/model/src/schema/antenna.rs +++ b/packages/backend/native-utils/crates/model/src/schema/antenna.rs @@ -46,7 +46,7 @@ pub enum AntennaSrc { Instances, } -impl TryFrom for AntennaSrc { +impl TryFrom for super::AntennaSrc { type Error = crate::error::Error; fn try_from(value: AntennaSrcEnum) -> Result { @@ -55,12 +55,13 @@ impl TryFrom for AntennaSrc { } // ---- TODO: could be macro -impl Schema for Antenna {} -pub static VALIDATOR: Lazy = Lazy::new(|| Antenna::validator()); +impl Schema for super::Antenna {} +pub static VALIDATOR: Lazy = Lazy::new(|| super::Antenna::validator()); // ---- #[cfg(test)] mod unit_test { + use pretty_assertions::assert_eq; use serde_json::json; use crate::{entity::sea_orm_active_enums::AntennaSrcEnum, schema::AntennaSrc}; @@ -128,20 +129,23 @@ mod unit_test { let result = VALIDATOR .validate(&instance) .expect_err("validation must fail"); - let mut paths: Vec = result.map(|e| e.schema_path.to_string()).collect(); + let mut paths: Vec = result + .map(|e| e.instance_path.to_string()) + .filter(|e| !e.is_empty()) + .collect(); paths.sort(); assert_eq!( paths, vec![ - "/properties/caseSensitive/type", - "/properties/createdAt/format", - "/properties/excludeKeywords/type", - "/properties/id/type", - "/properties/keywords/type", - "/properties/src/enum", - "/properties/userListId/type", - "/properties/users/items/type", - "/required" + "/caseSensitive", + #[cfg(not(feature = "napi"))] + "/createdAt", + "/excludeKeywords", + "/id", + "/keywords", + "/src", + "/userListId", + "/users/0" ] ); } diff --git a/packages/backend/native-utils/crates/model/src/schema/app.rs b/packages/backend/native-utils/crates/model/src/schema/app.rs index b45cad6d66..f24c1387be 100644 --- a/packages/backend/native-utils/crates/model/src/schema/app.rs +++ b/packages/backend/native-utils/crates/model/src/schema/app.rs @@ -13,14 +13,14 @@ pub struct App { #[schemars(url)] pub callback_url: Option, #[schema(inline)] - pub permission: Vec, + pub permission: Vec, pub secret: Option, pub is_authorized: Option, } /// This represents `permissions` in `packages/calckey-js/src/consts.ts`. #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] -pub enum Permission { +pub enum AppPermission { #[serde(rename = "read:account")] ReadAccount, #[serde(rename = "write:account")] diff --git a/packages/backend/native-utils/crates/model/src/schema/napi/antenna.rs b/packages/backend/native-utils/crates/model/src/schema/napi/antenna.rs index cfba50d7f6..ffb817c123 100644 --- a/packages/backend/native-utils/crates/model/src/schema/napi/antenna.rs +++ b/packages/backend/native-utils/crates/model/src/schema/napi/antenna.rs @@ -1,10 +1,9 @@ +use napi::bindgen_prelude::{FromNapiValue, ToNapiValue}; +use napi_derive::napi; use parse_display::FromStr; use schemars::JsonSchema; use utoipa::ToSchema; -use napi::bindgen_prelude::{FromNapiValue, ToNapiValue}; -use napi_derive::napi; - #[napi] #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] #[serde(rename_all = "camelCase")] diff --git a/packages/backend/native-utils/crates/model/tests/repository/antenna.rs b/packages/backend/native-utils/crates/model/tests/repository/antenna.rs index 90732130a9..2da1e59d44 100644 --- a/packages/backend/native-utils/crates/model/tests/repository/antenna.rs +++ b/packages/backend/native-utils/crates/model/tests/repository/antenna.rs @@ -4,6 +4,7 @@ mod int_test { repository::Repository, schema, }; + use pretty_assertions::assert_eq; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use crate::{cleanup, prepare}; diff --git a/packages/backend/native-utils/crates/util/Cargo.toml b/packages/backend/native-utils/crates/util/Cargo.toml index ad5486b4b6..6050342dd9 100644 --- a/packages/backend/native-utils/crates/util/Cargo.toml +++ b/packages/backend/native-utils/crates/util/Cargo.toml @@ -10,3 +10,6 @@ cuid2 = "0.1.0" once_cell = "1.17.1" rand = "0.8.5" thiserror = "1.0.40" + +[dev-dependencies] +pretty_assertions = "1.3.0" diff --git a/packages/backend/native-utils/crates/util/src/id.rs b/packages/backend/native-utils/crates/util/src/id.rs index 2831ef79dd..d3c5809ea5 100644 --- a/packages/backend/native-utils/crates/util/src/id.rs +++ b/packages/backend/native-utils/crates/util/src/id.rs @@ -22,6 +22,7 @@ pub fn create_id() -> Result { #[cfg(test)] mod unit_test { + use pretty_assertions::{assert_eq, assert_ne}; use std::thread; use crate::id; diff --git a/packages/backend/native-utils/crates/util/src/random.rs b/packages/backend/native-utils/crates/util/src/random.rs index 1c4a20d50f..5ee06d1282 100644 --- a/packages/backend/native-utils/crates/util/src/random.rs +++ b/packages/backend/native-utils/crates/util/src/random.rs @@ -10,6 +10,7 @@ pub fn gen_string(length: u16) -> String { #[cfg(test)] mod unit_test { + use pretty_assertions::{assert_eq, assert_ne}; use std::thread; use super::gen_string;