Test: read the phone's clear and ack frames in either order

This commit is contained in:
2026-11-04 17:47:17 +00:00
parent 3ac726828c
commit c83fb5406c
+28 -2
View File
@@ -77,6 +77,29 @@ func readFrame(t *testing.T, ctx context.Context, conn *websocket.Conn, want str
return msg
}
// readFrames reads exactly one frame of each wanted type, in any order.
func readFrames(t *testing.T, ctx context.Context, conn *websocket.Conn, want ...string) map[string]protocol.Message {
t.Helper()
got := map[string]protocol.Message{}
for len(got) < len(want) {
var msg protocol.Message
if err := wsjson.Read(ctx, conn, &msg); err != nil {
t.Fatal(err)
}
expected := false
for _, kind := range want {
if msg.Type == kind {
expected = true
}
}
if _, dup := got[msg.Type]; !expected || dup {
t.Fatalf("unexpected frame %q, want one each of %v: %+v", msg.Type, want, msg)
}
got[msg.Type] = msg
}
return got
}
func subscribeNotifications(t *testing.T, ctx context.Context, conn *websocket.Conn, after string) protocol.Message {
t.Helper()
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.SubscribeNotifications, ID: "sub", After: after}); err != nil {
@@ -171,12 +194,15 @@ func TestAppNotificationReplayAfterCursorAndCrossDeviceClear(t *testing.T) {
if reply.ID != "clr" || len(reply.IDs) != 1 || reply.IDs[0] != firstID {
t.Fatalf("clear reply = %+v", reply)
}
pushed := readFrame(t, ctx, phone, protocol.NotificationsCleared)
// The clear and the ack change are separate broadcasts with no ordering
// guarantee between them, so the phone's two frames are read in any order.
phoneFrames := readFrames(t, ctx, phone, protocol.NotificationsCleared, protocol.AttentionAcksChanged)
pushed := phoneFrames[protocol.NotificationsCleared]
if pushed.ID != "" || len(pushed.IDs) != 1 || pushed.IDs[0] != firstID {
t.Fatalf("phone cleared push = %+v", pushed)
}
// Same effect as the ack: the phone's Needs-you list hears it too.
changed := readFrame(t, ctx, phone, protocol.AttentionAcksChanged)
changed := phoneFrames[protocol.AttentionAcksChanged]
if changed.AttentionAcks == nil || len(changed.AttentionAcks.Dismissed) != 1 || changed.AttentionAcks.Dismissed[0] != fmt.Sprintf("%d:Failed", first) {
t.Fatalf("ack push = %+v", changed)
}