package chatbridge import ( "context" "strings" "testing" "time" ) var testNow = time.Date(2026, 11, 21, 12, 0, 0, 0, time.UTC) func testRouter() *Router { return &Router{ BotUserName: "artex_bot", Now: func() time.Time { return testNow }, Commands: []Command{{ Name: "echo", Usage: "", Summary: "repeat the text", Run: func(_ context.Context, req Request) string { return "echo:" + req.Args + ":" + req.UserName }, }}, } } func msg(chat, text string) Update { return Update{ID: 1, ChatID: chat, ChatType: ChatTypePrivate, UserID: "7", UserName: "op", Text: text, Date: testNow.Add(-time.Second)} } func TestRouteAllowedCommand(t *testing.T) { r := testRouter() d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/echo hello world ")) if d != DecisionHandled || reply != "echo:hello world:op" { t.Fatalf("got %v %q", d, reply) } } func TestRouteCommandAddressedToThisOrOtherBot(t *testing.T) { r := testRouter() if d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/ECHO@Artex_Bot x")); d != DecisionHandled || !strings.HasPrefix(reply, "echo:x") { t.Fatalf("own bot: got %v %q", d, reply) } if d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/echo@other_bot x")); d != DecisionIgnored || reply != "" { t.Fatalf("other bot: got %v %q", d, reply) } } func TestRouteUnknownChatRefusedOnceThenMuted(t *testing.T) { r := testRouter() d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("555", "/echo hi")) if d != DecisionRefused || !strings.Contains(reply, "555") { t.Fatalf("first contact: got %v %q", d, reply) } if strings.Contains(reply, "echo") { t.Fatalf("refusal must not run the command: %q", reply) } for i := 0; i < 3; i++ { if d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("555", "hello?")); d != DecisionMuted || reply != "" { t.Fatalf("repeat %d: got %v %q", i, d, reply) } } } func TestRouteEmptyAllowlistRefusesEveryone(t *testing.T) { r := testRouter() if d, _ := r.Route(context.Background(), KindTelegram, Access{}, msg("100", "/help")); d != DecisionRefused { t.Fatalf("got %v, want refused", d) } } func TestRouteRefusalMemoryIsBounded(t *testing.T) { r := testRouter() for i := 0; i < maxRefusedChats; i++ { r.Route(context.Background(), KindTelegram, Access{}, msg("c"+itoa(i), "hi")) } // Past the cap the memory starts over: a new chat (it may be the // operator's own) still gets its refusal with the chat ID. if d, reply := r.Route(context.Background(), KindTelegram, Access{}, msg("brand-new", "hi")); d != DecisionRefused || !strings.Contains(reply, "brand-new") { t.Fatalf("over the cap: got %v %q", d, reply) } if len(r.refused) > maxRefusedChats { t.Fatalf("refused set grew to %d", len(r.refused)) } if d, _ := r.Route(context.Background(), KindTelegram, Access{}, msg("brand-new", "again")); d != DecisionMuted { t.Fatalf("after the reset: got %v, want muted", d) } } func TestRouterKeepRefusals(t *testing.T) { prev := testRouter() prev.Route(context.Background(), KindTelegram, Access{}, msg("555", "hi")) next := testRouter() next.KeepRefusals(prev) if d, reply := next.Route(context.Background(), KindTelegram, Access{}, msg("555", "hi")); d != DecisionMuted || reply != "" { t.Fatalf("carried refusal: got %v %q", d, reply) } if d, _ := next.Route(context.Background(), KindTelegram, Access{}, msg("556", "hi")); d != DecisionRefused { t.Fatalf("new chat: got %v", d) } if d, _ := prev.Route(context.Background(), KindTelegram, Access{}, msg("556", "hi")); d != DecisionRefused { t.Fatalf("the previous router must not share the new memory: got %v", d) } } func TestRouteDropsStaleAndBotAndEmpty(t *testing.T) { r := testRouter() old := msg("100", "/echo late") old.Date = testNow.Add(-MaxUpdateAge - time.Second) if d, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, old); d != DecisionStale || reply != "" { t.Fatalf("stale: got %v %q", d, reply) } // A stale message from an unknown chat is dropped before the refusal, so // a backlog never produces a burst of refusals. old.ChatID = "999" if d, _ := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, old); d != DecisionStale { t.Fatalf("stale unknown chat: got %v", d) } b := msg("100", "/echo x") b.FromBot = true if d, _ := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, b); d != DecisionIgnored { t.Fatalf("bot sender: got %v", d) } if d, _ := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", " ")); d != DecisionIgnored { t.Fatalf("empty text: got %v", d) } } func TestRouteHelpAndUnknownAndPlainText(t *testing.T) { r := testRouter() _, help := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/start")) if !strings.Contains(help, "/echo - repeat the text") || !strings.Contains(help, "/help") { t.Fatalf("help = %q", help) } if _, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/nope")); !strings.Contains(reply, "Unknown command /nope") { t.Fatalf("unknown = %q", reply) } if _, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "status please")); !strings.Contains(reply, "/help") { t.Fatalf("plain text without handler = %q", reply) } r.Text = func(_ context.Context, req Request) string { return "text:" + req.Args } if _, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "status please")); reply != "text:status please" { t.Fatalf("plain text handler = %q", reply) } if _, reply := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/")); reply != "text:/" { t.Fatalf("bare slash = %q", reply) } } func TestNormalizeAllowedChats(t *testing.T) { tr, _ := Get(KindTelegram) got, err := NormalizeAllowedChats(tr, []string{" 123 ", "", "-1001234567890", "123"}) if err != nil || strings.Join(got, ",") != "123,-1001234567890" { t.Fatalf("got %v %v", got, err) } if _, err := NormalizeAllowedChats(tr, []string{"@channel"}); err == nil { t.Fatal("a username must be refused: updates carry numeric ids") } many := make([]string, MaxAllowedChats+1) for i := range many { many[i] = itoa(i + 1) } if _, err := NormalizeAllowedChats(tr, many); err == nil { t.Fatal("over the cap must be refused") } } func itoa(i int) string { if i == 0 { return "0" } var b []byte for ; i > 0; i /= 10 { b = append([]byte{byte('0' + i%10)}, b...) } return string(b) } func TestRouteGroupServesOnlyAllowedUsers(t *testing.T) { r := testRouter() acc := Access{Chats: []string{"-100"}, Users: []string{"7"}} group := func(user, text string) Update { u := msg("-100", text) u.ChatType, u.UserID = "supergroup", user return u } if d, reply := r.Route(context.Background(), KindTelegram, acc, group("7", "/echo hi")); d != DecisionHandled || !strings.HasPrefix(reply, "echo:hi") { t.Fatalf("allowed user in allowed group: got %v %q", d, reply) } // Another member of the same allowed group is refused once, by user ID, // then muted. d, reply := r.Route(context.Background(), KindTelegram, acc, group("9", "/echo hi")) if d != DecisionRefused || !strings.Contains(reply, "user ID 9") { t.Fatalf("other member: got %v %q", d, reply) } if d, reply := r.Route(context.Background(), KindTelegram, acc, group("9", "/echo again")); d != DecisionMuted || reply != "" { t.Fatalf("other member again: got %v %q", d, reply) } // The allowed user is still served after that. if d, _ := r.Route(context.Background(), KindTelegram, acc, group("7", "/echo x")); d != DecisionHandled { t.Fatalf("allowed user after a refusal: got %v", d) } // With no allowed users a group serves nobody. if d, _ := testRouter().Route(context.Background(), KindTelegram, Access{Chats: []string{"-100"}}, group("7", "/echo hi")); d != DecisionRefused { t.Fatalf("group with empty user list: got %v, want refused", d) } // A message with no sender (an anonymous admin) is ignored. if d, reply := r.Route(context.Background(), KindTelegram, acc, group("", "/echo hi")); d != DecisionIgnored || reply != "" { t.Fatalf("anonymous sender: got %v %q", d, reply) } // An unknown chat type is treated as shared, not as private. u := msg("-100", "/echo hi") u.ChatType, u.UserID = "", "9" if d, _ := testRouter().Route(context.Background(), KindTelegram, Access{Chats: []string{"-100"}}, u); d != DecisionRefused { t.Fatalf("unknown chat type: got %v, want refused", d) } // A private chat needs no user entry. if d, _ := r.Route(context.Background(), KindTelegram, Access{Chats: []string{"100"}}, msg("100", "/echo hi")); d != DecisionHandled { t.Fatalf("private chat: got %v", d) } } func TestRouteCallbackAccess(t *testing.T) { var got Request r := testRouter() r.Callback = func(_ context.Context, req Request) CallbackReply { got = req return CallbackReply{Toast: "done", Edit: "edited"} } press := func(chat, chatType, user string) Update { return Update{ID: 9, ChatID: chat, ChatType: chatType, UserID: user, CallbackID: "cb1", CallbackData: "ap:x:a", MessageID: "55"} } acc := Access{Chats: []string{"100", "-100"}, Users: []string{"7"}} d, reply := r.RouteCallback(context.Background(), KindTelegram, acc, press("100", ChatTypePrivate, "7")) if d != DecisionHandled || reply.Toast != "done" || reply.Edit != "edited" || got.Args != "ap:x:a" || got.MessageID != "55" || got.ChatType != ChatTypePrivate { t.Fatalf("allowed press: %v %+v, request %+v", d, reply, got) } // Unknown chat, and an unlisted member of an allowed group: a notice, // and the handler does not run. got = Request{} if d, reply := r.RouteCallback(context.Background(), KindTelegram, acc, press("999", ChatTypePrivate, "999")); d != DecisionRefused || reply.Toast == "" || reply.Edit != "" || got.Args != "" { t.Fatalf("unknown chat: %v %+v", d, reply) } if d, reply := r.RouteCallback(context.Background(), KindTelegram, acc, press("-100", "supergroup", "9")); d != DecisionRefused || !strings.Contains(reply.Toast, "9") || got.Args != "" { t.Fatalf("unlisted group member: %v %+v", d, reply) } if d, _ := r.RouteCallback(context.Background(), KindTelegram, acc, press("-100", "supergroup", "7")); d != DecisionHandled { t.Fatalf("listed group member: %v", d) } // A press from a bot, or no handler. b := press("100", ChatTypePrivate, "7") b.FromBot = true if d, _ := r.RouteCallback(context.Background(), KindTelegram, acc, b); d != DecisionIgnored { t.Fatalf("bot press: %v", d) } if d, reply := testRouter().RouteCallback(context.Background(), KindTelegram, acc, press("100", ChatTypePrivate, "7")); d != DecisionIgnored || reply.Toast == "" { t.Fatalf("no handler: %v %+v", d, reply) } // A press is not a message: Route ignores it. if d, _ := r.Route(context.Background(), KindTelegram, acc, press("100", ChatTypePrivate, "7")); d != DecisionIgnored { t.Fatalf("Route on a press: %v", d) } }