Skip to content
edgesync Artifact [bb04f5c09e]
Login

Artifact [bb04f5c09e]

Artifact bb04f5c09e429b9df3c3a446fcec12e23a63a5ed07b0483622b3c2fe50dfaf36:


package telemetry

import (
	"bytes"
	"context"
	"log/slog"
	"strings"
	"testing"
)

func TestCtxHandler_AddsAgentIDFromContext(t *testing.T) {
	var buf bytes.Buffer
	inner := slog.NewTextHandler(&buf, nil)
	h := NewCtxHandler(inner)

	ctx := WithAgentID(context.Background(), "agent-42")
	logger := slog.New(h)
	logger.InfoContext(ctx, "hello")

	if !strings.Contains(buf.String(), "agent_id=agent-42") {
		t.Fatalf("expected agent_id in output, got: %q", buf.String())
	}
}

func TestCtxHandler_NoAgentIDNoAttribute(t *testing.T) {
	var buf bytes.Buffer
	h := NewCtxHandler(slog.NewTextHandler(&buf, nil))

	slog.New(h).InfoContext(context.Background(), "hello")
	if strings.Contains(buf.String(), "agent_id") {
		t.Fatalf("agent_id should be absent, got: %q", buf.String())
	}
}