| 1 |
package generators |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/generators/basic" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/generators/xoauth2" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 |
) |
| 16 |
|
| 17 |
// TestWithInput tests the WithInput option function to ensure it properly |
| 18 |
// sets the input reader in generatorConfig. |
| 19 |
func TestWithInput(t *testing.T) { |
| 20 |
t.Parallel() |
| 21 |
|
| 22 |
type args struct { |
| 23 |
reader io.Reader |
| 24 |
} |
| 25 |
|
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
args args |
| 29 |
verify func(t *testing.T, config *generatorConfig) |
| 30 |
}{ |
| 31 |
{ |
| 32 |
name: "sets input reader correctly", |
| 33 |
args: args{ |
| 34 |
reader: strings.NewReader("test input"), |
| 35 |
}, |
| 36 |
verify: func(t *testing.T, config *generatorConfig) { |
| 37 |
t.Helper() |
| 38 |
assert.NotNil(t, config.input, "input should be set") |
| 39 |
|
| 40 |
// Verify the reader works by reading from it |
| 41 |
buf := make([]byte, 10) |
| 42 |
n, err := config.input.Read(buf) |
| 43 |
require.NoError(t, err, "should be able to read from input") |
| 44 |
assert.Equal(t, 10, n, "should read all bytes") |
| 45 |
assert.Equal(t, "test input", string(buf), "content should match") |
| 46 |
}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "sets nil reader", |
| 50 |
args: args{ |
| 51 |
reader: nil, |
| 52 |
}, |
| 53 |
verify: func(t *testing.T, config *generatorConfig) { |
| 54 |
t.Helper() |
| 55 |
assert.Nil(t, config.input, "input should be nil") |
| 56 |
}, |
| 57 |
}, |
| 58 |
} |
| 59 |
|
| 60 |
for _, tt := range tests { |
| 61 |
t.Run(tt.name, func(t *testing.T) { |
| 62 |
t.Parallel() |
| 63 |
|
| 64 |
// Create a config and apply the option |
| 65 |
config := &generatorConfig{} |
| 66 |
option := WithInput(tt.args.reader) |
| 67 |
option(config) |
| 68 |
|
| 69 |
// Verify the config |
| 70 |
tt.verify(t, config) |
| 71 |
}) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// TestListGenerators tests that ListGenerators returns the expected list |
| 76 |
// of available generators. |
| 77 |
func TestListGenerators(t *testing.T) { |
| 78 |
t.Parallel() |
| 79 |
|
| 80 |
tests := []struct { |
| 81 |
name string |
| 82 |
wantContain []string |
| 83 |
wantLen int |
| 84 |
}{ |
| 85 |
{ |
| 86 |
name: "returns non-empty list with expected generators", |
| 87 |
wantContain: []string{"basic", "oauth2", "telegram"}, |
| 88 |
wantLen: 3, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tt := range tests { |
| 93 |
t.Run(tt.name, func(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
|
| 96 |
got := ListGenerators() |
| 97 |
|
| 98 |
// Verify the list is not empty and has expected length |
| 99 |
require.NotEmpty(t, got, "ListGenerators() should return non-empty list") |
| 100 |
assert.Len(t, got, tt.wantLen, "ListGenerators() should return %d generators", tt.wantLen) |
| 101 |
|
| 102 |
// Verify it contains expected generators |
| 103 |
for _, want := range tt.wantContain { |
| 104 |
assert.Contains(t, got, want, "ListGenerators() should contain %q", want) |
| 105 |
} |
| 106 |
}) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// TestNewGenerator tests the NewGenerator function with various scenarios |
| 111 |
// including success cases and error cases. |
| 112 |
func TestNewGenerator(t *testing.T) { |
| 113 |
t.Parallel() |
| 114 |
|
| 115 |
type args struct { |
| 116 |
identifier string |
| 117 |
opts []GeneratorOption |
| 118 |
} |
| 119 |
|
| 120 |
tests := []struct { |
| 121 |
name string |
| 122 |
args args |
| 123 |
wantErr bool |
| 124 |
wantErrIs error |
| 125 |
verifyType func(t *testing.T, g types.Generator) |
| 126 |
verifyInputSet bool |
| 127 |
}{ |
| 128 |
{ |
| 129 |
name: "successful creation of basic generator without options", |
| 130 |
args: args{ |
| 131 |
identifier: "basic", |
| 132 |
opts: nil, |
| 133 |
}, |
| 134 |
wantErr: false, |
| 135 |
verifyType: func(t *testing.T, g types.Generator) { |
| 136 |
t.Helper() |
| 137 |
|
| 138 |
bg, ok := g.(*basic.Generator) |
| 139 |
require.True(t, ok, "generator should be *basic.Generator") |
| 140 |
assert.Nil(t, bg.Input, "Input should be nil when no WithInput option") |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "successful creation of basic generator with WithInput option", |
| 145 |
args: args{ |
| 146 |
identifier: "basic", |
| 147 |
opts: []GeneratorOption{ |
| 148 |
WithInput(strings.NewReader("test data")), |
| 149 |
}, |
| 150 |
}, |
| 151 |
wantErr: false, |
| 152 |
verifyInputSet: true, |
| 153 |
verifyType: func(t *testing.T, g types.Generator) { |
| 154 |
t.Helper() |
| 155 |
|
| 156 |
bg, ok := g.(*basic.Generator) |
| 157 |
require.True(t, ok, "generator should be *basic.Generator") |
| 158 |
assert.NotNil(t, bg.Input, "Input should be set") |
| 159 |
}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "successful creation of oauth2 generator", |
| 163 |
args: args{ |
| 164 |
identifier: "oauth2", |
| 165 |
opts: nil, |
| 166 |
}, |
| 167 |
wantErr: false, |
| 168 |
verifyType: func(t *testing.T, g types.Generator) { |
| 169 |
t.Helper() |
| 170 |
|
| 171 |
_, ok := g.(*xoauth2.Generator) |
| 172 |
assert.True(t, ok, "generator should be *xouath2.Generator") |
| 173 |
}, |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "successful creation of telegram generator without options", |
| 177 |
args: args{ |
| 178 |
identifier: "telegram", |
| 179 |
opts: nil, |
| 180 |
}, |
| 181 |
wantErr: false, |
| 182 |
verifyType: func(t *testing.T, g types.Generator) { |
| 183 |
t.Helper() |
| 184 |
|
| 185 |
tg, ok := g.(*telegram.Generator) |
| 186 |
require.True(t, ok, "generator should be *telegram.Generator") |
| 187 |
assert.Nil(t, tg.Reader, "Reader should be nil when no WithInput option") |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "successful creation of telegram generator with WithInput option", |
| 192 |
args: args{ |
| 193 |
identifier: "telegram", |
| 194 |
opts: []GeneratorOption{ |
| 195 |
WithInput(strings.NewReader("telegram test")), |
| 196 |
}, |
| 197 |
}, |
| 198 |
wantErr: false, |
| 199 |
verifyType: func(t *testing.T, g types.Generator) { |
| 200 |
t.Helper() |
| 201 |
|
| 202 |
tg, ok := g.(*telegram.Generator) |
| 203 |
require.True(t, ok, "generator should be *telegram.Generator") |
| 204 |
assert.NotNil(t, tg.Reader, "Reader should be set") |
| 205 |
}, |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "error on unknown generator", |
| 209 |
args: args{ |
| 210 |
identifier: "unknown", |
| 211 |
opts: nil, |
| 212 |
}, |
| 213 |
wantErr: true, |
| 214 |
wantErrIs: ErrUnknownGenerator, |
| 215 |
verifyType: func(t *testing.T, g types.Generator) { |
| 216 |
t.Helper() |
| 217 |
assert.Nil(t, g, "generator should be nil on error") |
| 218 |
}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "error on empty identifier", |
| 222 |
args: args{ |
| 223 |
identifier: "", |
| 224 |
opts: nil, |
| 225 |
}, |
| 226 |
wantErr: true, |
| 227 |
wantErrIs: ErrUnknownGenerator, |
| 228 |
verifyType: func(t *testing.T, g types.Generator) { |
| 229 |
t.Helper() |
| 230 |
assert.Nil(t, g, "generator should be nil on error") |
| 231 |
}, |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "case insensitive - uppercase BASIC", |
| 235 |
args: args{ |
| 236 |
identifier: "BASIC", |
| 237 |
opts: nil, |
| 238 |
}, |
| 239 |
wantErr: false, |
| 240 |
verifyType: func(t *testing.T, g types.Generator) { |
| 241 |
t.Helper() |
| 242 |
|
| 243 |
_, ok := g.(*basic.Generator) |
| 244 |
assert.True(t, ok, "generator should be *basic.Generator (case insensitive)") |
| 245 |
}, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "case insensitive - mixed case Oauth2", |
| 249 |
args: args{ |
| 250 |
identifier: "Oauth2", |
| 251 |
opts: nil, |
| 252 |
}, |
| 253 |
wantErr: false, |
| 254 |
verifyType: func(t *testing.T, g types.Generator) { |
| 255 |
t.Helper() |
| 256 |
|
| 257 |
_, ok := g.(*xoauth2.Generator) |
| 258 |
assert.True(t, ok, "generator should be *xouath2.Generator (case insensitive)") |
| 259 |
}, |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "multiple options - only last WithInput wins", |
| 263 |
args: args{ |
| 264 |
identifier: "basic", |
| 265 |
opts: []GeneratorOption{ |
| 266 |
WithInput(strings.NewReader("first")), |
| 267 |
WithInput(strings.NewReader("second")), |
| 268 |
}, |
| 269 |
}, |
| 270 |
wantErr: false, |
| 271 |
verifyType: func(t *testing.T, g types.Generator) { |
| 272 |
t.Helper() |
| 273 |
|
| 274 |
bg, ok := g.(*basic.Generator) |
| 275 |
require.True(t, ok, "generator should be *basic.Generator") |
| 276 |
assert.NotNil(t, bg.Input, "Input should be set with the last option") |
| 277 |
|
| 278 |
// Note: We verify the Input is set but don't read from it, |
| 279 |
// as strings.Reader is single-use and would cause EOF error |
| 280 |
// if the generator already consumed it. |
| 281 |
}, |
| 282 |
}, |
| 283 |
} |
| 284 |
|
| 285 |
for _, tt := range tests { |
| 286 |
t.Run(tt.name, func(t *testing.T) { |
| 287 |
t.Parallel() |
| 288 |
|
| 289 |
got, err := NewGenerator(tt.args.identifier, tt.args.opts...) |
| 290 |
|
| 291 |
if tt.wantErr { |
| 292 |
require.Error(t, err, "NewGenerator() should return an error") |
| 293 |
|
| 294 |
if tt.wantErrIs != nil { |
| 295 |
require.ErrorIs(t, err, tt.wantErrIs, "error should wrap %v", tt.wantErrIs) |
| 296 |
} |
| 297 |
} else { |
| 298 |
require.NoError(t, err, "NewGenerator() should not return an error") |
| 299 |
require.NotNil(t, got, "NewGenerator() should return a non-nil generator") |
| 300 |
|
| 301 |
// Verify it implements the types.Generator interface |
| 302 |
_ = got |
| 303 |
|
| 304 |
// Run type-specific verification |
| 305 |
if tt.verifyType != nil { |
| 306 |
tt.verifyType(t, got) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
// Run common verification |
| 311 |
if tt.verifyType != nil { |
| 312 |
tt.verifyType(t, got) |
| 313 |
} |
| 314 |
}) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// TestNewGenerator_VerifyInterface ensures all created generators implement |
| 319 |
// the types.Generator interface properly. |
| 320 |
func TestNewGenerator_VerifyInterface(t *testing.T) { |
| 321 |
t.Parallel() |
| 322 |
|
| 323 |
generators := []string{"basic", "oauth2", "telegram"} |
| 324 |
|
| 325 |
for _, gen := range generators { |
| 326 |
t.Run(gen, func(t *testing.T) { |
| 327 |
t.Parallel() |
| 328 |
|
| 329 |
g, err := NewGenerator(gen) |
| 330 |
require.NoError(t, err, "should create %s generator", gen) |
| 331 |
require.NotNil(t, g, "generator should not be nil") |
| 332 |
|
| 333 |
// Verify interface implementation |
| 334 |
assert.Implements(t, (*types.Generator)(nil), g, "generator should implement types.Generator") |
| 335 |
|
| 336 |
// Verify the Generate method exists and has correct signature |
| 337 |
// by checking it can be called (with nil args for this test) |
| 338 |
assert.NotPanics(t, func() { |
| 339 |
// We just verify the method exists, not the actual functionality |
| 340 |
_ = g.Generate |
| 341 |
}, "generator should have Generate method") |
| 342 |
}) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// TestErrUnknownGenerator checks that ErrUnknownGenerator is properly defined |
| 347 |
// and can be used with errors.Is. |
| 348 |
func TestErrUnknownGenerator(t *testing.T) { |
| 349 |
t.Parallel() |
| 350 |
|
| 351 |
// Verify the error is defined |
| 352 |
require.Error(t, ErrUnknownGenerator, "ErrUnknownGenerator should be defined") |
| 353 |
assert.Equal(t, "unknown generator", ErrUnknownGenerator.Error(), "error message should match") |
| 354 |
|
| 355 |
// Verify it works with errors.Is |
| 356 |
_, err := NewGenerator("nonexistent") |
| 357 |
require.Error(t, err) |
| 358 |
require.ErrorIs(t, err, ErrUnknownGenerator, "error should be ErrUnknownGenerator") |
| 359 |
assert.ErrorIs(t, err, ErrUnknownGenerator, "errors.Is should work") |
| 360 |
} |