github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/gallery_test.go (about) 1 package chat 2 3 import ( 4 "testing" 5 6 "github.com/keybase/client/go/chat/attachments" 7 "github.com/keybase/client/go/protocol/chat1" 8 "github.com/keybase/client/go/protocol/gregor1" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestAttachmentGalleryNextMessage(t *testing.T) { 13 ctc := makeChatTestContext(t, "TestAttachmentGalleryNextMessage", 1) 14 defer ctc.cleanup() 15 users := ctc.users() 16 defer func() { 17 useRemoteMock = true 18 }() 19 useRemoteMock = false 20 21 tc := ctc.world.Tcs[users[0].Username] 22 gallery := attachments.NewGallery(tc.Context()) 23 uid := gregor1.UID(users[0].GetUID().ToBytes()) 24 listener := newServerChatListener() 25 ctc.as(t, users[0]).h.G().NotifyRouter.AddListener(listener) 26 conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT, 27 chat1.ConversationMembersType_IMPTEAMNATIVE) 28 ctx := ctc.as(t, users[0]).startCtx 29 30 m0Res, err := postLocalForTest(t, ctc, users[0], conv, 31 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 32 Object: chat1.Asset{ 33 Path: "m0", 34 Metadata: chat1.NewAssetMetadataWithImage(chat1.AssetMetadataImage{}), 35 }, 36 })) 37 require.NoError(t, err) 38 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 39 m1Res, err := postLocalForTest(t, ctc, users[0], conv, 40 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 41 Object: chat1.Asset{ 42 Path: "m1", 43 }, 44 })) 45 require.NoError(t, err) 46 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 47 _, err = postLocalForTest(t, ctc, users[0], conv, chat1.NewMessageBodyWithText(chat1.MessageText{ 48 Body: "m2", 49 })) 50 require.NoError(t, err) 51 consumeNewMsgRemote(t, listener, chat1.MessageType_TEXT) 52 m3Res, err := postLocalForTest(t, ctc, users[0], conv, 53 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 54 Object: chat1.Asset{ 55 Path: "m3", 56 Metadata: chat1.NewAssetMetadataWithVideo(chat1.AssetMetadataVideo{}), 57 }, 58 })) 59 require.NoError(t, err) 60 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 61 62 t.Logf("case: backintime all attachments") 63 nm, _, err := gallery.NextMessage(ctx, uid, conv.Id, m3Res.MessageID, 64 attachments.NextMessageOptions{ 65 BackInTime: true, 66 }) 67 require.NoError(t, err) 68 require.NotNil(t, nm) 69 require.Equal(t, nm.GetMessageID(), m1Res.MessageID) 70 t.Logf("case: backintime imagesonly") 71 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m3Res.MessageID, 72 attachments.NextMessageOptions{ 73 BackInTime: true, 74 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 75 }) 76 require.NoError(t, err) 77 require.NotNil(t, nm) 78 require.Equal(t, nm.GetMessageID(), m0Res.MessageID) 79 t.Logf("case: forwardintime all attachments") 80 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m0Res.MessageID, 81 attachments.NextMessageOptions{ 82 BackInTime: false, 83 }) 84 require.NoError(t, err) 85 require.NotNil(t, nm) 86 require.Equal(t, nm.GetMessageID(), m1Res.MessageID) 87 t.Logf("case: forwardintime imagesonly") 88 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m0Res.MessageID, 89 attachments.NextMessageOptions{ 90 BackInTime: false, 91 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 92 }) 93 require.NoError(t, err) 94 require.NotNil(t, nm) 95 require.Equal(t, nm.GetMessageID(), m3Res.MessageID) 96 t.Logf("case: backintime off the end") 97 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m0Res.MessageID, 98 attachments.NextMessageOptions{ 99 BackInTime: true, 100 }) 101 require.NoError(t, err) 102 require.Nil(t, nm) 103 t.Logf("case: forwardintime off the end") 104 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m3Res.MessageID, 105 attachments.NextMessageOptions{ 106 BackInTime: false, 107 }) 108 require.NoError(t, err) 109 require.Nil(t, nm) 110 111 gallery.NextStride = 1 112 gallery.PrevStride = 1 113 t.Logf("case: backintime short stride") 114 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m3Res.MessageID, 115 attachments.NextMessageOptions{ 116 BackInTime: true, 117 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 118 }) 119 require.NoError(t, err) 120 require.NotNil(t, nm) 121 require.Equal(t, nm.GetMessageID(), m0Res.MessageID) 122 t.Logf("case: forwardintime short stride") 123 nm, _, err = gallery.NextMessage(ctx, uid, conv.Id, m0Res.MessageID, 124 attachments.NextMessageOptions{ 125 BackInTime: false, 126 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 127 }) 128 require.NoError(t, err) 129 require.NotNil(t, nm) 130 require.Equal(t, nm.GetMessageID(), m3Res.MessageID) 131 } 132 133 func TestAttachmentGalleryLinks(t *testing.T) { 134 ctc := makeChatTestContext(t, "TestAttachmentGalleryLinks", 1) 135 defer ctc.cleanup() 136 users := ctc.users() 137 defer func() { 138 useRemoteMock = true 139 }() 140 useRemoteMock = false 141 142 tc := ctc.world.Tcs[users[0].Username] 143 gallery := attachments.NewGallery(tc.Context()) 144 uid := gregor1.UID(users[0].GetUID().ToBytes()) 145 listener := newServerChatListener() 146 ctc.as(t, users[0]).h.G().NotifyRouter.AddListener(listener) 147 conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT, 148 chat1.ConversationMembersType_IMPTEAMNATIVE) 149 ctx := ctc.as(t, users[0]).startCtx 150 151 _, err := postLocalForTest(t, ctc, users[0], conv, 152 chat1.NewMessageBodyWithText(chat1.MessageText{ 153 Body: "https://www.google.com", 154 })) 155 require.NoError(t, err) 156 consumeNewMsgRemote(t, listener, chat1.MessageType_TEXT) 157 _, err = postLocalForTest(t, ctc, users[0], conv, 158 chat1.NewMessageBodyWithText(chat1.MessageText{ 159 Body: "MIKE", 160 })) 161 require.NoError(t, err) 162 consumeNewMsgRemote(t, listener, chat1.MessageType_TEXT) 163 m1Res, err := postLocalForTest(t, ctc, users[0], conv, 164 chat1.NewMessageBodyWithText(chat1.MessageText{ 165 Body: "https://www.keybase.io", 166 })) 167 require.NoError(t, err) 168 consumeNewMsgRemote(t, listener, chat1.MessageType_TEXT) 169 170 nm, last, err := gallery.NextMessages(ctx, uid, conv.Id, m1Res.MessageID+1, 5, 171 attachments.NextMessageOptions{ 172 BackInTime: true, 173 MessageType: chat1.MessageType_TEXT, 174 FilterLinks: true, 175 }, nil) 176 require.NoError(t, err) 177 require.True(t, last) 178 require.Equal(t, 2, len(nm)) 179 180 nm, last, err = gallery.NextMessages(ctx, uid, conv.Id, m1Res.MessageID+1, 1, 181 attachments.NextMessageOptions{ 182 BackInTime: true, 183 MessageType: chat1.MessageType_TEXT, 184 FilterLinks: true, 185 }, nil) 186 require.NoError(t, err) 187 require.False(t, last) 188 require.Equal(t, 1, len(nm)) 189 190 nm, last, err = gallery.NextMessages(ctx, uid, conv.Id, m1Res.MessageID, 2, 191 attachments.NextMessageOptions{ 192 BackInTime: true, 193 MessageType: chat1.MessageType_TEXT, 194 FilterLinks: true, 195 }, nil) 196 require.NoError(t, err) 197 require.True(t, last) 198 require.Equal(t, 1, len(nm)) 199 200 } 201 202 func TestAttachmentGalleryPagination(t *testing.T) { 203 ctc := makeChatTestContext(t, "TestAttachmentGalleryNextMessage", 1) 204 defer ctc.cleanup() 205 users := ctc.users() 206 defer func() { 207 useRemoteMock = true 208 }() 209 useRemoteMock = false 210 211 tc := ctc.world.Tcs[users[0].Username] 212 gallery := attachments.NewGallery(tc.Context()) 213 uid := gregor1.UID(users[0].GetUID().ToBytes()) 214 listener := newServerChatListener() 215 ctc.as(t, users[0]).h.G().NotifyRouter.AddListener(listener) 216 conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT, 217 chat1.ConversationMembersType_IMPTEAMNATIVE) 218 ctx := ctc.as(t, users[0]).startCtx 219 220 m0Res, err := postLocalForTest(t, ctc, users[0], conv, 221 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 222 Object: chat1.Asset{ 223 Path: "m0", 224 Metadata: chat1.NewAssetMetadataWithImage(chat1.AssetMetadataImage{}), 225 }, 226 })) 227 require.NoError(t, err) 228 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 229 for i := 0; i < 10; i++ { 230 _, err = postLocalForTest(t, ctc, users[0], conv, 231 chat1.NewMessageBodyWithText(chat1.MessageText{ 232 Body: "MIKE", 233 })) 234 require.NoError(t, err) 235 consumeNewMsgRemote(t, listener, chat1.MessageType_TEXT) 236 } 237 m2Res, err := postLocalForTest(t, ctc, users[0], conv, 238 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 239 Object: chat1.Asset{ 240 Path: "m2", 241 Metadata: chat1.NewAssetMetadataWithImage(chat1.AssetMetadataImage{}), 242 }, 243 })) 244 require.NoError(t, err) 245 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 246 m3Res, err := postLocalForTest(t, ctc, users[0], conv, 247 chat1.NewMessageBodyWithAttachment(chat1.MessageAttachment{ 248 Object: chat1.Asset{ 249 Path: "m3", 250 Metadata: chat1.NewAssetMetadataWithImage(chat1.AssetMetadataImage{}), 251 }, 252 })) 253 require.NoError(t, err) 254 consumeNewMsgRemote(t, listener, chat1.MessageType_ATTACHMENT) 255 256 nm, last, err := gallery.NextMessages(ctx, uid, conv.Id, m3Res.MessageID+1, 2, 257 attachments.NextMessageOptions{ 258 BackInTime: true, 259 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 260 }, nil) 261 require.NoError(t, err) 262 require.False(t, last) 263 require.Equal(t, 2, len(nm)) 264 265 nm, last, err = gallery.NextMessages(ctx, uid, conv.Id, m2Res.MessageID+1, 2, 266 attachments.NextMessageOptions{ 267 BackInTime: true, 268 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 269 }, nil) 270 require.NoError(t, err) 271 require.Equal(t, 2, len(nm)) 272 require.False(t, last) 273 274 nm, last, err = gallery.NextMessages(ctx, uid, conv.Id, m0Res.MessageID, 2, 275 attachments.NextMessageOptions{ 276 BackInTime: true, 277 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 278 }, nil) 279 require.NoError(t, err) 280 require.Zero(t, len(nm)) 281 require.True(t, last) 282 283 nm, last, err = gallery.NextMessages(ctx, uid, conv.Id, m3Res.MessageID+1, 5, 284 attachments.NextMessageOptions{ 285 BackInTime: true, 286 AssetTypes: []chat1.AssetMetadataType{chat1.AssetMetadataType_IMAGE, chat1.AssetMetadataType_VIDEO}, 287 }, nil) 288 require.NoError(t, err) 289 require.Equal(t, 3, len(nm)) 290 require.True(t, last) 291 }