Align console warning types with RFC

This commit is contained in:
Snider 2026-04-15 14:29:06 +01:00
parent 75ed16743a
commit 284d39de18
3 changed files with 34 additions and 17 deletions

View file

@ -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]"

View file

@ -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

View file

@ -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,