feat: enhance message broadcasting to exclude sender's connection and improve IRC compliance

This commit is contained in:
HueByte
2026-07-17 17:05:03 +02:00
parent 953e081123
commit 96736d69df
9 changed files with 47 additions and 16 deletions
+21 -4
View File
@@ -111,23 +111,40 @@ public class IrcBroadcasterTests
}
[Fact]
public async Task SendMessage_SkipsSender()
public async Task SendMessage_SkipsOnlyOriginConnection()
{
var (_, aliceStream) = AddConnectionWithCapture("alice", "general");
var (aliceConn, aliceStream) = AddConnectionWithCapture("alice", "general");
var (_, bobStream) = AddConnectionWithCapture("bob", "general");
var message = new MessageDto(
Guid.NewGuid(), _encryption.Encrypt("Hi"), "alice", null, "general", DateTimeOffset.UtcNow);
await _broadcaster.SendMessageToChannelAsync("general", message);
await _broadcaster.SendMessageToChannelAsync("general", message, aliceConn.ConnectionId);
// Alice (sender) should NOT receive the message
// The connection that sent it should NOT get an echo
Assert.Empty(aliceStream.GetOutputLines());
// Bob should receive it
Assert.NotEmpty(bobStream.GetOutputLines());
}
[Fact]
public async Task SendMessage_SendersOtherSessionsStillReceive()
{
// Same account online twice (e.g. TUI + IRC, or two IRC clients): a message sent
// from one session must still reach the other — skipping by nickname used to
// swallow these until the IRC client reconnected.
var (_, ircStream) = AddConnectionWithCapture("alice", "general");
var message = new MessageDto(
Guid.NewGuid(), _encryption.Encrypt("sent from the TUI"), "alice", null, "general", DateTimeOffset.UtcNow);
// Origin is a SignalR connection, not this IRC one
await _broadcaster.SendMessageToChannelAsync("general", message, "signalr-conn-123");
Assert.Contains(ircStream.GetOutputLines(), l => l.Contains("sent from the TUI"));
}
[Fact]
public async Task SendMessage_OnlySendsToChannelMembers()
{
+1 -1
View File
@@ -188,7 +188,7 @@ internal sealed class FakeChatService : IChatService
return Task.CompletedTask;
}
public Task<string?> SendMessageAsync(Guid userId, string username, string channelName, string content)
public Task<string?> SendMessageAsync(Guid userId, string username, string channelName, string content, string? originConnectionId = null)
{
SentMessages.Add((channelName, content));
return Task.FromResult(SendMessageError);