package gateway import ( "context" "testing" "time" "ai-operator/internal/asterisk/ari" "ai-operator/internal/call" "ai-operator/internal/config" ) type fakeARI struct{ bridgeCreated, added, removed, bridgeDeleted, mediaCreated, hungup int } func (f *fakeARI) AnswerChannel(ctx context.Context, channelID string) error { return nil } func (f *fakeARI) HangupChannel(ctx context.Context, channelID string) error { f.hungup++; return nil } func (f *fakeARI) GetChannel(ctx context.Context, channelID string) (*ari.ARIChannel, error) { return &ari.ARIChannel{ID: channelID}, nil } func (f *fakeARI) CreateBridge(ctx context.Context, bridgeID string, bridgeType string) error { f.bridgeCreated++ return nil } func (f *fakeARI) AddChannelToBridge(ctx context.Context, bridgeID string, channelID string) error { f.added++ return nil } func (f *fakeARI) RemoveChannelFromBridge(ctx context.Context, bridgeID string, channelID string) error { f.removed++ return nil } func (f *fakeARI) DeleteBridge(ctx context.Context, bridgeID string) error { f.bridgeDeleted++ return nil } func (f *fakeARI) CreateExternalMediaChannel(ctx context.Context, req ari.ExternalMediaRequest) (*ari.ARIChannel, error) { f.mediaCreated++ return &ari.ARIChannel{ID: req.ChannelID}, nil } func (f *fakeARI) GetChannelVariable(ctx context.Context, channelID string, variable string) (string, error) { return "conn-1", nil } func TestGatewayStartCallMediaAttemptsSetup(t *testing.T) { // Uses an unreachable media URL, so setup should fail after bridge/media ARI setup without panic. fake := &fakeARI{} gw := New(config.Config{Asterisk: config.AsteriskConfig{ARIApp: "ai-operator", MediaWSBaseURL: "ws://127.0.0.1:1/media"}}, fake, nil) ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() s := &call.CallSession{CallID: "call/1", AsteriskChannelID: "caller-1"} _, _ = gw.StartCallMedia(ctx, s) if fake.bridgeCreated != 1 || fake.added != 1 || fake.mediaCreated != 1 { t.Fatalf("fake=%+v", fake) } if s.BridgeID == "" || s.MediaChannelID == "" || s.MediaConnectionID != "conn-1" { t.Fatalf("session=%+v", s) } }