Address review feedback: Remove redundant Realtime triggers, correctly reorder hydration, and add E2E tests

This commit is contained in:
bhardwajparth51
2026-04-03 22:07:24 +05:30
parent ccc74dea74
commit 912ea37af6
7 changed files with 166 additions and 12 deletions
@@ -84,7 +84,6 @@ class Decrement extends Action
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -92,7 +91,7 @@ class Decrement extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, string $documentId, string $attribute, int|float $value, int|float|null $min, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Event $queueForRealtime, Context $usage, array $plan, Authorization $authorization, User $user): void
public function action(string $databaseId, string $collectionId, string $documentId, string $attribute, int|float $value, int|float|null $min, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, array $plan, Authorization $authorization, User $user): void
{
$isAPIKey = $user->isApp($authorization->getRoles());
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
@@ -219,7 +218,5 @@ class Decrement extends Action
->setContext('database', $database)
->setContext($this->getCollectionsEventsContext(), $collection)
->setPayload($response->getPayload(), sensitive: $relationships);
$queueForRealtime->from($queueForEvents)->trigger();
}
}
@@ -84,7 +84,6 @@ class Increment extends Action
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -92,7 +91,7 @@ class Increment extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, string $documentId, string $attribute, int|float $value, int|float|null $max, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Event $queueForRealtime, Context $usage, array $plan, Authorization $authorization, User $user): void
public function action(string $databaseId, string $collectionId, string $documentId, string $attribute, int|float $value, int|float|null $max, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, array $plan, Authorization $authorization, User $user): void
{
$isAPIKey = $user->isApp($authorization->getRoles());
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
@@ -219,7 +218,5 @@ class Increment extends Action
->setContext('database', $database)
->setContext($this->getCollectionsEventsContext(), $collection)
->setPayload($response->getPayload(), sensitive: $relationships);
$queueForRealtime->from($queueForEvents)->trigger();
}
}
@@ -65,7 +65,6 @@ class Decrement extends DecrementDocumentAttribute
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -65,7 +65,6 @@ class Increment extends IncrementDocumentAttribute
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -67,7 +67,6 @@ class Decrement extends DecrementDocumentAttribute
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -67,7 +67,6 @@ class Increment extends IncrementDocumentAttribute
->inject('dbForProject')
->inject('getDatabasesDB')
->inject('queueForEvents')
->inject('queueForRealtime')
->inject('usage')
->inject('plan')
->inject('authorization')
@@ -5248,4 +5248,168 @@ class RealtimeCustomClientTest extends Scope
$client->close();
}
public function testChannelDatabaseAtomicOperations()
{
$user = $this->getUser();
$session = $user['session'] ?? '';
$projectId = $this->getProject()['$id'];
$client = $this->getWebsocket(['documents', 'collections'], [
'origin' => 'http://localhost',
'cookie' => 'a_session_' . $projectId . '=' . $session,
], null);
$response = json_decode($client->receive(), true);
$this->assertEquals('connected', $response['type']);
/**
* Test Database Create
*/
$database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'databaseId' => ID::unique(),
'name' => 'Atomic DB',
]);
$databaseId = $database['body']['$id'];
/**
* Test Collection Create
*/
$actors = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'Atomic Actors',
'permissions' => [
Permission::create(Role::user($this->getUser()['$id'])),
],
'documentSecurity' => true,
]);
$actorsId = $actors['body']['$id'];
/**
* Test Attribute Create
*/
$scoreAttr = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'score',
'required' => true,
]);
$this->assertEventually(function () use ($databaseId, $actorsId) {
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/score', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals('available', $response['body']['status']);
}, 30000, 250);
/**
* Test Document Create
*/
$document = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'score' => 10
],
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]);
$documentId = $document['body']['$id'];
// Receive document create event
$client->receive();
/**
* Test Document Increment
*/
$increment = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents/' . $documentId . '/score/increment', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'value' => 5
]);
$this->assertEquals(200, $increment['headers']['status-code']);
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('event', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertArrayHasKey('timestamp', $response['data']);
$this->assertCount(8, $response['data']['channels']);
$this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}.update", $response['data']['events']);
$this->assertNotEmpty($response['data']['payload']);
$this->assertIsArray($response['data']['payload']);
$this->assertArrayHasKey('$id', $response['data']['payload']);
$this->assertEquals(15, $response['data']['payload']['score']);
// Wait a bit to ensure no event is received
sleep(1);
try {
$client->receive();
$this->fail('Should not receive duplicate event');
} catch (TimeoutException $e) {
// Expected - no event should be triggered
$this->assertTrue(true);
}
/**
* Test Document Decrement
*/
$decrement = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents/' . $documentId . '/score/decrement', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'value' => 3
]);
$this->assertEquals(200, $decrement['headers']['status-code']);
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('event', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertArrayHasKey('timestamp', $response['data']);
$this->assertCount(8, $response['data']['channels']);
$this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$documentId}.update", $response['data']['events']);
$this->assertNotEmpty($response['data']['payload']);
$this->assertIsArray($response['data']['payload']);
$this->assertArrayHasKey('$id', $response['data']['payload']);
$this->assertEquals(12, $response['data']['payload']['score']);
// Wait a bit to ensure no event is received
sleep(1);
try {
$client->receive();
$this->fail('Should not receive duplicate event');
} catch (TimeoutException $e) {
// Expected - no event should be triggered
$this->assertTrue(true);
}
$client->close();
}
}