Commit f74437c
authored
[test-improver] Improve tests for server/circuit_breaker (#3837)
## File Analyzed
- **Test File**: `internal/server/circuit_breaker_test.go`
- **Package**: `internal/server`
- **Lines Added**: +129
## Improvements Made
### 1. Better Testing Patterns
- ✅ All new tests use table-driven structure with `t.Parallel()`
subtests
- ✅ Descriptive test names following `TestType_Scenario` convention
- ✅ Uses `require` for fatal preconditions, `assert` for non-fatal
checks
### 2. Increased Coverage
Four previously untested code paths now have direct tests:
| New Test | Code Path Covered |
|---|---|
| `TestCircuitBreakerState_String` | `String()` method for all 4 states
including defensive `default: "UNKNOWN"` case |
| `TestFormatResetAt` | `formatResetAt()` helper — zero time and
non-zero RFC3339+duration output |
| `TestIsRateLimitText_Direct` | `isRateLimitText()` — all 5 match
patterns plus edge cases (case insensitivity, empty string, `"rate
limit"` without qualifier) |
| `TestCircuitBreaker_RecordRateLimitWhenAlreadyOpen` |
`RecordRateLimit()` OPEN→OPEN path — verifies `resetAt` is updated on
subsequent errors while circuit stays OPEN |
Previously these were either:
- **Never tested**: `String()` default case, `formatResetAt()` directly
- **Only tested indirectly**: `isRateLimitText()` via
`isRateLimitToolResult()`, the OPEN→OPEN path via logging only
### 3. No Regressions
- All existing tests are preserved unchanged
- No test helpers or fixtures modified
## Why These Changes?
The circuit breaker is security-critical infrastructure (rate-limit
protection). The four gaps found were:
1. The `String()` method's `default` case is defensive code that can
never occur with valid iota values — but it should be documented via a
test so future readers know it exists and won't get dropped in
refactoring.
2. `formatResetAt` is called in `ErrCircuitOpen.Error()` and several log
lines but had no isolated test — a regression in its formatting would
only surface through integration test failures.
3. `isRateLimitText` has 5 OR-combined patterns. The indirect tests
covered only 4 of them through `isRateLimitToolResult`. The "rate limit
combined with 403" branch (`strings.Contains(lower, "rate limit") &&
strings.Contains(lower, "403")`) had no test for a string that contains
`"rate limit"` without `"403"` being present — confirming the boundary
between true/false is correctly placed.
4. The OPEN→OPEN `RecordRateLimit` path (updating `resetAt` while
already open) is a normal operational scenario (multiple rate-limit
errors in a row) that had no test at all.
---
*Generated by Test Improver Workflow*
*Focuses on better patterns, increased coverage, and more stable tests*
> Generated by [Test
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/24452358045/agentic_workflow)
· ● 14.7M ·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, model:
auto, id: 24452358045, workflow_id: test-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/24452358045 -->
<!-- gh-aw-workflow-id: test-improver -->1 file changed
Lines changed: 129 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
433 | 433 | | |
434 | 434 | | |
435 | 435 | | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
0 commit comments