xonecas.io
60255
Zn

Zoea-Nova / commit ff81459

feat: add state validation helper for message acceptance

xonecas2026-02-07 ff814598e73227679d3179c18552368ea208541c parent 4cdc72c

2 files changed

internal/core/mysis.go+23−0
@@ -146,6 +146,29 @@ func (m *Mysis) State() MysisState {
146146 return a.state
147147 }
148148
149+// validateCanAcceptMessage checks if a mysis in the given state can accept messages.
150+// Returns nil if messages are allowed, error with user-facing message if not.
151+//
152+// Valid states for accepting messages:
153+// - idle: Mysis is not running but can accept messages (will process when started)
154+// - running: Mysis is actively running and processing messages
155+//
156+// Invalid states for accepting messages:
157+// - stopped: User explicitly stopped the mysis, requires relaunch
158+// - errored: Mysis encountered an error, requires relaunch
159+func validateCanAcceptMessage(state MysisState) error {
160+ switch state {
161+ case MysisStateIdle, MysisStateRunning:
162+ return nil
163+ case MysisStateStopped:
164+ return fmt.Errorf("mysis stopped - press 'r' to relaunch")
165+ case MysisStateErrored:
166+ return fmt.Errorf("mysis errored - press 'r' to relaunch")
167+ default:
168+ return fmt.Errorf("unknown mysis state: %s", state)
169+ }
170+}
171+
149172 // ProviderName returns the name of the mysis provider.
150173 func (m *Mysis) ProviderName() string {
151174 a := m
internal/core/mysis_test.go+31−0
@@ -1694,3 +1694,34 @@ func TestNudgeCircuitBreaker(t *testing.T) {
16941694 }
16951695 })
16961696 }
1697+
1698+func TestCanAcceptMessages(t *testing.T) {
1699+ tests := []struct {
1700+ state MysisState
1701+ canAccept bool
1702+ errMsg string
1703+ }{
1704+ {MysisStateIdle, true, ""},
1705+ {MysisStateRunning, true, ""},
1706+ {MysisStateStopped, false, "mysis stopped - press 'r' to relaunch"},
1707+ {MysisStateErrored, false, "mysis errored - press 'r' to relaunch"},
1708+ }
1709+
1710+ for _, tt := range tests {
1711+ t.Run(string(tt.state), func(t *testing.T) {
1712+ err := validateCanAcceptMessage(tt.state)
1713+
1714+ if tt.canAccept {
1715+ if err != nil {
1716+ t.Errorf("state %s should accept messages, got error: %v", tt.state, err)
1717+ }
1718+ } else {
1719+ if err == nil {
1720+ t.Errorf("state %s should reject messages, got nil error", tt.state)
1721+ } else if err.Error() != tt.errMsg {
1722+ t.Errorf("state %s: expected error %q, got %q", tt.state, tt.errMsg, err.Error())
1723+ }
1724+ }
1725+ })
1726+ }
1727+}