Skip to content

Commit

Permalink
WIP: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 10, 2023
1 parent 1a3c656 commit 878500b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,8 @@ public function requestObject(Payload $payload): Response
$response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request));

$contents = $response->getBody()->getContents();
// echo var_export((json_decode($contents, true, 512, JSON_THROW_ON_ERROR)));
// exit();

if (str_contains($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value)) {
return Response::from($contents, $response->getHeaders());
}

$this->throwIfJsonError($response, $contents);

try {
/** @var array{error?: array{message: string, type: string, code: string}} $data */
$data = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $jsonException) {
throw new UnserializableResponse($jsonException);
}

return Response::from($data, $response->getHeaders());
echo var_export((json_decode($contents, true, 512, JSON_THROW_ON_ERROR)));
exit();
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Resources/Threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,43 @@
->toBeInstanceOf(MetaInformation::class);
});

test('create and run', function () {
$client = mockClient('POST', 'threads', [
'assistant_id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'thread' => [
'messages' => [
[
'role' => 'user',
'content' => 'Explain deep learning to a 5 year old.',
],
],
],
], Response::from(threadResource(), metaHeaders()));

$result = $client->threads()->createAndRun([
'assistant_id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'thread' => [
'messages' => [
[
'role' => 'user',
'content' => 'Explain deep learning to a 5 year old.',
],
],
],
]);

expect($result)
->toBeInstanceOf(ThreadResponse::class)
->id->toBe('thread_agvtHUGezjTCt4SKgQg0NJ2Y')
->object->toBe('thread')
->createdAt->toBe(1699621778)
->metadata->toBeArray()
->metadata->toBeEmpty();

expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});

test('modify', function () {
$client = mockClient('POST', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y', [
'metadata' => [
Expand Down

0 comments on commit 878500b

Please sign in to comment.