Skip to content
Closed
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
89 changes: 89 additions & 0 deletions src/Netclaw.Actors.Tests/Channels/SlackBlockConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,93 @@ public void BoldAndItalicNested_HandledCorrectly()
Assert.NotNull(styled);
Assert.Equal("bold and italic", styled.Text);
}

[Fact]
public void UserMention_ProducesRichTextUser()
{
var blocks = SlackBlockConverter.Convert("Ping <@U01SU57E553> when ready");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var user = Assert.Single(section.Elements.OfType<RichTextUser>());
Assert.Equal("U01SU57E553", user.UserId);

var texts = section.Elements.OfType<RichTextText>().ToList();
Assert.Equal("Ping ", texts[0].Text);
Assert.Equal(" when ready", texts[1].Text);
}

[Fact]
public void UserMention_WithLabel_ProducesRichTextUser()
{
var blocks = SlackBlockConverter.Convert("Thanks <@U01SU57E553|david>!");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var user = Assert.Single(section.Elements.OfType<RichTextUser>());
Assert.Equal("U01SU57E553", user.UserId);
}

[Fact]
public void UserGroupMention_ProducesRichTextUserGroup()
{
var blocks = SlackBlockConverter.Convert("Heads up <@subteam^S0123ABC>");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var group = Assert.Single(section.Elements.OfType<RichTextUserGroup>());
Assert.Equal("S0123ABC", group.UserGroupId);
}

[Fact]
public void UserGroupMention_WithLabel_ProducesRichTextUserGroup()
{
var blocks = SlackBlockConverter.Convert("Escalate to <@subteam^S0123ABC|oncall> now");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var group = Assert.Single(section.Elements.OfType<RichTextUserGroup>());
Assert.Equal("S0123ABC", group.UserGroupId);
}

[Fact]
public void PrivateChannelMention_ProducesRichTextChannel()
{
var blocks = SlackBlockConverter.Convert("Also in <#G0123ABC|private-ops>");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var channel = Assert.Single(section.Elements.OfType<RichTextChannel>());
Assert.Equal("G0123ABC", channel.ChannelId);
}

[Fact]
public void ChannelMention_ProducesRichTextChannel()
{
var blocks = SlackBlockConverter.Convert("Posted in <#C0AM51E342X> already");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var section = Assert.Single(rtb.Elements.OfType<RichTextSection>());

var channel = Assert.Single(section.Elements.OfType<RichTextChannel>());
Assert.Equal("C0AM51E342X", channel.ChannelId);
}

[Fact]
public void UserMention_InListItem_ProducesRichTextUser()
{
var blocks = SlackBlockConverter.Convert("- David: approve <@U01SU57E553>");

var rtb = Assert.Single(blocks.OfType<RichTextBlock>());
var list = Assert.Single(rtb.Elements.OfType<RichTextList>());
var item = list.Elements[0];

var user = Assert.Single(item.Elements.OfType<RichTextUser>());
Assert.Equal("U01SU57E553", user.UserId);
}
}
31 changes: 31 additions & 0 deletions src/Netclaw.Channels.Slack/SlackBlockConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ private static (int Index, int Length, RichTextSectionElement? Element, string?
: new RichTextLink { Url = url };
});

// Slack-native mentions passed through by the agent: <@U...>
// user, <@subteam^S...> user group, <#C...> channel. Without
// explicit rich-text elements the Block Kit surface renders
// the syntax as literal text (the mrkdwn Text fallback handles
// them natively, but blocks win on every modern client).
TryMatch(UserMentionRegex(), text, ref best, (m) =>
new RichTextUser { UserId = m.Groups[1].Value });

TryMatch(UserGroupMentionRegex(), text, ref best, (m) =>
new RichTextUserGroup { UserGroupId = m.Groups[1].Value });

TryMatch(ChannelMentionRegex(), text, ref best, (m) =>
new RichTextChannel { ChannelId = m.Groups[1].Value });

if (best.Element is null)
return (0, 0, null, null);

Expand Down Expand Up @@ -357,4 +371,21 @@ private static string StripInlineMarkdown(string text)
// Ordered list prefix: 1. or 2. etc.
[GeneratedRegex(@"^\d+\.\s")]
private static partial Regex OrderedListPrefix();

// Slack user mention: <@U0123ABC> or <@W0123ABC>, optionally with a
// fallback label (<@U0123ABC|david>).
[GeneratedRegex(@"<@([UW][0-9A-Z]+)(?:\|[^>]+)?>")]
private static partial Regex UserMentionRegex();

// Slack user-group mention: <@subteam^S0123ABC>, optionally with a
// label (<@subteam^S0123ABC|oncall>). Captures the bare group ID —
// the rich-text usergroup element takes the ID as returned by
// usergroups.list, without the subteam^ prefix.
[GeneratedRegex(@"<@subteam\^([S0-9A-Z]+)(?:\|[^>]+)?>")]
private static partial Regex UserGroupMentionRegex();

// Slack channel mention: <#C0123ABC>, optionally with a label.
// C = public channel, G = private channel/group DM, D = DM.
[GeneratedRegex(@"<#([CGD][0-9A-Z]+)(?:\|[^>]+)?>")]
private static partial Regex ChannelMentionRegex();
}
Loading