package steppir

import "testing"

// An operator's log read as "the antenna stops responding": every tune was
// commanded, acknowledged with the requested frequency on the next poll, then
// replaced on the poll after by a different one — 21075 asked, 21075 confirmed,
// 21050 reported — with the operator tuning again each time. Nothing in the log
// named that, so it looked like a dead link.
func TestCheckDriftReportsOnceAndRecovers(t *testing.T) {
	c := &Client{}
	c.lastSetKHz = 21075

	// Where it was asked to go: nothing to say.
	c.checkDrift(&Status{Frequency: 21075})
	if c.lastDriftKHz != 0 {
		t.Errorf("a controller on frequency was reported as drifting (%d)", c.lastDriftKHz)
	}
	// Its own grid is not a fault.
	c.checkDrift(&Status{Frequency: 21070})
	if c.lastDriftKHz != 0 {
		t.Errorf("%d kHz is within tolerance and was reported", 21070)
	}
	// Somewhere else entirely: recorded, so it is said once.
	c.checkDrift(&Status{Frequency: 21050})
	if c.lastDriftKHz != 21050 {
		t.Errorf("lastDriftKHz = %d, want 21050", c.lastDriftKHz)
	}
	// Still there on the next poll: nothing new to say, and the marker stands.
	c.checkDrift(&Status{Frequency: 21050})
	if c.lastDriftKHz != 21050 {
		t.Errorf("lastDriftKHz = %d after a repeat, want it unchanged", c.lastDriftKHz)
	}
	// Back where it belongs: the marker clears, so the next divergence is said.
	c.checkDrift(&Status{Frequency: 21075})
	if c.lastDriftKHz != 0 {
		t.Errorf("lastDriftKHz = %d after recovery, want 0", c.lastDriftKHz)
	}
}

// A frequency read while the elements are travelling is not a disagreement, it
// is an antenna on its way — reporting it would cry wolf on every single tune.
func TestCheckDriftIgnoresMovingMotors(t *testing.T) {
	c := &Client{}
	c.lastSetKHz = 21075
	c.checkDrift(&Status{Frequency: 21000, MotorsMoving: 1})
	if c.lastDriftKHz != 0 {
		t.Errorf("a moving antenna was reported as drifting (%d)", c.lastDriftKHz)
	}
}
