From c83fb5406c1ccab726b7e4e02603f35c7003d32b Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 4 Nov 2026 17:47:17 +0000 Subject: [PATCH] Test: read the phone's clear and ack frames in either order --- internal/server/app_notifications_test.go | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/server/app_notifications_test.go b/internal/server/app_notifications_test.go index cb55ecfa..e4d5d2be 100644 --- a/internal/server/app_notifications_test.go +++ b/internal/server/app_notifications_test.go @@ -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) }