161 lines
5.0 KiB
Go
161 lines
5.0 KiB
Go
package websocket
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/kandev/kandev/internal/events"
|
|
"github.com/kandev/kandev/internal/events/bus"
|
|
)
|
|
|
|
// TestOfficeEventBroadcaster_SubscriptionCount verifies that
|
|
// RegisterOfficeNotifications creates exactly one subscription per event type.
|
|
func TestOfficeEventBroadcaster_SubscriptionCount(t *testing.T) {
|
|
log := testLogger()
|
|
eventBus := bus.NewMemoryEventBus(log)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
hub := NewHub(nil, log)
|
|
go hub.Run(ctx)
|
|
|
|
b := RegisterOfficeNotifications(ctx, eventBus, hub, log)
|
|
|
|
// One subscription per event type subscribed in RegisterOfficeNotifications.
|
|
// Update this count when adding/removing event subscriptions.
|
|
const wantSubscriptions = 20
|
|
if got := len(b.subscriptions); got != wantSubscriptions {
|
|
t.Errorf("RegisterOfficeNotifications created %d subscriptions, want %d",
|
|
got, wantSubscriptions)
|
|
}
|
|
}
|
|
|
|
// TestOfficeEventBroadcaster_BroadcastsEvent verifies that publishing an
|
|
// office event on the bus results in a hub.Broadcast call.
|
|
func TestOfficeEventBroadcaster_BroadcastsEvent(t *testing.T) {
|
|
log := testLogger()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
testCases := []struct {
|
|
subject string
|
|
}{
|
|
{events.OfficeCommentCreated},
|
|
{events.OfficeApprovalCreated},
|
|
{events.OfficeTaskStatusChanged},
|
|
{events.AgentCompleted},
|
|
{events.AgentFailed},
|
|
{events.TaskMoved},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.subject, func(t *testing.T) {
|
|
eb := bus.NewMemoryEventBus(log)
|
|
hub := NewHub(nil, log)
|
|
go hub.Run(ctx)
|
|
|
|
_ = RegisterOfficeNotifications(ctx, eb, hub, log)
|
|
|
|
// Track whether the office broadcaster's handler ran by counting
|
|
// all subscribers on this subject (broadcaster + our counter).
|
|
var handlerCalled int
|
|
_, _ = eb.Subscribe(tc.subject, func(_ context.Context, _ *bus.Event) error {
|
|
handlerCalled++
|
|
return nil
|
|
})
|
|
|
|
data := map[string]interface{}{
|
|
"workspace_id": "ws-123",
|
|
"task_id": "t-456",
|
|
}
|
|
evt := bus.NewEvent(tc.subject, "test", data)
|
|
if err := eb.Publish(context.Background(), tc.subject, evt); err != nil {
|
|
t.Fatalf("Publish failed: %v", err)
|
|
}
|
|
|
|
// Our counter should have been called exactly once.
|
|
if handlerCalled != 1 {
|
|
t.Errorf("handler called %d times, want 1", handlerCalled)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestStripPayloadKeys verifies the office re-broadcast payload transform:
|
|
// office.task.moved is workspace-scoped, so the session-scoped session_id it
|
|
// inherits from the source task.moved event must be dropped (otherwise the FE
|
|
// WS-account stamps the envelope session-routed and the bridge audit then
|
|
// expects a per-session cache mutation that no office handler makes on a
|
|
// non-office page). workspace_id and the rest must survive, and the source
|
|
// payload (shared with other subscribers) must not be mutated.
|
|
func TestStripPayloadKeys(t *testing.T) {
|
|
source := map[string]interface{}{
|
|
"workspace_id": "ws-1",
|
|
"task_id": "t-1",
|
|
"session_id": "sess-1",
|
|
"to_step_id": "step-2",
|
|
}
|
|
|
|
out := stripPayloadKeys(source, []string{"session_id"})
|
|
m, ok := out.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected map output, got %T", out)
|
|
}
|
|
if _, present := m["session_id"]; present {
|
|
t.Error("session_id must be stripped from the office.task.moved payload")
|
|
}
|
|
if m["workspace_id"] != "ws-1" || m["task_id"] != "t-1" || m["to_step_id"] != "step-2" {
|
|
t.Errorf("non-dropped fields must be preserved, got %#v", m)
|
|
}
|
|
// Source must be untouched — every other subscriber shares this map.
|
|
if source["session_id"] != "sess-1" {
|
|
t.Error("source payload must not be mutated")
|
|
}
|
|
}
|
|
|
|
func TestStripPayloadKeys_NoKeysOrNonMap(t *testing.T) {
|
|
source := map[string]interface{}{"a": 1}
|
|
if got := stripPayloadKeys(source, nil); got == nil {
|
|
t.Fatal("nil dropKeys should return the payload unchanged")
|
|
}
|
|
if got := stripPayloadKeys("not-a-map", []string{"x"}); got != "not-a-map" {
|
|
t.Errorf("non-map payload should pass through, got %v", got)
|
|
}
|
|
}
|
|
|
|
// TestOfficeEventBroadcaster_NilEventBus verifies no panic when event bus is nil.
|
|
func TestOfficeEventBroadcaster_NilEventBus(t *testing.T) {
|
|
log := testLogger()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
hub := NewHub(nil, log)
|
|
go hub.Run(ctx)
|
|
|
|
b := RegisterOfficeNotifications(ctx, nil, hub, log)
|
|
if len(b.subscriptions) != 0 {
|
|
t.Errorf("expected 0 subscriptions with nil event bus, got %d", len(b.subscriptions))
|
|
}
|
|
}
|
|
|
|
// TestOfficeEventBroadcaster_Close verifies that Close unsubscribes all subscriptions.
|
|
func TestOfficeEventBroadcaster_Close(t *testing.T) {
|
|
log := testLogger()
|
|
eb := bus.NewMemoryEventBus(log)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
hub := NewHub(nil, log)
|
|
go hub.Run(ctx)
|
|
|
|
b := RegisterOfficeNotifications(ctx, eb, hub, log)
|
|
if len(b.subscriptions) == 0 {
|
|
t.Fatal("expected subscriptions before close")
|
|
}
|
|
|
|
b.Close()
|
|
if len(b.subscriptions) != 0 {
|
|
t.Errorf("expected 0 subscriptions after Close, got %d", len(b.subscriptions))
|
|
}
|
|
}
|