diff --git a/console.go b/console.go index 780f2e3..27e42a2 100644 --- a/console.go +++ b/console.go @@ -60,8 +60,8 @@ func NewConsoleWatcher(wv *Webview) *ConsoleWatcher { // normalizeConsoleType converts CDP event types to package-level values. func normalizeConsoleType(raw string) string { normalized := strings.ToLower(core.Trim(core.Sprint(raw))) - if normalized == "warn" { - return "warning" + if normalized == "warning" { + return "warn" } return normalized } @@ -291,7 +291,7 @@ func (cw *ConsoleWatcher) WarningsAll() iter.Seq[ConsoleMessage] { defer cw.mu.RUnlock() for _, msg := range cw.messages { - if msg.Type == "warning" { + if isWarningType(msg.Type) { if !yield(msg) { return } @@ -450,8 +450,16 @@ func (cw *ConsoleWatcher) matchesFilter(msg ConsoleMessage) bool { // matchesSingleFilter checks if a message matches a specific filter. func (cw *ConsoleWatcher) matchesSingleFilter(msg ConsoleMessage, filter ConsoleFilter) bool { - if filter.Type != "" && msg.Type != filter.Type { - return false + if filter.Type != "" { + filterType := normalizeConsoleType(filter.Type) + messageType := normalizeConsoleType(msg.Type) + if isWarningType(filterType) { + if !isWarningType(messageType) { + return false + } + } else if messageType != filterType { + return false + } } if filter.Pattern != "" { // Simple substring match @@ -462,6 +470,10 @@ func (cw *ConsoleWatcher) matchesSingleFilter(msg ConsoleMessage, filter Console return true } +func isWarningType(messageType string) bool { + return messageType == "warn" || messageType == "warning" +} + // containsString checks if s contains substr (case-sensitive). func containsString(s, substr string) bool { return len(substr) == 0 || (len(s) >= len(substr) && findString(s, substr) >= 0) @@ -675,7 +687,7 @@ func FormatConsoleOutput(messages []ConsoleMessage) string { switch normalizeConsoleType(msg.Type) { case "error": prefix = "[ERROR]" - case "warning", "warn": + case "warn": prefix = "[WARN]" case "info": prefix = "[INFO]" diff --git a/webview.go b/webview.go index b77b1fe..fd97814 100644 --- a/webview.go +++ b/webview.go @@ -47,7 +47,7 @@ type Webview struct { // ConsoleMessage represents a captured console log message. type ConsoleMessage struct { - Type string `json:"type"` // log, warning, error, info, debug + Type string `json:"type"` // log, warn, error, info, debug Text string `json:"text"` // Message text Timestamp time.Time `json:"timestamp"` // When the message was logged URL string `json:"url"` // Source URL diff --git a/webview_test.go b/webview_test.go index 13458f8..f7ffae6 100644 --- a/webview_test.go +++ b/webview_test.go @@ -373,15 +373,15 @@ func TestFormatConsoleOutput_Good_Empty(t *testing.T) { // TestNormalizeConsoleType_Good verifies CDP warning aliases are normalised. func TestNormalizeConsoleType_Good(t *testing.T) { - if got := normalizeConsoleType("warn"); got != "warning" { - t.Fatalf("normalizeConsoleType(\"warn\") = %q, want %q", got, "warning") + if got := normalizeConsoleType("warn"); got != "warn" { + t.Fatalf("normalizeConsoleType(\"warn\") = %q, want %q", got, "warn") } - if got := normalizeConsoleType("WARNING"); got != "warning" { - t.Fatalf("normalizeConsoleType(\"WARNING\") = %q, want %q", got, "warning") + if got := normalizeConsoleType("WARNING"); got != "warn" { + t.Fatalf("normalizeConsoleType(\"WARNING\") = %q, want %q", got, "warn") } } -// TestWebviewHandleConsoleEvent_Good_NormalizesWarningType verifies CDP warn events are stored as warnings. +// TestWebviewHandleConsoleEvent_Good_NormalizesWarningType verifies CDP warning aliases are stored as warn. func TestWebviewHandleConsoleEvent_Good_NormalizesWarningType(t *testing.T) { wv := &Webview{ consoleLogs: make([]ConsoleMessage, 0), @@ -398,8 +398,8 @@ func TestWebviewHandleConsoleEvent_Good_NormalizesWarningType(t *testing.T) { if len(wv.consoleLogs) != 1 { t.Fatalf("Expected one console message, got %d", len(wv.consoleLogs)) } - if wv.consoleLogs[0].Type != "warning" { - t.Fatalf("Expected warning type, got %q", wv.consoleLogs[0].Type) + if wv.consoleLogs[0].Type != "warn" { + t.Fatalf("Expected warn type, got %q", wv.consoleLogs[0].Type) } if wv.consoleLogs[0].Text != "deprecated" { t.Fatalf("Expected text %q, got %q", "deprecated", wv.consoleLogs[0].Text) @@ -620,8 +620,13 @@ func TestConsoleWatcherFilter_Good(t *testing.T) { if !cw.matchesFilter(ConsoleMessage{Type: "warning", Text: "deprecated"}) { t.Error("Expected warning message to match warning filter") } - if cw.matchesFilter(ConsoleMessage{Type: "warn", Text: "deprecated"}) { - t.Error("Expected warn message not to match warning filter") + if !cw.matchesFilter(ConsoleMessage{Type: "warn", Text: "deprecated"}) { + t.Error("Expected warn message to match warning filter") + } + cw.ClearFilters() + cw.AddFilter(ConsoleFilter{Type: "warn"}) + if !cw.matchesFilter(ConsoleMessage{Type: "warning", Text: "deprecated"}) { + t.Error("Expected warning message to match warn filter") } } @@ -633,7 +638,7 @@ func TestConsoleWatcherCounts_Good(t *testing.T) { {Type: "error", Text: "err 1"}, {Type: "log", Text: "info 2"}, {Type: "error", Text: "err 2"}, - {Type: "warning", Text: "warn 1"}, + {Type: "warn", Text: "warn 1"}, }, filters: make([]ConsoleFilter, 0), limit: 1000,