|
| 1 | +package telegram |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// FindCommand tests |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +func TestFindCommand_Exists(t *testing.T) { |
| 13 | + knownCommands := []string{"start", "help", "new", "stats", "stop", "mode"} |
| 14 | + |
| 15 | + for _, name := range knownCommands { |
| 16 | + t.Run(name, func(t *testing.T) { |
| 17 | + cmd := FindCommand(name) |
| 18 | + if cmd == nil { |
| 19 | + t.Fatalf("FindCommand(%q) returned nil, expected a descriptor", name) |
| 20 | + } |
| 21 | + if cmd.Command != name { |
| 22 | + t.Errorf("FindCommand(%q).Command = %q, want %q", name, cmd.Command, name) |
| 23 | + } |
| 24 | + if cmd.Handler == nil { |
| 25 | + t.Errorf("FindCommand(%q).Handler is nil, expected a function", name) |
| 26 | + } |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestFindCommand_Unknown(t *testing.T) { |
| 32 | + unknownNames := []string{"unknown", "foo", "delete", "", "restart"} |
| 33 | + |
| 34 | + for _, name := range unknownNames { |
| 35 | + t.Run(name, func(t *testing.T) { |
| 36 | + cmd := FindCommand(name) |
| 37 | + if cmd != nil { |
| 38 | + t.Errorf("FindCommand(%q) = %+v, want nil", name, cmd) |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// --------------------------------------------------------------------------- |
| 45 | +// CommandDescriptors tests |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | + |
| 48 | +func TestCommandDescriptors_Count(t *testing.T) { |
| 49 | + descs := CommandDescriptors() |
| 50 | + if got, want := len(descs), len(DefaultCommands); got != want { |
| 51 | + t.Errorf("CommandDescriptors() returned %d items, want %d", got, want) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestCommandDescriptors_Values(t *testing.T) { |
| 56 | + descs := CommandDescriptors() |
| 57 | + |
| 58 | + // Build a lookup from the result. |
| 59 | + got := make(map[string]string) |
| 60 | + for _, d := range descs { |
| 61 | + got[d.Command] = d.Description |
| 62 | + } |
| 63 | + |
| 64 | + // Verify against DefaultCommands. |
| 65 | + for _, expected := range DefaultCommands { |
| 66 | + desc, ok := got[expected.Command] |
| 67 | + if !ok { |
| 68 | + t.Errorf("CommandDescriptors() missing command %q", expected.Command) |
| 69 | + continue |
| 70 | + } |
| 71 | + if desc != expected.Description { |
| 72 | + t.Errorf("CommandDescriptors()[%q].Description = %q, want %q", |
| 73 | + expected.Command, desc, expected.Description) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// --------------------------------------------------------------------------- |
| 79 | +// Handler output content tests |
| 80 | +// --------------------------------------------------------------------------- |
| 81 | + |
| 82 | +func TestStartHandler_ContainsOdek(t *testing.T) { |
| 83 | + cmd := FindCommand("start") |
| 84 | + if cmd == nil { |
| 85 | + t.Fatal("start command not found") |
| 86 | + } |
| 87 | + got, err := cmd.Handler("") |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("start handler returned error: %v", err) |
| 90 | + } |
| 91 | + if !strings.Contains(got, "odek") { |
| 92 | + t.Errorf("start handler output does not contain 'odek':\n%s", got) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestHelpHandler_ContainsStart(t *testing.T) { |
| 97 | + cmd := FindCommand("help") |
| 98 | + if cmd == nil { |
| 99 | + t.Fatal("help command not found") |
| 100 | + } |
| 101 | + got, err := cmd.Handler("") |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("help handler returned error: %v", err) |
| 104 | + } |
| 105 | + if !strings.Contains(got, "/start") { |
| 106 | + t.Errorf("help handler output does not contain '/start':\n%s", got) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestNewHandler_ContainsReset(t *testing.T) { |
| 111 | + cmd := FindCommand("new") |
| 112 | + if cmd == nil { |
| 113 | + t.Fatal("new command not found") |
| 114 | + } |
| 115 | + got, err := cmd.Handler("") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("new handler returned error: %v", err) |
| 118 | + } |
| 119 | + if !strings.Contains(got, "reset") && !strings.Contains(got, "Reset") { |
| 120 | + t.Errorf("new handler output does not contain 'reset':\n%s", got) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestStatsHandler_ContainsStats(t *testing.T) { |
| 125 | + cmd := FindCommand("stats") |
| 126 | + if cmd == nil { |
| 127 | + t.Fatal("stats command not found") |
| 128 | + } |
| 129 | + got, err := cmd.Handler("") |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("stats handler returned error: %v", err) |
| 132 | + } |
| 133 | + if !strings.Contains(got, "Stats") && !strings.Contains(got, "stats") { |
| 134 | + t.Errorf("stats handler output does not contain 'Stats':\n%s", got) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestStopHandler_ContainsStop(t *testing.T) { |
| 139 | + cmd := FindCommand("stop") |
| 140 | + if cmd == nil { |
| 141 | + t.Fatal("stop command not found") |
| 142 | + } |
| 143 | + got, err := cmd.Handler("") |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("stop handler returned error: %v", err) |
| 146 | + } |
| 147 | + if !strings.Contains(got, "Stop") && !strings.Contains(got, "stop") { |
| 148 | + t.Errorf("stop handler output does not contain 'Stop':\n%s", got) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestModeHandler_ContainsMode(t *testing.T) { |
| 153 | + cmd := FindCommand("mode") |
| 154 | + if cmd == nil { |
| 155 | + t.Fatal("mode command not found") |
| 156 | + } |
| 157 | + got, err := cmd.Handler("") |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("mode handler returned error: %v", err) |
| 160 | + } |
| 161 | + if !strings.Contains(got, "Mode") && !strings.Contains(got, "mode") { |
| 162 | + t.Errorf("mode handler output does not contain 'Mode':\n%s", got) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// --------------------------------------------------------------------------- |
| 167 | +// All handler outputs are non-empty and error-free |
| 168 | +// --------------------------------------------------------------------------- |
| 169 | + |
| 170 | +func TestAllHandlers_ReturnNoError(t *testing.T) { |
| 171 | + for _, cmd := range DefaultCommands { |
| 172 | + t.Run(cmd.Command, func(t *testing.T) { |
| 173 | + got, err := cmd.Handler("") |
| 174 | + if err != nil { |
| 175 | + t.Errorf("%s handler returned error: %v", cmd.Command, err) |
| 176 | + } |
| 177 | + if got == "" { |
| 178 | + t.Errorf("%s handler returned empty string", cmd.Command) |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
0 commit comments