github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/pinmessage_test.go (about) 1 package chat 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/keybase/client/go/chat/types" 8 "github.com/keybase/client/go/chat/utils" 9 "github.com/keybase/client/go/protocol/chat1" 10 "github.com/keybase/client/go/protocol/gregor1" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestPinMessages(t *testing.T) { 15 useRemoteMock = false 16 defer func() { useRemoteMock = true }() 17 ctc := makeChatTestContext(t, "TestPinMessages", 2) 18 defer ctc.cleanup() 19 20 timeout := 20 * time.Second 21 users := ctc.users() 22 ctx := ctc.as(t, users[0]).startCtx 23 ctx1 := ctc.as(t, users[1]).startCtx 24 tc := ctc.world.Tcs[users[0].Username] 25 tc1 := ctc.world.Tcs[users[1].Username] 26 uid := gregor1.UID(users[0].GetUID().ToBytes()) 27 uid1 := gregor1.UID(users[1].GetUID().ToBytes()) 28 listener0 := newServerChatListener() 29 ctc.as(t, users[0]).h.G().NotifyRouter.AddListener(listener0) 30 listener1 := newServerChatListener() 31 ctc.as(t, users[1]).h.G().NotifyRouter.AddListener(listener1) 32 33 t.Logf("test impteam") 34 impConv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT, 35 chat1.ConversationMembersType_IMPTEAMNATIVE, users[1]) 36 msgID := mustPostLocalForTest(t, ctc, users[0], impConv, chat1.NewMessageBodyWithText(chat1.MessageText{ 37 Body: "PIN THIS NOW", 38 })) 39 consumeNewMsgRemote(t, listener0, chat1.MessageType_TEXT) 40 consumeNewMsgRemote(t, listener1, chat1.MessageType_TEXT) 41 _, err := ctc.as(t, users[0]).chatLocalHandler().PinMessage(ctx, chat1.PinMessageArg{ 42 ConvID: impConv.Id, 43 MsgID: msgID, 44 }) 45 require.NoError(t, err) 46 consumeNewMsgRemote(t, listener0, chat1.MessageType_PIN) 47 consumeNewMsgRemote(t, listener1, chat1.MessageType_PIN) 48 conv, err := utils.GetVerifiedConv(ctx, tc.Context(), uid, impConv.Id, types.InboxSourceDataSourceAll) 49 require.NoError(t, err) 50 require.NotNil(t, conv.Info.PinnedMsg) 51 require.Equal(t, msgID, conv.Info.PinnedMsg.Message.GetMessageID()) 52 conv, err = utils.GetVerifiedConv(ctx1, tc1.Context(), uid1, impConv.Id, types.InboxSourceDataSourceAll) 53 require.NoError(t, err) 54 require.NotNil(t, conv.Info.PinnedMsg) 55 require.Equal(t, msgID, conv.Info.PinnedMsg.Message.GetMessageID()) 56 57 _, err = ctc.as(t, users[1]).chatLocalHandler().UnpinMessage(ctx1, impConv.Id) 58 require.Error(t, err) 59 require.NoError(t, ctc.as(t, users[1]).chatLocalHandler().IgnorePinnedMessage(ctx1, impConv.Id)) 60 select { 61 case <-listener1.convUpdate: 62 case <-time.After(timeout): 63 require.Fail(t, "no stale") 64 } 65 conv, err = utils.GetVerifiedConv(ctx1, tc1.Context(), uid1, impConv.Id, types.InboxSourceDataSourceAll) 66 require.NoError(t, err) 67 require.Nil(t, conv.Info.PinnedMsg) 68 _, err = ctc.as(t, users[0]).chatLocalHandler().UnpinMessage(ctx, impConv.Id) 69 require.NoError(t, err) 70 consumeNewMsgRemote(t, listener0, chat1.MessageType_DELETE) 71 consumeNewMsgRemote(t, listener1, chat1.MessageType_DELETE) 72 conv, err = utils.GetVerifiedConv(ctx, tc.Context(), uid, impConv.Id, types.InboxSourceDataSourceAll) 73 require.NoError(t, err) 74 require.Nil(t, conv.Info.PinnedMsg) 75 76 t.Logf("test team") 77 teamConv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT, 78 chat1.ConversationMembersType_TEAM, users[1]) 79 msgID = mustPostLocalForTest(t, ctc, users[0], teamConv, chat1.NewMessageBodyWithText(chat1.MessageText{ 80 Body: "PIN THIS NOW", 81 })) 82 consumeNewMsgRemote(t, listener0, chat1.MessageType_TEXT) 83 consumeNewMsgRemote(t, listener1, chat1.MessageType_TEXT) 84 _, err = ctc.as(t, users[1]).chatLocalHandler().PinMessage(ctx1, chat1.PinMessageArg{ 85 ConvID: teamConv.Id, 86 MsgID: msgID, 87 }) 88 require.NoError(t, err) 89 consumeNewMsgRemote(t, listener0, chat1.MessageType_PIN) 90 consumeNewMsgRemote(t, listener1, chat1.MessageType_PIN) 91 _, err = ctc.as(t, users[0]).chatLocalHandler().UnpinMessage(ctx, teamConv.Id) 92 require.NoError(t, err) 93 consumeNewMsgRemote(t, listener0, chat1.MessageType_DELETE) 94 consumeNewMsgRemote(t, listener1, chat1.MessageType_DELETE) 95 conv, err = utils.GetVerifiedConv(ctx1, tc1.Context(), uid1, teamConv.Id, types.InboxSourceDataSourceAll) 96 require.NoError(t, err) 97 require.Nil(t, conv.Info.PinnedMsg) 98 }