Harden nil event handling
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

This commit is contained in:
Snider 2026-04-17 21:01:05 +01:00
parent 4329b18b36
commit 99cfa72fa1
2 changed files with 25 additions and 0 deletions

View file

@ -49,6 +49,9 @@ func (s *Service) OnStartup(_ context.Context) core.Result {
return core.Result{Value: err, OK: false}
}
cancel := s.platform.On(t.Name, func(event *CustomEvent) {
if event == nil {
return
}
_ = s.Core().ACTION(ActionEventFired{Event: *event})
})
s.mu.Lock()

View file

@ -184,6 +184,28 @@ func TestTaskOn_Good(t *testing.T) {
assert.Equal(t, "dark", received[0].Event.Data)
}
func TestTaskOn_NilEvent_Ignores(t *testing.T) {
_, c, mock := newTestService(t)
var received []ActionEventFired
c.RegisterAction(func(_ *core.Core, msg core.Message) core.Result {
if fired, ok := msg.(ActionEventFired); ok {
received = append(received, fired)
}
return core.Result{OK: true}
})
r := taskRun(c, "events.on", TaskOn{Name: "theme:changed"})
require.True(t, r.OK)
require.NotEmpty(t, mock.listeners["theme:changed"])
require.NotPanics(t, func() {
mock.listeners["theme:changed"][0].callback(nil)
})
assert.Empty(t, received)
}
func TestTaskOff_Good(t *testing.T) {
_, c, mock := newTestService(t)