diff --git a/tests/Fixtures/ThreadMessage.php b/tests/Fixtures/ThreadMessage.php new file mode 100644 index 00000000..5e9e4d29 --- /dev/null +++ b/tests/Fixtures/ThreadMessage.php @@ -0,0 +1,60 @@ + + */ +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 + */ +function threadMessageListResource(): array +{ + return [ + 'object' => 'list', + 'data' => [ + threadMessageResource(), + threadMessageResource(), + ], + 'first_id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ', + 'last_id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ', + 'has_more' => false, + ]; +} + +/** + * @return array + */ +function threadMessageDeleteResource(): array +{ + return [ + 'id' => 'msg_KNsDDwE41BUAHhcPNpDkdHWZ', + 'object' => 'thread.message.deleted', + 'deleted' => true, + ]; +} diff --git a/tests/Resources/ThreadsMessages.php b/tests/Resources/ThreadsMessages.php new file mode 100644 index 00000000..ac241260 --- /dev/null +++ b/tests/Resources/ThreadsMessages.php @@ -0,0 +1,139 @@ +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); +});