Keep guest rooms moderated and restore owner moderation
CI/CD / test_and_check_app (push) Successful in 2m22s
CI/CD / Publish immutable image (push) Successful in 1m16s

This commit is contained in:
2026-09-24 15:02:05 +02:00
parent 4dfe79ce5c
commit 33df9e49ab
6 changed files with 267 additions and 11 deletions
+92 -2
View File
@@ -4,6 +4,7 @@ use App\Enums\RoomStateChange;
use App\Events\RoomStateChanged;
use App\Jobs\RemoveAbsentPlayers;
use App\Models\Room;
use App\Models\User;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
@@ -13,7 +14,7 @@ test('the player table no longer stores an inactive status', function () {
expect(Schema::hasColumn('players', 'inactive_at'))->toBeFalse();
});
test('the migration removes old inactive and absent players while keeping present players', function () {
test('migrations remove absent players and reconcile guest rooms', function () {
Schema::table('players', function (Blueprint $table): void {
$table->timestamp('inactive_at')->nullable();
});
@@ -34,15 +35,31 @@ test('the migration removes old inactive and absent players while keeping presen
'name' => 'Anna',
'last_seen_at' => now(),
]);
$emptyGuestRoom = Room::create([
'title' => 'Empty guest room',
'deck_config' => ['name' => 'Fibonacci', 'cards' => []],
]);
$owner = User::factory()->create();
$emptyAccountRoom = Room::create([
'owner_id' => $owner->id,
'is_persistent' => true,
'title' => 'Persistent account room',
'deck_config' => ['name' => 'Fibonacci', 'cards' => []],
]);
DB::table('players')->where('id', $previouslyInactive->id)->update(['inactive_at' => now()]);
$migration = require database_path('migrations/2026_09_24_123048_remove_previously_inactive_players.php');
$migration->up();
$guestRoomMigration = require database_path('migrations/2026_09_24_125529_reconcile_guest_rooms_after_player_cleanup.php');
$guestRoomMigration->up();
$this->assertModelMissing($previouslyInactive);
$this->assertModelMissing($absent);
$this->assertModelExists($present);
$this->assertSoftDeleted($emptyGuestRoom);
$this->assertModelExists($emptyAccountRoom);
expect($present->fresh()->is_moderator)->toBeTrue();
expect(Schema::hasColumn('players', 'inactive_at'))->toBeFalse();
});
@@ -54,6 +71,7 @@ test('the timeout removes absent players and their votes but keeps recent player
]);
$absentPlayer = $room->players()->create([
'name' => 'Nico',
'is_moderator' => true,
'last_seen_at' => now()->subHour(),
]);
$recentPlayer = $room->players()->create([
@@ -76,12 +94,14 @@ test('the timeout removes absent players and their votes but keeps recent player
$this->assertModelMissing($vote);
$this->assertModelExists($recentPlayer);
expect($room->fresh()->state_version)->toBe($previousVersion + 1);
expect($recentPlayer->fresh()->is_moderator)->toBeTrue();
Event::assertDispatched(
RoomStateChanged::class,
fn (RoomStateChanged $event): bool => $event->change === RoomStateChange::PlayerKicked
&& $event->affectedPlayerId === $absentPlayer->id
&& collect($event->state['players'])->doesntContain('id', $absentPlayer->id),
&& collect($event->state['players'])->doesntContain('id', $absentPlayer->id)
&& collect($event->state['players'])->firstWhere('id', $recentPlayer->id)['is_moderator'] === true,
);
Event::assertDispatchedTimes(RoomStateChanged::class, 1);
@@ -92,3 +112,73 @@ test('the timeout removes absent players and their votes but keeps recent player
expect($room->players()->where('name', 'Nico')->sole()->id)->not->toBe($absentPlayer->id);
});
test('the timeout deletes a guest room when its last player is removed', function () {
$room = Room::create([
'title' => 'Guest planning',
'deck_config' => ['name' => 'Fibonacci', 'cards' => []],
]);
$moderator = $room->players()->create([
'name' => 'Nico',
'is_moderator' => true,
'last_seen_at' => now()->subHour(),
]);
Event::fake([RoomStateChanged::class]);
app()->call([new RemoveAbsentPlayers, 'handle']);
app()->call([new RemoveAbsentPlayers, 'handle']);
$this->assertModelMissing($moderator);
$this->assertSoftDeleted($room);
Event::assertDispatched(
RoomStateChanged::class,
fn (RoomStateChanged $event): bool => $event->roomId === $room->id
&& $event->change === RoomStateChange::RoomDeleted,
);
Event::assertDispatchedTimes(RoomStateChanged::class, 1);
$this->post(route('room.code'), [
'join_code' => $room->join_code,
'player_name' => 'Nico',
])->assertSessionHasErrors('join_code');
});
test('the owner regains moderation when joining a persistent room after a timeout', function () {
$owner = User::factory()->create();
$room = Room::create([
'owner_id' => $owner->id,
'is_persistent' => true,
'title' => 'Permanent planning',
'deck_config' => ['name' => 'Fibonacci', 'cards' => []],
]);
$oldModerator = $room->players()->create([
'user_id' => $owner->id,
'name' => 'Nico',
'is_moderator' => true,
'last_seen_at' => now()->subHour(),
]);
$guest = $room->players()->create([
'name' => 'Anna',
'last_seen_at' => now(),
]);
app()->call([new RemoveAbsentPlayers, 'handle']);
$this->assertModelMissing($oldModerator);
$this->assertModelExists($room);
expect($guest->fresh()->is_moderator)->toBeFalse();
$this->actingAs($owner)
->withSession(["room_players.{$room->id}" => $oldModerator->id])
->post(route('room.code'), [
'join_code' => $room->join_code,
'player_name' => 'Nico',
])
->assertRedirect(route('room.show', $room));
$newModerator = $room->players()->where('user_id', $owner->id)->sole();
expect($newModerator->id)->not->toBe($oldModerator->id)
->and($newModerator->is_moderator)->toBeTrue();
});