package api import ( "io" "log" "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) // newRestartTestRouter builds a minimal Router with a recording restart seam (so the // process is never actually killed) and a config path seeded with `prior`. func newRestartTestRouter(t *testing.T, prior []byte) (*Router, *int, string) { t.Helper() dir := t.TempDir() path := filepath.Join(dir, "controller.yaml") if prior != nil { if err := os.WriteFile(path, prior, 0o600); err != nil { t.Fatal(err) } } calls := 0 r := &Router{configPath: path, logger: log.New(io.Discard, "", 0)} r.restart = func() { calls++ } return r, &calls, path } func TestSelfRestart_InvokesRestarter(t *testing.T) { r, calls, _ := newRestartTestRouter(t, nil) req := httptest.NewRequest(http.MethodPost, "/api/selfrestart", nil) rec := httptest.NewRecorder() r.selfRestart(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d, want 200", rec.Code) } if *calls != 1 { t.Fatalf("restart called %d times, want 1", *calls) } }