From 2a38b3ed4cc95d342af539723c03ad075c0acb84 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 4 Sep 2025 08:13:45 +0300 Subject: logging: add tests for Logf/Preview and ChatLogger.LogStart; achieve >90% package coverage --- internal/logging/logging_test.go | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 internal/logging/logging_test.go (limited to 'internal/logging/logging_test.go') diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go new file mode 100644 index 0000000..be8c93f --- /dev/null +++ b/internal/logging/logging_test.go @@ -0,0 +1,48 @@ +package logging + +import ( + "bytes" + "log" + "strings" + "testing" +) + +func TestLogf_WithBindAndPreview(t *testing.T) { + var buf bytes.Buffer + l := log.New(&buf, "", 0) + Bind(l) + + SetLogPreviewLimit(5) + if got := PreviewForLog("abcdef"); got != "abcde…" { + t.Fatalf("preview truncation failed: %q", got) + } + if got := PreviewForLog("abcd"); got != "abcd" { + t.Fatalf("preview (no trunc) failed: %q", got) + } + SetLogPreviewLimit(0) + if got := PreviewForLog("abcdef"); got != "abcdef" { + t.Fatalf("preview unlimited failed: %q", got) + } + + Logf("mod ", "hello %s", "world") + out := buf.String() + if !strings.Contains(out, "hello world") || !strings.Contains(out, AnsiBase) || !strings.Contains(out, AnsiReset) { + t.Fatalf("log output missing parts: %q", out) + } +} + +func TestChatLogger_LogStart(t *testing.T) { + var buf bytes.Buffer + Bind(log.New(&buf, "", 0)) + SetLogPreviewLimit(3) + cl := NewChatLogger("prov") + msgs := []struct{ Role, Content string }{{"user", "abcdef"}, {"assistant", "xyz"}} + cl.LogStart(true, "m", 0.2, 128, []string{"END"}, msgs) + out := buf.String() + if !strings.Contains(out, "stream start model=m") || !strings.Contains(out, "messages=2") { + t.Fatalf("missing header log: %q", out) + } + if !strings.Contains(out, "msg[0] role=user") || !strings.Contains(out, "preview=") { + t.Fatalf("missing message logs: %q", out) + } +} -- cgit v1.2.3