packagemetricsimport("honeypot/internal/types""net/http""net/http/httptest""testing")funcTestSanitizeUTF8(t*testing.T){tests:=[]struct{namestringinputstringwantstring}{{"valid utf8","hello","hello"},{"invalid utf8","hello\xffworld","hello\ufffdworld"},{"empty","",""},}for_,tt:=rangetests{t.Run(tt.name,func(t*testing.T){ifgot:=SanitizeUTF8(tt.input);got!=tt.want{t.Errorf("SanitizeUTF8(%q) = %q, want %q",tt.input,got,tt.want)}})}}funcTestNewMetricsCollector(t*testing.T){mc:=NewMetricsCollector(false)ifmc==nil{t.Fatal("NewMetricsCollector returned nil")}ifmc.packetsTotal==nil{t.Error("packetsTotal counter vec is nil")}ifmc.fieldCounts==nil{t.Error("fieldCounts map is nil")}}funcTestRecordEvent(t*testing.T){mc:=NewMetricsCollector(false)e:=types.LogEvent{Type:"ssh",Event:types.EventAuthAttempt,RemoteAddr:"1.2.3.4",Fields:map[string]interface{}{"username":"admin"},}mc.RecordEvent(e)mc.mu.RLock()defermc.mu.RUnlock()ifmc.fieldCounts["ssh"]==nil{t.Error("fieldCounts['ssh'] should not be nil")}ifmc.fieldCounts["ssh"]["username"]["admin"]!=1{t.Errorf("expected count 1 for ssh/username/admin, got %v",mc.fieldCounts["ssh"]["username"]["admin"])}ifmc.fieldCounts["ssh"]["remote_addr"]["1.2.3.4"]!=1{t.Errorf("expected count 1 for ssh/remote_addr/1.2.3.4, got %v",mc.fieldCounts["ssh"]["remote_addr"]["1.2.3.4"])}}funcTestGetTopN(t*testing.T){counts:=map[string]int64{"a":10,"b":20,"c":5,"d":15,}top2:=getTopN(counts,2)iflen(top2)!=2{t.Errorf("expected 2 items, got %v",len(top2))}iftop2["b"]!=20||top2["d"]!=15{t.Errorf("unexpected top items: %v",top2)}top10:=getTopN(counts,10)iflen(top10)!=4{t.Errorf("expected 4 items, got %v",len(top10))}}funcTestGetHandler(t*testing.T){mc:=NewMetricsCollector(true)// disable HW metrics to avoid /proc access errors in some environmentshandler:=mc.GetHandler()ifhandler==nil{t.Fatal("GetHandler returned nil")}req:=httptest.NewRequest("GET","/metrics",nil)rr:=httptest.NewRecorder()handler.ServeHTTP(rr,req)ifrr.Code!=http.StatusOK{t.Errorf("expected status OK, got %v",rr.Code)}}funcTestRegisterTopNField(t*testing.T){mc:=NewMetricsCollector(false)mc.RegisterTopNField("test_type","test_field")mc.mu.RLock()registered:=mc.registeredFields["test_type"]["test_field"]mc.mu.RUnlock()if!registered{t.Error("test_field for test_type was not registered")}}