Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,23 @@ public async Task TestChannelIsPresent()
Assert.True(isPresent, "someChannel.IsUserPresent() doesn't return true for most recently joined channel!");
}

[Test]
public async Task TestChannelHasAndGetMember()
{
var someChannel = TestUtils.AssertOperation(await chat.CreatePublicConversation());
await someChannel.Join();

await Task.Delay(4000);

var hasMember = TestUtils.AssertOperation(await someChannel.HasMember(user.Id));
Assert.True(hasMember, "someChannel.HasMember() doesn't return true for most recently joined channel!");

var getMember = TestUtils.AssertOperation(await someChannel.GetMember(user.Id));
Assert.True(getMember.ChannelId == someChannel.Id, "Wrong GetMember() channel id");
Assert.True(getMember.UserId == user.Id, "Wrong GetMember() user id");

}

[Test]
public async Task TestChannelWhoIsPresent()
{
Expand Down Expand Up @@ -405,6 +422,38 @@ public async Task TestPresenceCallback()
Assert.True(presenceReceived, "did not receive presence callback");
}

[Test]
public async Task TestFetchReadReceipts()
{
var someChannel = TestUtils.AssertOperation(await chat.CreatePublicConversation());
await someChannel.Join();
await Task.Delay(2500);

var reset = new ManualResetEvent(false);
Message readMessage = null;
var messageValue = "READ MEEEE";
someChannel.OnMessageReceived += message =>
{
if (message.MessageText == messageValue)
{
readMessage = message;
reset.Set();
}
};
await someChannel.SendText(messageValue);

var gotMessage = reset.WaitOne(20000);
Assert.True(gotMessage, "Never received message callback.");

var membership = TestUtils.AssertOperation(await user.GetMembership(someChannel.Id));
TestUtils.AssertOperation(await membership.SetLastReadMessage(readMessage));
await Task.Delay(8000);

var receipts = TestUtils.AssertOperation(await someChannel.GetReadReceipts());

Assert.True(receipts.Any(x => x.Key == readMessage.TimeToken && x.Value.Contains(user.Id)));
}

