|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package bot |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/FreshLabDev/tg" |
| 11 | + |
| 12 | + "github.com/FreshLabDev/maply/internal/round" |
| 13 | + "github.com/FreshLabDev/maply/internal/schedule" |
| 14 | +) |
| 15 | + |
| 16 | +type fakeRefresher struct { |
| 17 | + r schedule.Refresh |
| 18 | + calls []round.Date |
| 19 | +} |
| 20 | + |
| 21 | +func (f *fakeRefresher) RefreshWorld(_ context.Context, _, _ int64, d round.Date) (schedule.Refresh, error) { |
| 22 | + f.calls = append(f.calls, d) |
| 23 | + return f.r, nil |
| 24 | +} |
| 25 | + |
| 26 | +// Refresh works in the person's own DM only and says what it does. |
| 27 | +func TestRefreshWorld(t *testing.T) { |
| 28 | + ctx := context.Background() |
| 29 | + d := round.NewDate(2026, 11, 2) |
| 30 | + press := func(chat tg.Chat, from int64) *tg.CallbackQuery { |
| 31 | + return &tg.CallbackQuery{ID: "q", From: tg.User{ID: from, LanguageCode: "en"}, Data: "wr:" + d.String(), |
| 32 | + Message: tg.Message{MessageID: 5, Chat: chat}} |
| 33 | + } |
| 34 | + for _, c := range []struct { |
| 35 | + name string |
| 36 | + r schedule.Refresh |
| 37 | + cq *tg.CallbackQuery |
| 38 | + calls int |
| 39 | + toast string |
| 40 | + }{ |
| 41 | + {"drawing", schedule.RefreshDrawing, press(tg.Chat{ID: 9, Type: "private"}, 9), 1, "Drawing the map with the new answers, this takes up to a minute."}, |
| 42 | + {"nothing new", schedule.RefreshSame, press(tg.Chat{ID: 9, Type: "private"}, 9), 1, "No new answers yet."}, |
| 43 | + {"final", schedule.RefreshFinal, press(tg.Chat{ID: 9, Type: "private"}, 9), 1, "The day's ranking is final."}, |
| 44 | + {"gone", schedule.RefreshGone, press(tg.Chat{ID: 9, Type: "private"}, 9), 1, ""}, |
| 45 | + {"in a group", schedule.RefreshDrawing, press(tg.Chat{ID: -100, Type: "supergroup"}, 9), 0, ""}, |
| 46 | + } { |
| 47 | + t.Run(c.name, func(t *testing.T) { |
| 48 | + client := &fakeTelegram{} |
| 49 | + world := &fakeRefresher{r: c.r} |
| 50 | + b := New(newStore(), client, slog.New(slog.NewTextHandler(io.Discard, nil)), Options{World: world}) |
| 51 | + if err := b.onCallback(ctx, c.cq); err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if len(world.calls) != c.calls || (c.calls > 0 && world.calls[0] != d) { |
| 55 | + t.Errorf("refreshes = %v", world.calls) |
| 56 | + } |
| 57 | + if len(client.toasts) != 1 || client.toasts[0] != c.toast { |
| 58 | + t.Errorf("toasts = %q, want %q", client.toasts, c.toast) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments