package main import ( "strings" "testing" "github.com/go-telegram/bot/models" "github.com/samcharles93/archipelago/sdk/channel" ) func TestParseSettings(t *testing.T) { for _, tc := range []struct { name string cfg channel.Config wantErr string check func(t *testing.T, s settings) }{ { name: "token and allowlist", cfg: channel.Config{ Settings: map[string]string{"allowed_user_ids": " 111, 222 ,,333 "}, Secrets: map[string]string{"token": "123:ABC"}, }, check: func(t *testing.T, s settings) { if s.token != "123:ABC" { t.Errorf("token = %q", s.token) } if len(s.allowed) != 3 || s.allowed[0] != 111 || s.allowed[2] != 333 { t.Errorf("allowed = %v", s.allowed) } if s.showTools { t.Error("showTools defaulted true") } }, }, { name: "show_tool_calls on", cfg: channel.Config{ Settings: map[string]string{"allowed_user_ids": "1", "show_tool_calls": "true"}, Secrets: map[string]string{"token": "t"}, }, check: func(t *testing.T, s settings) { if !s.showTools { t.Error("showTools = false, want true") } }, }, {name: "missing token", cfg: channel.Config{Settings: map[string]string{"allowed_user_ids": "1"}}, wantErr: "token_ref"}, {name: "missing allowlist", cfg: channel.Config{Secrets: map[string]string{"token": "t"}}, wantErr: "allowed_user_ids"}, {name: "empty allowlist", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": " ,"}}, wantErr: "allowed_user_ids"}, {name: "bad user id", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": "abc"}}, wantErr: "not a positive"}, {name: "negative user id", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": "-5"}}, wantErr: "not a positive"}, } { t.Run(tc.name, func(t *testing.T) { s, err := parseSettings(tc.cfg) if tc.wantErr != "" { if err == nil || !strings.Contains(err.Error(), tc.wantErr) { t.Fatalf("err = %v, want it to mention %q", err, tc.wantErr) } return } if err != nil { t.Fatalf("parseSettings: %v", err) } if tc.check != nil { tc.check(t, s) } }) } } func TestConversationKey(t *testing.T) { for _, tc := range []struct { chatID, thread int64 want string }{ {123, 0, "123"}, {123, 7, "123/7"}, {-100, 42, "-100/42"}, } { msg := &models.Message{Chat: models.Chat{ID: tc.chatID}, MessageThreadID: int(tc.thread)} if got := conversationKey(msg); got != tc.want { t.Errorf("conversationKey(%d, %d) = %q, want %q", tc.chatID, tc.thread, got, tc.want) } } } func TestInboundMessage(t *testing.T) { msg := &models.Message{ ID: 42, Chat: models.Chat{ID: -100}, MessageThreadID: 7, From: &models.User{ID: 111, Username: "sam"}, Text: "hello", Date: 1700000000, } m := inboundMessage(msg, "hello", nil) if m.GetMessageId() != "42" || m.GetSourceId() != "42" { t.Errorf("ids = %q/%q, want both 42", m.GetMessageId(), m.GetSourceId()) } if m.GetChannelId() != "-100" || m.GetThreadId() != "7" { t.Errorf("channel/thread = %q/%q", m.GetChannelId(), m.GetThreadId()) } if m.GetFrom() != "sam" || m.GetSenderId() != "111" || m.GetPlatform() != "telegram" { t.Errorf("from/sender/platform = %q/%q/%q", m.GetFrom(), m.GetSenderId(), m.GetPlatform()) } if m.GetAt() == nil || m.GetAt().GetSeconds() != 1700000000 { t.Errorf("at = %v", m.GetAt()) } }