added browser tests
This commit is contained in:
@@ -5,61 +5,82 @@ namespace Tests\Browser;
|
||||
use App\Models\Dynamic;
|
||||
use App\Models\User;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
test('multiple sessions can communicate in real time through websockets', function () {
|
||||
// 1. Create realistic database state
|
||||
$owner = User::factory()->create([
|
||||
'name' => 'TU Test User',
|
||||
'email' => 'test-owner@example.com',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
class RealtimeChatTest extends DuskTestCase
|
||||
{
|
||||
/**
|
||||
* Test that multiple browser sessions can communicate in real time through websockets.
|
||||
*/
|
||||
public function test_multiple_sessions_can_communicate_in_real_time(): void
|
||||
{
|
||||
// 1. Create realistic database state
|
||||
$owner = User::factory()->create([
|
||||
'name' => 'Owner Alice',
|
||||
'email' => 'alice-owner-' . uniqid() . '@example.com',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
|
||||
$participant = User::factory()->create([
|
||||
'name' => 'Submissive Bob',
|
||||
'email' => 'test-sub@example.com',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
$participant = User::factory()->create([
|
||||
'name' => 'Submissive Bob',
|
||||
'email' => 'bob-participant-' . uniqid() . '@example.com',
|
||||
'password' => bcrypt('password'),
|
||||
]);
|
||||
|
||||
$dynamic = Dynamic::create([
|
||||
'name' => 'The Test Sanctuary',
|
||||
'rules' => 'Rules for realtime testing.',
|
||||
]);
|
||||
$dynamic = Dynamic::create([
|
||||
'name' => 'The Velvet Realtime Test Sanctuary',
|
||||
'rules' => 'Rules for realtime testing.',
|
||||
]);
|
||||
|
||||
$dynamic->participants()->attach($owner->id, ['role' => 'owner']);
|
||||
$dynamic->participants()->attach($participant->id, ['role' => 'participant']);
|
||||
$dynamic->participants()->attach($owner->id, ['role' => 'owner']);
|
||||
$dynamic->participants()->attach($participant->id, ['role' => 'participant']);
|
||||
|
||||
// 2. Spawn two separate browser sessions/browsers in parallel
|
||||
$this->browse(function (Browser $sessionA, Browser $sessionB) use ($dynamic, $owner, $participant) {
|
||||
// 2. Spawn two separate browser sessions/browsers in parallel
|
||||
$this->browse(function (Browser $sessionA, Browser $sessionB) use ($dynamic, $owner, $participant) {
|
||||
try {
|
||||
// --- SESSION A: Owner ---
|
||||
$sessionA->loginAs($owner)
|
||||
->visit('/dynamics/' . $dynamic->uuid)
|
||||
->waitForText('The Velvet Realtime Test Sanctuary')
|
||||
->assertSee('Owner Alice');
|
||||
|
||||
// --- SESSION A: Owner ---
|
||||
$sessionA->loginAs($owner)
|
||||
->visit(route('dynamics.show', $dynamic))
|
||||
->waitForText('The Test Sanctuary')
|
||||
->assertSee('TU Test User'); // Verify loaded in as Owner
|
||||
// --- SESSION B: Participant ---
|
||||
$sessionB->loginAs($participant)
|
||||
->visit('/dynamics/' . $dynamic->uuid)
|
||||
->waitForText('The Velvet Realtime Test Sanctuary')
|
||||
->assertSee('Submissive Bob');
|
||||
|
||||
// --- SESSION B: Participant ---
|
||||
$sessionB->loginAs($participant)
|
||||
->visit(route('dynamics.show', $dynamic))
|
||||
->waitForText('The Test Sanctuary')
|
||||
->assertSee('Submissive Bob'); // Verify loaded in as Submissive/Participant
|
||||
// --- REAL-TIME COMMUNICATING ---
|
||||
// Owner types and sends a message in chat
|
||||
$sessionA->type('#content', 'Hello Submissive Bob, did you complete your daily chores?')
|
||||
->click('.c-chat__button')
|
||||
->waitForText('Hello Submissive Bob, did you complete your daily chores?');
|
||||
|
||||
// --- REAL-TIME COMMUNICATING ---
|
||||
// Owner types and sends a message in chat
|
||||
$sessionA->type('#content', 'Hello Submissive Bob, did you complete your daily chores?')
|
||||
->click('.c-chat__button')
|
||||
->waitForText('Hello Submissive Bob');
|
||||
// Since websockets broadcast in real-time, Session B receives it without reloading
|
||||
$sessionB->waitForText('Hello Submissive Bob, did you complete your daily chores?', 10)
|
||||
->assertSee('Hello Submissive Bob, did you complete your daily chores?');
|
||||
|
||||
// Since websockets broadcast in real-time, Session B receives it without reloading
|
||||
$sessionB->waitForText('Hello Submissive Bob', 5)
|
||||
->assertSee('Hello Submissive Bob, did you complete your daily chores?');
|
||||
// Participant replies in real-time
|
||||
$sessionB->type('#content', 'Yes Master, everything is complete and logged!')
|
||||
->click('.c-chat__button')
|
||||
->waitForText('Yes Master, everything is complete and logged!');
|
||||
|
||||
// Participant replies in real-time
|
||||
$sessionB->type('#content', 'Yes Master, everything is complete and logged in the ledger!')
|
||||
->click('.c-chat__button')
|
||||
->waitForText('Yes Master, everything is complete');
|
||||
// Session A receives the reply in real-time without reloading
|
||||
$sessionA->waitForText('Yes Master, everything is complete and logged!', 10)
|
||||
->assertSee('Yes Master, everything is complete and logged!');
|
||||
} catch (\Exception $e) {
|
||||
echo "\n=== SESSION A CONSOLE LOGS ===\n";
|
||||
print_r($sessionA->driver->manage()->getLog('browser'));
|
||||
echo "\n=== SESSION B CONSOLE LOGS ===\n";
|
||||
print_r($sessionB->driver->manage()->getLog('browser'));
|
||||
throw $e;
|
||||
}
|
||||
});
|
||||
|
||||
// Session A receives the reply in real-time without reloading
|
||||
$sessionA->waitForText('Yes Master, everything is complete', 5)
|
||||
->assertSee('Yes Master, everything is complete and logged in the ledger!');
|
||||
});
|
||||
});
|
||||
// Clean up
|
||||
$dynamic->participants()->detach();
|
||||
$dynamic->delete();
|
||||
$owner->delete();
|
||||
$participant->delete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user