mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-08 12:45:55 +08:00
feat: improve challenge test case (#8825)
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
name: 'Rust setup'
|
||||||
|
description: 'Rust setup, including cache configuration'
|
||||||
|
inputs:
|
||||||
|
components:
|
||||||
|
description: 'Cargo components'
|
||||||
|
required: false
|
||||||
|
targets:
|
||||||
|
description: 'Cargo target'
|
||||||
|
required: false
|
||||||
|
toolchain:
|
||||||
|
description: 'Rustup toolchain'
|
||||||
|
required: false
|
||||||
|
default: 'stable'
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: 'composite'
|
||||||
|
steps:
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
with:
|
||||||
|
toolchain: ${{ inputs.toolchain }}
|
||||||
|
targets: ${{ inputs.targets }}
|
||||||
|
components: ${{ inputs.components }}
|
||||||
|
- name: Add Targets
|
||||||
|
if: ${{ inputs.targets }}
|
||||||
|
run: rustup target add ${{ inputs.targets }}
|
||||||
|
shell: bash
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
@@ -379,6 +379,23 @@ jobs:
|
|||||||
name: affine
|
name: affine
|
||||||
fail_ci_if_error: false
|
fail_ci_if_error: false
|
||||||
|
|
||||||
|
server-native-test:
|
||||||
|
name: Run server native tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
RUSTFLAGS: -D warnings
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: ./.github/actions/setup-rust
|
||||||
|
|
||||||
|
- name: Install latest nextest release
|
||||||
|
uses: taiki-e/install-action@nextest
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: cargo nextest run --release
|
||||||
|
|
||||||
copilot-api-test:
|
copilot-api-test:
|
||||||
name: Server Copilot Api Test
|
name: Server Copilot Api Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -755,6 +772,8 @@ jobs:
|
|||||||
- build-server-native
|
- build-server-native
|
||||||
- build-electron-renderer
|
- build-electron-renderer
|
||||||
- server-test
|
- server-test
|
||||||
|
- server-native-test
|
||||||
|
- copilot-api-test
|
||||||
- copilot-e2e-test
|
- copilot-e2e-test
|
||||||
- server-e2e-test
|
- server-e2e-test
|
||||||
- desktop-test
|
- desktop-test
|
||||||
|
|||||||
Generated
+42
-1
@@ -32,6 +32,7 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"rand",
|
"rand",
|
||||||
|
"rayon",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha3",
|
"sha3",
|
||||||
@@ -55,6 +56,7 @@ dependencies = [
|
|||||||
"napi-build",
|
"napi-build",
|
||||||
"napi-derive",
|
"napi-derive",
|
||||||
"rand",
|
"rand",
|
||||||
|
"rayon",
|
||||||
"sha3",
|
"sha3",
|
||||||
"tiktoken-rs",
|
"tiktoken-rs",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -331,6 +333,25 @@ version = "2.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
|
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-queue"
|
name = "crossbeam-queue"
|
||||||
version = "0.3.11"
|
version = "0.3.11"
|
||||||
@@ -1013,7 +1034,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
|
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"windows-targets 0.48.5",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1514,6 +1535,26 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.7"
|
version = "0.5.7"
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ notify = { version = "7", features = ["serde"] }
|
|||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
parking_lot = "0.12"
|
parking_lot = "0.12"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
|
rayon = "1.10"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
sha3 = "0.10"
|
sha3 = "0.10"
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ mimalloc = { workspace = true }
|
|||||||
mimalloc = { workspace = true, features = ["local_dynamic_tls"] }
|
mimalloc = { workspace = true, features = ["local_dynamic_tls"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = "1"
|
rayon = { workspace = true }
|
||||||
|
tokio = { workspace = true }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
napi-build = { workspace = true }
|
napi-build = { workspace = true }
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ sqlx = { workspace = true, default-features = false, features = ["chron
|
|||||||
tokio = { workspace = true, features = ["full"] }
|
tokio = { workspace = true, features = ["full"] }
|
||||||
uuid = { workspace = true, features = ["fast-rng", "serde", "v4"] }
|
uuid = { workspace = true, features = ["fast-rng", "serde", "v4"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rayon = { workspace = true }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
affine_schema = { path = "./schema" }
|
affine_schema = { path = "./schema" }
|
||||||
dotenv = { workspace = true }
|
dotenv = { workspace = true }
|
||||||
|
|||||||
@@ -104,7 +104,12 @@ impl TryFrom<&str> for Stamp {
|
|||||||
|
|
||||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||||
let stamp_vec = value.split(':').collect::<Vec<&str>>();
|
let stamp_vec = value.split(':').collect::<Vec<&str>>();
|
||||||
if stamp_vec.len() != 7 {
|
if stamp_vec.len() != 7
|
||||||
|
|| stamp_vec
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.any(|(i, s)| i != 4 && s.is_empty())
|
||||||
|
{
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"Malformed stamp, expected 6 parts, got {}",
|
"Malformed stamp, expected 6 parts, got {}",
|
||||||
stamp_vec.len()
|
stamp_vec.len()
|
||||||
@@ -191,35 +196,75 @@ pub fn mint_challenge_response(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Stamp;
|
use super::Stamp;
|
||||||
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
|
use rayon::prelude::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mint() {
|
fn test_mint() {
|
||||||
let response = Stamp::mint("test".into(), Some(22)).format();
|
{
|
||||||
assert!(Stamp::try_from(response.as_str())
|
let response = Stamp::mint("test".into(), Some(20)).format();
|
||||||
.unwrap()
|
assert!(
|
||||||
.check(22, "test"));
|
Stamp::try_from(response.as_str())
|
||||||
|
.unwrap()
|
||||||
|
.check(20, "test"),
|
||||||
|
"should pass"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let response = Stamp::mint("test".into(), Some(19)).format();
|
||||||
|
assert!(
|
||||||
|
!Stamp::try_from(response.as_str())
|
||||||
|
.unwrap()
|
||||||
|
.check(20, "test"),
|
||||||
|
"should fail with lower bits"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let response = Stamp::mint("test".into(), Some(20)).format();
|
||||||
|
assert!(
|
||||||
|
!Stamp::try_from(response.as_str())
|
||||||
|
.unwrap()
|
||||||
|
.check(20, "test2"),
|
||||||
|
"should fail with different resource"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_check() {
|
fn test_check_expiration() {
|
||||||
assert!(Stamp::try_from("1:20:20202116:test::Z4p8WaiO:31c14")
|
let response = Stamp::mint("test".into(), Some(20));
|
||||||
.unwrap()
|
assert!(response.check_expiration());
|
||||||
.check(20, "test"));
|
}
|
||||||
assert!(!Stamp::try_from("1:20:20202116:test1::Z4p8WaiO:31c14")
|
|
||||||
.unwrap()
|
#[test]
|
||||||
.check(20, "test"));
|
fn test_format() {
|
||||||
assert!(!Stamp::try_from("1:20:20202116:test::z4p8WaiO:31c14")
|
let response = Stamp::mint("test".into(), Some(20));
|
||||||
.unwrap()
|
assert_eq!(
|
||||||
.check(20, "test"));
|
response.format(),
|
||||||
assert!(!Stamp::try_from("1:20:20202116:test::Z4p8WaiO:31C14")
|
format!(
|
||||||
.unwrap()
|
"1:20:{}:test::{}:{}",
|
||||||
.check(20, "test"));
|
response.ts, response.rand, response.counter
|
||||||
assert!(Stamp::try_from("0:20:20202116:test::Z4p8WaiO:31c14").is_err());
|
)
|
||||||
assert!(!Stamp::try_from("1:19:20202116:test::Z4p8WaiO:31c14")
|
);
|
||||||
.unwrap()
|
}
|
||||||
.check(20, "test"));
|
|
||||||
assert!(!Stamp::try_from("1:20:20202115:test::Z4p8WaiO:31c14")
|
#[test]
|
||||||
.unwrap()
|
fn test_fuzz() {
|
||||||
.check(20, "test"));
|
(0..1000).into_par_iter().for_each(|_| {
|
||||||
|
let bit = rand::random::<u32>() % 20 + 1;
|
||||||
|
let resource = rand::thread_rng()
|
||||||
|
.sample_iter(&Alphanumeric)
|
||||||
|
.take(7)
|
||||||
|
.map(char::from)
|
||||||
|
.collect::<String>();
|
||||||
|
let response = Stamp::mint(resource.clone(), Some(bit)).format();
|
||||||
|
assert!(
|
||||||
|
Stamp::try_from(response.as_str())
|
||||||
|
.unwrap()
|
||||||
|
.check(bit, resource),
|
||||||
|
"should pass"
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user