|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Cachet\Enums\ComponentGroupVisibilityEnum; |
| 4 | +use Cachet\Enums\IncidentStatusEnum; |
3 | 5 | use Cachet\Enums\ResourceVisibilityEnum; |
| 6 | +use Cachet\Models\Component; |
4 | 7 | use Cachet\Models\ComponentGroup; |
| 8 | +use Cachet\Models\Incident; |
5 | 9 |
|
6 | 10 | it('can have components', function () { |
7 | 11 | $group = ComponentGroup::factory()->hasComponents(2)->create(); |
|
21 | 25 | ->and(ComponentGroup::query()->visibility(ResourceVisibilityEnum::guest)->count())->toBe(1) |
22 | 26 | ->and(ComponentGroup::query()->visibility(ResourceVisibilityEnum::hidden)->count())->toBe(1); |
23 | 27 | }); |
| 28 | + |
| 29 | +it('is always collapsed when collapsed option is set', function () { |
| 30 | + $group = ComponentGroup::factory() |
| 31 | + ->hasComponents(1) |
| 32 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed]); |
| 33 | + |
| 34 | + expect($group->isExpanded())->toBeFalse(); |
| 35 | +}); |
| 36 | + |
| 37 | +it('is always expanded when expanded option is set', function () { |
| 38 | + $group = ComponentGroup::factory() |
| 39 | + ->hasComponents(1) |
| 40 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::expanded]); |
| 41 | + |
| 42 | + expect($group->isExpanded())->toBeTrue(); |
| 43 | +}); |
| 44 | + |
| 45 | +it('is collapsed when collapsed_unless_incident is set and no incidents exist', function () { |
| 46 | + $group = ComponentGroup::factory() |
| 47 | + ->hasComponents(1) |
| 48 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 49 | + |
| 50 | + expect($group->isExpanded())->toBeFalse(); |
| 51 | +}); |
| 52 | + |
| 53 | +it('is expanded when collapsed_unless_incident is set and an unresolved incident exists', function () { |
| 54 | + $group = ComponentGroup::factory() |
| 55 | + ->hasComponents(1) |
| 56 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 57 | + |
| 58 | + $component = $group->components->first(); |
| 59 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::investigating]); |
| 60 | + $incident->components()->attach($component); |
| 61 | + |
| 62 | + expect($group->isExpanded())->toBeTrue(); |
| 63 | +}); |
| 64 | + |
| 65 | +it('is collapsed when collapsed_unless_incident is set and only resolved incidents exist', function () { |
| 66 | + $group = ComponentGroup::factory() |
| 67 | + ->hasComponents(1) |
| 68 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 69 | + |
| 70 | + $component = $group->components->first(); |
| 71 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::fixed]); |
| 72 | + $incident->components()->attach($component); |
| 73 | + |
| 74 | + expect($group->isExpanded())->toBeFalse(); |
| 75 | +}); |
| 76 | + |
| 77 | +it('expands when an incident is started on a component', function () { |
| 78 | + $group = ComponentGroup::factory() |
| 79 | + ->hasComponents(1) |
| 80 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 81 | + |
| 82 | + $component = $group->components->first(); |
| 83 | + |
| 84 | + // Initially collapsed |
| 85 | + expect($group->isExpanded())->toBeFalse(); |
| 86 | + |
| 87 | + // Create an incident and attach it to the component |
| 88 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::investigating]); |
| 89 | + $incident->components()->attach($component); |
| 90 | + |
| 91 | + // Should now be expanded |
| 92 | + expect($group->fresh()->isExpanded())->toBeTrue(); |
| 93 | +}); |
| 94 | + |
| 95 | +it('collapses when all incidents are resolved', function () { |
| 96 | + $group = ComponentGroup::factory() |
| 97 | + ->hasComponents(1) |
| 98 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 99 | + |
| 100 | + $component = $group->components->first(); |
| 101 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::investigating]); |
| 102 | + $incident->components()->attach($component); |
| 103 | + |
| 104 | + // Should be expanded with active incident |
| 105 | + expect($group->isExpanded())->toBeTrue(); |
| 106 | + |
| 107 | + // Mark incident as fixed |
| 108 | + $incident->update(['status' => IncidentStatusEnum::fixed]); |
| 109 | + |
| 110 | + // Should now be collapsed |
| 111 | + expect($group->fresh()->isExpanded())->toBeFalse(); |
| 112 | +}); |
| 113 | + |
| 114 | +it('has active incident returns true for all unresolved statuses', function (IncidentStatusEnum $status) { |
| 115 | + $group = ComponentGroup::factory() |
| 116 | + ->hasComponents(1) |
| 117 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 118 | + |
| 119 | + $component = $group->components->first(); |
| 120 | + $incident = Incident::factory()->create(['status' => $status]); |
| 121 | + $incident->components()->attach($component); |
| 122 | + |
| 123 | + expect($group->hasActiveIncident())->toBeTrue(); |
| 124 | +})->with([ |
| 125 | + 'unknown' => IncidentStatusEnum::unknown, |
| 126 | + 'investigating' => IncidentStatusEnum::investigating, |
| 127 | + 'identified' => IncidentStatusEnum::identified, |
| 128 | + 'watching' => IncidentStatusEnum::watching, |
| 129 | +]); |
| 130 | + |
| 131 | +it('has active incident returns false for fixed status', function () { |
| 132 | + $group = ComponentGroup::factory() |
| 133 | + ->hasComponents(1) |
| 134 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 135 | + |
| 136 | + $component = $group->components->first(); |
| 137 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::fixed]); |
| 138 | + $incident->components()->attach($component); |
| 139 | + |
| 140 | + expect($group->hasActiveIncident())->toBeFalse(); |
| 141 | +}); |
| 142 | + |
| 143 | +it('expands when any component in the group has an active incident', function () { |
| 144 | + $group = ComponentGroup::factory() |
| 145 | + ->hasComponents(3) |
| 146 | + ->create(['collapsed' => ComponentGroupVisibilityEnum::collapsed_unless_incident]); |
| 147 | + |
| 148 | + $components = $group->components; |
| 149 | + |
| 150 | + // No incidents - should be collapsed |
| 151 | + expect($group->isExpanded())->toBeFalse(); |
| 152 | + |
| 153 | + // Add incident to just one component |
| 154 | + $incident = Incident::factory()->create(['status' => IncidentStatusEnum::investigating]); |
| 155 | + $incident->components()->attach($components->first()); |
| 156 | + |
| 157 | + // Should be expanded |
| 158 | + expect($group->fresh()->isExpanded())->toBeTrue(); |
| 159 | +}); |
0 commit comments