package fed import ( "context" "encoding/json" "testing" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) type fake struct { name string online bool } func (f fake) Name() string { return f.name } func (f fake) Export(rg store.Range) store.PeerExport { return store.PeerExport{V: store.ExportVersion, Summary: store.Summary{Host: f.name, Hosts: []store.HostInfo{{Name: f.name, Self: true}}}, Traffic: map[string]store.TrafficData{}} } func (f fake) Study(q store.PeriodQuery) store.PeerStudy { return store.PeerStudy{} } func (f fake) Egress(a, b int64) json.RawMessage { return json.RawMessage(`{"findings":[]}`) } func (f fake) Subscribe() (<-chan store.LiveEvent, func()) { return make(chan store.LiveEvent), func() {} } func (f fake) SubscribeVisits() (<-chan store.VisitMsg, func()) { return make(chan store.VisitMsg), func() {} } func (f fake) Online() bool { return f.online } func (f fake) Location() *store.Location { return nil } func TestHubServesBackendsEvenOffline(t *testing.T) { h := NewHub(func() []Backend { return []Backend{fake{"b", false}, fake{"a", true}} }) if got := h.PeerNames(); len(got) != 2 || got[0] != "a" || !h.Has("b") || h.Has("c") { t.Fatalf("%v", got) } rg := store.ParseRange("24h") if s := h.SummariesFor(context.Background(), rg, ""); len(s) != 2 || s[0].Host != "a" || s[0].Hosts[0].Self { t.Fatalf("%+v", s) } if s := h.SummariesFor(context.Background(), rg, "b"); len(s) != 1 { t.Fatalf("only: %+v", s) } hosts := h.Hosts() if len(hosts) != 2 || hosts[0].Online != true || hosts[1].Online != false { t.Fatalf("%+v", hosts) } if m := h.EgressFor(context.Background(), 0, 1, ""); len(m) != 2 { t.Fatalf("%v", m) } }