package web import ( "path/filepath" "testing" "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" "github.com/yuanyuanxiang/SimpleRemoter/server/go/devicehistory" "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" ) func TestListDevicesWithOffline(t *testing.T) { h := hub.New() hist, err := devicehistory.Open(filepath.Join(t.TempDir(), "devices.json"), 100, nil) if err != nil { t.Fatalf("Open history: %v", err) } t.Cleanup(func() { _ = hist.Close() }) h.Subscribe(hist) h.Register(&hub.Device{ID: "online", Name: "Online"}, &connection.Context{RemoteAddr: "10.0.0.1:1000"}) h.Register(&hub.Device{ID: "gone", Name: "Gone"}, &connection.Context{RemoteAddr: "10.0.0.2:1000"}) h.Unregister("gone") wh := &wsHub{devices: h, history: hist} got := wh.listDevicesWithOffline() if len(got) != 2 { t.Fatalf("devices = %d, want 2 (1 online + 1 offline)", len(got)) } if got[0].ID != "online" || !got[0].Online { t.Fatalf("first = %+v, want online device", got[0]) } if got[1].ID != "gone" || got[1].Online { t.Fatalf("second = %+v, want offline device", got[1]) } if got[1].LastSeen == 0 { t.Fatal("offline device missing last_seen") } } func TestListDevicesWithoutHistoryIsOnlineOnly(t *testing.T) { h := hub.New() h.Register(&hub.Device{ID: "online", Name: "Online"}, &connection.Context{RemoteAddr: "10.0.0.1:1000"}) wh := &wsHub{devices: h} got := wh.listDevicesWithOffline() if len(got) != 1 || got[0].ID != "online" { t.Fatalf("devices = %+v, want just the online device", got) } }