fix(staterpc): preserve marshal_error marker in eventDataJSON

WIRE: docs/prds/state-store-contract.md §11 requires the remote wire and the
local adapter to behave identically. The local adapter (internal/store/events.go)
records a {"marshal_error":%q} marker when json.Marshal of event Data fails,
while eventDataJSON silently returned "" on the same failure, emptying the
event's Data on the wire. Carry the same marker so an unmarshalable Data
value survives the hop with the local adapter's fidelity.
This commit is contained in:
sam committed 2026-09-15 12:50:57 +10:00
1 parent 7fd393492d
commit 42adee2f87
2 files changed
+39 -1

No files matched your search

+1 -1
View File
@@ -84,7 +84,7 @@ func eventDataJSON(data map[string]any) string {
}
b, err := json.Marshal(data)
if err != nil {
return ""
return fmt.Sprintf(`{"marshal_error":%q}`, err.Error())
}
return string(b)
}
@@ -0,0 +1,38 @@
package staterpc
import (
"encoding/json"
"fmt"
"testing"
)
// TestEventDataJSONPreservesMarshalErrorMarker pins §11's conformance
// invariant: the local store (internal/store/events.go) records a
// {"marshal_error":%q} marker when json.Marshal of event Data fails, so the
// remote wire must carry the same marker rather than silently emptying the
// Data on the hop.
func TestEventDataJSONPreservesMarshalErrorMarker(t *testing.T) {
data := map[string]any{"channel": make(chan int)}
got := eventDataJSON(data)
if got == "" {
t.Fatal(`eventDataJSON = "", want the local adapter's marshal_error marker`)
}
_, err := json.Marshal(data)
if err == nil {
t.Fatal("test input must fail json.Marshal")
}
want := fmt.Sprintf(`{"marshal_error":%q}`, err.Error())
if got != want {
t.Fatalf("eventDataJSON = %q, want %q (local-adapter fidelity)", got, want)
}
// The server side rehydrates the marker back into a Data map, exactly
// as the local store's scanEvents rehydrates its on-disk column.
round := eventDataValue(got)
message, ok := round["marshal_error"].(string)
if !ok || message == "" {
t.Fatalf("eventDataValue = %#v, want a non-empty marshal_error string", round)
}
}