feat: filter receive messages by since

This commit is contained in:
zhaoyilun
2026-07-10 01:27:27 +08:00
parent e7324d8aa6
commit aa636f5a33
9 changed files with 164 additions and 14 deletions

View File

@@ -94,6 +94,47 @@ func TestEncryptedPacketLogSourceFiltersByConversationAndQuery(t *testing.T) {
}
}
func TestEncryptedPacketLogSourceFiltersBySince(t *testing.T) {
first := `--------------------------------------------------------------------------------------------------------------------------------------------
2026/7/7 15:30:07
<message id="msg-before" from="sender@imopenfire1-lanzhou/imp_pc_4.1.2.6842" to="receiver@imopenfire1-lanzhou" type="chat">
<body>before threshold</body>
<received xmlns="urn:xmpp:receipts" id="receipt-before" type="1" stamp="" />
<subject>TASK_UPDATE</subject>
<isphere xmlns="isphere.im" type="1001" sendtime="1783423807000" version="1" />
<readed />
</message>`
second := `--------------------------------------------------------------------------------------------------------------------------------------------
2026/7/7 15:31:07
<message id="msg-after" from="sender@imopenfire1-lanzhou/imp_pc_4.1.2.6842" to="receiver@imopenfire1-lanzhou" type="chat">
<body>after threshold</body>
<received xmlns="urn:xmpp:receipts" id="receipt-after" type="1" stamp="" />
<subject>TASK_UPDATE</subject>
<isphere xmlns="isphere.im" type="1001" sendtime="1783423867000" version="1" />
<readed />
</message>`
source := EncryptedPacketLogSource{Lines: []string{
encryptPacketLineForTest(t, first),
encryptPacketLineForTest(t, second),
}}
got, err := source.ReceiveMessages(context.Background(), ReceiveMessagesQuery{
Since: "2026-07-07T11:31:00Z",
Limit: 10,
})
if err != nil {
t.Fatalf("ReceiveMessages returned error: %v", err)
}
if len(got.Messages) != 1 || got.Messages[0].ID != "msg-after" {
t.Fatalf("messages = %+v, want only msg-after", got.Messages)
}
_, err = source.ReceiveMessages(context.Background(), ReceiveMessagesQuery{Since: "not-a-time"})
if err == nil {
t.Fatalf("ReceiveMessages accepted invalid since")
}
}
func encryptPacketLineForTest(t *testing.T, plaintext string) string {
t.Helper()
block, err := des.NewCipher([]byte("hyhccdtm"))