package steppir

import (
	"testing"
	"time"
)

// The controller says nothing until its own poll comes round, so a move has to
// be reported from the moment it is COMMANDED. Without it the app's "block TX
// while the elements travel" had a hole: the grace covering the command ran out
// before the first poll that would have seen the movement, and the transmitter
// was released in the middle of a move.
func TestAJustCommandedMoveReportsMotion(t *testing.T) {
	c := &Client{}
	c.lastStatus = &Status{Connected: true} // idle, as the controller last said

	if st, _ := c.GetStatus(); st.MotorsMoving != 0 {
		t.Fatal("an idle antenna reports motion")
	}

	c.statusMu.Lock()
	c.moveCmdAt = time.Now()
	c.statusMu.Unlock()

	st, _ := c.GetStatus()
	if st.MotorsMoving == 0 {
		t.Error("a move commanded a moment ago is not reported as motion")
	}
	if c.lastStatus.MotorsMoving != 0 {
		t.Error("the optimistic flag leaked into the cached status the poll owns")
	}

	// Bounded: an antenna that never reports motion must not latch the transmit
	// inhibit on for ever.
	c.statusMu.Lock()
	c.moveCmdAt = time.Now().Add(-moveOptimisticWindow - time.Second)
	c.statusMu.Unlock()
	if st, _ := c.GetStatus(); st.MotorsMoving != 0 {
		t.Error("the optimistic window never expires")
	}
}
