package bridge import ( "net" "testing" "time" "git.smesh.lol/nostrino/pkg/release" ) // TestHealthDistinguishesIdleFromBroken: a process that is up but has never announced, or whose // node has not answered, is not healthy — that is the state a dead gateway looks like from the // relay's side, and a health report has to say so. func TestHealthDistinguishesIdleFromBroken(t *testing.T) { srv := &Server{ cfg: Config{ Version: "test", Chains: []string{"btc", "blake"}, Nodes: map[string]Node{ "btc": {Addr: "127.0.0.1:1"}, "blake": {Addr: "127.0.0.1:2"}, }, Logf: func(string, ...any) {}, }, conns: make(map[net.Conn]struct{}), done: make(chan struct{}), pid: 42, health: newHealthState(time.Now().Add(-90*time.Second), []string{"btc", "blake"}), } health := srv.Health() if health.Healthy { t.Fatal("a gateway that has never published presence reported healthy") } requireField(t, health, "btc") requireField(t, health, "blake") if health.PID != 42 || health.UptimeSeconds < 80 { t.Fatalf("health %+v", health) } } func requireField(t *testing.T, health Health, chain string) { t.Helper() if _, ok := health.Nodes[chain]; !ok { t.Fatalf("health has no node report for %s", chain) } } // TestHealthReportsWhetherItCanUpdateItself: the first question about a gateway that will not // update is whether it was started with the channel at all, and the second is whether it could // write beside its own binary. Both are reported rather than inferred from a log. func TestHealthReportsWhetherItCanUpdateItself(t *testing.T) { srv := &Server{ cfg: Config{Version: "test", Chains: []string{"btc"}, Logf: func(string, ...any) {}}, health: newHealthState(time.Now(), []string{"btc"}), } health := srv.Health() if health.Exe == "" { t.Fatal("health does not name the running binary") } if len(health.Admins) != 0 { t.Fatalf("a gateway with no -admin flags lists administrators: %v", health.Admins) } if health.UpdateConfigured || health.AdminEnabled { t.Fatalf("a gateway with no admin or update flags reports them enabled: %+v", health) } srv.cfg.Admin = true srv.cfg.Admins = []string{"00"} health = srv.Health() if !health.AdminEnabled { t.Fatal("admin channel is configured but health says it is not") } if len(health.Admins) != 1 || health.Admins[0] != "00" { t.Fatalf("health does not list the configured administrator: %v", health.Admins) } if health.UpdateConfigured { t.Fatal("update channel is not configured but health says it is") } srv.cfg.Update = release.Options{ManifestURL: "https://example/manifest.json", PublicKey: []byte{0}} health = srv.Health() if !health.UpdateConfigured || health.UpdateURL != "https://example/manifest.json" { t.Fatalf("update channel is configured but health does not say so: %+v", health) } }