@@ -23,6 +23,7 @@ import (
2323 "testing"
2424 "time"
2525
26+ "github.com/google/go-cmp/cmp"
2627 "google.golang.org/protobuf/types/known/timestamppb"
2728
2829 pipelinev1alpha1 "github.com/crossplane/crossplane-runtime/v2/apis/pipelineinspector/proto/v1alpha1"
@@ -144,6 +145,7 @@ func TestEmitRequest_Text(t *testing.T) {
144145 CompositeResourceUid : "uid-123" ,
145146 CompositeResourceNamespace : "my-namespace" ,
146147 CompositionName : "my-composition" ,
148+ StepName : "my-step" ,
147149 FunctionName : "my-function" ,
148150 TraceId : "trace-abc" ,
149151 SpanId : "span-def" ,
@@ -159,44 +161,23 @@ func TestEmitRequest_Text(t *testing.T) {
159161
160162 _ , _ = inspector .EmitRequest (context .Background (), req )
161163
162- output := buf .String ()
163-
164- // Verify header.
165- if ! strings .Contains (output , "=== REQUEST ===" ) {
166- t .Errorf ("expected REQUEST header, got: %s" , output )
167- }
168-
169- // Verify XR info.
170- if ! strings .Contains (output , "XR: example.org/v1/XDatabase (my-xr)" ) {
171- t .Errorf ("expected XR info in output, got: %s" , output )
172- }
173-
174- // Verify UID is included.
175- if ! strings .Contains (output , "XR UID: uid-123" ) {
176- t .Errorf ("expected XR UID in output, got: %s" , output )
177- }
178-
179- // Verify namespace is included.
180- if ! strings .Contains (output , "XR NS: my-namespace" ) {
181- t .Errorf ("expected XR namespace in output, got: %s" , output )
182- }
164+ want := `=== REQUEST ===
165+ XR: example.org/v1/XDatabase (my-xr)
166+ XR UID: uid-123
167+ XR NS: my-namespace
168+ Composition: my-composition
169+ Step: my-step (index 1, iteration 2)
170+ Function: my-function
171+ Trace ID: trace-abc
172+ Span ID: span-def
173+ Timestamp: 2026-01-15T10:30:00.000Z
174+ Payload:
175+ apiVersion: apiextensions.crossplane.io/v1
183176
184- // Verify composition name.
185- if ! strings .Contains (output , "Composition: my-composition" ) {
186- t .Errorf ("expected composition name in output, got: %s" , output )
187- }
188-
189- // Verify function info.
190- if ! strings .Contains (output , "Function: my-function (step 1, iteration 2)" ) {
191- t .Errorf ("expected function info in output, got: %s" , output )
192- }
193177
194- // Verify trace and span IDs.
195- if ! strings .Contains (output , "Trace ID: trace-abc" ) {
196- t .Errorf ("expected trace ID in output, got: %s" , output )
197- }
198- if ! strings .Contains (output , "Span ID: span-def" ) {
199- t .Errorf ("expected span ID in output, got: %s" , output )
178+ `
179+ if diff := cmp .Diff (want , buf .String ()); diff != "" {
180+ t .Errorf ("EmitRequest text output mismatch (-want +got):\n %s" , diff )
200181 }
201182}
202183
@@ -212,8 +193,9 @@ func TestEmitRequest_Text_NoNamespace(t *testing.T) {
212193 CompositeResourceUid : "uid-456" ,
213194 CompositeResourceNamespace : "" , // Empty for cluster-scoped.
214195 CompositionName : "cluster-composition" ,
196+ StepName : "my-step" ,
215197 FunctionName : "my-function" ,
216- Timestamp : timestamppb .New (time .Now ( )),
198+ Timestamp : timestamppb .New (time .Date ( 2026 , 1 , 15 , 10 , 30 , 0 , 0 , time . UTC )),
217199 }
218200
219201 req := & pipelinev1alpha1.EmitRequestRequest {
@@ -223,11 +205,20 @@ func TestEmitRequest_Text_NoNamespace(t *testing.T) {
223205
224206 _ , _ = inspector .EmitRequest (context .Background (), req )
225207
226- output := buf .String ()
227-
228- // Verify namespace line is NOT included for cluster-scoped resources.
229- if strings .Contains (output , "XR NS:" ) {
230- t .Errorf ("expected no XR NS line for cluster-scoped resource, got: %s" , output )
208+ want := "=== REQUEST ===\n " +
209+ " XR: example.org/v1/XClusterDatabase (my-cluster-xr)\n " +
210+ " XR UID: uid-456\n " +
211+ " Composition: cluster-composition\n " +
212+ " Step: my-step (index 0, iteration 0)\n " +
213+ " Function: my-function\n " +
214+ " Trace ID: \n " +
215+ " Span ID: \n " +
216+ " Timestamp: 2026-01-15T10:30:00.000Z\n " +
217+ " Payload:\n " +
218+ " {}\n " +
219+ "\n \n "
220+ if diff := cmp .Diff (want , buf .String ()); diff != "" {
221+ t .Errorf ("EmitRequest text output mismatch (-want +got):\n %s" , diff )
231222 }
232223}
233224
@@ -236,8 +227,12 @@ func TestEmitResponse_Text_WithError(t *testing.T) {
236227 inspector := NewInspector ("text" , WithOutput (& buf ))
237228
238229 meta := & pipelinev1alpha1.StepMeta {
239- FunctionName : "failing-function" ,
240- Timestamp : timestamppb .New (time .Now ()),
230+ CompositeResourceApiVersion : "example.org/v1" ,
231+ CompositeResourceKind : "XDatabase" ,
232+ CompositeResourceName : "my-xr" ,
233+ StepName : "failing-step" ,
234+ FunctionName : "failing-function" ,
235+ Timestamp : timestamppb .New (time .Date (2026 , 1 , 15 , 10 , 30 , 0 , 0 , time .UTC )),
241236 }
242237
243238 req := & pipelinev1alpha1.EmitResponseRequest {
@@ -248,9 +243,19 @@ func TestEmitResponse_Text_WithError(t *testing.T) {
248243
249244 _ , _ = inspector .EmitResponse (context .Background (), req )
250245
251- output := buf .String ()
252- if ! strings .Contains (output , "Error: something went wrong" ) {
253- t .Errorf ("expected error in output, got: %s" , output )
246+ want := "=== RESPONSE ===\n " +
247+ " XR: example.org/v1/XDatabase (my-xr)\n " +
248+ " XR UID: \n " +
249+ " Composition: \n " +
250+ " Step: failing-step (index 0, iteration 0)\n " +
251+ " Function: failing-function\n " +
252+ " Trace ID: \n " +
253+ " Span ID: \n " +
254+ " Timestamp: 2026-01-15T10:30:00.000Z\n " +
255+ " Error: something went wrong\n " +
256+ "\n "
257+ if diff := cmp .Diff (want , buf .String ()); diff != "" {
258+ t .Errorf ("EmitResponse text output mismatch (-want +got):\n %s" , diff )
254259 }
255260}
256261
@@ -351,9 +356,9 @@ func TestIndentLines(t *testing.T) {
351356
352357 for _ , tt := range tests {
353358 t .Run (tt .name , func (t * testing.T ) {
354- result := indentLines (tt .input , tt .prefix )
355- if result != tt .expected {
356- t .Errorf ("indentLines(%q, %q) = %q, want %q " , tt .input , tt .prefix , result , tt . expected )
359+ got := indentLines (tt .input , tt .prefix )
360+ if diff := cmp . Diff ( tt .expected , got ); diff != "" {
361+ t .Errorf ("indentLines(%q, %q) mismatch (- want +got): \n %s " , tt .input , tt .prefix , diff )
357362 }
358363 })
359364 }
0 commit comments