| 1 | package events |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "launchpad.net/email-reminder/internal/util" |
| 9 | ) |
| 10 | |
| 11 | const defaultMaxRecipients = 10000 |
| 12 | |
| 13 | var defaultRecipient = util.EmailRecipient{Email: "[email protected]"} |
| 14 | |
| 15 | func TestGetOccurrence(t *testing.T) { |
| 16 | tests := []struct { |
| 17 | event Event |
| 18 | now time.Time |
| 19 | wantValue int |
| 20 | wantOccurred bool |
| 21 | }{ |
| 22 | { |
| 23 | event: Event{ |
| 24 | Type: "birthday", |
| 25 | Year: 2000, |
| 26 | Month: 5, |
| 27 | Day: EventDay(15), |
| 28 | }, |
| 29 | now: time.Date(2025, 5, 15, 10, 0, 0, 0, time.UTC), |
| 30 | wantValue: 25, |
| 31 | wantOccurred: true, |
| 32 | }, |
| 33 | { |
| 34 | event: Event{ |
| 35 | Type: "birthday", |
| 36 | Year: 2000, |
| 37 | Month: 5, |
| 38 | Day: EventDay(15), |
| 39 | }, |
| 40 | now: time.Date(2025, 7, 20, 10, 0, 0, 0, time.UTC), |
| 41 | wantValue: 25, |
| 42 | wantOccurred: true, |
| 43 | }, |
| 44 | { |
| 45 | event: Event{ |
| 46 | Type: "birthday", |
| 47 | Year: 2000, |
| 48 | Month: 5, |
| 49 | Day: EventDay(15), |
| 50 | }, |
| 51 | now: time.Date(2025, 5, 10, 10, 0, 0, 0, time.UTC), |
| 52 | wantValue: 24, |
| 53 | wantOccurred: true, |
| 54 | }, |
| 55 | { |
| 56 | event: Event{ |
| 57 | Type: "birthday", |
| 58 | Year: 2000, |
| 59 | Month: 8, |
| 60 | Day: EventDay(15), |
| 61 | }, |
| 62 | now: time.Date(2025, 5, 20, 10, 0, 0, 0, time.UTC), |
| 63 | wantValue: 24, |
| 64 | wantOccurred: true, |
| 65 | }, |
| 66 | { |
| 67 | event: Event{ |
| 68 | Type: "birthday", |
| 69 | Year: 2000, |
| 70 | Month: 2, |
| 71 | Day: EventDay(29), |
| 72 | }, |
| 73 | now: time.Date(2025, 2, 28, 10, 0, 0, 0, time.UTC), // 2025 is not a leap year |
| 74 | wantValue: 25, |
| 75 | wantOccurred: true, |
| 76 | }, |
| 77 | { |
| 78 | event: Event{ |
| 79 | Type: "birthday", |
| 80 | Year: 2000, |
| 81 | Month: 2, |
| 82 | Day: EventDay(29), |
| 83 | }, |
| 84 | now: time.Date(2024, 2, 28, 10, 0, 0, 0, time.UTC), // 2024 is a leap year |
| 85 | wantValue: 23, |
| 86 | wantOccurred: true, |
| 87 | }, |
| 88 | { |
| 89 | event: Event{ |
| 90 | Type: "birthday", |
| 91 | Year: 2000, |
| 92 | Month: 2, |
| 93 | Day: EventDay(29), |
| 94 | }, |
| 95 | now: time.Date(2024, 2, 29, 10, 0, 0, 0, time.UTC), // 2024 is a leap year |
| 96 | wantValue: 24, |
| 97 | wantOccurred: true, |
| 98 | }, |
| 99 | { |
| 100 | event: Event{ |
| 101 | Type: "yearly", |
| 102 | Year: 2020, |
| 103 | Month: 5, |
| 104 | Day: EventDay(15), |
| 105 | }, |
| 106 | now: time.Date(2025, 5, 15, 10, 0, 0, 0, time.UTC), |
| 107 | wantValue: 5, |
| 108 | wantOccurred: true, |
| 109 | }, |
| 110 | { |
| 111 | event: Event{ |
| 112 | Type: "birthday", |
| 113 | Year: 2025, |
| 114 | Month: 5, |
| 115 | Day: EventDay(15), |
| 116 | }, |
| 117 | now: time.Date(2025, 5, 15, 10, 0, 0, 0, time.UTC), |
| 118 | wantValue: 0, |
| 119 | wantOccurred: true, |
| 120 | }, |
| 121 | { |
| 122 | event: Event{ |
| 123 | Type: "birthday", |
| 124 | Year: 2025, |
| 125 | Month: 8, |
| 126 | Day: EventDay(15), |
| 127 | }, |
| 128 | now: time.Date(2025, 5, 15, 10, 0, 0, 0, time.UTC), |
| 129 | wantValue: -1, |
| 130 | wantOccurred: false, // unborn person |
| 131 | }, |
| 132 | { |
| 133 | event: Event{ |
| 134 | Type: "birthday", |
| 135 | Year: 2000, |
| 136 | Month: 5, |
| 137 | Day: EventDay(15), |
| 138 | }, |
| 139 | now: time.Date(2025, 3, 20, 10, 0, 0, 0, time.UTC), |
| 140 | wantValue: 24, |
| 141 | wantOccurred: true, |
| 142 | }, |
| 143 | } |
| 144 | |
| 145 | for i, tt := range tests { |
| 146 | gotValue, gotOccurred := tt.event.GetOccurrence(tt.now) |
| 147 | if gotValue != tt.wantValue { |
| 148 | t.Errorf("Test %d: GetOccurrence() gotValue = %v, want %v", i, gotValue, tt.wantValue) |
| 149 | } |
| 150 | if gotOccurred != tt.wantOccurred { |
| 151 | t.Errorf("Test %d: GetOccurrence() gotOccurred = %v, want %v", i, gotOccurred, tt.wantOccurred) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestGetRecipients(t *testing.T) { |
| 157 | tests := []struct { |
| 158 | name string |
| 159 | event Event |
| 160 | want []util.EmailRecipient |
| 161 | }{ |
| 162 | { |
| 163 | name: "no recipients - use default email", |
| 164 | event: Event{ |
| 165 | Name: "Test Event", |
| 166 | Recipients: []Recipient{}, |
| 167 | }, |
| 168 | want: []util.EmailRecipient{defaultRecipient}, |
| 169 | }, |
| 170 | { |
| 171 | name: "single recipient with name and email", |
| 172 | event: Event{ |
| 173 | Name: "Test Event", |
| 174 | Recipients: []Recipient{ |
| 175 | {Name: "John Doe", Email: "[email protected]"}, |
| 176 | }, |
| 177 | }, |
| 178 | want: []util.EmailRecipient{{Email: "[email protected]", FirstName: "John", LastName: "Doe"}}, |
| 179 | }, |
| 180 | { |
| 181 | name: "single recipient with email only", |
| 182 | event: Event{ |
| 183 | Name: "Test Event", |
| 184 | Recipients: []Recipient{ |
| 185 | {Email: "[email protected]"}, |
| 186 | }, |
| 187 | }, |
| 188 | want: []util.EmailRecipient{{Email: "[email protected]"}}, |
| 189 | }, |
| 190 | { |
| 191 | name: "multiple recipients", |
| 192 | event: Event{ |
| 193 | Name: "Test Event", |
| 194 | Recipients: []Recipient{ |
| 195 | {Name: "John Doe", Email: "[email protected]"}, |
| 196 | {Email: "[email protected]"}, |
| 197 | {Name: "Bob Smith", Email: "[email protected]"}, |
| 198 | }, |
| 199 | }, |
| 200 | want: []util.EmailRecipient{ |
| 201 | {Email: "[email protected]", FirstName: "John", LastName: "Doe"}, |
| 202 | {Email: "[email protected]"}, |
| 203 | {Email: "[email protected]", FirstName: "Bob", LastName: "Smith"}, |
| 204 | }, |
| 205 | }, |
| 206 | { |
| 207 | name: "recipient with empty email - should be ignored", |
| 208 | event: Event{ |
| 209 | Name: "Test Event", |
| 210 | Recipients: []Recipient{ |
| 211 | {Name: "John Doe Smith", Email: "[email protected]"}, |
| 212 | {Name: "Invalid", Email: ""}, |
| 213 | {Email: "[email protected]"}, |
| 214 | }, |
| 215 | }, |
| 216 | want: []util.EmailRecipient{ |
| 217 | {Email: "[email protected]", FirstName: "John", LastName: "Doe Smith"}, |
| 218 | {Email: "[email protected]"}, |
| 219 | }, |
| 220 | }, |
| 221 | { |
| 222 | name: "all recipients invalid - use default email", |
| 223 | event: Event{ |
| 224 | Name: "Test Event", |
| 225 | Recipients: []Recipient{ |
| 226 | {Name: "Invalid1", Email: ""}, |
| 227 | {Name: "Invalid2", Email: " "}, |
| 228 | }, |
| 229 | }, |
| 230 | want: []util.EmailRecipient{defaultRecipient}, |
| 231 | }, |
| 232 | { |
| 233 | name: "recipient with whitespace in name and email", |
| 234 | event: Event{ |
| 235 | Name: "Test Event", |
| 236 | Recipients: []Recipient{ |
| 237 | {Name: " John Doe ", Email: " [email protected] "}, |
| 238 | }, |
| 239 | }, |
| 240 | want: []util.EmailRecipient{{Email: "[email protected]", FirstName: "John", LastName: "Doe"}}, |
| 241 | }, |
| 242 | } |
| 243 | |
| 244 | for _, tt := range tests { |
| 245 | t.Run(tt.name, func(t *testing.T) { |
| 246 | got := tt.event.GetRecipients(defaultRecipient, defaultMaxRecipients) |
| 247 | if len(got) != len(tt.want) { |
| 248 | t.Errorf("GetRecipients() length = %d, want %d", len(got), len(tt.want)) |
| 249 | return |
| 250 | } |
| 251 | for i, recipient := range got { |
| 252 | if recipient.Email != tt.want[i].Email { |
| 253 | t.Errorf("GetRecipients()[%d].Email = %s, want %s", i, recipient.Email, tt.want[i].Email) |
| 254 | } |
| 255 | if recipient.FirstName != tt.want[i].FirstName { |
| 256 | t.Errorf("GetRecipients()[%d].FirstName = %s, want %s", i, recipient.FirstName, tt.want[i].FirstName) |
| 257 | } |
| 258 | } |
| 259 | }) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | func TestGetRecipientsMaxLimit(t *testing.T) { |
| 264 | tests := []struct { |
| 265 | name string |
| 266 | event Event |
| 267 | maxRecipients int |
| 268 | wantCount int |
| 269 | }{ |
| 270 | { |
| 271 | name: "within limit - 2 recipients with max 10", |
| 272 | event: Event{ |
| 273 | Recipients: []Recipient{ |
| 274 | {Email: "[email protected]"}, |
| 275 | {Email: "[email protected]"}, |
| 276 | }, |
| 277 | }, |
| 278 | maxRecipients: 10, |
| 279 | wantCount: 2, |
| 280 | }, |
| 281 | { |
| 282 | name: "at limit - 2 recipients with max 2", |
| 283 | event: Event{ |
| 284 | Recipients: []Recipient{ |
| 285 | {Email: "[email protected]"}, |
| 286 | {Email: "[email protected]"}, |
| 287 | }, |
| 288 | }, |
| 289 | maxRecipients: 2, |
| 290 | wantCount: 2, |
| 291 | }, |
| 292 | { |
| 293 | name: "exceeds limit - 5 recipients with max 3", |
| 294 | event: Event{ |
| 295 | Name: "Test Event", |
| 296 | Recipients: []Recipient{ |
| 297 | {Email: "[email protected]"}, |
| 298 | {Email: "[email protected]"}, |
| 299 | {Email: "[email protected]"}, |
| 300 | {Email: "[email protected]"}, |
| 301 | {Email: "[email protected]"}, |
| 302 | }, |
| 303 | }, |
| 304 | maxRecipients: 3, |
| 305 | wantCount: 3, // Only first 3 should be returned |
| 306 | }, |
| 307 | { |
| 308 | name: "no recipients - fallback used", |
| 309 | event: Event{}, |
| 310 | maxRecipients: 10, |
| 311 | wantCount: 1, |
| 312 | }, |
| 313 | } |
| 314 | |
| 315 | for _, tt := range tests { |
| 316 | t.Run(tt.name, func(t *testing.T) { |
| 317 | got := tt.event.GetRecipients(defaultRecipient, tt.maxRecipients) |
| 318 | if len(got) != tt.wantCount { |
| 319 | t.Errorf("GetRecipients() returned %d recipients, want %d", len(got), tt.wantCount) |
| 320 | } |
| 321 | }) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | func TestAnniversaryContact(t *testing.T) { |
| 326 | tests := []struct { |
| 327 | name string |
| 328 | event Event |
| 329 | want string |
| 330 | }{ |
| 331 | { |
| 332 | name: "both emails present", |
| 333 | event: Event{ |
| 334 | Name: "John", |
| 335 | Email: " [email protected]", |
| 336 | PartnerName: "Jane", |
| 337 | PartnerEmail: " [email protected] ", |
| 338 | }, |
| 339 | want: "You can reach them at [email protected] and [email protected] respectively.", |
| 340 | }, |
| 341 | { |
| 342 | name: "only event email present", |
| 343 | event: Event{ |
| 344 | Name: "John", |
| 345 | Email: " [email protected]", |
| 346 | PartnerName: "Jane", |
| 347 | PartnerEmail: "", |
| 348 | }, |
| 349 | want: "You can reach John at [email protected].", |
| 350 | }, |
| 351 | { |
| 352 | name: "only partner email present", |
| 353 | event: Event{ |
| 354 | Name: "John", |
| 355 | Email: "", |
| 356 | PartnerName: "Jane", |
| 357 | PartnerEmail: "[email protected] ", |
| 358 | }, |
| 359 | want: "You can reach Jane at [email protected].", |
| 360 | }, |
| 361 | { |
| 362 | name: "no emails present", |
| 363 | event: Event{ |
| 364 | Name: "John", |
| 365 | Email: "", |
| 366 | PartnerName: "Jane", |
| 367 | PartnerEmail: "", |
| 368 | }, |
| 369 | want: "", |
| 370 | }, |
| 371 | { |
| 372 | name: "empty strings for emails", |
| 373 | event: Event{ |
| 374 | Name: "John", |
| 375 | Email: " ", |
| 376 | PartnerName: "Jane", |
| 377 | PartnerEmail: " ", |
| 378 | }, |
| 379 | want: "", |
| 380 | }, |
| 381 | } |
| 382 | |
| 383 | for _, tt := range tests { |
| 384 | t.Run(tt.name, func(t *testing.T) { |
| 385 | got := tt.event.AnniversaryContact() |
| 386 | if got != tt.want { |
| 387 | t.Errorf("AnniversaryContact() = %q, want %q", got, tt.want) |
| 388 | } |
| 389 | }) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | func TestEventValidate(t *testing.T) { |
| 394 | tests := []struct { |
| 395 | name string |
| 396 | event Event |
| 397 | wantValid bool |
| 398 | }{ |
| 399 | // Valid events |
| 400 | { |
| 401 | name: "valid anniversary", |
| 402 | event: Event{ |
| 403 | Type: "anniversary", |
| 404 | Name: "John", |
| 405 | PartnerName: "Jane", |
| 406 | Day: EventDay(15), |
| 407 | Month: 6, |
| 408 | Year: 2020, |
| 409 | }, |
| 410 | wantValid: true, |
| 411 | }, |
| 412 | { |
| 413 | name: "valid birthday", |
| 414 | event: Event{ |
| 415 | Type: "birthday", |
| 416 | Name: "Alice", |
| 417 | Day: EventDay(1), |
| 418 | Month: 1, |
| 419 | }, |
| 420 | wantValid: true, |
| 421 | }, |
| 422 | { |
| 423 | name: "valid yearly", |
| 424 | event: Event{ |
| 425 | Type: "yearly", |
| 426 | Name: "Company Anniversary", |
| 427 | Day: EventDay(31), |
| 428 | Month: 12, |
| 429 | }, |
| 430 | wantValid: true, |
| 431 | }, |
| 432 | { |
| 433 | name: "valid monthly", |
| 434 | event: Event{ |
| 435 | Type: "monthly", |
| 436 | Name: "Monthly Review", |
| 437 | Day: EventDay(15), |
| 438 | }, |
| 439 | wantValid: true, |
| 440 | }, |
| 441 | { |
| 442 | name: "valid weekly on Sunday", |
| 443 | event: Event{ |
| 444 | Type: "weekly", |
| 445 | Name: "Weekly Meeting", |
| 446 | Day: EventDay(0), |
| 447 | }, |
| 448 | wantValid: true, |
| 449 | }, |
| 450 | { |
| 451 | name: "valid weekly on Saturday", |
| 452 | event: Event{ |
| 453 | Type: "weekly", |
| 454 | Name: "Weekly Meeting", |
| 455 | Day: EventDay(6), |
| 456 | }, |
| 457 | wantValid: true, |
| 458 | }, |
| 459 | |
| 460 | // Invalid events - empty name |
| 461 | { |
| 462 | name: "empty name", |
| 463 | event: Event{ |
| 464 | Type: "birthday", |
| 465 | Name: "", |
| 466 | Day: EventDay(15), |
| 467 | Month: 6, |
| 468 | }, |
| 469 | wantValid: false, |
| 470 | }, |
| 471 | { |
| 472 | name: "whitespace only name", |
| 473 | event: Event{ |
| 474 | Type: "birthday", |
| 475 | Name: " \t\n ", |
| 476 | Day: EventDay(15), |
| 477 | Month: 6, |
| 478 | }, |
| 479 | wantValid: false, |
| 480 | }, |
| 481 | |
| 482 | // Invalid events - anniversary specific validation |
| 483 | { |
| 484 | name: "anniversary with empty partner name", |
| 485 | event: Event{ |
| 486 | Type: "anniversary", |
| 487 | Name: "John", |
| 488 | PartnerName: "", |
| 489 | Day: EventDay(15), |
| 490 | Month: 6, |
| 491 | }, |
| 492 | wantValid: false, |
| 493 | }, |
| 494 | { |
| 495 | name: "anniversary with whitespace only partner name", |
| 496 | event: Event{ |
| 497 | Type: "anniversary", |
| 498 | Name: "John", |
| 499 | PartnerName: " \t\n ", |
| 500 | Day: EventDay(15), |
| 501 | Month: 6, |
| 502 | }, |
| 503 | wantValid: false, |
| 504 | }, |
| 505 | |
| 506 | // Invalid events - bad day values for anniversary/birthday/yearly |
| 507 | { |
| 508 | name: "anniversary with day 0", |
| 509 | event: Event{ |
| 510 | Type: "anniversary", |
| 511 | Name: "Test", |
| 512 | PartnerName: "Partner", |
| 513 | Day: EventDay(0), |
| 514 | Month: 6, |
| 515 | }, |
| 516 | wantValid: false, |
| 517 | }, |
| 518 | { |
| 519 | name: "birthday with day 32", |
| 520 | event: Event{ |
| 521 | Type: "birthday", |
| 522 | Name: "Test", |
| 523 | Day: EventDay(32), |
| 524 | Month: 6, |
| 525 | }, |
| 526 | wantValid: false, |
| 527 | }, |
| 528 | |
| 529 | // Invalid events - bad month values for anniversary/birthday/yearly |
| 530 | { |
| 531 | name: "anniversary with month 0", |
| 532 | event: Event{ |
| 533 | Type: "anniversary", |
| 534 | Name: "Test", |
| 535 | PartnerName: "Partner", |
| 536 | Day: EventDay(15), |
| 537 | Month: 0, |
| 538 | }, |
| 539 | wantValid: false, |
| 540 | }, |
| 541 | { |
| 542 | name: "yearly with month 13", |
| 543 | event: Event{ |
| 544 | Type: "yearly", |
| 545 | Name: "Test", |
| 546 | Day: EventDay(15), |
| 547 | Month: 13, |
| 548 | }, |
| 549 | wantValid: false, |
| 550 | }, |
| 551 | |
| 552 | // Invalid events - bad day values for monthly |
| 553 | { |
| 554 | name: "monthly with day 0", |
| 555 | event: Event{ |
| 556 | Type: "monthly", |
| 557 | Name: "Test", |
| 558 | Day: EventDay(0), |
| 559 | }, |
| 560 | wantValid: false, |
| 561 | }, |
| 562 | { |
| 563 | name: "monthly with day 32", |
| 564 | event: Event{ |
| 565 | Type: "monthly", |
| 566 | Name: "Test", |
| 567 | Day: EventDay(32), |
| 568 | }, |
| 569 | wantValid: false, |
| 570 | }, |
| 571 | |
| 572 | // Invalid events - unknown type |
| 573 | { |
| 574 | name: "unknown event type", |
| 575 | event: Event{ |
| 576 | Type: "unknown", |
| 577 | Name: "Test", |
| 578 | Day: EventDay(15), |
| 579 | }, |
| 580 | wantValid: false, |
| 581 | }, |
| 582 | |
| 583 | // Invalid events - negative year |
| 584 | { |
| 585 | name: "birthday with large negative year", |
| 586 | event: Event{ |
| 587 | Type: "birthday", |
| 588 | Name: "Test", |
| 589 | Day: EventDay(15), |
| 590 | Month: 6, |
| 591 | Year: -9999, |
| 592 | }, |
| 593 | wantValid: false, |
| 594 | }, |
| 595 | } |
| 596 | |
| 597 | for _, tt := range tests { |
| 598 | t.Run(tt.name, func(t *testing.T) { |
| 599 | err := tt.event.Validate() |
| 600 | |
| 601 | if tt.wantValid { |
| 602 | if err != nil { |
| 603 | t.Errorf("Validate() unexpected error = %v", err) |
| 604 | } |
| 605 | } else { |
| 606 | if err == nil { |
| 607 | t.Errorf("Validate() error = nil, want non-empty error") |
| 608 | } else if err.Error() == "" { |
| 609 | t.Errorf("Validate() error message is empty, want non-empty error message") |
| 610 | } |
| 611 | } |
| 612 | }) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | func TestIsOccurring(t *testing.T) { |
| 617 | tests := []struct { |
| 618 | name string |
| 619 | event Event |
| 620 | now time.Time |
| 621 | reminder Reminder |
| 622 | wantOccurring bool |
| 623 | wantWhen string |
| 624 | wantError bool |
| 625 | }{ |
| 626 | // Same day and days before occurring tests |
| 627 | { |
| 628 | name: "anniversary same day", |
| 629 | event: Event{ |
| 630 | Type: "anniversary", |
| 631 | Name: "Alice", |
| 632 | PartnerName: "Bob", |
| 633 | Day: EventDay(15), |
| 634 | Month: 6, |
| 635 | }, |
| 636 | now: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC), |
| 637 | reminder: Reminder{ |
| 638 | Type: "same day", |
| 639 | }, |
| 640 | wantOccurring: true, |
| 641 | wantWhen: "today", |
| 642 | wantError: false, |
| 643 | }, |
| 644 | { |
| 645 | name: "birthday 1 day before", |
| 646 | event: Event{ |
| 647 | Type: "birthday", |
| 648 | Name: "John", |
| 649 | Day: EventDay(25), |
| 650 | Month: 12, |
| 651 | }, |
| 652 | now: time.Date(2025, 12, 24, 10, 0, 0, 0, time.UTC), |
| 653 | reminder: Reminder{ |
| 654 | Type: "days before", |
| 655 | Days: 1, |
| 656 | }, |
| 657 | wantOccurring: true, |
| 658 | wantWhen: "tomorrow (Thu Dec 25)", |
| 659 | wantError: false, |
| 660 | }, |
| 661 | { |
| 662 | name: "birthday 42 days before (should fire)", |
| 663 | event: Event{ |
| 664 | Type: "birthday", |
| 665 | Name: "Test Person", |
| 666 | Day: EventDay(14), |
| 667 | Month: 5, |
| 668 | Year: 1948, |
| 669 | }, |
| 670 | now: time.Date(2011, 4, 2, 10, 0, 0, 0, time.UTC), |
| 671 | reminder: Reminder{ |
| 672 | Type: "days before", |
| 673 | Days: 42, |
| 674 | }, |
| 675 | wantOccurring: true, |
| 676 | wantWhen: "in 42 days (Sat May 14)", |
| 677 | wantError: false, |
| 678 | }, |
| 679 | { |
| 680 | name: "birthday 42 days before (should not fire)", |
| 681 | event: Event{ |
| 682 | Type: "birthday", |
| 683 | Name: "Test Person", |
| 684 | Day: EventDay(14), |
| 685 | Month: 5, |
| 686 | Year: 1948, |
| 687 | }, |
| 688 | now: time.Date(2011, 4, 1, 10, 0, 0, 0, time.UTC), |
| 689 | reminder: Reminder{ |
| 690 | Type: "days before", |
| 691 | Days: 42, |
| 692 | }, |
| 693 | wantOccurring: false, |
| 694 | wantWhen: "in 42 days (Fri May 13)", |
| 695 | wantError: false, |
| 696 | }, |
| 697 | { |
| 698 | name: "yearly same day", |
| 699 | event: Event{ |
| 700 | Type: "yearly", |
| 701 | Name: "Annual Meeting", |
| 702 | Day: EventDay(10), |
| 703 | Month: 3, |
| 704 | }, |
| 705 | now: time.Date(2025, 3, 10, 10, 0, 0, 0, time.UTC), |
| 706 | reminder: Reminder{ |
| 707 | Type: "same day", |
| 708 | }, |
| 709 | wantOccurring: true, |
| 710 | wantWhen: "today", |
| 711 | wantError: false, |
| 712 | }, |
| 713 | { |
| 714 | name: "monthly 2 days before", |
| 715 | event: Event{ |
| 716 | Type: "monthly", |
| 717 | Name: "Monthly Report", |
| 718 | Day: EventDay(15), |
| 719 | }, |
| 720 | now: time.Date(2025, 9, 13, 10, 0, 0, 0, time.UTC), |
| 721 | reminder: Reminder{ |
| 722 | Type: "days before", |
| 723 | Days: 2, |
| 724 | }, |
| 725 | wantOccurring: true, |
| 726 | wantWhen: "in 2 days (Mon Sep 15)", |
| 727 | wantError: false, |
| 728 | }, |
| 729 | { |
| 730 | name: "weekly 1 day before", |
| 731 | event: Event{ |
| 732 | Type: "weekly", |
| 733 | Name: "Team Meeting", |
| 734 | Day: EventDay(1), // Monday |
| 735 | }, |
| 736 | now: time.Date(2025, 9, 14, 10, 0, 0, 0, time.UTC), // Sunday |
| 737 | reminder: Reminder{ |
| 738 | Type: "days before", |
| 739 | Days: 1, |
| 740 | }, |
| 741 | wantOccurring: true, |
| 742 | wantWhen: "tomorrow (Mon Sep 15)", |
| 743 | wantError: false, |
| 744 | }, |
| 745 | { |
| 746 | name: "weekly same day - Sunday", |
| 747 | event: Event{ |
| 748 | Type: "weekly", |
| 749 | Name: "Sunday Service", |
| 750 | Day: EventDay(0), // Sunday |
| 751 | }, |
| 752 | now: time.Date(2025, 9, 14, 10, 0, 0, 0, time.UTC), // Sunday |
| 753 | reminder: Reminder{ |
| 754 | Type: "same day", |
| 755 | }, |
| 756 | wantOccurring: true, |
| 757 | wantWhen: "today", |
| 758 | wantError: false, |
| 759 | }, |
| 760 | |
| 761 | // Non-occurring tests |
| 762 | { |
| 763 | name: "birthday different day", |
| 764 | event: Event{ |
| 765 | Type: "birthday", |
| 766 | Name: "Jane", |
| 767 | Day: EventDay(25), |
| 768 | Month: 12, |
| 769 | }, |
| 770 | now: time.Date(2025, 12, 20, 10, 0, 0, 0, time.UTC), |
| 771 | reminder: Reminder{ |
| 772 | Type: "same day", |
| 773 | }, |
| 774 | wantOccurring: false, |
| 775 | wantWhen: "today", |
| 776 | wantError: false, |
| 777 | }, |
| 778 | { |
| 779 | name: "monthly different day", |
| 780 | event: Event{ |
| 781 | Type: "monthly", |
| 782 | Name: "TPS Report", |
| 783 | Day: EventDay(15), |
| 784 | }, |
| 785 | now: time.Date(2025, 9, 10, 10, 0, 0, 0, time.UTC), |
| 786 | reminder: Reminder{ |
| 787 | Type: "same day", |
| 788 | }, |
| 789 | wantOccurring: false, |
| 790 | wantWhen: "today", |
| 791 | wantError: false, |
| 792 | }, |
| 793 | { |
| 794 | name: "weekly different day", |
| 795 | event: Event{ |
| 796 | Type: "weekly", |
| 797 | Name: "Garbage", |
| 798 | Day: EventDay(1), // Monday |
| 799 | }, |
| 800 | now: time.Date(2025, 9, 16, 10, 0, 0, 0, time.UTC), // Tuesday |
| 801 | reminder: Reminder{ |
| 802 | Type: "same day", |
| 803 | }, |
| 804 | wantOccurring: false, |
| 805 | wantWhen: "today", |
| 806 | wantError: false, |
| 807 | }, |
| 808 | |
| 809 | // Edge cases |
| 810 | { |
| 811 | name: "monthly last day of February non-leap year", |
| 812 | event: Event{ |
| 813 | Type: "monthly", |
| 814 | Name: "End of Month Report", |
| 815 | Day: EventDay(31), // Requested on 31st but February only has 28 days |
| 816 | }, |
| 817 | now: time.Date(2025, 2, 28, 10, 0, 0, 0, time.UTC), // Last day of February in non-leap year |
| 818 | reminder: Reminder{ |
| 819 | Type: "same day", |
| 820 | }, |
| 821 | wantOccurring: true, |
| 822 | wantWhen: "today", |
| 823 | wantError: false, |
| 824 | }, |
| 825 | { |
| 826 | name: "leap year February 29th birthday on Feb 28th", |
| 827 | event: Event{ |
| 828 | Type: "birthday", |
| 829 | Name: "Leap Year Baby", |
| 830 | Day: EventDay(29), |
| 831 | Month: 2, |
| 832 | Year: 2024, |
| 833 | }, |
| 834 | now: time.Date(2025, 2, 28, 10, 0, 0, 0, time.UTC), // 2025 is not a leap year |
| 835 | reminder: Reminder{ |
| 836 | Type: "same day", |
| 837 | }, |
| 838 | wantOccurring: true, |
| 839 | wantWhen: "today", |
| 840 | wantError: false, |
| 841 | }, |
| 842 | { |
| 843 | name: "leap year February 29th birthday on actual Feb 29th", |
| 844 | event: Event{ |
| 845 | Type: "birthday", |
| 846 | Name: "Leap Year Baby", |
| 847 | Day: EventDay(29), |
| 848 | Month: 2, |
| 849 | Year: 2024, |
| 850 | }, |
| 851 | now: time.Date(2028, 2, 29, 10, 0, 0, 0, time.UTC), // 2028 is a leap year |
| 852 | reminder: Reminder{ |
| 853 | Type: "same day", |
| 854 | }, |
| 855 | wantOccurring: true, |
| 856 | wantWhen: "today", |
| 857 | wantError: false, |
| 858 | }, |
| 859 | { |
| 860 | name: "anniversary Feb 29 celebrated on Feb 28 in non-leap year", |
| 861 | event: Event{ |
| 862 | Type: "anniversary", |
| 863 | Name: "Alice", |
| 864 | PartnerName: "Bob", |
| 865 | Day: EventDay(29), |
| 866 | Month: 2, |
| 867 | Year: 2000, // leap year base |
| 868 | }, |
| 869 | now: time.Date(2025, 2, 28, 10, 0, 0, 0, time.UTC), // Non-leap year Feb 28 |
| 870 | reminder: Reminder{Type: "same day"}, |
| 871 | wantOccurring: true, |
| 872 | wantWhen: "today", |
| 873 | wantError: false, |
| 874 | }, |
| 875 | |
| 876 | // Error cases |
| 877 | { |
| 878 | name: "invalid reminder type", |
| 879 | event: Event{ |
| 880 | Type: "birthday", |
| 881 | Name: "Robert", |
| 882 | Day: EventDay(25), |
| 883 | Month: 12, |
| 884 | }, |
| 885 | now: time.Date(2025, 12, 25, 10, 0, 0, 0, time.UTC), |
| 886 | reminder: Reminder{ |
| 887 | Type: "invalid", |
| 888 | }, |
| 889 | wantOccurring: false, |
| 890 | wantWhen: "", |
| 891 | wantError: true, |
| 892 | }, |
| 893 | { |
| 894 | name: "invalid event type", |
| 895 | event: Event{ |
| 896 | Type: "invalid", |
| 897 | Name: "Invalid Event", |
| 898 | Day: EventDay(25), |
| 899 | }, |
| 900 | now: time.Date(2025, 12, 25, 10, 0, 0, 0, time.UTC), |
| 901 | reminder: Reminder{ |
| 902 | Type: "same day", |
| 903 | }, |
| 904 | wantOccurring: false, |
| 905 | wantWhen: "today", |
| 906 | wantError: true, |
| 907 | }, |
| 908 | { |
| 909 | name: "days before with zero days", |
| 910 | event: Event{ |
| 911 | Type: "birthday", |
| 912 | Name: "Alice", |
| 913 | Day: EventDay(25), |
| 914 | Month: 12, |
| 915 | }, |
| 916 | now: time.Date(2025, 12, 25, 10, 0, 0, 0, time.UTC), |
| 917 | reminder: Reminder{ |
| 918 | Type: "days before", |
| 919 | Days: 0, |
| 920 | }, |
| 921 | wantOccurring: false, |
| 922 | wantWhen: "", |
| 923 | wantError: true, |
| 924 | }, |
| 925 | // Additional weekly edge/fallback cases |
| 926 | { |
| 927 | name: "weekly day 7 treated as Sunday (same day reminder)", |
| 928 | event: Event{ |
| 929 | Type: "weekly", |
| 930 | Name: "Overflow Weekly", |
| 931 | Day: EventDay(7), // out of normal range; should default to Sunday |
| 932 | }, |
| 933 | now: time.Date(2025, 9, 14, 10, 0, 0, 0, time.UTC), // Sunday |
| 934 | reminder: Reminder{ |
| 935 | Type: "same day", |
| 936 | }, |
| 937 | wantOccurring: true, |
| 938 | wantWhen: "today", |
| 939 | wantError: false, |
| 940 | }, |
| 941 | { |
| 942 | name: "weekly negative day treated as Sunday (days before reminder)", |
| 943 | event: Event{ |
| 944 | Type: "weekly", |
| 945 | Name: "Negative Weekly", |
| 946 | Day: EventDay(-1), // negative; should default to Sunday |
| 947 | }, |
| 948 | now: time.Date(2025, 9, 13, 10, 0, 0, 0, time.UTC), // Saturday |
| 949 | reminder: Reminder{ |
| 950 | Type: "days before", |
| 951 | Days: 1, |
| 952 | }, |
| 953 | wantOccurring: true, // Saturday + 1 day = Sunday (defaulted) |
| 954 | wantWhen: "tomorrow (Sun Sep 14)", |
| 955 | wantError: false, |
| 956 | }, |
| 957 | { |
| 958 | name: "birthday with days before reminder crossing year and month boundaries in leap and non-leap years", |
| 959 | event: Event{ |
| 960 | Type: "birthday", |
| 961 | Name: "John", |
| 962 | Day: EventDay(2), |
| 963 | Month: 3, |
| 964 | Year: 2000, |
| 965 | }, |
| 966 | now: time.Date(2023, 12, 29, 10, 0, 0, 0, time.UTC), // 2023 is a non-leap year, 2024 is a leap year |
| 967 | reminder: Reminder{ |
| 968 | Type: "days before", |
| 969 | Days: 64, // 33 days before Jan 31st, 29 more days before Feb 29th, 2 more days before March 2nd |
| 970 | }, |
| 971 | wantOccurring: true, |
| 972 | wantWhen: "in 64 days (Sat Mar 2)", |
| 973 | wantError: false, |
| 974 | }, |
| 975 | { |
| 976 | name: "monthly event on 31st crossing a month boundary", |
| 977 | event: Event{ |
| 978 | Type: "monthly", |
| 979 | Name: "Monthly 31st Event", |
| 980 | Day: EventDay(31), |
| 981 | }, |
| 982 | now: time.Date(2025, 2, 25, 10, 0, 0, 0, time.UTC), // non-leap year |
| 983 | reminder: Reminder{ |
| 984 | Type: "days before", |
| 985 | Days: 34, // 3 days before Feb 28th, 31 more days before March 31st |
| 986 | }, |
| 987 | wantOccurring: true, |
| 988 | wantWhen: "in 34 days (Mon Mar 31)", |
| 989 | wantError: false, |
| 990 | }, |
| 991 | { |
| 992 | name: "yearly event with days before reminder crossing a month boundary on a leap year", |
| 993 | event: Event{ |
| 994 | Type: "yearly", |
| 995 | Name: "Test Event", |
| 996 | Day: EventDay(31), |
| 997 | Month: 3, // March |
| 998 | Year: 2020, |
| 999 | }, |
| 1000 | now: time.Date(2024, 2, 28, 10, 0, 0, 0, time.UTC), // leap year |
| 1001 | reminder: Reminder{ |
| 1002 | Type: "days before", |
| 1003 | Days: 31, |
| 1004 | }, |
| 1005 | wantOccurring: false, // Feb 28 + 31 days = March 30th due to leap year |
| 1006 | wantWhen: "in 31 days (Sat Mar 30)", |
| 1007 | wantError: false, |
| 1008 | }, |
| 1009 | { |
| 1010 | name: "monthly event on 31st with days before instead of same-day", |
| 1011 | event: Event{ |
| 1012 | Type: "monthly", |
| 1013 | Name: "Monthly Report", |
| 1014 | Day: EventDay(31), |
| 1015 | }, |
| 1016 | now: time.Date(2025, 4, 30, 10, 0, 0, 0, time.UTC), // last day of the month |
| 1017 | reminder: Reminder{ |
| 1018 | Type: "days before", |
| 1019 | Days: 1, |
| 1020 | }, |
| 1021 | wantOccurring: false, |
| 1022 | wantWhen: "tomorrow (Thu May 1)", |
| 1023 | wantError: false, |
| 1024 | }, |
| 1025 | { |
| 1026 | name: "monthly event on 31st with days before on leap year", |
| 1027 | event: Event{ |
| 1028 | Type: "monthly", |
| 1029 | Name: "End of Month Report", |
| 1030 | Day: EventDay(31), |
| 1031 | }, |
| 1032 | now: time.Date(2024, 1, 29, 10, 0, 0, 0, time.UTC), // leap year |
| 1033 | reminder: Reminder{ |
| 1034 | Type: "days before", |
| 1035 | Days: 31, // 2 days before Jan 31st, 29 more days before Feb 29th |
| 1036 | }, |
| 1037 | wantOccurring: true, // Feb 29th is the last day of the month |
| 1038 | wantWhen: "in 31 days (Thu Feb 29)", |
| 1039 | wantError: false, |
| 1040 | }, |
| 1041 | } |
| 1042 | |
| 1043 | for _, tt := range tests { |
| 1044 | t.Run(tt.name, func(t *testing.T) { |
| 1045 | occurring, when, err := tt.event.IsOccurring(tt.now, tt.reminder) |
| 1046 | |
| 1047 | if occurring != tt.wantOccurring { |
| 1048 | t.Errorf("IsOccurring() occurring = %v, want %v", occurring, tt.wantOccurring) |
| 1049 | } |
| 1050 | |
| 1051 | if when != tt.wantWhen { |
| 1052 | t.Errorf("IsOccurring() when = %q, want %q", when, tt.wantWhen) |
| 1053 | } |
| 1054 | |
| 1055 | if (err != nil) != tt.wantError { |
| 1056 | t.Errorf("IsOccurring() error = %v, wantError %v", err, tt.wantError) |
| 1057 | } |
| 1058 | }) |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | func TestReminderMessage(t *testing.T) { |
| 1063 | defaultNow := time.Date(2025, 4, 15, 10, 0, 0, 0, time.UTC) |
| 1064 | |
| 1065 | tests := []struct { |
| 1066 | name string |
| 1067 | event Event |
| 1068 | occurringWhen string |
| 1069 | now time.Time // Zero value means use defaultNow |
| 1070 | wantMessage string |
| 1071 | wantSubject string |
| 1072 | wantError bool |
| 1073 | }{ |
| 1074 | { |
| 1075 | name: "anniversary comprehensive - occurrence, special name, both contacts", |
| 1076 | event: Event{ |
| 1077 | Type: "anniversary", |
| 1078 | Name: " Alice ", // Test whitespace trimming |
| 1079 | PartnerName: "Bob", |
| 1080 | Email: "[email protected]", |
| 1081 | PartnerEmail: "[email protected]", |
| 1082 | Year: 2000, |
| 1083 | Month: 4, |
| 1084 | Day: EventDay(13), |
| 1085 | }, |
| 1086 | occurringWhen: "today", |
| 1087 | wantMessage: "I just want to remind you that the 25th anniversary (Silver) of Alice and Bob is today.\n\nYou can reach them at [email protected] and [email protected] respectively.", |
| 1088 | wantSubject: "25th anniversary of Alice and Bob", |
| 1089 | wantError: false, |
| 1090 | }, |
| 1091 | { |
| 1092 | name: "anniversary without occurrence and partial contact info", |
| 1093 | event: Event{ |
| 1094 | Type: "anniversary", |
| 1095 | Name: "Charlie", |
| 1096 | PartnerName: "Diana", |
| 1097 | Email: "[email protected]", // Only one contact |
| 1098 | }, |
| 1099 | occurringWhen: "tomorrow (Wed Apr 16)", |
| 1100 | wantMessage: "I just want to remind you that the anniversary of Charlie and Diana is tomorrow (Wed Apr 16).\n\nYou can reach Charlie at [email protected].", |
| 1101 | wantSubject: "Anniversary of Charlie and Diana", |
| 1102 | wantError: false, |
| 1103 | }, |
| 1104 | { |
| 1105 | name: "birthday comprehensive - age calculation before birthday in year, email, today timing", |
| 1106 | event: Event{ |
| 1107 | Type: "birthday", |
| 1108 | Name: "John ", |
| 1109 | Email: "[email protected]", |
| 1110 | Year: 2000, |
| 1111 | Month: 9, |
| 1112 | Day: EventDay(13), |
| 1113 | }, |
| 1114 | occurringWhen: "today", |
| 1115 | // defaultNow is April 15 2025, which is before the Sept 13 birthday, so John is not 25 yet |
| 1116 | wantMessage: "I just want to remind you that John ([email protected]) is turning 24 today.", |
| 1117 | wantSubject: "John is now 24", |
| 1118 | wantError: false, |
| 1119 | }, |
| 1120 | { |
| 1121 | name: "birthday after birthday in year with today timing uses new age in subject", |
| 1122 | event: Event{ |
| 1123 | Type: "birthday", |
| 1124 | Name: "John", |
| 1125 | Email: "[email protected]", |
| 1126 | Year: 2000, |
| 1127 | Month: 9, |
| 1128 | Day: EventDay(13), |
| 1129 | }, |
| 1130 | occurringWhen: "today", |
| 1131 | now: time.Date(2025, 9, 14, 10, 0, 0, 0, time.UTC), // After/birthday date in the same year |
| 1132 | wantMessage: "I just want to remind you that John ([email protected]) is turning 25 today.", |
| 1133 | wantSubject: "John is now 25", |
| 1134 | wantError: false, |
| 1135 | }, |
| 1136 | { |
| 1137 | name: "birthday without age and non-today timing", |
| 1138 | event: Event{ |
| 1139 | Type: "birthday", |
| 1140 | Name: "Jane", |
| 1141 | }, |
| 1142 | occurringWhen: "in 2 days (Thu Apr 17)", |
| 1143 | wantMessage: "I just want to remind you that Jane is getting one year older in 2 days (Thu Apr 17).", |
| 1144 | wantSubject: "Jane's birthday", |
| 1145 | wantError: false, |
| 1146 | }, |
| 1147 | { |
| 1148 | name: "yearly comprehensive - occurrence calculation", |
| 1149 | event: Event{ |
| 1150 | Type: "yearly", |
| 1151 | Name: "Company Founded", |
| 1152 | Year: 2020, |
| 1153 | Month: 4, |
| 1154 | Day: EventDay(13), |
| 1155 | }, |
| 1156 | occurringWhen: "today", |
| 1157 | wantMessage: "I just want to remind you of the following event today:\n\n6th Company Founded", |
| 1158 | wantSubject: "Company Founded", |
| 1159 | wantError: false, |
| 1160 | }, |
| 1161 | { |
| 1162 | name: "yearly without occurrence", |
| 1163 | event: Event{ |
| 1164 | Type: "yearly", |
| 1165 | Name: "Memorial Day", |
| 1166 | Month: 5, |
| 1167 | Day: EventDay(30), |
| 1168 | }, |
| 1169 | occurringWhen: "tomorrow (Wed Apr 16)", |
| 1170 | wantMessage: "I just want to remind you of the following event tomorrow (Wed Apr 16):\n\nMemorial Day", |
| 1171 | wantSubject: "Memorial Day", |
| 1172 | wantError: false, |
| 1173 | }, |
| 1174 | { |
| 1175 | name: "monthly event", |
| 1176 | event: Event{ |
| 1177 | Type: "monthly", |
| 1178 | Name: " Team Meeting", |
| 1179 | Day: EventDay(15), |
| 1180 | }, |
| 1181 | occurringWhen: "today", |
| 1182 | wantMessage: "I just want to remind you of the following event today:\n\nTeam Meeting", |
| 1183 | wantSubject: "Team Meeting", |
| 1184 | wantError: false, |
| 1185 | }, |
| 1186 | { |
| 1187 | name: "weekly event", |
| 1188 | event: Event{ |
| 1189 | Type: "weekly", |
| 1190 | Name: "Garbage Collection ", |
| 1191 | Day: EventDay(1), |
| 1192 | }, |
| 1193 | occurringWhen: "tomorrow (Wed Apr 16)", |
| 1194 | wantMessage: "I just want to remind you of the following event tomorrow (Wed Apr 16):\n\nGarbage Collection", |
| 1195 | wantSubject: "Garbage Collection", |
| 1196 | wantError: false, |
| 1197 | }, |
| 1198 | |
| 1199 | // Edge cases for date handling |
| 1200 | { |
| 1201 | name: "birthday Feb 29th in non-leap year", |
| 1202 | event: Event{ |
| 1203 | Type: "birthday", |
| 1204 | Name: "Leap Year Baby", |
| 1205 | Year: 2000, // Born in leap year |
| 1206 | Month: 2, |
| 1207 | Day: EventDay(29), |
| 1208 | }, |
| 1209 | occurringWhen: "today", |
| 1210 | now: time.Date(2025, 2, 28, 10, 0, 0, 0, time.UTC), // Feb 28th in non-leap year 2025 |
| 1211 | wantMessage: "I just want to remind you that Leap Year Baby is turning 25 today.", // 2025 is non-leap year, so Feb 29th becomes Feb 28th |
| 1212 | wantSubject: "Leap Year Baby is now 25", |
| 1213 | wantError: false, |
| 1214 | }, |
| 1215 | { |
| 1216 | name: "monthly event 31st in 30-day month", |
| 1217 | event: Event{ |
| 1218 | Type: "monthly", |
| 1219 | Name: "End of Month Report", |
| 1220 | Day: EventDay(31), // Set to 31st but testing in a 30-day month |
| 1221 | }, |
| 1222 | occurringWhen: "today", |
| 1223 | now: time.Date(2025, 9, 30, 10, 0, 0, 0, time.UTC), // Sept 30th (last day of 30-day month) |
| 1224 | wantMessage: "I just want to remind you of the following event today:\n\nEnd of Month Report", |
| 1225 | wantSubject: "End of Month Report", |
| 1226 | wantError: false, |
| 1227 | }, |
| 1228 | |
| 1229 | // Error case |
| 1230 | { |
| 1231 | name: "unknown event type", |
| 1232 | event: Event{ |
| 1233 | Type: "invalid", |
| 1234 | Name: "Test Event", |
| 1235 | }, |
| 1236 | occurringWhen: "today", |
| 1237 | wantMessage: "", |
| 1238 | wantSubject: "", |
| 1239 | wantError: true, |
| 1240 | }, |
| 1241 | } |
| 1242 | |
| 1243 | for _, tt := range tests { |
| 1244 | t.Run(tt.name, func(t *testing.T) { |
| 1245 | testNow := defaultNow |
| 1246 | if !tt.now.IsZero() { |
| 1247 | testNow = tt.now |
| 1248 | } |
| 1249 | |
| 1250 | gotMessage, gotSubject, err := tt.event.ReminderMessage(testNow, tt.occurringWhen) |
| 1251 | |
| 1252 | if (err != nil) != tt.wantError { |
| 1253 | t.Errorf("ReminderMessage() error = %v, wantError %v", err, tt.wantError) |
| 1254 | return |
| 1255 | } |
| 1256 | |
| 1257 | if err != nil { |
| 1258 | return // Don't check message/subject if we expected an error |
| 1259 | } |
| 1260 | |
| 1261 | if gotMessage != tt.wantMessage { |
| 1262 | t.Errorf("ReminderMessage() gotMessage = %q, want %q", gotMessage, tt.wantMessage) |
| 1263 | } |
| 1264 | if gotSubject != tt.wantSubject { |
| 1265 | t.Errorf("ReminderMessage() gotSubject = %q, want %q", gotSubject, tt.wantSubject) |
| 1266 | } |
| 1267 | }) |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | func TestEventDayUnmarshalText(t *testing.T) { |
| 1272 | tests := []struct { |
| 1273 | input string |
| 1274 | want EventDay |
| 1275 | }{ |
| 1276 | {"0", EventDay(0)}, |
| 1277 | {"1", EventDay(1)}, |
| 1278 | {"15", EventDay(15)}, |
| 1279 | {"32", EventDay(32)}, |
| 1280 | {"100", EventDay(100)}, |
| 1281 | |
| 1282 | // Full day names |
| 1283 | {"Sunday", EventDay(0)}, |
| 1284 | {"monday", EventDay(1)}, |
| 1285 | {"Tuesday ", EventDay(2)}, |
| 1286 | {"Wednesday", EventDay(3)}, |
| 1287 | {" Thursday ", EventDay(4)}, |
| 1288 | {"Friday", EventDay(5)}, |
| 1289 | {"SATURDAY", EventDay(6)}, |
| 1290 | |
| 1291 | // Common abbreviations |
| 1292 | {"Sun", EventDay(0)}, |
| 1293 | {"Mon", EventDay(1)}, |
| 1294 | {"Tue", EventDay(2)}, |
| 1295 | {" Wed ", EventDay(3)}, |
| 1296 | {"Thu", EventDay(4)}, |
| 1297 | {"Fri", EventDay(5)}, |
| 1298 | {"Sat", EventDay(6)}, |
| 1299 | |
| 1300 | // Invalid inputs |
| 1301 | {"invalid", EventDay(-1)}, |
| 1302 | {"-1", EventDay(-1)}, |
| 1303 | {"", EventDay(-1)}, |
| 1304 | } |
| 1305 | |
| 1306 | for _, tt := range tests { |
| 1307 | t.Run(tt.input, func(t *testing.T) { |
| 1308 | var ed EventDay |
| 1309 | err := ed.UnmarshalText([]byte(tt.input)) |
| 1310 | if err != nil && (ed != -1 || tt.want != -1) { |
| 1311 | t.Errorf("EventDay.UnmarshalText(%q) unexpected error: %v", tt.input, err) |
| 1312 | } |
| 1313 | if ed != tt.want { |
| 1314 | t.Errorf("EventDay.UnmarshalText(%q) = %d, want %d", tt.input, ed, tt.want) |
| 1315 | } |
| 1316 | }) |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | func TestIsSameDay(t *testing.T) { |
| 1321 | tests := []struct { |
| 1322 | name string |
| 1323 | now time.Time |
| 1324 | originalDay EventDay |
| 1325 | originalMonth time.Month |
| 1326 | want bool |
| 1327 | }{ |
| 1328 | { |
| 1329 | name: "exact match - regular date", |
| 1330 | now: time.Date(2023, time.March, 15, 10, 30, 0, 0, time.UTC), |
| 1331 | originalDay: EventDay(15), |
| 1332 | originalMonth: time.March, |
| 1333 | want: true, |
| 1334 | }, |
| 1335 | { |
| 1336 | name: "different day same month", |
| 1337 | now: time.Date(2023, time.March, 16, 10, 30, 0, 0, time.UTC), |
| 1338 | originalDay: EventDay(15), |
| 1339 | originalMonth: time.March, |
| 1340 | want: false, |
| 1341 | }, |
| 1342 | { |
| 1343 | name: "same day different month", |
| 1344 | now: time.Date(2023, time.April, 15, 10, 30, 0, 0, time.UTC), |
| 1345 | originalDay: EventDay(15), |
| 1346 | originalMonth: time.March, |
| 1347 | want: false, |
| 1348 | }, |
| 1349 | { |
| 1350 | name: "Feb 29 birthday on Feb 28 in non-leap year", |
| 1351 | now: time.Date(2023, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 1352 | originalDay: EventDay(29), |
| 1353 | originalMonth: time.February, |
| 1354 | want: true, |
| 1355 | }, |
| 1356 | { |
| 1357 | name: "Feb 29 birthday on Feb 29 in leap year", |
| 1358 | now: time.Date(2024, time.February, 29, 10, 30, 0, 0, time.UTC), |
| 1359 | originalDay: EventDay(29), |
| 1360 | originalMonth: time.February, |
| 1361 | want: true, |
| 1362 | }, |
| 1363 | { |
| 1364 | name: "Feb 29 birthday on Feb 28 in leap year (should not match)", |
| 1365 | now: time.Date(2024, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 1366 | originalDay: EventDay(29), |
| 1367 | originalMonth: time.February, |
| 1368 | want: false, |
| 1369 | }, |
| 1370 | { |
| 1371 | name: "regular Feb 28 birthday on Feb 28 in non-leap year", |
| 1372 | now: time.Date(2023, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 1373 | originalDay: EventDay(28), |
| 1374 | originalMonth: time.February, |
| 1375 | want: true, |
| 1376 | }, |
| 1377 | { |
| 1378 | name: "regular Feb 28 birthday on Feb 28 in leap year", |
| 1379 | now: time.Date(2024, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 1380 | originalDay: EventDay(28), |
| 1381 | originalMonth: time.February, |
| 1382 | want: true, |
| 1383 | }, |
| 1384 | { |
| 1385 | name: "Feb 27 should not match Feb 29 in non-leap year", |
| 1386 | now: time.Date(2023, time.February, 27, 10, 30, 0, 0, time.UTC), |
| 1387 | originalDay: EventDay(29), |
| 1388 | originalMonth: time.February, |
| 1389 | want: false, |
| 1390 | }, |
| 1391 | { |
| 1392 | name: "March 1 should not match Feb 29 in non-leap year", |
| 1393 | now: time.Date(2023, time.March, 1, 10, 30, 0, 0, time.UTC), |
| 1394 | originalDay: EventDay(29), |
| 1395 | originalMonth: time.February, |
| 1396 | want: false, |
| 1397 | }, |
| 1398 | } |
| 1399 | |
| 1400 | for _, tt := range tests { |
| 1401 | t.Run(tt.name, func(t *testing.T) { |
| 1402 | event := Event{ |
| 1403 | Day: tt.originalDay, |
| 1404 | Month: tt.originalMonth, |
| 1405 | } |
| 1406 | result := event.IsSameDay(tt.now) |
| 1407 | if result != tt.want { |
| 1408 | t.Errorf("Event{Day: %d, Month: %v}.IsSameDay(%v) = %v, want %v", |
| 1409 | tt.originalDay, tt.originalMonth, tt.now, result, tt.want) |
| 1410 | } |
| 1411 | }) |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | func TestEventDayDayOfTheWeek(t *testing.T) { |
| 1416 | tests := []struct { |
| 1417 | day EventDay |
| 1418 | want time.Weekday |
| 1419 | }{ |
| 1420 | {EventDay(1), time.Monday}, |
| 1421 | {EventDay(2), time.Tuesday}, |
| 1422 | {EventDay(6), time.Saturday}, |
| 1423 | |
| 1424 | // Sunday cases (0 and 7 should both become Sunday) |
| 1425 | {EventDay(0), time.Sunday}, // already 0 in Go |
| 1426 | {EventDay(7), time.Sunday}, // cron-style |
| 1427 | |
| 1428 | // Out of range values should default to Sunday |
| 1429 | {EventDay(-1), time.Sunday}, |
| 1430 | {EventDay(8), time.Sunday}, |
| 1431 | } |
| 1432 | |
| 1433 | for _, tt := range tests { |
| 1434 | t.Run(fmt.Sprintf("day_%d", tt.day), func(t *testing.T) { |
| 1435 | got := tt.day.DayOfTheWeek() |
| 1436 | if got != tt.want { |
| 1437 | t.Errorf("EventDay(%d).DayOfTheWeek() = %v, want %v", tt.day, got, tt.want) |
| 1438 | } |
| 1439 | }) |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | func TestEventTypeUnmarshalText(t *testing.T) { |
| 1444 | tests := []struct { |
| 1445 | input string |
| 1446 | want EventType |
| 1447 | }{ |
| 1448 | {"anniversary", EventTypeAnniversary}, |
| 1449 | {"birthday", EventTypeBirthday}, |
| 1450 | {"yearly", EventTypeYearly}, |
| 1451 | {"monthly", EventTypeMonthly}, |
| 1452 | {"weekly", EventTypeWeekly}, |
| 1453 | {"BIRTHDAY", EventTypeBirthday}, |
| 1454 | {" Anniversary ", EventTypeAnniversary}, |
| 1455 | {"unknown", EventType("unknown")}, |
| 1456 | {"", EventType("")}, |
| 1457 | } |
| 1458 | |
| 1459 | for _, tt := range tests { |
| 1460 | t.Run(tt.input, func(t *testing.T) { |
| 1461 | var et EventType |
| 1462 | err := et.UnmarshalText([]byte(tt.input)) |
| 1463 | if err != nil { |
| 1464 | t.Errorf("EventType.UnmarshalText(%q) unexpected error: %v", tt.input, err) |
| 1465 | } |
| 1466 | if et != tt.want { |
| 1467 | t.Errorf("EventType.UnmarshalText(%q) = %q, want %q", tt.input, et, tt.want) |
| 1468 | } |
| 1469 | }) |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | func TestAnniversaryName(t *testing.T) { |
| 1474 | tests := []struct { |
| 1475 | occurrence int |
| 1476 | want string |
| 1477 | }{ |
| 1478 | // Test a few representative values |
| 1479 | {1, "Paper"}, |
| 1480 | {25, "Silver"}, |
| 1481 | {50, "Gold"}, |
| 1482 | {70, "Platinum"}, |
| 1483 | |
| 1484 | // Test degenerate cases |
| 1485 | {0, ""}, |
| 1486 | {-1, ""}, |
| 1487 | {16, ""}, // year without special name |
| 1488 | {100, ""}, // year without special name |
| 1489 | } |
| 1490 | |
| 1491 | for _, tt := range tests { |
| 1492 | got := anniversaryName(tt.occurrence) |
| 1493 | if got != tt.want { |
| 1494 | t.Errorf("anniversaryName(%d) = %q, want %q", tt.occurrence, got, tt.want) |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | func TestFormatOrdinal(t *testing.T) { |
| 1500 | tests := []struct { |
| 1501 | input int |
| 1502 | want string |
| 1503 | }{ |
| 1504 | // Test the special cases for 11th, 12th, 13th |
| 1505 | {11, "11th"}, |
| 1506 | {12, "12th"}, |
| 1507 | {13, "13th"}, |
| 1508 | {111, "111th"}, |
| 1509 | {112, "112th"}, |
| 1510 | {113, "113th"}, |
| 1511 | |
| 1512 | // Test 1st endings |
| 1513 | {1, "1st"}, |
| 1514 | {21, "21st"}, |
| 1515 | {31, "31st"}, |
| 1516 | {101, "101st"}, |
| 1517 | {121, "121st"}, |
| 1518 | |
| 1519 | // Test 2nd endings |
| 1520 | {2, "2nd"}, |
| 1521 | {22, "22nd"}, |
| 1522 | {32, "32nd"}, |
| 1523 | {102, "102nd"}, |
| 1524 | {122, "122nd"}, |
| 1525 | |
| 1526 | // Test 3rd endings |
| 1527 | {3, "3rd"}, |
| 1528 | {23, "23rd"}, |
| 1529 | {33, "33rd"}, |
| 1530 | {103, "103rd"}, |
| 1531 | {123, "123rd"}, |
| 1532 | |
| 1533 | // Test th endings for other numbers |
| 1534 | {0, "0th"}, |
| 1535 | {4, "4th"}, |
| 1536 | {5, "5th"}, |
| 1537 | {6, "6th"}, |
| 1538 | {7, "7th"}, |
| 1539 | {8, "8th"}, |
| 1540 | {9, "9th"}, |
| 1541 | {10, "10th"}, |
| 1542 | {14, "14th"}, |
| 1543 | {20, "20th"}, |
| 1544 | {28, "28th"}, |
| 1545 | {100, "100th"}, |
| 1546 | {109, "109th"}, |
| 1547 | {1000, "1000th"}, |
| 1548 | } |
| 1549 | |
| 1550 | for _, tt := range tests { |
| 1551 | result := formatOrdinal(tt.input) |
| 1552 | if result != tt.want { |
| 1553 | t.Errorf("formatOrdinal(%d) = %s; want %s", tt.input, result, tt.want) |
| 1554 | } |
| 1555 | } |
| 1556 | } |