[Test]
public async Task TestReportCallback()
{
Expand Down
139 changes: 131 additions & 8 deletions c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ public class ChatTests
[SetUp]
public async Task Setup()
{
chat = TestUtils.AssertOperation(await Chat.CreateInstance(new PubnubChatConfig(storeUserActivityTimestamp: true),
chat = TestUtils.AssertOperation(await Chat.CreateInstance(
new PubnubChatConfig(
storeUserActivityTimestamp: true,
emitReadReceiptEvents:new Dictionary<string, bool>()
{
{"public", true},
{"group", true},
{"direct", true},
}),
new PNConfiguration(new UserId("chats_tests_user_fresh_3"))
{
PublishKey = PubnubTestsParameters.PublishKey,
SubscribeKey = PubnubTestsParameters.SubscribeKey
}));
channel = TestUtils.AssertOperation(await chat.CreatePublicConversation("chat_tests_channel_2"));
channel = TestUtils.AssertOperation(await chat.CreatePublicConversation());
currentUser = TestUtils.AssertOperation(await chat.GetCurrentUser());
await channel.Join();
await Task.Delay(3500);
Expand All @@ -30,6 +38,8 @@ public async Task CleanUp()
{
await channel.Leave();
await Task.Delay(1000);
await channel.Delete();
await Task.Delay(1000);
chat.Destroy();
await Task.Delay(1000);
}
Expand Down Expand Up @@ -231,13 +241,9 @@ public async Task TestReadReceipts()
await Task.Delay(2500);

var receiptReset = new ManualResetEvent(false);
otherChatChannel.OnReadReceiptEvent += readReceipts =>
otherChatChannel.OnReadReceiptEvent += readReceipt =>
{
if (readReceipts.Count == 0)
{
return;
}
Assert.True(readReceipts.Values.Any(x => x != null && x.Contains(currentUser.Id)));
Assert.True(readReceipt.UserId == currentUser.Id);
receiptReset.Set();
};
await otherChatChannel.SendText("READ MEEEE");
Expand All @@ -248,6 +254,123 @@ public async Task TestReadReceipts()
var receipt = receiptReset.WaitOne(15000);
Assert.True(receipt);
}

[Test]
public async Task TestDisableReadReceiptsInChatConfig()
{
var otherUserId = "other_chat_user";
var otherChat = TestUtils.AssertOperation(await Chat.CreateInstance(new PubnubChatConfig(
storeUserActivityTimestamp: true,
emitReadReceiptEvents: new Dictionary<string, bool>()
{
{"public", false}
}),
new PNConfiguration(new UserId(otherUserId))
{
PublishKey = PubnubTestsParameters.PublishKey,
SubscribeKey = PubnubTestsParameters.SubscribeKey
}));
var otherChatChannel = TestUtils.AssertOperation(await otherChat.GetChannel(channel.Id));
await otherChatChannel.Join();
await Task.Delay(2500);

channel.StreamReadReceipts(true);
await Task.Delay(2500);

var receiptReset = new ManualResetEvent(false);
channel.OnReadReceiptEvent += readReceipt =>
{
Assert.True(readReceipt.UserId == otherUserId);
receiptReset.Set();
};
await channel.SendText("READ MEEEE");

await Task.Delay(5000);

await otherChat.MarkAllMessagesAsRead(filter:$"channel.id LIKE \"{channel.Id}\"");
var receipt = receiptReset.WaitOne(15000);
Assert.False(receipt, "Received read receipt even with config set not to send events");
}

[Test]
public async Task TestEnableReadReceiptsInChannelInstance()
{
var otherUserId = "other_chat_user";
var otherChat = TestUtils.AssertOperation(await Chat.CreateInstance(new PubnubChatConfig(
storeUserActivityTimestamp: true,
emitReadReceiptEvents: new Dictionary<string, bool>()
{
{"public", false}
}),
new PNConfiguration(new UserId(otherUserId))
{
PublishKey = PubnubTestsParameters.PublishKey,
SubscribeKey = PubnubTestsParameters.SubscribeKey
}));
var otherChatChannel = TestUtils.AssertOperation(await otherChat.GetChannel(channel.Id));
TestUtils.AssertOperation(await otherChatChannel.Update(new ChatChannelData()
{ EmitReadReceiptEvents = true }));
await Task.Delay(2500);
await otherChatChannel.Join();
await Task.Delay(2500);

channel.StreamReadReceipts(true);
await Task.Delay(2500);

var receiptReset = new ManualResetEvent(false);
channel.OnReadReceiptEvent += readReceipt =>
{
Assert.True(readReceipt.UserId == otherUserId);
receiptReset.Set();
};
await channel.SendText("READ MEEEE");

await Task.Delay(5000);

await otherChat.MarkAllMessagesAsRead(filter:$"channel.id LIKE \"{channel.Id}\"");
var receipt = receiptReset.WaitOne(15000);
Assert.True(receipt, "Didn't receive read receipt even with channel instance set to emit them");
}

[Test]
public async Task TestDisableReadReceiptsInChannelInstance()
{
var otherUserId = "other_chat_user";
var otherChat = TestUtils.AssertOperation(await Chat.CreateInstance(new PubnubChatConfig(
storeUserActivityTimestamp: true,
emitReadReceiptEvents: new Dictionary<string, bool>()
{
{"public", true}
}),
new PNConfiguration(new UserId(otherUserId))
{
PublishKey = PubnubTestsParameters.PublishKey,
SubscribeKey = PubnubTestsParameters.SubscribeKey
}));
var otherChatChannel = TestUtils.AssertOperation(await otherChat.GetChannel(channel.Id));
TestUtils.AssertOperation(await otherChatChannel.Update(new ChatChannelData()
{ EmitReadReceiptEvents = false }));
await Task.Delay(2500);
await otherChatChannel.Join();
await Task.Delay(2500);

channel.StreamReadReceipts(true);
await Task.Delay(2500);

var receiptReset = new ManualResetEvent(false);
channel.OnReadReceiptEvent += readReceipt =>
{
Assert.True(readReceipt.UserId == otherUserId);
receiptReset.Set();
};
await channel.SendText("READ MEEEE");

await Task.Delay(5000);

await otherChat.MarkAllMessagesAsRead(filter:$"channel.id LIKE \"{channel.Id}\"");
var receipt = receiptReset.WaitOne(15000);
Assert.False(receipt, "Received read receipt even with channel instance set not to send events");
}

[Test]
public async Task TestCanI()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task TestInviteMultiple()
[Test]
public async Task TestLastRead()
{
var testChannel = TestUtils.AssertOperation(await chat.CreatePublicConversation("last_read_test_channel_57"));
var testChannel = TestUtils.AssertOperation(await chat.CreatePublicConversation());
await testChannel.Join();

await Task.Delay(4000);
Expand Down Expand Up @@ -149,6 +149,9 @@ public async Task TestLastRead()

var received = messageReceivedManual.WaitOne(90000);
Assert.True(received);

//Cleanup
await testChannel.Delete();
}

[Test]
Expand Down
24 changes: 24 additions & 0 deletions c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,30 @@ public async Task TestMessageReactions()
var reacted = manualReset.WaitOne(10000);
Assert.True(reacted);
}

[Test]
public async Task TestNewMessageReactions()
{
var manualReset = new ManualResetEvent(false);
channel.OnMessageReceived += async message =>
{
await message.ToggleReaction("happy");

await Task.Delay(3000);

var has = message.HasUserReaction("happy");
Assert.True(has);
var newReactions = message.MessageReactions();
Assert.True(newReactions.Count == 1
&& newReactions.Any(
x => x is { Value: "happy", IsMine: true, UserIds.Count: 1 }
&& x.UserIds.Contains(chat.PubnubInstance.GetCurrentUserId())));
manualReset.Set();
};
await channel.SendText("a_message");
var reacted = manualReset.WaitOne(10000);
Assert.True(reacted);
}

[Test]
public async Task TestMessageReport()
Expand Down
16 changes: 16 additions & 0 deletions c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,20 @@ public async Task TestUserIsPresentOn()

Assert.True(isOn, "user.IsPresentOn() doesn't return true for most recently joined channel!");
}

[Test]
public async Task TestUserIsAndGetMember()
{
var someChannel = TestUtils.AssertOperation(await chat.CreatePublicConversation());
await someChannel.Join();

await Task.Delay(4000);

var isMember = TestUtils.AssertOperation(await user.IsMemberOn(someChannel.Id));
Assert.True(isMember, "user.IsMemberOn() doesn't return true for most recently joined channel!");

var getMembership = TestUtils.AssertOperation(await user.GetMembership(someChannel.Id));
Assert.True(getMembership.ChannelId == someChannel.Id, "Wrong GetMembership() channel id");
Assert.True(getMembership.UserId == user.Id, "Wrong GetMembership() user id");
}
}
Loading