WIP: ACN state handling & tests
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
decentral1se 2023-10-01 12:24:28 +02:00
parent c453bd831c
commit 50397ee806
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
3 changed files with 31 additions and 27 deletions

26
acn.go
View File

@ -35,17 +35,6 @@ func isConnected(m model) (bool, tea.Msg) {
}
func turnAcnOn(m *model) tea.Msg {
if ok, msg := isConnected(*m); !ok {
switch msg.(type) {
case acnConnectingMsg:
return msg
case acnAlreadyOnlineMsg:
return msg
}
}
m.acnState = connecting
mrand.New(mrand.NewSource(int64(time.Now().Nanosecond())))
port := mrand.Intn(1000) + 9600
controlPort := port + 1
@ -113,22 +102,9 @@ func turnAcnOn(m *model) tea.Msg {
}
func turnAcnOff(m *model, quitProgram bool) tea.Msg {
switch m.acnState {
case offline:
if !quitProgram {
return acnOfflineMsg{}
}
case connecting:
return acnConnectingMsg{}
}
if m.app != nil {
if m.acnState != offline {
m.app.Shutdown()
}
if m.acn != nil {
m.acn.Close()
}
return acnOffMsg{quitProgram: quitProgram}
}

View File

@ -263,6 +263,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, m.sendStatusCmd(msg.output...))
case turnAcnOffMsg:
switch m.acnState {
case offline:
if !msg.quitProgram {
cmds = append(cmds, func() tea.Msg { return acnOfflineMsg{} })
return m, tea.Batch(cmds...)
}
case connecting:
cmds = append(cmds, func() tea.Msg { return acnConnectingMsg{} })
return m, tea.Batch(cmds...)
}
m.input.Reset()
cmds = append(cmds, func() tea.Msg {
return turnAcnOff(&m, msg.quitProgram)
@ -276,9 +287,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.acnState = offline
case turnAcnOnMsg:
// TODO: stop if we're already connecting!
// state management is getting a little tricky :/
switch m.acnState {
case connecting:
cmds = append(cmds, func() tea.Msg { return acnConnectingMsg{} })
return m, tea.Batch(cmds...)
case connected:
cmds = append(cmds, func() tea.Msg { return acnAlreadyOnlineMsg{} })
return m, tea.Batch(cmds...)
}
m.acnState = connecting
m.input.Reset()
cmds = append(cmds, func() tea.Msg {
return turnAcnOn(&m)
})

View File

@ -25,3 +25,10 @@ func TestCairdeOnOffWorks(t *testing.T) {
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
// test acn on and then off, clean
// test acn on and then quit, clean
// test acn on and then on, asks to hold
// test acn on, then off, asks to hold, wait, then off
// test acn on and then quit, asks to hold
// test acn off, tells it is off