refactor: split node agent and Go control plane
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using System.Text;
|
||||
using WxAgent.Core;
|
||||
using Xunit;
|
||||
using ZstdSharp;
|
||||
|
||||
namespace WxAgent.Core.Tests;
|
||||
|
||||
public sealed class WechatDbMessageTests
|
||||
{
|
||||
[Fact]
|
||||
public void TableNameForMatchesMd5HexOfChatId()
|
||||
{
|
||||
Assert.Equal("Msg_" + Md5Hex("wxid_test"), WechatDbMessage.TableNameFor("wxid_test"));
|
||||
Assert.Equal("Msg_" + Md5Hex("测试群组"), WechatDbMessage.TableNameFor("测试群组"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecodeContentHandlesPlainHexUtf8()
|
||||
{
|
||||
Assert.Equal("hello", WechatDbMessage.DecodeContent(Convert.ToHexString(Encoding.UTF8.GetBytes("hello")), compressed: false));
|
||||
Assert.Empty(WechatDbMessage.DecodeContent("", compressed: false));
|
||||
Assert.Empty(WechatDbMessage.DecodeContent("not-hex!", compressed: false));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecodeContentDecompressesZstdPayloads()
|
||||
{
|
||||
var payload = Encoding.UTF8.GetBytes("wx-agent-zstd-roundtrip-消息测试");
|
||||
using var compressor = new CompressionStream(new MemoryStream());
|
||||
// ZstdSharp.CompressionStream writes on dispose; wrap in a memory stream instead.
|
||||
var compressed = Compress(payload);
|
||||
var hex = Convert.ToHexString(compressed);
|
||||
var decoded = WechatDbMessage.DecodeContent(hex, compressed: true);
|
||||
Assert.Equal("wx-agent-zstd-roundtrip-消息测试", decoded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecodeContentTreatsCompressedFlagWithoutMagicAsUndecodable()
|
||||
{
|
||||
var hex = Convert.ToHexString(Encoding.UTF8.GetBytes("plain"));
|
||||
Assert.Empty(WechatDbMessage.DecodeContent(hex, compressed: true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsSelfUsesWxIdPrefixOfAccountDirectory()
|
||||
{
|
||||
Assert.True(WechatDbMessage.IsSelf("wxid_abc123", "wxid_abc123_def456"));
|
||||
Assert.False(WechatDbMessage.IsSelf("wxid_other", "wxid_abc123_def456"));
|
||||
Assert.Null(WechatDbMessage.IsSelf(null, "wxid_abc123_def456"));
|
||||
Assert.Null(WechatDbMessage.IsSelf("wxid_abc123", null));
|
||||
}
|
||||
|
||||
private static string Md5Hex(string value) =>
|
||||
Convert.ToHexString(System.Security.Cryptography.MD5.HashData(Encoding.UTF8.GetBytes(value))).ToLowerInvariant();
|
||||
|
||||
private static byte[] Compress(byte[] payload)
|
||||
{
|
||||
using var output = new MemoryStream();
|
||||
using (var compressor = new CompressionStream(output))
|
||||
{
|
||||
compressor.Write(payload);
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user