-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/php/com/openai/unittest/OpenAIEndpointTest.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php namespace com\openai\unittest; | ||
|
||
use com\openai\rest\OpenAIEndpoint; | ||
use test\{Assert, Test}; | ||
use webservices\rest\TestEndpoint; | ||
|
||
class OpenAIEndpointTest { | ||
const URI= 'https://[email protected]/v1'; | ||
|
||
/** Returns a testing API endpoint */ | ||
private function testingEndpoint(): TestEndpoint { | ||
return new TestEndpoint([ | ||
'POST /chat/completions' => function($call) { | ||
if ($call->request()->payload()->value()['stream'] ?? false) { | ||
$headers= ['Content-Type' => 'text/event-stream']; | ||
$payload= implode("\n", [ | ||
'data: {"choices":[{"delta":{"role":"assistant"}}]}', | ||
'data: {"choices":[{"delta":{"content":"Test"}}]}', | ||
'data: [DONE]', | ||
]); | ||
} else { | ||
$headers= ['Content-Type' => 'application/json']; | ||
$payload= '{"choices":[{"message":{"role":"assistant","content":"Test"}}]}'; | ||
} | ||
|
||
return $call->respond(200, 'OK', $headers, $payload); | ||
} | ||
]); | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new OpenAIEndpoint(self::URI); | ||
} | ||
|
||
#[Test] | ||
public function invoke() { | ||
$endpoint= new OpenAIEndpoint($this->testingEndpoint()); | ||
Assert::equals( | ||
['choices' => [['message' => ['role' => 'assistant', 'content' => 'Test']]]], | ||
$endpoint->api('/chat/completions')->invoke(['stream' => false]) | ||
); | ||
} | ||
|
||
#[Test] | ||
public function stream() { | ||
$endpoint= new OpenAIEndpoint($this->testingEndpoint()); | ||
Assert::equals( | ||
['choices' => [['message' => ['role' => 'assistant', 'content' => 'Test']]]], | ||
$endpoint->api('/chat/completions')->stream(['stream' => true])->result() | ||
); | ||
} | ||
} |