summaryrefslogtreecommitdiff
path: root/internal/daemon/daemon_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-14 10:07:52 +0300
committerPaul Buetow <paul@buetow.org>2026-04-14 10:07:52 +0300
commit00a015a9642baee69def9a104602b4d59f980c63 (patch)
treec3251e70b01af4eae273cf418279614a4f9c7bd6 /internal/daemon/daemon_test.go
parent806ff16a0ad70ae2883a666bdc4158ba20ca4d4f (diff)
daemon: stdout-only slog logging for HTTP (task 43)
- Route http.Server ErrorLog and request access lines through slog text to stdout (or Config.LogOutput for tests). - Log daemon_listen on start and http_request per request (method, path, status, duration_ms). - CLI daemon: flag usage and missing-stats-dir message on stdout. Made-with: Cursor
Diffstat (limited to 'internal/daemon/daemon_test.go')
-rw-r--r--internal/daemon/daemon_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go
index b140c9e..9f25ca3 100644
--- a/internal/daemon/daemon_test.go
+++ b/internal/daemon/daemon_test.go
@@ -1,13 +1,16 @@
package daemon
import (
+ "bytes"
"context"
"io"
+ "log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
+ "time"
)
func TestHealth(t *testing.T) {
@@ -131,3 +134,44 @@ func TestRunEmptyAddr(t *testing.T) {
t.Fatal("expected error")
}
}
+
+func TestRunWritesDaemonListenToLogOutput(t *testing.T) {
+ var buf bytes.Buffer
+ ctx, cancel := context.WithCancel(context.Background())
+ cfg := Config{StatsDir: t.TempDir(), Addr: "127.0.0.1:0", LogOutput: &buf}
+ done := make(chan struct{})
+ go func() {
+ _ = Run(ctx, cfg)
+ close(done)
+ }()
+ deadline := time.After(2 * time.Second)
+ for !strings.Contains(buf.String(), "daemon_listen") {
+ select {
+ case <-deadline:
+ t.Fatalf("timeout waiting for daemon_listen, got %q", buf.String())
+ case <-time.After(5 * time.Millisecond):
+ }
+ }
+ cancel()
+ <-done
+}
+
+func TestAccessLogLineToWriter(t *testing.T) {
+ var buf bytes.Buffer
+ h := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelInfo})
+ log := slog.New(h)
+ srv := httptest.NewServer(withAccessLog(log, routes(t.TempDir())))
+ defer srv.Close()
+ res, err := http.Get(srv.URL + "/health")
+ if err != nil {
+ t.Fatal(err)
+ }
+ res.Body.Close()
+ body := buf.String()
+ if !strings.Contains(body, "http_request") || !strings.Contains(body, "method=GET") {
+ t.Fatalf("expected http_request line with method=GET, got %q", body)
+ }
+ if !strings.Contains(body, "path=/health") || !strings.Contains(body, "status=200") {
+ t.Fatalf("expected path and status in log, got %q", body)
+ }
+}