Files
worker/checks/llmhttp/result_test.go
Gleb Tv 2c884c5612
Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
refactor: adopt worker module path
2026-07-13 17:56:12 +03:00

134 строки
2.8 KiB
Go

package llmhttp
import (
"errors"
"testing"
"time"
"rocketgit.ru/rsmon/worker/app/models"
"rocketgit.ru/rsmon/worker/internal/checkresult"
)
func TestInfluxTags(t *testing.T) {
c := models.Check{ID: 123}
r := &Result{
CheckResult: checkresult.CheckResult{
State: "OK",
Error: nil,
Warnings: nil,
Duration: 100 * time.Millisecond,
},
StatusCode: 200,
}
tags := r.InfluxTags(c)
if tags["check"] != "123" {
t.Errorf("check = %q, want 123", tags["check"])
}
if tags["state"] != "OK" {
t.Errorf("state = %q, want OK", tags["state"])
}
if tags["code"] != "200" {
t.Errorf("code = %q, want 200", tags["code"])
}
if _, ok := tags["error"]; ok {
t.Error("error tag should not be present when Error is nil")
}
rWithErr := &Result{
CheckResult: checkresult.CheckResult{
State: "ERR",
Error: errors.New("something went wrong"),
Warnings: nil,
Duration: 100 * time.Millisecond,
},
StatusCode: 500,
}
tags = rWithErr.InfluxTags(c)
if tags["error"] != "something went wrong" {
t.Errorf("error = %q, want 'something went wrong'", tags["error"])
}
}
func TestInfluxTags_Warnings(t *testing.T) {
c := models.Check{ID: 456}
r := &Result{
CheckResult: checkresult.CheckResult{
State: "WARN",
Warnings: []string{"redirect detected", "slow response"},
Duration: time.Second,
},
StatusCode: 301,
}
tags := r.InfluxTags(c)
wantWarnings := "redirect detected,slow response"
if tags["warnings"] != wantWarnings {
t.Errorf("warnings = %q, want %q", tags["warnings"], wantWarnings)
}
}
func TestInfluxFields(t *testing.T) {
r := &Result{
CheckResult: checkresult.CheckResult{
State: "OK",
Duration: 1234 * time.Millisecond,
},
}
fields := r.InfluxFields()
if took, ok := fields["took"]; !ok {
t.Error("took field missing")
} else if took != int64(1234) {
t.Errorf("took = %v (%T), want int64(1234)", took, took)
}
}
func TestInfluxFields_NegativeDuration(t *testing.T) {
r := &Result{
CheckResult: checkresult.CheckResult{
State: "OK",
Duration: 0,
},
}
fields := r.InfluxFields()
if took, ok := fields["took"]; !ok {
t.Error("took field missing")
} else if took != int64(0) {
t.Errorf("took = %v, want 0", took)
}
}
func TestWarningsJoined(t *testing.T) {
c := models.Check{ID: 1}
tests := []struct {
name string
warnings []string
want string
}{
{"nil", nil, ""},
{"empty", []string{}, ""},
{"single", []string{"a"}, "a"},
{"multiple", []string{"a", "b", "c"}, "a,b,c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Result{
CheckResult: checkresult.CheckResult{
Warnings: tt.warnings,
},
}
tags := r.InfluxTags(c)
if tags["warnings"] != tt.want {
t.Errorf("warnings = %q, want %q", tags["warnings"], tt.want)
}
})
}
}