Skip to content

Commit

Permalink
tests: Add integration test for summary
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 8, 2024
1 parent 489a747 commit e2f05d7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
80 changes: 78 additions & 2 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ClientException;
use OC\TaskProcessing\SynchronousBackgroundJob;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -40,6 +41,8 @@ class FeatureContext implements Context, SnippetAcceptingContext {
/** @var array<int, string> */
protected static array $messageIdToText;
/** @var array<string, int> */
protected static array $aiTaskIds;
/** @var array<string, int> */
protected static array $remoteToInviteId;
/** @var array<int, string> */
protected static array $inviteIdToRemote;
Expand Down Expand Up @@ -113,6 +116,7 @@ class FeatureContext implements Context, SnippetAcceptingContext {
private ?SharingContext $sharingContext;

private array $guestsAppWasEnabled = [];
private array $testingAppWasEnabled = [];

private array $guestsOldWhitelist = [];

Expand Down Expand Up @@ -184,6 +188,7 @@ public function __construct() {
foreach (['LOCAL', 'REMOTE'] as $server) {
$this->changedConfigs[$server] = [];
$this->guestsAppWasEnabled[$server] = null;
$this->testingAppWasEnabled[$server] = null;
$this->guestsOldWhitelist[$server] = '';
}
}
Expand Down Expand Up @@ -2422,6 +2427,40 @@ public function userSharesRichObjectToRoom($user, $type, $id, $metaData, $identi
}
}

/**
* @Then /^user "([^"]*)" requests summary for "([^"]*)" starting from ("[^"]*"|'[^']*') with (\d+)(?: \((v1)\))?$/
*/
public function userSummarizesRoom(string $user, string $identifier, string $message, string $statusCode, string $apiVersion = 'v1', ?TableNode $tableNode = null): void {
$message = substr($message, 1, -1);
$fromMessageId = self::$textToMessageId[$message];

$this->setCurrentUser($user, $identifier);
$this->sendRequest(
'POST', '/apps/spreed/api/' . $apiVersion . '/chat/' . self::$identifierToToken[$identifier] . '/summarize',
['fromMessageId' => $fromMessageId],
);
$this->assertStatusCode($this->response, $statusCode);
sleep(1); // make sure Postgres manages the order of the messages

$response = $this->getDataFromResponse($this->response);
Assert::assertSame(self::$textToMessageId[$tableNode->getRowsHash()['nextOffset']], $response['nextOffset'], 'Offset ID does not match');
self::$aiTaskIds[$user . '/summary/' . self::$identifierToToken[$identifier]] = $response['taskId'];
}

/**
* @Then /^user "([^"]*)" receives summary for "([^"]*)" with (\d+)$/
*/
public function userReceivesSummary(string $user, string $identifier, string $statusCode, ?TableNode $tableNode = null): void {
$this->sendRequest(
'GET', '/taskprocessing/task/' . self::$aiTaskIds[$user . '/summary/' . self::$identifierToToken[$identifier]],
);
$this->assertStatusCode($this->response, $statusCode);
$response = $this->getDataFromResponse($this->response);
var_dump($response);
Assert::assertNotNull($response['task']['output'], 'Task output should not be null');
Assert::assertStringContainsString(self::$textToMessageId[$tableNode->getRowsHash()['contains']], $response['task']['output']);
}

/**
* @Then /^user "([^"]*)" creates a poll in room "([^"]*)" with (\d+)(?: \((v1)\))?$/
*
Expand Down Expand Up @@ -3881,6 +3920,25 @@ public function allowGuestAccountsCreation(): void {
$this->setCurrentUser($currentUser);
}

/**
* @Given /^Fake summary task provider is enabled$/
*/
public function enableTestingApp(): void {
$currentUser = $this->setCurrentUser('admin');

// save old state and restore at the end
$this->sendRequest('GET', '/cloud/apps?filter=enabled');
$this->assertStatusCode($this->response, 200);
$data = $this->getDataFromResponse($this->response);
$this->testingAppWasEnabled[$this->currentServer] = in_array('testing', $data['apps'], true);

if (!$this->testingAppWasEnabled[$this->currentServer]) {
$this->runOcc(['app:enable', 'testing']);
}

$this->setCurrentUser($currentUser);
}

/**
* @BeforeScenario
* @AfterScenario
Expand Down Expand Up @@ -3909,13 +3967,13 @@ public function resetSpreedAppData() {
/**
* @AfterScenario
*/
public function resetGuestsAppState() {
public function resetAppsState() {
foreach (['LOCAL', 'REMOTE'] as $server) {
$this->usingServer($server);

if ($this->guestsAppWasEnabled[$server] === null) {
// Guests app was not touched
return;
continue;
}

$currentUser = $this->setCurrentUser('admin');
Expand All @@ -3937,6 +3995,24 @@ public function resetGuestsAppState() {

$this->guestsAppWasEnabled[$server] = null;
}

foreach (['LOCAL', 'REMOTE'] as $server) {
$this->usingServer($server);

if ($this->testingAppWasEnabled[$server] === null) {
// Testing app was not touched
continue;
}

$currentUser = $this->setCurrentUser('admin');

// restore app's enabled state
$this->sendRequest($this->testingAppWasEnabled[$server] ? 'POST' : 'DELETE', '/cloud/apps/testing');

$this->setCurrentUser($currentUser);

$this->testingAppWasEnabled[$server] = null;
}
}

/*
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/features/chat-4/summary.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Feature: chat-4/summary
Background:
Given user "participant1" exists
Given user "participant2" exists

Scenario: sending a message clears unread counter for sender
Given Fake summary task provider is enabled
Given user "participant1" creates room "room" (v4)
| roomType | 2 |
| roomName | room |
And user "participant1" adds user "participant2" to room "room" with 200 (v4)
When user "participant1" sends message "Message 1" to room "room" with 201
And user "participant1" sends message "Message 2" to room "room" with 201
And user "participant1" sends message "Message 3" to room "room" with 201
And user "participant1" sends message "Message 4" to room "room" with 201
And user "participant2" requests summary for "room" starting from "Message 1" with 201
| nextOffset | Message 4 |
And force run "OC\TaskProcessing\SynchronousBackgroundJob" background jobs
Then user "participant2" receives summary for "room" with 200
| containing | Message 3 |

0 comments on commit e2f05d7

Please sign in to comment.