<commit_before>package main

import (
	"fmt"
	"regexp"
	"testing"
)

type mockLogEntry struct {
	Content string `json:"content"`
}

func (entry mockLogEntry) String() string {
	return fmt.Sprintf("test %v", entry.Content)
}

func (mockLogEntry) eventType() string {
	return "test"
}

func TestPlainWithTimestamps(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:       false,
			Timestamps: true,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"lorem"})
	logs := logBuffer.String()
	expectedLogs := regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[127\.0\.0\.1:1234\] test lorem
$`)
	if !expectedLogs.MatchString(logs) {
		t.Errorf("logs=%v, want match for %v", logs, expectedLogs)
	}
}

func TestJSONWithTimestamps(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:       true,
			Timestamps: true,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"ipsum"})
	logs := logBuffer.String()
	expectedLogs := regexp.MustCompile(`^{"time":"[^"]+","source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"ipsum"}}
$`)
	if !expectedLogs.MatchString(logs) {
		t.Errorf("logs=%v, want match for %v", logs, expectedLogs)
	}
}

func TestPlainWithoutTimestamps(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:       false,
			Timestamps: false,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"dolor"})
	logs := logBuffer.String()
	expectedLogs := `[127.0.0.1:1234] test dolor
`
	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}

func TestJSONWithoutTimestamps(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:       true,
			Timestamps: false,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"sit"})
	logs := logBuffer.String()
	expectedLogs := `{"source":"127.0.0.1:1234","event_type":"test","event":{"content":"sit"}}
`
	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}

func TestPlainWithAddressSplitting(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:          false,
			SplitHostPort: true,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"amet"})
	logs := logBuffer.String()
	expectedLogs := `[127.0.0.1:1234] test amet
`

	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}

func TestJSONWithAddressSplitting(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:          true,
			SplitHostPort: true,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"consectetur"})
	logs := logBuffer.String()
	expectedLogs := `{"source":{"host":"127.0.0.1","port":1234},"event_type":"test","event":{"content":"consectetur"}}
`

	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}

func TestPlainWithoutAddressSplitting(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:          false,
			SplitHostPort: false,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"adipiscing"})
	logs := logBuffer.String()
	expectedLogs := `[127.0.0.1:1234] test adipiscing
`

	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}

func TestJSONWithoutAddressSplitting(t *testing.T) {
	cfg := &config{
		Logging: loggingConfig{
			JSON:          true,
			SplitHostPort: false,
		},
	}
	logBuffer := setupLogBuffer(t, cfg)
	connContext{ConnMetadata: mockConnContext{}, cfg: cfg}.logEvent(mockLogEntry{"elit"})
	logs := logBuffer.String()
	expectedLogs := `{"source":"127.0.0.1:1234","event_type":"test","event":{"content":"elit"}}
`

	if logs != expectedLogs {
		t.Errorf("logs=%v, want %v", logs, expectedLogs)
	}
}<commit_msg>logging_test: add helper and consolidate tests<commit_after>package main

import (
	"fmt"
	"regexp"
	"testing"
)

type mockLogEntry struct {
	Content string `json:"content"`
}

func (entry mockLogEntry) String() string {
	return fmt.Sprintf("test %v", entry.Content)
}

func (mockLogEntry) eventType() string {
	return "test"
}

func testLogging(t *testing.T, cfg *loggingConfig, log logEntry, expectedLogs *regexp.Regexp) {
	t.Helper()
	c := &config{
		Logging: *cfg,
	}
	logBuffer := setupLogBuffer(t, c)
	connContext{ConnMetadata: mockConnContext{}, cfg: c}.logEvent(log)
	logs := logBuffer.String()
	// Remove trailing newline
	logs = logs[:len(logs)-1]
	if !expectedLogs.MatchString(logs) {
		t.Errorf("logs=%v, want match for %v", logs, expectedLogs)
	}
}

func TestPlainWithTimestamps(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:       false,
		Timestamps: true,
	}, mockLogEntry{"lorem"}, regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[127\.0\.0\.1:1234\] test lorem$`))
}

func TestJSONWithTimestamps(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:       true,
		Timestamps: true,
	}, mockLogEntry{"ipsum"}, regexp.MustCompile(`^{"time":"[^"]+","source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"ipsum"}}$`))
}

func TestPlainWithoutTimestamps(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:       false,
		Timestamps: false,
	}, mockLogEntry{"dolor"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test dolor$`))
}

func TestJSONWithoutTimestamps(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:       true,
		Timestamps: false,
	}, mockLogEntry{"sit"}, regexp.MustCompile(`^{"source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"sit"}}$`))
}

func TestPlainWithAddressSplitting(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:          false,
		SplitHostPort: true,
	}, mockLogEntry{"amet"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test amet$`))
}

func TestJSONWithAddressSplitting(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:          true,
		SplitHostPort: true,
	}, mockLogEntry{"consectetur"}, regexp.MustCompile(`^{"source":{"host":"127\.0\.0\.1","port":1234},"event_type":"test","event":{"content":"consectetur"}}$`))
}

func TestPlainWithoutAddressSplitting(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:          false,
		SplitHostPort: false,
	}, mockLogEntry{"adipiscing"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test adipiscing$`))
}

func TestJSONWithoutAddressSplitting(t *testing.T) {
	testLogging(t, &loggingConfig{
		JSON:          true,
		SplitHostPort: false,
	}, mockLogEntry{"elit"}, regexp.MustCompile(`^{"source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"elit"}}$`))
}
{'fraction_non_alphanumeric': 0.18792927078766622, 'fraction_numerical': 0.025281309367236594, 'mean_word_length': 4.992994746059544, 'pattern_counts': {'":': 38, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5782776', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2498, 'n_words': 446}