Skip to content

Commit

Permalink
test it
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Dec 20, 2024
1 parent e820767 commit a287b35
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ALTER TABLE groups ADD COLUMN dm_id TEXT;
ALTER TABLE groups ADD COLUMN last_message_ns BIGINT NOT NULL DEFAULT ((strftime('%s', 'now') * 1000000000) + (strftime('%f', 'now') * 1000000));
ALTER TABLE groups ADD COLUMN last_message_ns BIGINT;

-- Fill the dm_id column
UPDATE groups
Expand Down
4 changes: 3 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,9 @@ pub(crate) mod tests {

let now = now_ns();
let one_second = 1_000_000_000;
assert!(((now - one_second)..(now + one_second)).contains(&dm_group.last_message_ns));
assert!(
((now - one_second)..(now + one_second)).contains(&dm_group.last_message_ns.unwrap())
);

let dm_group = alix.group(dm_group.id).unwrap();
let alix_msgs = dm_group
Expand Down
8 changes: 4 additions & 4 deletions xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct StoredGroup {
/// The inbox_id of the DM target
pub dm_id: Option<String>,
/// Timestamp of when the last message was sent for this group (updated automatically in a trigger)
pub last_message_ns: i64,
pub last_message_ns: Option<i64>,
}

impl_fetch!(StoredGroup, groups, Vec<u8>);
Expand All @@ -73,7 +73,7 @@ impl StoredGroup {
welcome_id: Some(welcome_id),
rotated_at_ns: 0,
dm_id: dm_members.map(String::from),
last_message_ns: now_ns(),
last_message_ns: Some(now_ns()),
}
}

Expand All @@ -98,7 +98,7 @@ impl StoredGroup {
welcome_id: None,
rotated_at_ns: 0,
dm_id: dm_members.map(String::from),
last_message_ns: now_ns(),
last_message_ns: Some(now_ns()),
}
}

Expand All @@ -119,7 +119,7 @@ impl StoredGroup {
welcome_id: None,
rotated_at_ns: 0,
dm_id: None,
last_message_ns: now_ns(),
last_message_ns: Some(now_ns()),
}
}
}
Expand Down
84 changes: 80 additions & 4 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ impl EncryptedMessageStore {
) -> Result<Self, StorageError> {
tracing::info!("Setting up DB connection pool");
let db = native::NativeDb::new(&opts, enc_key)?;
let mut this = Self { db, opts };
this.init_db()?;
Ok(this)
let mut store = Self { db, opts };
store.init_db()?;
Ok(store)
}
}

Expand Down Expand Up @@ -473,6 +473,9 @@ where
pub(crate) mod tests {
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker);
use diesel::sql_types::{BigInt, Blob, Integer, Text};
use group::ConversationType;
use schema::groups;
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;
Expand All @@ -483,7 +486,7 @@ pub(crate) mod tests {
},
Fetch, Store, StreamHandle as _, XmtpOpenMlsProvider,
};
use xmtp_common::{rand_vec, tmp_path};
use xmtp_common::{rand_vec, time::now_ns, tmp_path};

/// Test harness that loads an Ephemeral store.
pub async fn with_connection<F, R>(fun: F) -> R
Expand Down Expand Up @@ -586,6 +589,79 @@ pub(crate) mod tests {
EncryptedMessageStore::remove_db_files(db_path)
}

#[tokio::test]
async fn test_dm_id_migration() {
let db_path = tmp_path();
let opts = StorageOption::Persistent(db_path.clone());

let db =
native::NativeDb::new(&opts, Some(EncryptedMessageStore::generate_enc_key())).unwrap();
let store = EncryptedMessageStore { db, opts };
store.db.validate(&store.opts).unwrap();

store
.db
.conn()
.unwrap()
.raw_query(|conn| {
for _ in 0..15 {
conn.run_next_migration(MIGRATIONS)?;
}

sql_query(
r#"
INSERT INTO groups (
id,
created_at_ns,
membership_state,
installations_last_checked,
added_by_inbox_id,
rotated_at_ns,
conversation_type,
dm_inbox_id
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"#,
)
.bind::<Blob, _>(vec![1, 2, 3, 4, 5])
.bind::<BigInt, _>(now_ns())
.bind::<Integer, _>(GroupMembershipState::Allowed as i32)
.bind::<BigInt, _>(now_ns())
.bind::<Text, _>("121212")
.bind::<BigInt, _>(now_ns())
.bind::<Integer, _>(ConversationType::Dm as i32)
.bind::<Text, _>("98765")
.execute(conn)?;

Ok::<_, StorageError>(())
})
.unwrap();

let conn = store.db.conn().unwrap();

let inbox_id = "inbox_id";
StoredIdentity::new(inbox_id.to_string(), rand_vec::<24>(), rand_vec::<24>())
.store(&conn)
.unwrap();

let fetched_identity: StoredIdentity = conn.fetch(&()).unwrap().unwrap();
assert_eq!(fetched_identity.inbox_id, inbox_id);

store
.db
.conn()
.unwrap()
.raw_query(|conn| {
conn.run_pending_migrations(MIGRATIONS)?;
Ok::<_, StorageError>(())
})
.unwrap();

let groups = conn
.raw_query(|conn| groups::table.load::<StoredGroup>(conn))
.unwrap();
assert_eq!(groups.len(), 1);
assert_eq!(&*groups[0].dm_id.as_ref().unwrap(), "dm:98765:inbox_id");
}

#[tokio::test]
async fn mismatched_encryption_key() {
let mut enc_key = [1u8; 32];
Expand Down
2 changes: 1 addition & 1 deletion xmtp_mls/src/storage/encrypted_store/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ diesel::table! {
rotated_at_ns -> BigInt,
conversation_type -> Integer,
dm_id -> Nullable<Text>,
last_message_ns -> BigInt,
last_message_ns -> Nullable<BigInt>,
}
}

Expand Down

0 comments on commit a287b35

Please sign in to comment.