Skip to content

Commit

Permalink
Test string representations for OpenAI, AzureAI & Realtime APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Nov 2, 2024
1 parent 587c1ca commit ddb1cdd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/main/php/com/openai/realtime/RealtimeApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use com\openai\Tools;
use com\openai\tools\Functions;
use lang\IllegalStateException;
use lang\{IllegalStateException, Value};
use text\json\Json;
use util\URI;
use util\data\Marshalling;
use util\log\Traceable;
use util\{Comparison, URI};
use websocket\WebSocket;

/**
Expand All @@ -17,7 +17,9 @@
* @test com.openai.unittest.RealtimeApiTest
* @see https://platform.openai.com/docs/guides/realtime
*/
class RealtimeApi implements Traceable {
class RealtimeApi implements Traceable, Value {
use Comparison;

private $ws, $marshalling;
private $cat= null;

Expand Down Expand Up @@ -117,4 +119,16 @@ public function transmit($payload) {
public function __destruct() {
$this->ws && $this->ws->close();
}

/** @return string */
public function toString() {
$socket= $this->ws->socket();
return sprintf(
'%s(->wss://%s:%d%s)',
nameof($this),
$socket->host,
$socket->port,
$this->ws->path()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public function version_extracted_from_uri() {
public function api_key_header_set() {
Assert::equals('1e51...', $this->fixture(self::URI)->headers()['API-Key']);
}

#[Test]
public function string_representation() {
Assert::equals(
'com.openai.rest.AzureAIEndpoint(->https://test.openai.azure.com/openai/deployments/omni/?api-version=2024-02-01)',
$this->fixture(self::URI, '2024-02-01')->toString()
);
}
}
8 changes: 8 additions & 0 deletions src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public function optional_project_header() {
$this->fixture(self::URI, 'org-test', 'prj-test')->headers()['OpenAI-Project']
);
}

#[Test]
public function string_representation() {
Assert::equals(
'com.openai.rest.OpenAIEndpoint(->https://api.openai.example.com/v1/)',
$this->fixture(self::URI)->toString()
);
}
}
11 changes: 10 additions & 1 deletion src/test/php/com/openai/unittest/RealtimeApiTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use test\{Assert, Expect, Test, Values};

class RealtimeApiTest {
const URI= 'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01';
const SESSION_CREATED= '{"type": "session.created"}';

/** Returns authentications */
Expand All @@ -15,7 +16,7 @@ private function authentications(): iterable {

#[Test]
public function can_create() {
new RealtimeApi('wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01');
new RealtimeApi(self::URI);
}

#[Test]
Expand Down Expand Up @@ -91,4 +92,12 @@ public function transmit() {

Assert::equals(['type' => 'conversation.item.created'], $response);
}

#[Test]
public function string_representation() {
Assert::equals(
'com.openai.realtime.RealtimeApi(->wss://api.openai.com:443/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01)',
(new RealtimeApi(self::URI))->toString()
);
}
}

0 comments on commit ddb1cdd

Please sign in to comment.