2026-05-05 13:23:25 +08:00
|
|
|
package log_test
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-05 22:52:55 +08:00
|
|
|
"os"
|
2026-05-05 21:45:19 +08:00
|
|
|
"strings"
|
2026-05-05 13:23:25 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"apigo.cc/go/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-05 21:45:19 +08:00
|
|
|
func TestViewable(t *testing.T) {
|
|
|
|
|
entry := &log.InfoLog{
|
|
|
|
|
BaseLog: log.BaseLog{
|
|
|
|
|
LogName: "test-app",
|
|
|
|
|
LogType: "info",
|
2026-05-05 22:52:55 +08:00
|
|
|
LogTime: 1714896000000000000,
|
|
|
|
|
TraceId: "trace-123",
|
2026-05-05 21:45:19 +08:00
|
|
|
},
|
|
|
|
|
Info: "hello world",
|
|
|
|
|
}
|
|
|
|
|
log.RegisterType("info", entry)
|
|
|
|
|
|
2026-05-05 22:52:55 +08:00
|
|
|
line := string(log.ToArrayBytes(entry, nil))
|
2026-05-05 21:45:19 +08:00
|
|
|
out := log.Viewable(line)
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(out, "hello world") {
|
|
|
|
|
t.Errorf("expected 'hello world' in output, got: %s", out)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(out, "trace-123") {
|
|
|
|
|
t.Errorf("expected 'trace-123' in output, got: %s", out)
|
|
|
|
|
}
|
2026-05-05 22:52:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestToJSON(t *testing.T) {
|
|
|
|
|
entry := &log.InfoLog{
|
|
|
|
|
BaseLog: log.BaseLog{
|
|
|
|
|
LogName: "test-app",
|
|
|
|
|
LogType: "info",
|
|
|
|
|
LogTime: 1714896000000000000,
|
|
|
|
|
TraceId: "trace-123",
|
|
|
|
|
},
|
|
|
|
|
Info: "hello world",
|
2026-05-05 21:45:19 +08:00
|
|
|
}
|
2026-05-05 22:52:55 +08:00
|
|
|
entry.Extra = map[string]any{"key": "value"}
|
|
|
|
|
log.RegisterType("info", entry)
|
|
|
|
|
|
|
|
|
|
line := string(log.ToArrayBytes(entry, nil))
|
|
|
|
|
jsonStr := log.ToJSON(line)
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(jsonStr, `"Info":"hello world"`) {
|
|
|
|
|
t.Errorf("expected Info field in JSON, got: %s", jsonStr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(jsonStr, `"TraceId":"trace-123"`) {
|
|
|
|
|
t.Errorf("expected TraceId field in JSON, got: %s", jsonStr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(jsonStr, `"key":"value"`) {
|
|
|
|
|
t.Errorf("expected Extra fields merged in JSON, got: %s", jsonStr)
|
2026-05-05 13:23:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 22:52:55 +08:00
|
|
|
func TestLoadMeta(t *testing.T) {
|
|
|
|
|
// Create a temporary meta file
|
|
|
|
|
metaData := `{"test-type":[{"index":0,"name":"Field1"},{"index":1,"name":"Field2"}]}`
|
|
|
|
|
_ = os.WriteFile(".test_meta.json", []byte(metaData), 0644)
|
|
|
|
|
defer os.Remove(".test_meta.json")
|
|
|
|
|
|
|
|
|
|
err := log.LoadMeta(".test_meta.json")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to load meta: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
meta := log.GetMeta("test-type")
|
|
|
|
|
if len(meta) != 2 {
|
|
|
|
|
t.Errorf("expected 2 meta fields, got %d", len(meta))
|
|
|
|
|
}
|
|
|
|
|
if meta[0].Name != "Field1" {
|
|
|
|
|
t.Errorf("expected Field1, got %s", meta[0].Name)
|
2026-05-05 13:23:25 +08:00
|
|
|
}
|
|
|
|
|
}
|