MCPcopy Index your code
hub / github.com/tinode/chat / TestReplyDelMsgUpdatesUnreadCounters

Function TestReplyDelMsgUpdatesUnreadCounters

server/topic_test.go:3244–3307  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3242}
3243
3244func TestReplyDelMsgUpdatesUnreadCounters(t *testing.T) {
3245 // This test simulates the scenario from issue #898:
3246 // 1. User1 sends messages to User2
3247 // 2. User1 deletes some messages (soft delete)
3248 // 3. Verify that the unread calculation logic works correctly
3249
3250 topicName := "p2pTest"
3251 helper := TopicTestHelper{}
3252 helper.setUp(t, 2, types.TopicCatP2P, topicName, true)
3253 defer helper.tearDown()
3254
3255 user1 := helper.uids[0] // Sender/deleter
3256 user2 := helper.uids[1] // Recipient
3257
3258 // Set up initial state: user2 has read up to message 5, topic has messages up to 10
3259 // So user2 has 5 unread messages (6, 7, 8, 9, 10)
3260 helper.topic.lastID = 10
3261
3262 pud1 := helper.topic.perUser[user1]
3263 pud1.readID = 10 // user1 has read all
3264 helper.topic.perUser[user1] = pud1
3265
3266 pud2 := helper.topic.perUser[user2]
3267 pud2.readID = 5 // user2 has 5 unread messages
3268 helper.topic.perUser[user2] = pud2
3269
3270 // Simulate user1 deleting messages 7 and 8 (2 of user2's unread messages)
3271 msg := &ClientComMessage{
3272 Del: &MsgClientDel{
3273 Id: "del123",
3274 What: "msg",
3275 DelSeq: []MsgRange{
3276 {LowId: 7, HiId: 9}, // Deletes messages 7 and 8 [7, 9)
3277 },
3278 Hard: false, // Soft delete
3279 },
3280 AsUser: user1.UserId(),
3281 sess: helper.sessions[0],
3282 init: true,
3283 }
3284
3285 // Mock the message deletion
3286 helper.mm.EXPECT().DeleteList(topicName, 1, user1, time.Duration(0), []types.Range{{Low: 7, Hi: 9}}).Return(nil)
3287
3288 // Call the function under test
3289 err := helper.topic.replyDelMsg(helper.sessions[0], user1, false, msg)
3290
3291 // Verify
3292 if err != nil {
3293 t.Fatalf("replyDelMsg failed: %v", err)
3294 }
3295
3296 // Verify session got success response
3297 helper.finish()
3298 registerSessionVerifyOutputs(t, helper.results[0], []int{http.StatusOK})
3299
3300 // The key verification is that calculateUnreadInRanges should have been called
3301 // with the correct parameters. We can test this indirectly by testing the function:

Callers

nothing calls this directly

Calls 9

setUpMethod · 0.95
tearDownMethod · 0.95
finishMethod · 0.95
calculateUnreadInRangesFunction · 0.85
UserIdMethod · 0.80
replyDelMsgMethod · 0.80
DeleteListMethod · 0.65
EXPECTMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…