Files
2026-07-13 12:35:43 +08:00

86 lines
2.7 KiB
Go

package logging
import (
"strings"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
func TestLogFormatterPrintsVersionField(t *testing.T) {
entry := log.NewEntry(log.New())
entry.Time = time.Date(2026, 6, 9, 11, 10, 2, 0, time.Local)
entry.Level = log.InfoLevel
entry.Message = "fetched latest antigravity version"
entry.Data["version"] = "2.1.0"
formatted, errFormat := (&LogFormatter{}).Format(entry)
if errFormat != nil {
t.Fatalf("Format() error = %v", errFormat)
}
line := string(formatted)
if !strings.Contains(line, "version=2.1.0") {
t.Fatalf("formatted line %q missing version field", line)
}
}
func TestLogFormatterPrintsPluginFields(t *testing.T) {
entry := log.NewEntry(log.New())
entry.Time = time.Date(2026, 6, 25, 20, 10, 0, 0, time.Local)
entry.Level = log.InfoLevel
entry.Message = "pluginhost: plugin loaded"
entry.Data["plugin_id"] = "sample-provider"
entry.Data["plugin_name"] = "Sample Provider"
entry.Data["version"] = "0.2.0"
entry.Data["active_version"] = "0.1.0"
entry.Data["retired_version"] = "0.2.0"
entry.Data["path"] = "plugins/windows/amd64/sample-provider-v0.2.0.dll"
entry.Data["active_path"] = "plugins/windows/amd64/sample-provider-v0.1.0.dll"
entry.Data["retired_path"] = "plugins/windows/amd64/sample-provider-v0.2.0.dll"
formatted, errFormat := (&LogFormatter{}).Format(entry)
if errFormat != nil {
t.Fatalf("Format() error = %v", errFormat)
}
line := string(formatted)
for _, want := range []string{
"plugin_id=sample-provider",
"plugin_name=Sample Provider",
"version=0.2.0",
"active_version=0.1.0",
"retired_version=0.2.0",
"path=plugins/windows/amd64/sample-provider-v0.2.0.dll",
"active_path=plugins/windows/amd64/sample-provider-v0.1.0.dll",
"retired_path=plugins/windows/amd64/sample-provider-v0.2.0.dll",
} {
if !strings.Contains(line, want) {
t.Fatalf("formatted line %q missing %s", line, want)
}
}
}
func TestLogFormatterOmitsGenericPathField(t *testing.T) {
entry := log.NewEntry(log.New())
entry.Time = time.Date(2026, 6, 25, 20, 20, 0, 0, time.Local)
entry.Level = log.WarnLevel
entry.Message = "failed to roll back token"
entry.Data["path"] = "auths/private-token.json"
entry.Data["active_path"] = "plugins/windows/amd64/sample-provider-v0.1.0.dll"
entry.Data["retired_path"] = "plugins/windows/amd64/sample-provider-v0.2.0.dll"
formatted, errFormat := (&LogFormatter{}).Format(entry)
if errFormat != nil {
t.Fatalf("Format() error = %v", errFormat)
}
line := string(formatted)
for _, forbidden := range []string{"path=", "active_path=", "retired_path="} {
if strings.Contains(line, forbidden) {
t.Fatalf("formatted line %q contains generic %s field", line, forbidden)
}
}
}