@@ -41,11 +41,30 @@ func TestAsyncMiddlewareRecoversPanic(t *testing.T) {
|
||||
if event.Err == nil {
|
||||
t.Fatal("panic error was not reported")
|
||||
}
|
||||
if !errors.Is(event.Err, ErrHandlerPanic) {
|
||||
t.Fatalf("expected ErrHandlerPanic, got %v", event.Err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for async middleware error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncMiddlewareRecoversPanic(t *testing.T) {
|
||||
observer := &middlewareErrorObserver{errors: make(chan ErrorEvent, 1)}
|
||||
ctx := &MessageContext{observer: observer}
|
||||
middleware := NewMiddleware[NoData]("panic", func(ctx *MessageContext, db NoData) bool {
|
||||
panic("boom")
|
||||
})
|
||||
|
||||
if middleware.Execute(ctx, NoData{}) {
|
||||
t.Fatal("panicking synchronous middleware continued execution")
|
||||
}
|
||||
event := <-observer.errors
|
||||
if !errors.Is(event.Err, ErrHandlerPanic) {
|
||||
t.Fatalf("expected ErrHandlerPanic, got %v", event.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMiddlewareRejectsNilExecutor(t *testing.T) {
|
||||
observer := &middlewareErrorObserver{errors: make(chan ErrorEvent, 1)}
|
||||
ctx := &MessageContext{observer: observer}
|
||||
@@ -171,3 +190,14 @@ func TestPluginCommandGroupRegistersBuiltCommands(t *testing.T) {
|
||||
plugin.CommandGroup("ignored", nil)
|
||||
plugin.AddCommandGroup(nil)
|
||||
}
|
||||
|
||||
func TestPluginSkipsNilHandlers(t *testing.T) {
|
||||
plugin := NewPlugin[NoData]("nil")
|
||||
plugin.AddCommand(NewCommand[NoData]("command", nil))
|
||||
plugin.AddPayload(NewCommand[NoData]("payload", nil))
|
||||
plugin.AddUpdateHandler(tgapi.UpdateTypeEditedMessage, nil)
|
||||
|
||||
if len(plugin.commands) != 0 || len(plugin.payloads) != 0 || len(plugin.handlers) != 0 {
|
||||
t.Fatalf("nil handlers were registered: commands=%d payloads=%d updates=%d", len(plugin.commands), len(plugin.payloads), len(plugin.handlers))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user