|
| 1 | +package health |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "mcpproxy-go/internal/contracts" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIsHealthy_WithHealthyLevel(t *testing.T) { |
| 12 | + health := &contracts.HealthStatus{ |
| 13 | + Level: LevelHealthy, |
| 14 | + AdminState: StateEnabled, |
| 15 | + Summary: "Connected (5 tools)", |
| 16 | + } |
| 17 | + |
| 18 | + assert.True(t, IsHealthy(health, false), "should return true when health.level is healthy") |
| 19 | + assert.True(t, IsHealthy(health, true), "should return true when health.level is healthy, ignoring legacy") |
| 20 | +} |
| 21 | + |
| 22 | +func TestIsHealthy_WithDegradedLevel(t *testing.T) { |
| 23 | + health := &contracts.HealthStatus{ |
| 24 | + Level: LevelDegraded, |
| 25 | + AdminState: StateEnabled, |
| 26 | + Summary: "Token expiring soon", |
| 27 | + } |
| 28 | + |
| 29 | + assert.False(t, IsHealthy(health, false), "should return false when health.level is degraded") |
| 30 | + assert.False(t, IsHealthy(health, true), "should return false when health.level is degraded, ignoring legacy") |
| 31 | +} |
| 32 | + |
| 33 | +func TestIsHealthy_WithUnhealthyLevel(t *testing.T) { |
| 34 | + health := &contracts.HealthStatus{ |
| 35 | + Level: LevelUnhealthy, |
| 36 | + AdminState: StateEnabled, |
| 37 | + Summary: "Connection error", |
| 38 | + } |
| 39 | + |
| 40 | + assert.False(t, IsHealthy(health, false), "should return false when health.level is unhealthy") |
| 41 | + assert.False(t, IsHealthy(health, true), "should return false when health.level is unhealthy, ignoring legacy") |
| 42 | +} |
| 43 | + |
| 44 | +func TestIsHealthy_NilHealthFallsBackToLegacy(t *testing.T) { |
| 45 | + // When health is nil, should fall back to legacy connected field |
| 46 | + assert.True(t, IsHealthy(nil, true), "should return legacy connected value when health is nil") |
| 47 | + assert.False(t, IsHealthy(nil, false), "should return legacy connected value when health is nil") |
| 48 | +} |
| 49 | + |
| 50 | +func TestIsHealthy_DisabledServerIsHealthy(t *testing.T) { |
| 51 | + // Disabled servers are intentionally not running, so they're considered "healthy" |
| 52 | + // (admin made a deliberate choice) |
| 53 | + health := &contracts.HealthStatus{ |
| 54 | + Level: LevelHealthy, |
| 55 | + AdminState: StateDisabled, |
| 56 | + Summary: "Disabled", |
| 57 | + } |
| 58 | + |
| 59 | + assert.True(t, IsHealthy(health, false), "disabled servers should be considered healthy") |
| 60 | +} |
| 61 | + |
| 62 | +func TestIsHealthy_QuarantinedServerIsHealthy(t *testing.T) { |
| 63 | + // Quarantined servers are intentionally blocked, so they're considered "healthy" |
| 64 | + // (admin made a deliberate choice) |
| 65 | + health := &contracts.HealthStatus{ |
| 66 | + Level: LevelHealthy, |
| 67 | + AdminState: StateQuarantined, |
| 68 | + Summary: "Quarantined for review", |
| 69 | + } |
| 70 | + |
| 71 | + assert.True(t, IsHealthy(health, false), "quarantined servers should be considered healthy") |
| 72 | +} |
0 commit comments