feat: generalize session sending with multiline text and structured mentions

Add named-session file/image/reply sending, preserve multiline text, and share safe member/all mention handling. Protect existing drafts, bind popup selection to the current window, validate structured mention tokens, and require fresh matching visible messages without automatic resend. Core tests: 121 passed. Windows 4.1.13.63 final validation: 9/9 passed. Record explicit coverage limits and sanitized evidence.
This commit is contained in:
2026-09-06 17:10:53 +08:00
parent ad22a89185
commit c49f6a10b2
23 changed files with 531 additions and 69 deletions
@@ -0,0 +1,31 @@
using WxAgent.Core;
using Xunit;
namespace WxAgent.Core.Tests;
public sealed class WechatMentionConfirmationTests
{
[Theory]
[InlineData("\uFFFC", true)]
[InlineData("\uFFFC\u2005", true)]
[InlineData("@Hao 豪", false)]
[InlineData("@", false)]
[InlineData("\uFFFCunexpected", false)]
[InlineData("\uFFFC\uFFFC", false)]
public void RequiresExactlyOneStructuredMentionToken(string value, bool expected) =>
Assert.Equal(expected, WechatSendConfirmation.IsMentionToken(value));
[Theory]
[InlineData("@Hao 豪 marker", "Hao 豪", true)]
[InlineData("@Hao 豪\u2005marker", "Hao 豪", true)]
[InlineData("@所有人 marker", "所有人", true)]
[InlineData("@Hao 豪 other", "Hao 豪", false)]
[InlineData("@Other marker", "Hao 豪", false)]
public void RequiresFreshExactMentionAndBody(string text, string member, bool expected)
{
var message = new ChatMessageSnapshot(text, "fresh", 0, ChatMessageType.Text);
Assert.Equal(expected, WechatSendConfirmation.MatchesMention(message, new HashSet<string>(), member, "marker"));
Assert.False(WechatSendConfirmation.MatchesMention(message, new HashSet<string> { "fresh" }, member, "marker"));
Assert.False(WechatSendConfirmation.MatchesMention(message with { Type = ChatMessageType.Quote }, new HashSet<string>(), member, "marker"));
}
}
@@ -0,0 +1,39 @@
using WxAgent.Core;
using Xunit;
namespace WxAgent.Core.Tests;
public sealed class WechatSendConfirmationTests
{
[Fact]
public void RequiresFreshMatchingTypeAndExactReplyText()
{
var baseline = new HashSet<string> { "old" };
var reply = new ChatMessageSnapshot("reply", "new", 0, ChatMessageType.Quote);
Assert.True(WechatSendConfirmation.Matches(reply, baseline, ChatMessageType.Quote, "reply"));
Assert.False(WechatSendConfirmation.Matches(reply with { Fingerprint = "old" }, baseline, ChatMessageType.Quote, "reply"));
Assert.False(WechatSendConfirmation.Matches(reply with { Type = ChatMessageType.Text }, baseline, ChatMessageType.Quote, "reply"));
Assert.False(WechatSendConfirmation.Matches(reply with { Text = "another reply" }, baseline, ChatMessageType.Quote, "reply"));
}
[Theory]
[InlineData("report.txt", true)]
[InlineData("文件\nreport.txt\n1 KB", true)]
[InlineData("文件\r\nreport.txt\r\n1 KB", true)]
[InlineData("other-report.txt", false)]
[InlineData("report.txt.backup", false)]
public void FileConfirmationRequiresExactFilenameLine(string text, bool expected)
{
var message = new ChatMessageSnapshot(text, "new", 0, ChatMessageType.File);
Assert.Equal(expected, WechatSendConfirmation.Matches(message, new HashSet<string>(), ChatMessageType.File, "report.txt"));
}
[Fact]
public void ImageConfirmationRejectsOtherTypesAndOldRows()
{
var message = new ChatMessageSnapshot("[图片]", "new", 0, ChatMessageType.Image);
Assert.True(WechatSendConfirmation.Matches(message, new HashSet<string>(), ChatMessageType.Image, null));
Assert.False(WechatSendConfirmation.Matches(message, new HashSet<string> { "new" }, ChatMessageType.Image, null));
Assert.False(WechatSendConfirmation.Matches(message with { Type = ChatMessageType.Text }, new HashSet<string>(), ChatMessageType.Image, null));
}
}
@@ -0,0 +1,34 @@
using WxAgent.Core;
using Xunit;
namespace WxAgent.Core.Tests;
public sealed class WechatTextInputTests
{
[Fact]
public void PreservesMultilineWhitespaceWithCanonicalLineEndings()
{
Assert.Equal("first\n\n second\t\nthird", WechatTextInput.Prepare("first\r\n\r\n second\t\rthird"));
Assert.Equal(new string('x', 4000), WechatTextInput.Prepare(new string('x', 4000)));
Assert.Throws<WxAgentException>(() => WechatTextInput.Prepare(new string('x', 4001)));
}
[Theory]
[InlineData("")]
[InlineData(" \r\n\t")]
[InlineData("before\0after")]
[InlineData("before\u001bafter")]
public void RejectsEmptyOrUnsafeInput(string input) =>
Assert.Throws<WxAgentException>(() => WechatTextInput.Prepare(input));
[Fact]
public void QuoteParserPreservesBlankLinesAndIndentation()
{
const string reply = "first\n\n second ";
var parsed = Assert.Single(VisibleMessageParser.Parse([reply + "\n引用 Alice 的消息 : original"]));
Assert.Equal(reply, parsed.Text);
Assert.Equal(ChatMessageType.Quote, parsed.Type);
Assert.True(WechatSendConfirmation.Matches(parsed, new HashSet<string>(), ChatMessageType.Quote, reply));
Assert.Equal("original", parsed.Quote!.Text);
}
}