chorus/tests/gateway.rs

119 lines
3.7 KiB
Rust
Raw Normal View History

2023-06-08 19:51:32 +02:00
mod common;
use std::sync::{Arc, RwLock};
2023-06-08 19:51:32 +02:00
use chorus::gateway::*;
use chorus::types::{self, ChannelModifySchema, RoleCreateModifySchema, RoleObject};
2023-06-08 19:51:32 +02:00
#[tokio::test]
/// Tests establishing a connection (hello and heartbeats) on the local gateway;
async fn test_gateway_establish() {
let bundle = common::setup().await;
Gateway::get_handle(bundle.urls.wss.clone()).await.unwrap();
2023-07-22 14:38:55 +02:00
common::teardown(bundle).await
2023-06-08 19:51:32 +02:00
}
#[tokio::test]
/// Tests establishing a connection and authenticating
async fn test_gateway_authenticate() {
let bundle = common::setup().await;
let gateway = Gateway::get_handle(bundle.urls.wss.clone()).await.unwrap();
2023-06-08 19:51:32 +02:00
let mut identify = types::GatewayIdentifyPayload::common();
2023-07-22 14:38:55 +02:00
identify.token = bundle.user.token.clone();
2023-06-08 19:51:32 +02:00
gateway.send_identify(identify).await;
2023-07-22 14:38:55 +02:00
common::teardown(bundle).await
2023-06-08 19:51:32 +02:00
}
#[tokio::test]
async fn test_self_updating_structs() {
let mut bundle = common::setup().await;
2023-08-16 01:11:32 +02:00
let received_channel = bundle
.user
.gateway
.observe_and_into_inner(bundle.channel.clone())
.await;
2023-08-15 20:20:58 +02:00
assert_eq!(received_channel, bundle.channel.read().unwrap().clone());
let modify_schema = ChannelModifySchema {
name: Some("selfupdating".to_string()),
..Default::default()
};
2023-08-16 01:11:32 +02:00
received_channel
.modify(modify_schema, None, &mut bundle.user)
.await
.unwrap();
2023-08-12 22:40:40 +02:00
assert_eq!(
2023-08-16 01:11:32 +02:00
bundle
.user
.gateway
.observe_and_into_inner(bundle.channel.clone())
.await
.name
.unwrap(),
"selfupdating".to_string()
2023-08-12 22:40:40 +02:00
);
2023-07-22 14:38:55 +02:00
common::teardown(bundle).await
}
2023-08-16 14:06:04 +02:00
#[tokio::test]
async fn test_recursive_self_updating_structs() {
2023-08-16 21:26:19 +02:00
// Setup
let mut bundle = common::setup().await;
let guild = bundle.guild.clone();
// Observe Guild, make sure it has no channels
let guild = bundle.user.gateway.observe(guild.clone()).await;
let inner_guild = guild.read().unwrap().clone();
assert!(inner_guild.roles.is_none());
// Create Role
let permissions = types::PermissionFlags::CONNECT | types::PermissionFlags::MANAGE_EVENTS;
let permissions = Some(permissions.to_string());
let mut role_create_schema: types::RoleCreateModifySchema = RoleCreateModifySchema {
name: Some("cool person".to_string()),
permissions,
hoist: Some(true),
icon: None,
unicode_emoji: Some("".to_string()),
mentionable: Some(true),
position: None,
color: None,
};
let guild_id = inner_guild.id;
let role = RoleObject::create(&mut bundle.user, guild_id, role_create_schema.clone())
.await
.unwrap();
// Watch role;
bundle
.user
.gateway
.observe(Arc::new(RwLock::new(role.clone())))
.await;
// Update Guild and check for Guild
let inner_guild = guild.read().unwrap().clone();
assert!(inner_guild.roles.is_some());
// Update the Role
role_create_schema.name = Some("yippieee".to_string());
RoleObject::modify(&mut bundle.user, guild_id, role.id, role_create_schema)
.await
.unwrap();
2023-08-17 18:23:57 +02:00
let role_inner = bundle
.user
.gateway
.observe_and_into_inner(Arc::new(RwLock::new(role.clone())))
.await;
assert_eq!(role_inner.name, "yippieee");
// Check if the change propagated
2023-08-17 18:23:57 +02:00
let guild = bundle.user.gateway.observe(bundle.guild.clone()).await;
let inner_guild = guild.read().unwrap().clone();
let guild_roles = inner_guild.roles;
let guild_role = guild_roles.unwrap();
let guild_role_inner = guild_role.get(0).unwrap().read().unwrap().clone();
assert_eq!(guild_role_inner.name, "yippieee".to_string());
2023-08-16 14:06:04 +02:00
common::teardown(bundle).await;
}