Restructured project for V2 refactor: **Structure Changes:** - Moved all V1 code to orig/ folder (preserved with git mv) - Created docs/planning/ directory - Added orig/README_V1.md explaining V1 preservation **Planning Documents:** - 00_V2_MASTER_PLAN.md: Complete architecture overview - Executive summary of critical V1 issues - High-level component architecture diagrams - 5-phase implementation roadmap - Success metrics and risk mitigation - 07_TASK_BREAKDOWN.md: Atomic task breakdown - 99+ hours of detailed tasks - Every task < 2 hours (atomic) - Clear dependencies and success criteria - Organized by implementation phase **V2 Key Improvements:** - Per-exchange parsers (factory pattern) - Multi-layer strict validation - Multi-index pool cache - Background validation pipeline - Comprehensive observability **Critical Issues Addressed:** - Zero address tokens (strict validation + cache enrichment) - Parsing accuracy (protocol-specific parsers) - No audit trail (background validation channel) - Inefficient lookups (multi-index cache) - Stats disconnection (event-driven metrics) Next Steps: 1. Review planning documents 2. Begin Phase 1: Foundation (P1-001 through P1-010) 3. Implement parsers in Phase 2 4. Build cache system in Phase 3 5. Add validation pipeline in Phase 4 6. Migrate and test in Phase 5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
244 lines
5.0 KiB
Go
244 lines
5.0 KiB
Go
package logger
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewLogger(t *testing.T) {
|
|
// Test creating a logger with stdout
|
|
logger := New("info", "text", "")
|
|
assert.NotNil(t, logger)
|
|
assert.NotNil(t, logger.logger)
|
|
assert.Equal(t, "info", logger.levelName)
|
|
}
|
|
|
|
func TestNewLoggerWithFile(t *testing.T) {
|
|
// Create a temporary file for testing
|
|
tmpFile, err := os.CreateTemp("", "logger_test_*.log")
|
|
assert.NoError(t, err)
|
|
defer os.Remove(tmpFile.Name())
|
|
err = tmpFile.Close()
|
|
assert.NoError(t, err)
|
|
|
|
// Test creating a logger with a file
|
|
logger := New("info", "text", tmpFile.Name())
|
|
assert.NotNil(t, logger)
|
|
assert.Equal(t, "info", logger.levelName)
|
|
}
|
|
|
|
func TestDebug(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with debug level
|
|
logger := New("debug", "text", "")
|
|
|
|
// Log a debug message
|
|
logger.Debug("test debug message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Check that the log message contains the expected content with brackets
|
|
assert.Contains(t, output, "[DEBUG] test debug message")
|
|
}
|
|
|
|
func TestDebugWithInfoLevel(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with info level (should not log debug messages)
|
|
logger := New("info", "text", "")
|
|
|
|
// Log a debug message
|
|
logger.Debug("test debug message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output does not contain the debug message
|
|
assert.NotContains(t, output, "DEBUG:")
|
|
assert.NotContains(t, output, "test debug message")
|
|
}
|
|
|
|
func TestInfo(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with info level
|
|
logger := New("info", "text", "")
|
|
|
|
// Log an info message
|
|
logger.Info("test info message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the info message
|
|
assert.Contains(t, output, "[INFO]")
|
|
assert.Contains(t, output, "test info message")
|
|
}
|
|
|
|
func TestInfoWithDebugLevel(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with debug level
|
|
logger := New("debug", "text", "")
|
|
|
|
// Log an info message
|
|
logger.Info("test info message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the info message
|
|
assert.Contains(t, output, "[INFO]")
|
|
assert.Contains(t, output, "test info message")
|
|
}
|
|
|
|
func TestWarn(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with warn level
|
|
logger := New("warn", "text", "")
|
|
|
|
// Log a warning message
|
|
logger.Warn("test warn message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the warning message
|
|
assert.Contains(t, output, "[WARN]")
|
|
assert.Contains(t, output, "test warn message")
|
|
}
|
|
|
|
func TestWarnWithInfoLevel(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with info level (should log warnings)
|
|
logger := New("info", "text", "")
|
|
|
|
// Log a warning message
|
|
logger.Warn("test warn message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the warning message
|
|
assert.Contains(t, output, "[WARN]")
|
|
assert.Contains(t, output, "test warn message")
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger
|
|
logger := New("error", "text", "")
|
|
|
|
// Log an error message
|
|
logger.Error("test error message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the error message
|
|
assert.Contains(t, output, "[ERROR]")
|
|
assert.Contains(t, output, "test error message")
|
|
}
|
|
|
|
func TestErrorWithAllLevels(t *testing.T) {
|
|
// Test that error messages are logged at all levels
|
|
levels := []string{"debug", "info", "warn", "error"}
|
|
for _, level := range levels {
|
|
// Capture stdout
|
|
old := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
// Create logger with current level
|
|
logger := New(level, "text", "")
|
|
|
|
// Log an error message
|
|
logger.Error("test error message")
|
|
|
|
// Restore stdout
|
|
w.Close()
|
|
os.Stdout = old
|
|
|
|
// Read the captured output
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, r)
|
|
output := buf.String()
|
|
|
|
// Verify the output contains the error message
|
|
assert.Contains(t, output, "[ERROR]")
|
|
assert.Contains(t, output, "test error message")
|
|
}
|
|
}
|