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 db7d1ce commit 9a20f1e
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/Fixtures/ThreadMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* @return array<string, mixed>
*/
function threadMessageResource(): array
{
return [
'id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
'object' => 'thread.message',
'created_at' => 1699623839,
'thread_id' => 'thread_agvtHUGezjTCt4SKgQg0NJ2Y',
'role' => 'user',
'content' => [
[
'type' => 'text',
'text' => [
'value' => 'How does AI work? Explain it in simple terms.',
'annotations' => [
],
],
],
],
'file_ids' => [
'file-DhxjnFCaSHc4ZELRGKwTMFtI',
],
'assistant_id' => null,
'run_id' => null,
'metadata' => [],
];
}

/**
* @return array<string, mixed>
*/
function threadMessageListResource(): array
{
return [
'object' => 'list',
'data' => [
threadMessageResource(),
threadMessageResource(),
],
'first_id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
'last_id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
'has_more' => false,
];
}

/**
* @return array<string, mixed>
*/
function threadMessageDeleteResource(): array
{
return [
'id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ',
'object' => 'thread.message.deleted',
'deleted' => true,
];
}
139 changes: 139 additions & 0 deletions tests/Resources/ThreadsMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponseContentTextObject;
use OpenAI\ValueObjects\Transporter\Response;

test('list', function () {
$client = mockClient('GET', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages', [], Response::from(threadMessageListResource(), metaHeaders()));

$result = $client->threads()->messages()->list('thread_agvtHUGezjTCt4SKgQg0NJ2Y');

expect($result)
->toBeInstanceOf(ThreadMessageListResponse::class)
->object->toBe('list')
->data->toBeArray()->toHaveCount(2)
->data->each->toBeInstanceOf(ThreadMessageResponse::class)
->firstId->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->lastId->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->hasMore->toBeFalse();

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

test('create', function () {
$client = mockClient('POST', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages', [
'role' => 'user',
'content' => 'How does AI work? Explain it in simple terms.',
'file_ids' => [
'file-DhxjnFCaSHc4ZELRGKwTMFtI',
],
], Response::from(threadMessageResource(), metaHeaders()));

$result = $client->threads()->messages()->create('thread_agvtHUGezjTCt4SKgQg0NJ2Y', [
'role' => 'user',
'content' => 'How does AI work? Explain it in simple terms.',
'file_ids' => [
'file-DhxjnFCaSHc4ZELRGKwTMFtI',
],
]);

expect($result)
->toBeInstanceOf(ThreadMessageResponse::class)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message')
->createdAt->toBe(1699623839)
->threadId->toBe('thread_agvtHUGezjTCt4SKgQg0NJ2Y')
->role->toBe('user')
->content->toBeArray()
->content->toHaveCount(1)
->content->each->toBeInstanceOf(ThreadMessageResponseContentTextObject::class)
->fileIds->toBeArray()
->fileIds->toBe(['file-DhxjnFCaSHc4ZELRGKwTMFtI'])
->assistantId->toBeNull()
->runId->toBeNull()
->metadata->toBeArray()
->metadata->toBeEmpty();

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

test('modify', function () {
$client = mockClient('POST', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages/msg_KNsDDwE41BUAHhcPNpDkdHWZ', [
'metadata' => [
'name' => 'My new message name',
],
], Response::from(threadMessageResource(), metaHeaders()));

$result = $client->threads()->messages()->modify('thread_agvtHUGezjTCt4SKgQg0NJ2Y', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ', [
'metadata' => [
'name' => 'My new message name',
],
]);

expect($result)
->toBeInstanceOf(ThreadMessageResponse::class)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message')
->createdAt->toBe(1699623839)
->threadId->toBe('thread_agvtHUGezjTCt4SKgQg0NJ2Y')
->role->toBe('user')
->content->toBeArray()
->content->toHaveCount(1)
->content->each->toBeInstanceOf(ThreadMessageResponseContentTextObject::class)
->fileIds->toBeArray()
->fileIds->toBe(['file-DhxjnFCaSHc4ZELRGKwTMFtI'])
->assistantId->toBeNull()
->runId->toBeNull()
->metadata->toBeArray()
->metadata->toBeEmpty();

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

test('retrieve', function () {
$client = mockClient('GET', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages/msg_KNsDDwE41BUAHhcPNpDkdHWZ', [], Response::from(threadMessageResource(), metaHeaders()));

$result = $client->threads()->messages()->retrieve('thread_agvtHUGezjTCt4SKgQg0NJ2Y', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');

expect($result)
->toBeInstanceOf(ThreadMessageResponse::class)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message')
->createdAt->toBe(1699623839)
->threadId->toBe('thread_agvtHUGezjTCt4SKgQg0NJ2Y')
->role->toBe('user')
->content->toBeArray()
->content->toHaveCount(1)
->content->each->toBeInstanceOf(ThreadMessageResponseContentTextObject::class)
->fileIds->toBeArray()
->fileIds->toBe(['file-DhxjnFCaSHc4ZELRGKwTMFtI'])
->assistantId->toBeNull()
->runId->toBeNull()
->metadata->toBeArray()
->metadata->toBeEmpty();

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

test('delete', function () {
$client = mockClient('DELETE', 'threads/thread_agvtHUGezjTCt4SKgQg0NJ2Y/messages/msg_KNsDDwE41BUAHhcPNpDkdHWZ', [], Response::from(threadMessageDeleteResource(), metaHeaders()));

$result = $client->threads()->messages()->delete('thread_agvtHUGezjTCt4SKgQg0NJ2Y', 'msg_KNsDDwE41BUAHhcPNpDkdHWZ');

expect($result)
->toBeInstanceOf(ThreadMessageDeleteResponse::class)
->id->toBe('msg_KNsDDwE41BUAHhcPNpDkdHWZ')
->object->toBe('thread.message.deleted')
->deleted->toBe(true);

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

0 comments on commit 9a20f1e

Please sign in to comment.