High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: crashes.recent serialises as null while its sibling failures.recent is always an array, and the metrics doc publishes [] for both (PIN-181)
Luigi Agosti6be2926ce017
diff --git a/internal/bridge/crash.go b/internal/bridge/crash.goindex ebdb67a9..8b6c8f63 100644--- a/internal/bridge/crash.go+++ b/internal/bridge/crash.go@@ -114,7 +114,8 @@ func CrashForBrowserContext(ctx context.Context) (CrashEvent, bool) {// CrashSnapshot returns recent crash diagnostics for /health and /metrics.func CrashSnapshot() map[string]any {crashMu.Lock()- recent := append([]CrashEvent(nil), recentCrashEvents...)+ recent := make([]CrashEvent, 0, len(recentCrashEvents))+ recent = append(recent, recentCrashEvents...)crashMu.Unlock()return map[string]any{"total": atomic.LoadUint64(&crashEventsTotal),diff --git a/internal/bridge/crash_test.go b/internal/bridge/crash_test.gonew file mode 100644index 00000000..3ebf0d58--- /dev/null+++ b/internal/bridge/crash_test.go@@ -0,0 +1,46 @@+package bridge++import (+ "encoding/json"+ "strings"+ "testing"+ "time"+)++func TestCrashSnapshotRecentIsAnArrayWhenEmptyAndWhenPopulated(t *testing.T) {+ ResetCrashMonitoringForTests()+ t.Cleanup(ResetCrashMonitoringForTests)++ empty, err := json.Marshal(CrashSnapshot())+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(empty), `"recent":[]`) {+ t.Errorf("empty snapshot = %s, want recent to marshal as [] like failures.recent and the documented example, not null", empty)+ }++ RecordCrashForTests(CrashEvent{Time: time.Unix(1700000000, 0).UTC(), TabID: "tab1", Reason: "targetCrashed"})++ var populated struct {+ Total uint64 `json:"total"`+ Recent []json.RawMessage `json:"recent"`+ }+ raw, err := json.Marshal(CrashSnapshot())+ if err != nil {+ t.Fatal(err)+ }+ if err := json.Unmarshal(raw, &populated); err != nil {+ t.Fatalf("populated snapshot %s did not decode with recent as an array: %v", raw, err)+ }+ if populated.Total != 1 {+ t.Errorf("total = %d, want 1", populated.Total)+ }+ if len(populated.Recent) != 1 {+ t.Fatalf("recent = %s, want the one recorded event", raw)+ }+ for _, field := range []string{`"tabId":"tab1"`, `"reason":"targetCrashed"`} {+ if !strings.Contains(string(populated.Recent[0]), field) {+ t.Errorf("event = %s, want it to carry %s", populated.Recent[0], field)+ }+ }+}diff --git a/internal/handlers/observability_layers_test.go b/internal/handlers/observability_layers_test.goindex 8d9d38f3..ed5cce4c 100644--- a/internal/handlers/observability_layers_test.go+++ b/internal/handlers/observability_layers_test.go@@ -2,10 +2,12 @@ package handlersimport ("encoding/json"+ "fmt""net/http""net/http/httptest""testing"+ "github.com/pinchtab/pinchtab/internal/bridge""github.com/pinchtab/pinchtab/internal/config")@@ -81,6 +83,49 @@ func TestBothModesExposeTheSameDiagnosticKeys(t *testing.T) {}}+func TestEmptyDiagnosticsEnvelopeContainsNoNulls(t *testing.T) {+ resetObservabilityForTests()+ bridge.ResetCrashMonitoringForTests()+ t.Cleanup(func() {+ resetObservabilityForTests()+ bridge.ResetCrashMonitoringForTests()+ })++ for _, layer := range []string{LayerFrontDoor, LayerInstance} {+ raw, err := json.Marshal(DiagnosticsSnapshot(layer))+ if err != nil {+ t.Fatal(err)+ }+ var envelope any+ if err := json.Unmarshal(raw, &envelope); err != nil {+ t.Fatal(err)+ }+ for _, path := range nullPaths(envelope, layer) {+ t.Errorf("%s is null in the empty envelope; an empty collection must marshal as its own type ([] or {}), since empty is the steady state a monitor reads", path)+ }+ }+}++func nullPaths(value any, path string) []string {+ switch v := value.(type) {+ case nil:+ return []string{path}+ case map[string]any:+ var paths []string+ for key, child := range v {+ paths = append(paths, nullPaths(child, path+"."+key)...)+ }+ return paths+ case []any:+ var paths []string+ for i, child := range v {+ paths = append(paths, nullPaths(child, fmt.Sprintf("%s[%d]", path, i))...)+ }+ return paths+ }+ return nil+}+// A bridge bug report has to be able to state which build produced it.func TestBridgeHealthReportsVersion(t *testing.T) {h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)