66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package probe
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"yovision/sense/internal/device"
|
|
)
|
|
|
|
type fakeRepository struct {
|
|
devices []device.Device
|
|
states map[string]device.ActualState
|
|
requested map[string]int
|
|
}
|
|
|
|
func (f *fakeRepository) RequestReconcile(_ context.Context, id string, _ time.Time) error {
|
|
if f.requested == nil {
|
|
f.requested = make(map[string]int)
|
|
}
|
|
f.requested[id]++
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeRepository) ListEnabledVideoDevices(context.Context, int) ([]device.Device, error) {
|
|
return f.devices, nil
|
|
}
|
|
|
|
func (f *fakeRepository) UpdateActualState(_ context.Context, id string, state device.ActualState, _ time.Time) error {
|
|
if f.states == nil {
|
|
f.states = make(map[string]device.ActualState)
|
|
}
|
|
f.states[id] = state
|
|
return nil
|
|
}
|
|
|
|
type fakeRuntime struct {
|
|
ready map[string]bool
|
|
errors map[string]error
|
|
}
|
|
|
|
func (f fakeRuntime) PathReady(_ context.Context, name string) (bool, error) {
|
|
return f.ready[name], f.errors[name]
|
|
}
|
|
|
|
func TestCheckerMapsReadyAndUnavailablePaths(t *testing.T) {
|
|
t.Parallel()
|
|
repository := &fakeRepository{devices: []device.Device{
|
|
{ID: "online", PathName: "online"}, {ID: "offline", PathName: "offline"},
|
|
}}
|
|
checker := New(repository, fakeRuntime{
|
|
ready: map[string]bool{"online": true}, errors: map[string]error{"offline": errors.New("unavailable")},
|
|
})
|
|
err := checker.RunOnce(context.Background())
|
|
if err == nil {
|
|
t.Fatal("probe transport error must remain observable")
|
|
}
|
|
if repository.states["online"] != device.ActualOnline || repository.states["offline"] != device.ActualOffline {
|
|
t.Fatalf("unexpected actual states: %+v", repository.states)
|
|
}
|
|
if repository.requested["offline"] != 1 || repository.requested["online"] != 0 {
|
|
t.Fatalf("unexpected reconcile requests: %+v", repository.requested)
|
|
}
|
|
}
|