196 lines
4.7 KiB
Go
196 lines
4.7 KiB
Go
package jlog
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/d1nch8g/jules/chat"
|
|
"github.com/d1nch8g/jules/database"
|
|
"github.com/d1nch8g/jules/engine/actions"
|
|
"github.com/d1nch8g/jules/engine/prompt"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFromMessage(t *testing.T) {
|
|
msg := chat.Message{
|
|
Chat: "telegram",
|
|
ID: "123456789",
|
|
Text: "Hello, Jules!",
|
|
}
|
|
|
|
ctx := context.Background()
|
|
e := FromMessage(ctx, msg)
|
|
|
|
assert.Len(t, e.attrs, 4)
|
|
}
|
|
|
|
func TestFromNotification(t *testing.T) {
|
|
notif := database.Notification{
|
|
ID: uuid.New(),
|
|
UserID: uuid.New(),
|
|
InitiatorID: uuid.New(),
|
|
Content: "call mom",
|
|
}
|
|
|
|
ctx := context.Background()
|
|
e := FromNotification(ctx, notif)
|
|
|
|
assert.Len(t, e.attrs, 5)
|
|
}
|
|
|
|
func TestEvent_User(t *testing.T) {
|
|
user := &database.User{
|
|
ID: uuid.New(),
|
|
Language: "ru",
|
|
Timezone: "Europe/Moscow",
|
|
PreferredChat: "telegram",
|
|
Role: "free",
|
|
}
|
|
|
|
e := FromMessage(context.Background(), chat.Message{}).User(user)
|
|
|
|
assert.Len(t, e.attrs, 9)
|
|
}
|
|
|
|
func TestEvent_Context(t *testing.T) {
|
|
ctx := &prompt.Context{
|
|
UserLanguage: "ru",
|
|
UserTimezone: "Europe/Moscow",
|
|
UserPreferredChat: "telegram",
|
|
UserBindCode: uuid.New(),
|
|
UserContactCode: uuid.New(),
|
|
Chats: []database.Chat{
|
|
{Platform: "telegram"},
|
|
{Platform: "whatsapp"},
|
|
},
|
|
Facts: []database.Fact{
|
|
{Value: "fact1"},
|
|
{Value: "fact2"},
|
|
},
|
|
Contacts: []database.Contact{
|
|
{Name: "Mom"},
|
|
},
|
|
IncomingNotifications: []database.Notification{
|
|
{Content: "notif1"},
|
|
},
|
|
OutgoingNotificaions: []database.Notification{
|
|
{Content: "notif2"},
|
|
},
|
|
RecentActions: []database.Action{
|
|
{Payload: json.RawMessage(`{"type":"reply"}`)},
|
|
},
|
|
}
|
|
|
|
e := FromMessage(context.Background(), chat.Message{}).Context(ctx)
|
|
|
|
assert.Len(t, e.attrs, 17)
|
|
}
|
|
|
|
func TestEvent_Context_Nil(t *testing.T) {
|
|
e := FromMessage(context.Background(), chat.Message{}).Context(nil)
|
|
|
|
assert.Len(t, e.attrs, 4)
|
|
}
|
|
|
|
func TestEvent_Info(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
handler := slog.NewJSONHandler(&buf, nil)
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
FromMessage(context.Background(), chat.Message{Chat: "telegram", ID: "123", Text: "hi"}).
|
|
Info("test")
|
|
|
|
assert.Contains(t, buf.String(), "test")
|
|
assert.Contains(t, buf.String(), "duration")
|
|
}
|
|
|
|
func TestEvent_Warn(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
handler := slog.NewJSONHandler(&buf, nil)
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
FromMessage(context.Background(), chat.Message{Chat: "telegram", ID: "123", Text: "hi"}).
|
|
Warn("warning", errors.New("nani"))
|
|
|
|
assert.Contains(t, buf.String(), "warning")
|
|
assert.Contains(t, buf.String(), "duration")
|
|
assert.Contains(t, buf.String(), "nani")
|
|
}
|
|
|
|
func TestEvent_Error(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
handler := slog.NewJSONHandler(&buf, nil)
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
FromMessage(context.Background(), chat.Message{Chat: "telegram", ID: "123", Text: "hi"}).
|
|
Error("error", assert.AnError)
|
|
|
|
assert.Contains(t, buf.String(), "error")
|
|
assert.Contains(t, buf.String(), "duration")
|
|
}
|
|
|
|
func TestEvent_Log_NoContext(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
handler := slog.NewJSONHandler(&buf, nil)
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
e := FromMessage(context.Background(), chat.Message{Chat: "telegram", ID: "123", Text: "hi"})
|
|
e.ctx = nil
|
|
e.Info("test")
|
|
|
|
assert.Contains(t, buf.String(), "test")
|
|
}
|
|
|
|
func TestEntry_Duration(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
handler := slog.NewJSONHandler(&buf, nil)
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
FromMessage(context.Background(), chat.Message{Chat: "telegram", ID: "123", Text: "hi"}).
|
|
Info("test")
|
|
assert.Contains(t, buf.String(), "duration")
|
|
|
|
buf.Reset()
|
|
|
|
FromNotification(context.Background(), database.Notification{ID: uuid.New(), UserID: uuid.New(), InitiatorID: uuid.New(), Content: "test"}).
|
|
Info("test")
|
|
assert.Contains(t, buf.String(), "duration")
|
|
}
|
|
|
|
func TestEvent_LLMResponse(t *testing.T) {
|
|
e := FromMessage(context.Background(), chat.Message{})
|
|
|
|
e.LLMResponse("first")
|
|
assert.Len(t, e.attrs, 5) // 4 base + llm_response
|
|
assert.Equal(t, "llm_response", e.attrs[4].Key)
|
|
assert.Equal(t, "first", e.attrs[4].Value.String())
|
|
|
|
e.LLMResponse("second")
|
|
assert.Len(t, e.attrs, 5) // still 5, replaced
|
|
assert.Equal(t, "llm_response", e.attrs[4].Key)
|
|
assert.Equal(t, "second", e.attrs[4].Value.String())
|
|
}
|
|
|
|
func TestEvent_Actions(t *testing.T) {
|
|
e := FromMessage(context.Background(), chat.Message{})
|
|
|
|
actions := []any{
|
|
actions.AddFact{Type: "add_fact", Value: "test fact"},
|
|
actions.Message{Type: "message", Platform: "telegram", Text: "hello"},
|
|
}
|
|
|
|
e.Actions(actions)
|
|
|
|
assert.Len(t, e.attrs, 6) // 4 from message + 2 actions
|
|
}
|