From bae6321ccd0eb9f7b912652a0f69089f2b005d5e Mon Sep 17 00:00:00 2001 From: Numinous Forge Date: Sun, 6 Sep 2026 14:17:57 +0000 Subject: [PATCH] Forge task c0b3ce44f544dcd1a4c2bd4daa506314 Verified tree: d1602cb7555e880fd87d7f209b6f7aee96039646 Policy: c0f26d7da586fda2c72702cf736512909371e691eaef0893b0d9eeea404ccd85 --- .../messages_type_null_duplicate_test.go | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 coordinator/protocol/messages_type_null_duplicate_test.go diff --git a/coordinator/protocol/messages_type_null_duplicate_test.go b/coordinator/protocol/messages_type_null_duplicate_test.go new file mode 100644 index 0000000000..1905c03088 --- /dev/null +++ b/coordinator/protocol/messages_type_null_duplicate_test.go @@ -0,0 +1,86 @@ +package protocol + +import "testing" + +// TestProviderMessageTypeNullAndDuplicateFields pins the decoded semantics of +// duplicate and null "type" fields. A duplicate (or case-variant) "type" key +// forces scanTopLevelString off the fast path, so encoding/json's rules decide +// the outcome: keys are processed in order, last-match-wins, matched +// case-insensitively, and a JSON `null` is a no-op that leaves any prior string +// in place rather than erasing it. A wrong JSON type (number) still fails the +// whole decode even when a later duplicate carries a valid string, because +// encoding/json records the type error and returns it. Expected outcomes are +// written out explicitly so each case exercises the production decoder against +// a fixed contract rather than treating the decoder as its own oracle. +func TestProviderMessageTypeNullAndDuplicateFields(t *testing.T) { + tests := []struct { + name string + in string + // accept is true when UnmarshalJSON must succeed; wantType and + // wantStatus are then checked against the decoded heartbeat payload. + accept bool + wantType string + wantStatus string + }{ + { + name: "known type then null", + in: `{"type":"heartbeat","status":"idle","type":null}`, + accept: true, + wantType: TypeHeartbeat, + wantStatus: "idle", + }, + { + name: "null then known type", + in: `{"type":null,"status":"busy","type":"heartbeat"}`, + accept: true, + wantType: TypeHeartbeat, + wantStatus: "busy", + }, + { + name: "mixed-case null duplicate", + in: `{"type":"heartbeat","status":"draining","Type":null}`, + accept: true, + wantType: TypeHeartbeat, + wantStatus: "draining", + }, + { + name: "unknown type then null", + in: `{"type":"bogus","type":null}`, + accept: false, + }, + { + name: "numeric duplicate then known type", + in: `{"type":123,"type":"heartbeat"}`, + accept: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var pm ProviderMessage + err := pm.UnmarshalJSON([]byte(tc.in)) + if tc.accept { + if err != nil { + t.Fatalf("UnmarshalJSON(%s) failed: %v", tc.in, err) + } + if pm.Type != tc.wantType { + t.Fatalf("pm.Type = %q, want %q", pm.Type, tc.wantType) + } + hb, ok := pm.Payload.(*HeartbeatMessage) + if !ok { + t.Fatalf("payload type = %T, want *HeartbeatMessage", pm.Payload) + } + if hb.Type != tc.wantType { + t.Fatalf("heartbeat.Type = %q, want %q", hb.Type, tc.wantType) + } + if hb.Status != tc.wantStatus { + t.Fatalf("heartbeat.Status = %q, want %q", hb.Status, tc.wantStatus) + } + return + } + if err == nil { + t.Fatalf("UnmarshalJSON(%s) accepted, want rejection (payload %T)", tc.in, pm.Payload) + } + }) + } +}