|
6 | 6 | "time" |
7 | 7 |
|
8 | 8 | "github.com/keybase/client/go/chat/storage" |
| 9 | + "github.com/keybase/client/go/gregor" |
9 | 10 | "github.com/keybase/client/go/kbtest" |
10 | 11 | "github.com/keybase/client/go/protocol/chat1" |
11 | 12 | "github.com/keybase/client/go/protocol/gregor1" |
@@ -172,3 +173,100 @@ func TestPushAppState(t *testing.T) { |
172 | 173 | require.Fail(t, "no message received") |
173 | 174 | } |
174 | 175 | } |
| 176 | + |
| 177 | +func makeTypingNotification(t *testing.T, uid gregor1.UID, convID chat1.ConversationID, typing bool) gregor.OutOfBandMessage { |
| 178 | + |
| 179 | + nm := chat1.RemoteUserTypingUpdate{ |
| 180 | + Uid: uid, |
| 181 | + ConvID: convID, |
| 182 | + Typing: typing, |
| 183 | + } |
| 184 | + var data []byte |
| 185 | + enc := codec.NewEncoderBytes(&data, &codec.MsgpackHandle{WriteExt: true}) |
| 186 | + require.NoError(t, enc.Encode(nm)) |
| 187 | + m := gregor1.OutOfBandMessage{ |
| 188 | + Uid_: uid, |
| 189 | + System_: "chat.typing", |
| 190 | + Body_: data, |
| 191 | + } |
| 192 | + return m |
| 193 | +} |
| 194 | + |
| 195 | +func TestPushTyping(t *testing.T) { |
| 196 | + world, ri2, _, sender, list, tlf := setupTest(t, 1) |
| 197 | + defer world.Cleanup() |
| 198 | + |
| 199 | + ri := ri2.(*kbtest.ChatRemoteMock) |
| 200 | + u := world.GetUsers()[0] |
| 201 | + uid := u.User.GetUID().ToBytes() |
| 202 | + tc := world.Tcs[u.Username] |
| 203 | + handler := NewPushHandler(tc.Context()) |
| 204 | + handler.SetClock(world.Fc) |
| 205 | + handler.typingMonitor.SetClock(world.Fc) |
| 206 | + handler.typingMonitor.SetTimeout(time.Minute) |
| 207 | + |
| 208 | + conv := newBlankConv(t, uid, ri, sender, tlf, u.Username) |
| 209 | + |
| 210 | + confirmTyping := func(list *chatListener) { |
| 211 | + select { |
| 212 | + case updates := <-list.typingUpdate: |
| 213 | + require.Equal(t, 1, len(updates)) |
| 214 | + require.Equal(t, conv.GetConvID(), updates[0].ConvID) |
| 215 | + require.Equal(t, 1, len(updates[0].Typers)) |
| 216 | + require.Equal(t, uid, updates[0].Typers[0].Uid.ToBytes()) |
| 217 | + case <-time.After(20 * time.Second): |
| 218 | + require.Fail(t, "no typing notification") |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + confirmNotTyping := func(list *chatListener) { |
| 223 | + select { |
| 224 | + case updates := <-list.typingUpdate: |
| 225 | + require.Equal(t, 1, len(updates)) |
| 226 | + require.Equal(t, conv.GetConvID(), updates[0].ConvID) |
| 227 | + require.Zero(t, len(updates[0].Typers)) |
| 228 | + case <-time.After(2 * time.Second): |
| 229 | + require.Fail(t, "no typing notification") |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + t.Logf("test basic") |
| 234 | + handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true)) |
| 235 | + confirmTyping(list) |
| 236 | + handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), false)) |
| 237 | + confirmNotTyping(list) |
| 238 | + |
| 239 | + t.Logf("test expiration") |
| 240 | + handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true)) |
| 241 | + confirmTyping(list) |
| 242 | + world.Fc.Advance(time.Hour) |
| 243 | + confirmNotTyping(list) |
| 244 | + |
| 245 | + t.Logf("test extend") |
| 246 | + extendCh := make(chan struct{}) |
| 247 | + handler.typingMonitor.extendCh = &extendCh |
| 248 | + handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true)) |
| 249 | + confirmTyping(list) |
| 250 | + world.Fc.Advance(30 * time.Second) |
| 251 | + handler.Typing(context.TODO(), makeTypingNotification(t, uid, conv.GetConvID(), true)) |
| 252 | + select { |
| 253 | + case <-list.typingUpdate: |
| 254 | + require.Fail(t, "should have extended") |
| 255 | + default: |
| 256 | + } |
| 257 | + select { |
| 258 | + case <-extendCh: |
| 259 | + case <-time.After(20 * time.Second): |
| 260 | + require.Fail(t, "no extend callback") |
| 261 | + } |
| 262 | + world.Fc.Advance(40 * time.Second) |
| 263 | + select { |
| 264 | + case <-list.typingUpdate: |
| 265 | + require.Fail(t, "not far enough") |
| 266 | + default: |
| 267 | + } |
| 268 | + world.Fc.Advance(40 * time.Second) |
| 269 | + confirmNotTyping(list) |
| 270 | + |
| 271 | + require.Zero(t, len(handler.typingMonitor.typers)) |
| 272 | +} |
0 commit comments