Skip to content

Commit

Permalink
Extend Jasmine tests with events and requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Nov 12, 2024
1 parent f82be2a commit 8b340c1
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 97 deletions.
1 change: 1 addition & 0 deletions application/apps/indexer/session/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ impl std::fmt::Display for CallbackEvent {
}
}

#[enum_ids(derive = "Debug")]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum LifecycleTransition {
Started { uuid: Uuid, alias: String },
Expand Down
2 changes: 2 additions & 0 deletions application/apps/indexer/session/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
events::{ComputationError, LifecycleTransition},
TRACKER_CHANNEL,
};
use enum_ids::enum_ids;
use log::{error, info};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand All @@ -21,6 +22,7 @@ pub struct Notification {
pub line: Option<usize>,
}

#[enum_ids(derive = "Debug")]
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Progress {
Expand Down
163 changes: 111 additions & 52 deletions application/apps/rustcore/rs-bindings/src/js/converting/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use node_bindgen::{
use proto::*;
use session::{
events::{CallbackEvent, CallbackEventId, NativeError, NativeErrorKind, OperationDone},
progress::{Notification, Progress, Ticks},
progress::{Notification, Progress, ProgressId, Ticks},
};
use session::{progress::Severity, state::AttachmentInfo};
use std::{collections::HashMap, mem, path::PathBuf};
Expand Down Expand Up @@ -157,88 +157,147 @@ pub fn test_cases() -> Vec<CallbackEventWrapped> {
.into_iter()
.flat_map(|id| match id {
CallbackEventId::FileRead => vec![CallbackEvent::FileRead],
CallbackEventId::AttachmentsUpdated => vec![CallbackEvent::AttachmentsUpdated {
len: 9,
attachment: AttachmentInfo {
uuid: Uuid::new_v4(),
filepath: PathBuf::from("fake/path"),
name: String::from("test"),
ext: Some(String::from("test")),
size: 999,
mime: Some(String::from("media")),
messages: vec![1, 2, 3, 100, 101, 102],
CallbackEventId::AttachmentsUpdated => vec![
CallbackEvent::AttachmentsUpdated {
len: 9,
attachment: AttachmentInfo {
uuid: Uuid::new_v4(),
filepath: PathBuf::from("fake/path"),
name: String::from("test"),
ext: Some(String::from("test")),
size: 999,
mime: Some(String::from("media")),
messages: vec![1, 2, 3, 100, 101, 102],
},
},
CallbackEvent::AttachmentsUpdated {
len: 9,
attachment: AttachmentInfo {
uuid: Uuid::new_v4(),
filepath: PathBuf::new(),
name: String::new(),
ext: None,
size: 0,
mime: None,
messages: Vec::new(),
},
},
}],
],
CallbackEventId::IndexedMapUpdated => {
vec![CallbackEvent::IndexedMapUpdated { len: 999 }]
vec![
CallbackEvent::IndexedMapUpdated { len: 999 },
CallbackEvent::IndexedMapUpdated { len: 0 },
]
}
CallbackEventId::OperationDone => vec![CallbackEvent::OperationDone(OperationDone {
uuid: Uuid::new_v4(),
result: Some(String::from("test")),
})],
CallbackEventId::OperationError => vec![CallbackEvent::OperationError {
uuid: Uuid::new_v4(),
error: NativeError {
severity: Severity::ERROR,
kind: NativeErrorKind::ChannelError,
message: Some(String::from("test")),
CallbackEventId::OperationDone => vec![
CallbackEvent::OperationDone(OperationDone {
uuid: Uuid::new_v4(),
result: Some(String::from("test")),
}),
CallbackEvent::OperationDone(OperationDone {
uuid: Uuid::new_v4(),
result: None,
}),
CallbackEvent::OperationDone(OperationDone {
uuid: Uuid::new_v4(),
result: Some(String::new()),
}),
],
CallbackEventId::OperationError => vec![
CallbackEvent::OperationError {
uuid: Uuid::new_v4(),
error: NativeError {
severity: Severity::ERROR,
kind: NativeErrorKind::ChannelError,
message: Some(String::from("test")),
},
},
}],
CallbackEvent::OperationError {
uuid: Uuid::new_v4(),
error: NativeError {
severity: Severity::WARNING,
kind: NativeErrorKind::ChannelError,
message: Some(String::new()),
},
},
CallbackEvent::OperationError {
uuid: Uuid::new_v4(),
error: NativeError {
severity: Severity::ERROR,
kind: NativeErrorKind::ChannelError,
message: None,
},
},
],
CallbackEventId::OperationProcessing => {
vec![CallbackEvent::OperationProcessing(Uuid::new_v4())]
}
CallbackEventId::OperationStarted => {
vec![CallbackEvent::OperationStarted(Uuid::new_v4())]
}
CallbackEventId::Progress => vec![
CallbackEvent::Progress {
uuid: Uuid::new_v4(),
progress: Progress::Ticks(Ticks {
count: 1,
state: Some(String::from("test")),
total: Some(100),
}),
},
CallbackEvent::Progress {
uuid: Uuid::new_v4(),
progress: Progress::Notification(Notification {
severity: Severity::ERROR,
content: String::from("test"),
line: Some(999),
}),
},
CallbackEvent::Progress {
CallbackEventId::Progress => ProgressId::as_vec()
.into_iter()
.map(|pid| CallbackEvent::Progress {
uuid: Uuid::new_v4(),
progress: Progress::Stopped,
},
],
progress: match pid {
ProgressId::Notification => Progress::Notification(Notification {
severity: Severity::ERROR,
content: String::from("test"),
line: Some(999),
}),
ProgressId::Stopped => Progress::Stopped,
ProgressId::Ticks => Progress::Ticks(Ticks {
count: 1,
state: Some(String::from("test")),
total: Some(100),
}),
},
})
.collect(),
CallbackEventId::SearchMapUpdated => {
vec![CallbackEvent::SearchMapUpdated(Some(String::from("test")))]
vec![
CallbackEvent::SearchMapUpdated(Some(String::from("test"))),
CallbackEvent::SearchMapUpdated(Some(String::new())),
CallbackEvent::SearchMapUpdated(None),
]
}
CallbackEventId::SearchUpdated => {
let mut stat = HashMap::new();
stat.insert(String::from("a"), 999);
stat.insert(String::from("b"), 999);
stat.insert(String::from("c"), 999);
vec![CallbackEvent::SearchUpdated {
found: 999 * 3,
stat,
}]
vec![
CallbackEvent::SearchUpdated {
found: 999 * 3,
stat,
},
CallbackEvent::SearchUpdated {
found: 0,
stat: HashMap::new(),
},
]
}
CallbackEventId::SearchValuesUpdated => {
let mut data = HashMap::new();
data.insert(1, (1.2, 10.2));
data.insert(2, (2.2, 20.2));
data.insert(3, (3.2, 30.2));
vec![CallbackEvent::SearchValuesUpdated(Some(data))]
vec![
CallbackEvent::SearchValuesUpdated(Some(data)),
CallbackEvent::SearchValuesUpdated(None),
CallbackEvent::SearchValuesUpdated(Some(HashMap::new())),
]
}
CallbackEventId::SessionDestroyed => vec![CallbackEvent::SessionDestroyed],
CallbackEventId::SessionError => vec![CallbackEvent::SessionError(NativeError {
severity: Severity::ERROR,
kind: NativeErrorKind::ChannelError,
message: Some(String::from("test")),
})],
CallbackEventId::StreamUpdated => vec![CallbackEvent::StreamUpdated(999)],
CallbackEventId::StreamUpdated => vec![
CallbackEvent::StreamUpdated(999),
CallbackEvent::StreamUpdated(0),
],
})
.collect();
events.into_iter().map(|ev| ev.into()).collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ use node_bindgen::{
};
use progress::{self, lifecycle_transition};
use proto::*;
use session::events::LifecycleTransition;
use session::{
events::{LifecycleTransition, LifecycleTransitionId},
progress::Ticks,
};
use uuid::Uuid;

#[derive(Debug)]
pub(crate) struct LifecycleTransitionWrapper(Option<LifecycleTransition>);
pub(crate) struct LifecycleTransitionWrapped(Option<LifecycleTransition>);

impl LifecycleTransitionWrapper {
impl LifecycleTransitionWrapped {
pub fn new(lt: LifecycleTransition) -> Self {
LifecycleTransitionWrapper(Some(lt))
LifecycleTransitionWrapped(Some(lt))
}
}

impl From<LifecycleTransition> for LifecycleTransitionWrapped {
fn from(ev: LifecycleTransition) -> LifecycleTransitionWrapped {
LifecycleTransitionWrapped::new(ev)
}
}

impl From<LifecycleTransitionWrapper> for Vec<u8> {
fn from(mut val: LifecycleTransitionWrapper) -> Self {
impl From<LifecycleTransitionWrapped> for Vec<u8> {
fn from(mut val: LifecycleTransitionWrapped) -> Self {
let ev = val
.0
.take()
Expand Down Expand Up @@ -50,8 +60,54 @@ impl From<LifecycleTransitionWrapper> for Vec<u8> {
}
}

impl TryIntoJs for LifecycleTransitionWrapper {
impl TryIntoJs for LifecycleTransitionWrapped {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

pub fn test_cases() -> Vec<LifecycleTransitionWrapped> {
let events: Vec<LifecycleTransition> = LifecycleTransitionId::as_vec()
.into_iter()
.flat_map(|id| match id {
LifecycleTransitionId::Started => vec![
LifecycleTransition::Started {
uuid: Uuid::new_v4(),
alias: String::from("test"),
},
LifecycleTransition::Started {
uuid: Uuid::new_v4(),
alias: String::new(),
},
],
LifecycleTransitionId::Stopped => vec![LifecycleTransition::Stopped(Uuid::new_v4())],
LifecycleTransitionId::Ticks => vec![
LifecycleTransition::Ticks {
uuid: Uuid::new_v4(),
ticks: Ticks {
count: 1,
state: Some(String::from("test")),
total: Some(100),
},
},
LifecycleTransition::Ticks {
uuid: Uuid::new_v4(),
ticks: Ticks {
count: 0,
state: Some(String::new()),
total: Some(0),
},
},
LifecycleTransition::Ticks {
uuid: Uuid::new_v4(),
ticks: Ticks {
count: 0,
state: None,
total: None,
},
},
],
})
.collect();
events.into_iter().map(|ev| ev.into()).collect()
}
9 changes: 9 additions & 0 deletions application/apps/rustcore/rs-bindings/src/js/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
event::{self, CallbackEventWrapped},
filter::WrappedSearchFilter,
grabbing::GrabbedElements,
progress,
ranges::RangeInclusiveList,
sde::SdeResponseWrapped,
source::WrappedSourceDefinition,
Expand Down Expand Up @@ -739,4 +740,12 @@ impl RustSession {
.map(|ev| SafeArrayBuffer::new(ev.into()))
.collect::<Vec<SafeArrayBuffer>>())
}

#[node_bindgen]
fn test_lt_events_as_proto(&self) -> Result<Vec<SafeArrayBuffer>, ComputationErrorWrapper> {
Ok(progress::test_cases()
.into_iter()
.map(|ev| SafeArrayBuffer::new(ev.into()))
.collect::<Vec<SafeArrayBuffer>>())
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::js::converting::{
errors::ComputationErrorWrapper, progress::LifecycleTransitionWrapper,
errors::ComputationErrorWrapper, progress::LifecycleTransitionWrapped,
};
use log::trace;
use node_bindgen::derive::node_bindgen;
Expand Down Expand Up @@ -27,7 +27,7 @@ impl RustProgressTracker {
}

#[node_bindgen(mt)]
async fn init<F: Fn(LifecycleTransitionWrapper) + Send + 'static>(
async fn init<F: Fn(LifecycleTransitionWrapped) + Send + 'static>(
&mut self,
callback: F,
) -> Result<(), ComputationErrorWrapper> {
Expand All @@ -43,7 +43,7 @@ impl RustProgressTracker {
Ok(mut rx) => {
let _ = result_tx.send(Ok(()));
while let Some(progress_report) = rx.recv().await {
callback(LifecycleTransitionWrapper::new(progress_report))
callback(LifecycleTransitionWrapped::new(progress_report))
}
}
Err(e) => {
Expand Down
4 changes: 3 additions & 1 deletion application/apps/rustcore/ts-bindings/spec/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"list": {
"1": "Test 1. CallbackEvent",
"2": "Test 2. Comparing JSON vs Protobuf",
"3": "Test 3. All CallbackEvens as Protobuf"
"3": "Test 3. All CallbackEvens as Protobuf",
"4": "Test 4. All LifecycleTransition as Protobuf"

},
"files": {
}
Expand Down
Loading

0 comments on commit 8b340c1

Please sign in to comment.