From f455bb7a58c67cc669d94d4a57e517e16ea96dc9 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 May 2026 17:11:49 +0530 Subject: [PATCH] refactor: add checks for queueForEvents and queueForRealtime in container setup; enhance presence resolution in tests --- app/realtime.php | 21 ++++--- tests/e2e/Services/Presence/PresenceBase.php | 58 +++++++++++++++----- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 87efec02b0..3221f83e95 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -404,15 +404,20 @@ $container->set('pools', function ($register) { return $register->get('pools'); }, ['register']); -$container->set('queueForEvents', function ($pools) { - return new QueueEvent(new BrokerPool( - publisher: $pools->get('publisher') - )); -}, ['pools']); +if (!$container->has('queueForEvents')) { + $container->set('queueForEvents', function ($pools) { + var_dump("ce"); + return new QueueEvent(new BrokerPool( + publisher: $pools->get('publisher') + )); + }, ['pools']); +} -$container->set('queueForRealtime', function () { - return new QueueRealtime(); -}, []); +if (!$container->has('queueForRealtime')) { + $container->set('queueForRealtime', function () { + return new QueueRealtime(); + }, []); +} $realtime = getRealtime(); $presenceState = new PresenceState(); diff --git a/tests/e2e/Services/Presence/PresenceBase.php b/tests/e2e/Services/Presence/PresenceBase.php index 9131af18dd..8f05b4144a 100644 --- a/tests/e2e/Services/Presence/PresenceBase.php +++ b/tests/e2e/Services/Presence/PresenceBase.php @@ -105,6 +105,26 @@ trait PresenceBase return $presence; } + protected function resolvePresenceForUser(string $userId, array $headers): array + { + $presence = $this->client->call( + Client::METHOD_GET, + '/presences', + $headers, + [ + 'queries' => [ + Query::equal('userId', [$userId])->toString(), + ], + ] + ); + + $this->assertEquals(200, $presence['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $presence['body']['total'] ?? 0); + $this->assertNotEmpty($presence['body']['presences'][0] ?? []); + + return $presence['body']['presences'][0]; + } + public function testUpsertAndGetPresence(): void { if ($this->getSide() === 'client') { @@ -270,16 +290,12 @@ trait PresenceBase } $projectId = $this->getProject()['$id']; - $user1 = $this->getUser(); - $headersUser1 = $this->getHeaders(false); - + $user1 = $this->getUser(true); $user2 = $this->getUser(true); - - // Avoid overwriting the cached user for the rest of the test run. - self::$user[$projectId] = $user1; - - $headersUser2 = $this->getHeaders(false); - $headersUser2['cookie'] = 'a_session_' . $projectId . '=' . $user2['session']; + $headersUser2 = [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_' . $projectId . '=' . $user2['session'], + ]; $permissionsForUser2 = [ Permission::read(Role::user($user2['$id'])), @@ -430,7 +446,14 @@ trait PresenceBase ); $this->assertEquals(200, $upsert['headers']['status-code']); - $presenceId = $upsert['body']['$id']; + $presence = $this->resolvePresenceForUser( + $upsert['body']['userId'], + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders(false)) + ); + $presenceId = $presence['$id']; $update = $this->client->call( Client::METHOD_PATCH, @@ -498,7 +521,14 @@ trait PresenceBase ); $this->assertEquals(200, $upsert['headers']['status-code']); - $presenceId = $upsert['body']['$id']; + $presence = $this->resolvePresenceForUser( + $upsert['body']['userId'], + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders(false)) + ); + $presenceId = $presence['$id']; $delete = $this->client->call( Client::METHOD_DELETE, @@ -547,11 +577,11 @@ trait PresenceBase ] ); $this->assertEquals(200, $upsert['headers']['status-code']); - $presence = $upsert['body']; $headers = \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders(false)); + $presence = $this->resolvePresenceForUser($upsert['body']['userId'], $headers); } else { $presence = $this->setupPresence([ 'status' => 'cache-update-setup', @@ -619,11 +649,11 @@ trait PresenceBase ] ); $this->assertEquals(200, $upsert['headers']['status-code']); - $presence = $upsert['body']; $headers = \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders(false)); + $presence = $this->resolvePresenceForUser($upsert['body']['userId'], $headers); } else { $presence = $this->setupPresence([ 'status' => 'cache-purge-only-setup', @@ -690,11 +720,11 @@ trait PresenceBase ] ); $this->assertEquals(200, $upsert['headers']['status-code']); - $presence = $upsert['body']; $headers = \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders(false)); + $presence = $this->resolvePresenceForUser($upsert['body']['userId'], $headers); } else { $presence = $this->setupPresence([ 'status' => 'cache-delete-setup',