From 874b0a2c697da3f2e934b09745fc1747b65a30d7 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 2 Jun 2023 23:01:53 -0400 Subject: [PATCH] add unit test --- packages/backend/native-utils/package.json | 4 +- .../native-utils/src/model/schema/antenna.rs | 10 ++-- .../native-utils/src/model/schema/app.rs | 48 +++++++++++++++++-- packages/backend/native-utils/src/util/id.rs | 4 +- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/packages/backend/native-utils/package.json b/packages/backend/native-utils/package.json index 1503bca19..029372548 100644 --- a/packages/backend/native-utils/package.json +++ b/packages/backend/native-utils/package.json @@ -41,7 +41,7 @@ "universal": "napi universal", "version": "napi version", "format": "cargo fmt", - "cargo:unit": "cargo test unit_test", - "cargo:integration": "cargo test --no-default-features -F noarray int_test -- --test-threads=1" + "cargo:unit": "cargo test unit_test && cargo test -F napi unit_test", + "cargo:integration": "cargo test -F noarray int_test -- --test-threads=1" } } diff --git a/packages/backend/native-utils/src/model/schema/antenna.rs b/packages/backend/native-utils/src/model/schema/antenna.rs index 00f1216ac..4ec1e0794 100644 --- a/packages/backend/native-utils/src/model/schema/antenna.rs +++ b/packages/backend/native-utils/src/model/schema/antenna.rs @@ -142,7 +142,7 @@ mod unit_test { #[test] fn antenna_valid() { let instance = json!({ - "id": "9f4x0bkx1u", + "id": "9fil64s6g7cskdrb", "createdAt": "2023-05-24T06:56:14.323Z", "name": "Valid Antenna", "keywords": [["first", "keyword"], ["second"]], @@ -150,7 +150,7 @@ mod unit_test { "src": "users", // "userListId" and "userGroupId" can be null or be omitted "userListId": null, - "users": ["9f4yjw6m13", "9f4yk2cp6d"], + "users": ["9fil64s6g7cskdrb", "9fil66brl1udxau2"], "instances": [], // "caseSensitive", "notify", "withReplies", "withFile", and // "hasUnreadNote" are false if ommited @@ -175,14 +175,14 @@ mod unit_test { "keywords": "invalid keyword", // "excludeKeywords" is required "excludeKeywords": null, - // "src" should be one of "home", "all", "users", "list", "group", and + // "src" must be one of "home", "all", "users", "list", "group", and // "instances" "src": "invalid_src", // "userListId" is string "userListId": ["9f4ziiqfxw"], // "users" must be an array of strings - "users": [1, "9f4ykyuza6"], - "instances": ["9f4ykyuybo"], + "users": [1, "9fil64s6g7cskdrb"], + "instances": ["9fil65jzhtjpi3xn"], // "caseSensitive" is boolean "caseSensitive": 0, "notify": true, diff --git a/packages/backend/native-utils/src/model/schema/app.rs b/packages/backend/native-utils/src/model/schema/app.rs index f24c1387b..682b82ec0 100644 --- a/packages/backend/native-utils/src/model/schema/app.rs +++ b/packages/backend/native-utils/src/model/schema/app.rs @@ -95,13 +95,53 @@ pub static VALIDATOR: Lazy = Lazy::new(|| App::validator()); #[cfg(test)] mod unit_test { + use pretty_assertions::assert_eq; + use serde_json::json; + + use crate::util::id::{create_id, init_id}; + use crate::util::random::gen_string; + + use super::VALIDATOR; + #[test] - fn valid() { - todo!(); + fn app_valid() { + init_id(12, ""); + let instance = json!({ + "id": create_id().unwrap(), + "name": "Test App", + "secret": gen_string(24), + "callbackUrl": "urn:ietf:wg:oauth:2.0:oob", + "permission": ["read:account", "write:account", "read:notes"], + }); + + assert!(VALIDATOR.is_valid(&instance)); } #[test] - fn invalid() { - todo!(); + fn app_invalid() { + init_id(12, ""); + let instance = json!({ + "id": create_id().unwrap(), + // "name" is required + "name": null, + // "permission" must be one of the app permissions + "permission": ["write:invalid_perm", "write:notes"], + // "secret" is a nullable string + "secret": 123, + // "is_authorized" is a nullable boolean + "isAuthorized": "true-ish", + }); + let result = VALIDATOR + .validate(&instance) + .expect_err("validation must fail"); + let mut paths: Vec = result + .map(|e| e.instance_path.to_string()) + .filter(|e| !e.is_empty()) + .collect(); + paths.sort(); + assert_eq!( + paths, + vec!["/isAuthorized", "/name", "/permission/0", "/secret"] + ); } } diff --git a/packages/backend/native-utils/src/util/id.rs b/packages/backend/native-utils/src/util/id.rs index b17b112d7..d922518f9 100644 --- a/packages/backend/native-utils/src/util/id.rs +++ b/packages/backend/native-utils/src/util/id.rs @@ -72,9 +72,9 @@ mod unit_test { #[test] fn can_generate_aid_compat_ids() { - id::native_init_id_generator(16, "".to_string()); + id::native_init_id_generator(20, "".to_string()); let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); - assert_eq!(id1.len(), 16); + assert_eq!(id1.len(), 20); let id1 = id::native_create_id(Utc::now().timestamp_millis().into()); let id2 = id::native_create_id(Utc::now().timestamp_millis().into()); assert_ne!(id1, id2);