package main import ( "context" "errors" "testing" "time" "tera-clone/services/tera-api/internal/serial" ) type stubProbeClient struct { result serial.ProbeResult err error calls int } func (s *stubProbeClient) Probe(context.Context, serial.ChannelConfig) (serial.ProbeResult, error) { s.calls++ return s.result, s.err } func TestInspectPassesAppliedReadbackAndRestore(t *testing.T) { requested := testConfig() client := &stubProbeClient{result: serial.ProbeResult{ Channel: representativeChannel, Device: representativeDevice, Previous: serial.LineSettings{BaudRate: 9600, DataBits: 8, Parity: "none", StopBits: 1, FlowControl: "none"}, Applied: serial.LineSettings{BaudRate: 115200, DataBits: 8, Parity: "none", StopBits: 1, FlowControl: "none"}, Restored: true, }} report := inspect(context.Background(), client, requested, time.Time{}) if report.Status != "pass" || report.DataSent || client.calls != 1 { t.Fatalf("report = %#v, calls = %d", report, client.calls) } } func TestInspectFailsWhenRestoreIsNotConfirmed(t *testing.T) { client := &stubProbeClient{result: serial.ProbeResult{ Channel: representativeChannel, Device: representativeDevice, Applied: serial.LineSettings{BaudRate: 115200, DataBits: 8, Parity: "none", StopBits: 1, FlowControl: "none"}, }} report := inspect(context.Background(), client, testConfig(), time.Time{}) if report.Status != "fail" || len(report.Findings) == 0 { t.Fatalf("report = %#v", report) } } func TestInspectFailsClosedOnProbeError(t *testing.T) { client := &stubProbeClient{err: errors.New("injected probe failure")} report := inspect(context.Background(), client, testConfig(), time.Time{}) if report.Status != "fail" || len(report.Findings) != 1 || report.Probe != nil { t.Fatalf("report = %#v", report) } } func testConfig() serial.ChannelConfig { return serial.ChannelConfig{ Channel: representativeChannel, BaudRate: 115200, DataBits: 8, Parity: "none", StopBits: 1, FlowControl: "none", } }