Skip to content

Commit 511aaa9

Browse files
committed
Add test for message stats, and fix bugs
1 parent 6395dba commit 511aaa9

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::login_param::ConfiguredLoginParam;
2222
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
2323
use crate::provider::{Provider, get_provider_by_id};
2424
use crate::sync::{self, Sync::*, SyncData};
25-
use crate::tools::get_abs_path;
25+
use crate::tools::{get_abs_path, time};
2626

2727
/// The available configuration keys.
2828
#[derive(
@@ -837,7 +837,7 @@ impl Context {
837837
Config::SelfReporting => {
838838
self.sql.set_raw_config(key.as_ref(), value).await?;
839839
self.sql
840-
.set_raw_config(Config::SelfReportingEnabledTimestamp.as_ref(), value)
840+
.set_raw_config_int64(Config::SelfReportingEnabledTimestamp.as_ref(), time())
841841
.await?;
842842
}
843843
_ => {

src/self_reporting.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ async fn get_message_stats(
433433
let to_verified = t.query_row(
434434
"SELECT COUNT(*) FROM msgs
435435
WHERE chat_id IN temp.verified_chats
436-
AND chat_id<>? AND id>9 AND timestamp_sent>?",
436+
AND chat_id<>? AND id>9 AND timestamp>=? AND hidden=0
437+
AND NOT (param GLOB '*\nS=*' OR param GLOB 'S=*')",
437438
(selfreporting_bot_chat_id, last_selfreport_time),
438439
|row| row.get(0),
439440
)?;
@@ -442,7 +443,8 @@ async fn get_message_stats(
442443
"SELECT COUNT(*) FROM msgs
443444
WHERE chat_id not IN temp.verified_chats
444445
AND (param GLOB '*\nc=1*' OR param GLOB 'c=1*')
445-
AND chat_id<>? AND id>9 AND timestamp_sent>?",
446+
AND chat_id<>? AND id>9 AND timestamp>=? AND hidden=0
447+
AND NOT (param GLOB '*\nS=*' OR param GLOB 'S=*')",
446448
(selfreporting_bot_chat_id, last_selfreport_time),
447449
|row| row.get(0),
448450
)?;
@@ -451,7 +453,8 @@ async fn get_message_stats(
451453
"SELECT COUNT(*) FROM msgs
452454
WHERE chat_id not IN temp.verified_chats
453455
AND NOT (param GLOB '*\nc=1*' OR param GLOB 'c=1*')
454-
AND chat_id<>? AND id>9 AND timestamp_sent>=?",
456+
AND chat_id<>? AND id>9 AND timestamp>=? AND hidden=0
457+
AND NOT (param GLOB '*\nS=*' OR param GLOB 'S=*')",
455458
(selfreporting_bot_chat_id, last_selfreport_time),
456459
|row| row.get(0),
457460
)?;

src/self_reporting/self_reporting_tests.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,54 @@ async fn test_self_report_one_contact() -> Result<()> {
8282
Ok(())
8383
}
8484

85+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
86+
async fn test_message_stats() -> Result<()> {
87+
let mut tcm = TestContextManager::new();
88+
let alice = &tcm.alice().await;
89+
let bob = &tcm.bob().await;
90+
alice.set_config_bool(Config::SelfReporting, true).await?;
91+
let email_chat = alice.create_email_chat(bob).await;
92+
let encrypted_chat = alice.create_chat(bob).await;
93+
94+
let mut expected = MessageStats {
95+
to_verified: 0,
96+
unverified_encrypted: 0,
97+
unencrypted: 0,
98+
};
99+
100+
check_message_stats_report(alice, &expected).await;
101+
102+
alice.send_text(email_chat.id, "foo").await;
103+
expected.unencrypted += 1;
104+
check_message_stats_report(alice, &expected).await;
105+
106+
alice.send_text(encrypted_chat.id, "foo").await;
107+
expected.unverified_encrypted += 1;
108+
check_message_stats_report(alice, &expected).await;
109+
110+
alice.send_text(encrypted_chat.id, "foo").await;
111+
expected.unverified_encrypted += 1;
112+
check_message_stats_report(alice, &expected).await;
113+
114+
tcm.execute_securejoin(alice, bob).await;
115+
expected.to_verified = expected.unverified_encrypted;
116+
expected.unverified_encrypted = 0;
117+
check_message_stats_report(alice, &expected).await;
118+
119+
Ok(())
120+
}
121+
122+
async fn check_message_stats_report(context: &TestContext, expected: &MessageStats) {
123+
let report = get_self_report(context, 0).await.unwrap();
124+
let actual: serde_json::Value = serde_json::from_str(&report).unwrap();
125+
let actual = &actual["message_stats"];
126+
127+
let expected = serde_json::to_string_pretty(&expected).unwrap();
128+
let expected: serde_json::Value = serde_json::from_str(&expected).unwrap();
129+
130+
assert_eq!(actual, &expected);
131+
}
132+
85133
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
86134
async fn test_self_report_securejoin_source_stats() -> Result<()> {
87135
let mut tcm = TestContextManager::new();
@@ -149,5 +197,5 @@ async fn check_securejoin_report(context: &TestContext, expected: &SecurejoinSou
149197
let expected = serde_json::to_string_pretty(&expected).unwrap();
150198
let expected: serde_json::Value = serde_json::from_str(&expected).unwrap();
151199

152-
assert_eq!(&expected, actual);
200+
assert_eq!(actual, &expected);
153201
}

0 commit comments

Comments
 (0)