From e1de04f72e2680ca3586a31945ecbd15285041d4 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Sat, 4 Jul 2026 05:16:25 +0200 Subject: [PATCH] test: add popField and field formatting tests --- internal/observability/field_test.go | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 internal/observability/field_test.go diff --git a/internal/observability/field_test.go b/internal/observability/field_test.go new file mode 100644 index 00000000..616894a1 --- /dev/null +++ b/internal/observability/field_test.go @@ -0,0 +1,52 @@ +package observability + +import ( + "testing" +) + +func TestPopFieldReturnsEmptyForNil(t *testing.T) { + if got := popField(nil, "key"); got != "" { + t.Fatalf("got %q, want empty", got) + } +} + +func TestPopFieldReturnsEmptyForMissingKey(t *testing.T) { + if got := popField(map[string]any{"a": 1}, "b"); got != "" { + t.Fatalf("got %q, want empty", got) + } +} + +func TestPopFieldReturnsAndDeletes(t *testing.T) { + m := map[string]any{"status": 200, "other": "x"} + got := popField(m, "status") + if got != "200" { + t.Fatalf("got %q, want %q", got, "200") + } + if _, exists := m["status"]; exists { + t.Fatal("popField did not delete key") + } + if m["other"] != "x" { + t.Fatal("popField deleted other key") + } +} + +func TestPopFieldFormatsDuration(t *testing.T) { + got := popField(map[string]any{"duration_ms": 123.4}, "duration_ms") + if got != "123.4ms" { + t.Fatalf("got %q, want %q", got, "123.4ms") + } +} + +func TestPopFieldFormatsBytes(t *testing.T) { + got := popField(map[string]any{"bytes": 2048.0}, "bytes") + if got != "2.0KB" { + t.Fatalf("got %q, want %q", got, "2.0KB") + } +} + +func TestPopFieldPassthroughString(t *testing.T) { + got := popField(map[string]any{"method": "GET"}, "method") + if got != "GET" { + t.Fatalf("got %q, want %q", got, "GET") + } +}