1
+ package workflowstate
2
+
3
+ import (
4
+ "bytes"
5
+ "log/slog"
6
+ "strings"
7
+ "testing"
8
+
9
+ "github.com/benbjohnson/clock"
10
+ "github.com/cschleiden/go-workflows/core"
11
+ "github.com/cschleiden/go-workflows/internal/log"
12
+ "github.com/stretchr/testify/assert"
13
+ "go.opentelemetry.io/otel/trace/noop"
14
+ )
15
+
16
+ func TestLogFieldDuplication (t * testing.T ) {
17
+ // Create a buffer to capture log output
18
+ var buf bytes.Buffer
19
+
20
+ // Create JSON logger to easily see field duplication
21
+ logger := slog .New (slog .NewJSONHandler (& buf , & slog.HandlerOptions {Level : slog .LevelDebug }))
22
+
23
+ // Create an instance
24
+ instance := & core.WorkflowInstance {
25
+ InstanceID : "test-instance-123" ,
26
+ ExecutionID : "test-execution-456" ,
27
+ }
28
+
29
+ // This mimics what happens in executor.go NewExecutor
30
+ logger = logger .With (
31
+ slog .String (log .InstanceIDKey , instance .InstanceID ),
32
+ slog .String (log .ExecutionIDKey , instance .ExecutionID ),
33
+ )
34
+
35
+ tracer := noop .NewTracerProvider ().Tracer ("test" )
36
+ clock := clock .New ()
37
+ state := NewWorkflowState (instance , logger , tracer , clock )
38
+
39
+ // Log something using the workflow state logger (which currently duplicates fields)
40
+ state .Logger ().Debug ("Test message from workflow state" )
41
+
42
+ // Parse and analyze the log output
43
+ logLines := strings .Split (strings .TrimSpace (buf .String ()), "\n " )
44
+
45
+ foundDuplicates := false
46
+ for _ , line := range logLines {
47
+ if strings .TrimSpace (line ) == "" {
48
+ continue
49
+ }
50
+
51
+ t .Logf ("Log entry: %s" , line )
52
+
53
+ // Count field occurrences in raw JSON string instead of unmarshaling
54
+ // since JSON unmarshaling overwrites duplicate keys
55
+ instanceCount := strings .Count (line , `"` + log .InstanceIDKey + `"` )
56
+ executionCount := strings .Count (line , `"` + log .ExecutionIDKey + `"` )
57
+
58
+ t .Logf ("Instance ID field count: %d" , instanceCount )
59
+ t .Logf ("Execution ID field count: %d" , executionCount )
60
+
61
+ if instanceCount > 1 || executionCount > 1 {
62
+ foundDuplicates = true
63
+ t .Logf ("❌ DUPLICATE FIELDS DETECTED!" )
64
+ } else {
65
+ t .Logf ("✅ No duplicates found" )
66
+ }
67
+ }
68
+
69
+ // After fix, we should NOT find duplicates
70
+ assert .False (t , foundDuplicates , "Should not find duplicate fields after fix" )
71
+ }
0 commit comments