package services import ( "testing" "git.saqut.com/saqut/mwse/internal/bridge" "git.saqut.com/saqut/mwse/internal/ws" ) // TestBridgeSendQueuesInInbox verifies that a bridge/send message from a client // is queued into the inbox and that a missing pack is rejected (#46). func TestBridgeSendQueuesInInbox(t *testing.T) { hub := ws.NewHub() inbox := bridge.NewInbox(0) Register(hub, WithBridgeInbox(inbox)) a, _ := connect(hub, "alice") // A well-formed bridge/send should be queued. res := asMap(t, hub.Handle(a, msg("bridge/send", "pack", map[string]any{"hello": "world"}))) if res["status"] != "success" { t.Fatalf("bridge/send = %v", res) } if inbox.Len() != 1 { t.Fatalf("inbox len = %d, want 1", inbox.Len()) } msgs := inbox.Drain() if msgs[0].From != "alice" { t.Fatalf("inbox.From = %q, want alice", msgs[0].From) } if pack, ok := msgs[0].Pack.(map[string]any); !ok || pack["hello"] != "world" { t.Fatalf("inbox.Pack = %v, want hello:world", msgs[0].Pack) } } func TestBridgeSendRejectsEmptyPack(t *testing.T) { hub := ws.NewHub() inbox := bridge.NewInbox(0) Register(hub, WithBridgeInbox(inbox)) a, _ := connect(hub, "bob") res := asMap(t, hub.Handle(a, msg("bridge/send"))) if res["message"] != "PACK_REQUIRED" { t.Fatalf("expected PACK_REQUIRED, got %v", res) } if inbox.Len() != 0 { t.Fatal("inbox should be empty after rejected send") } } func TestBridgeSendNotRegisteredWithoutOption(t *testing.T) { // Without WithBridgeInbox, bridge/send should not be registered. hub := ws.NewHub() Register(hub) // no bridge option a, _ := connect(hub, "charlie") res := asMap(t, hub.Handle(a, msg("bridge/send", "pack", "x"))) if res["message"] != "UNKNOWN_TYPE" { t.Fatalf("expected UNKNOWN_TYPE, got %v", res) } }