package main import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "git.gsdistance.org/tyuXX/RUD/internal/builder" "git.gsdistance.org/tyuXX/RUD/internal/config" ) func TestValidateConfigFile(t *testing.T) { tmpDir := t.TempDir() cleanPath := filepath.Join(tmpDir, "clean.json") cfg := config.DefaultConfig() if err := config.SaveConfig(cleanPath, &cfg); err != nil { t.Fatalf("SaveConfig failed: %v", err) } var out bytes.Buffer if err := validateConfigFile(cleanPath, true, &out); err != nil { t.Fatalf("expected clean config to pass strict validation, got: %v\n%s", err, out.String()) } if !strings.Contains(out.String(), "0 lint warnings") { t.Fatalf("expected 0 lint warnings message, got:\n%s", out.String()) } // File with an unknown field -> passes normal mode, fails strict mode warnPath := filepath.Join(tmpDir, "warn.json") raw := `{"unknown_field":1,"monitors":[{"id":"m1","method":"cmd","command":"echo ok"}]}` if err := os.WriteFile(warnPath, []byte(raw), 0644); err != nil { t.Fatalf("WriteFile failed: %v", err) } out.Reset() if err := validateConfigFile(warnPath, false, &out); err != nil { t.Fatalf("expected non-strict validate to pass with warnings, got: %v", err) } if !strings.Contains(out.String(), "unknown_field") { t.Fatalf("expected unknown_field warning, got:\n%s", out.String()) } out.Reset() if err := validateConfigFile(warnPath, true, &out); err == nil { t.Fatalf("expected strict validate to fail on lint warning") } // Invalid monitor config -> always fails badPath := filepath.Join(tmpDir, "bad.json") if err := os.WriteFile(badPath, []byte(`{"monitors":[{"id":"m1","method":"http"}]}`), 0644); err != nil { t.Fatalf("WriteFile failed: %v", err) } out.Reset() if err := validateConfigFile(badPath, false, &out); err == nil { t.Fatalf("expected invalid HTTP monitor without url to fail validation") } } func TestRunMonitorTestsCmd(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("pong")) })) defer ts.Close() tmpDir := t.TempDir() cfgPath := filepath.Join(tmpDir, "rud.json") cfg := builder.BlankMinimalConfig() cfg.Monitors = []config.MonitorConfig{ {ID: "ok-http", Name: "OK HTTP", Method: "http", URL: ts.URL, BodyRegex: `^pong$`}, {ID: "ok-cmd", Name: "OK Cmd", Method: "cmd", Command: "echo ok", Shell: true}, {ID: "fail-cmd", Name: "Fail Cmd", Method: "cmd", Command: "exit 2", Shell: true}, } if err := config.SaveConfig(cfgPath, &cfg); err != nil { t.Fatalf("SaveConfig failed: %v", err) } // Test passing monitors in table format var out bytes.Buffer if err := runMonitorTestsCmd(context.Background(), cfgPath, []string{"ok-http", "ok-cmd"}, false, false, &out); err != nil { t.Fatalf("expected passing monitors test to succeed, got: %v\n%s", err, out.String()) } if !strings.Contains(out.String(), "ok-http") || !strings.Contains(out.String(), "UP") { t.Fatalf("unexpected table output:\n%s", out.String()) } // Test passing monitor in JSON format out.Reset() if err := runMonitorTestsCmd(context.Background(), cfgPath, []string{"ok-http"}, false, true, &out); err != nil { t.Fatalf("expected JSON test to succeed, got: %v", err) } var results []builder.MonitorTestResult if err := json.Unmarshal(out.Bytes(), &results); err != nil { t.Fatalf("invalid JSON output: %v\n%s", err, out.String()) } if len(results) != 1 || results[0].ID != "ok-http" || results[0].State != "up" { t.Fatalf("unexpected JSON results: %+v", results) } // Test failing monitor returns non-nil error out.Reset() if err := runMonitorTestsCmd(context.Background(), cfgPath, []string{"fail-cmd"}, false, false, &out); err == nil { t.Fatalf("expected failing monitor to return error") } }