package engine import ( "context" "net/http" "net/http/httptest" "testing" ) // The engine's REAL reply, copied from a running instance. The point of this // test is the `"frozen": false` inside `paths`: Health.Paths was // map[string]string, so decoding failed on that one bool, and because a failed // decode fails the whole document a working engine was reported unreachable - // red tray, and a heartbeat carrying neither the model nor the cameras, so head // office showed "0 of 0 cameras" for a site that was watching one. const realHealthBody = `{"status":"ok","recognition_model":"w600k_r50", "paths":{"frozen":false,"install_root":"/opt/behavision","state_root":"/var/behavision", "config":"/var/behavision/config/default.yaml","data_dir":"/var/behavision/data", "models_dir":"/var/behavision/models"},"cameras":{"cam1":true},"uptime_seconds":42.5}` func TestHealthDecodesWhatTheEngineActuallySends(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(realHealthBody)) })) defer srv.Close() s := New(Options{HealthURL: srv.URL}) h, err := s.Health(context.Background()) if err != nil { t.Fatalf("the engine's own reply did not decode: %v", err) } if h.RecognitionModel != "w600k_r50" { t.Errorf("model = %q", h.RecognitionModel) } // The two fields the heartbeat carries. Losing these is what made a // working site look empty at head office. if up, ok := h.Cameras["cam1"]; !ok || !up { t.Errorf("cameras = %v, want cam1 connected", h.Cameras) } if h.Paths.StateRoot != "/var/behavision" || h.Paths.Frozen { t.Errorf("paths = %+v", h.Paths) } }