refactored main engine to have a map of chats - platform to be used as a key for ease of notification sending

This commit is contained in:
d1nch8g
2026-04-18 22:29:27 +03:00
parent 86e8860316
commit 03abd9fd89
3 changed files with 37 additions and 18 deletions
+6 -3
View File
@@ -32,7 +32,7 @@ type Parameters struct {
Database database.Database
LLM llm.LLM
Searcher search.Searcher
Chats []chat.Chat
Chats map[string]chat.Chat
}
type Engine struct {
@@ -67,9 +67,12 @@ func New(params *Parameters) (*Engine, error) {
return nil, errors.New("chats can't be empty")
}
for i, chat := range params.Chats {
for platform, chat := range params.Chats {
if platform == "" {
return nil, fmt.Errorf("platform name can't be empty")
}
if chat == nil {
return nil, fmt.Errorf("chat %d initialized as nil", i)
return nil, fmt.Errorf("%s initialized as nil", platform)
}
}
+26 -15
View File
@@ -35,7 +35,7 @@ func TestNew(t *testing.T) {
params: &Parameters{
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: true,
errMsg: "database can't be nil",
@@ -45,7 +45,7 @@ func TestNew(t *testing.T) {
params: &Parameters{
Database: &mockDB{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: true,
errMsg: "llm can't be nil",
@@ -55,7 +55,7 @@ func TestNew(t *testing.T) {
params: &Parameters{
Database: &mockDB{},
LLM: &mockLLM{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: true,
errMsg: "seach engine can't be nil",
@@ -66,7 +66,7 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{},
Chats: map[string]chat.Chat{},
},
wantErr: true,
errMsg: "chats can't be empty",
@@ -77,10 +77,10 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{nil},
Chats: map[string]chat.Chat{"telegram": nil},
},
wantErr: true,
errMsg: "chat 0 initialized as nil",
errMsg: "telegram initialized as nil",
},
{
name: "multiple chats with one nil",
@@ -88,10 +88,21 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}, nil, &mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}, "viber": nil, "whatsapp": &mockChat{}},
},
wantErr: true,
errMsg: "chat 1 initialized as nil",
errMsg: "viber initialized as nil",
},
{
name: "platform chat name empty",
params: &Parameters{
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: map[string]chat.Chat{"": &mockChat{}},
},
wantErr: true,
errMsg: "platform name can't be empty",
},
{
name: "valid params with defaults",
@@ -99,7 +110,7 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: false,
},
@@ -110,7 +121,7 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: false,
},
@@ -121,7 +132,7 @@ func TestNew(t *testing.T) {
Database: &mockDB{},
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{}},
Chats: map[string]chat.Chat{"telegram": &mockChat{}},
},
wantErr: false,
},
@@ -214,7 +225,7 @@ func TestRun(t *testing.T) {
Database: db,
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{receiveCh: ch}},
Chats: map[string]chat.Chat{"telegram": &mockChat{receiveCh: ch}},
DatabasePollingDuration: 10 * time.Millisecond,
NotificationBatchSize: 10,
},
@@ -251,7 +262,7 @@ func TestRun(t *testing.T) {
Database: db,
LLM: &mockLLM{},
Searcher: &mockSearcher{},
Chats: []chat.Chat{&mockChat{receiveCh: ch}},
Chats: map[string]chat.Chat{"telegram": &mockChat{receiveCh: ch}},
DatabasePollingDuration: 10 * time.Millisecond,
NotificationBatchSize: 10,
},
@@ -479,7 +490,7 @@ func TestConsumeChatMessages_ContextDoneDuringSend(t *testing.T) {
e := &Engine{
Parameters: &Parameters{
Chats: []chat.Chat{&mockChat{receiveCh: ch}},
Chats: map[string]chat.Chat{"telegram": &mockChat{receiveCh: ch}},
},
}
@@ -532,7 +543,7 @@ func TestConsumeChatMessages_ContextDone(t *testing.T) {
e := &Engine{
Parameters: &Parameters{
Chats: []chat.Chat{&mockChat{receiveCh: ch}},
Chats: map[string]chat.Chat{"telegram": &mockChat{receiveCh: ch}},
},
}
+5
View File
@@ -8,7 +8,12 @@ import (
)
func (e *Engine) defaultProcessMessage(ctx context.Context, msg chat.Message) {
// uuid, err := e.Database.Chats().GetUserID(ctx, msg.Source, msg.ID)
// if err != nil {
// if errors.Is(err, database.ErrNotFound) {
// }
// }
}
func (e *Engine) defaultProcessNotification(ctx context.Context, notif database.Notification) {