|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Fusio - Self-Hosted API Management for Builders. |
| 4 | + * For the current version and information visit <https://www.fusio-project.org/> |
| 5 | + * |
| 6 | + * Copyright (c) Christoph Kappestein <christoph.kappestein@gmail.com> |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Fusio\Impl\Tests\Backend\Api\Agent\Message; |
| 22 | + |
| 23 | +use Fusio\Impl\Table; |
| 24 | +use Fusio\Impl\Tests\DbTestCase; |
| 25 | +use PSX\Json\Parser; |
| 26 | + |
| 27 | +/** |
| 28 | + * CollectionTest |
| 29 | + * |
| 30 | + * @author Christoph Kappestein <christoph.kappestein@gmail.com> |
| 31 | + * @license http://www.apache.org/licenses/LICENSE-2.0 |
| 32 | + * @link https://www.fusio-project.org |
| 33 | + */ |
| 34 | +class CollectionTest extends DbTestCase |
| 35 | +{ |
| 36 | + public function testGet() |
| 37 | + { |
| 38 | + $response = $this->sendRequest('/backend/agent/1/message', 'GET', array( |
| 39 | + 'User-Agent' => 'Fusio TestCase', |
| 40 | + 'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf' |
| 41 | + )); |
| 42 | + |
| 43 | + $body = (string) $response->getBody(); |
| 44 | + $expect = <<<'JSON' |
| 45 | +{ |
| 46 | + "totalResults": 1, |
| 47 | + "startIndex": 0, |
| 48 | + "itemsPerPage": 16, |
| 49 | + "entry": [ |
| 50 | + { |
| 51 | + "id": 1, |
| 52 | + "role": "user", |
| 53 | + "content": { |
| 54 | + "type": "text", |
| 55 | + "content": "This is a test message" |
| 56 | + }, |
| 57 | + "insertDate": "2026-02-22T19:17:00Z" |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
| 61 | +JSON; |
| 62 | + |
| 63 | + $this->assertEquals(200, $response->getStatusCode(), $body); |
| 64 | + $this->assertJsonStringEqualsJsonString($expect, $body, $body); |
| 65 | + } |
| 66 | + public function testGetParent() |
| 67 | + { |
| 68 | + $response = $this->sendRequest('/backend/agent/1/message?parent=1', 'GET', array( |
| 69 | + 'User-Agent' => 'Fusio TestCase', |
| 70 | + 'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf' |
| 71 | + )); |
| 72 | + |
| 73 | + $body = (string) $response->getBody(); |
| 74 | + $expect = <<<'JSON' |
| 75 | +{ |
| 76 | + "totalResults": 1, |
| 77 | + "startIndex": 0, |
| 78 | + "itemsPerPage": 16, |
| 79 | + "entry": [ |
| 80 | + { |
| 81 | + "id": 2, |
| 82 | + "role": "assistant", |
| 83 | + "content": { |
| 84 | + "type": "text", |
| 85 | + "content": "And an agent response" |
| 86 | + }, |
| 87 | + "insertDate": "2026-02-22T19:17:00Z" |
| 88 | + } |
| 89 | + ] |
| 90 | +} |
| 91 | +JSON; |
| 92 | + |
| 93 | + $this->assertEquals(200, $response->getStatusCode(), $body); |
| 94 | + $this->assertJsonStringEqualsJsonString($expect, $body, $body); |
| 95 | + } |
| 96 | + |
| 97 | + public function testPost() |
| 98 | + { |
| 99 | + $response = $this->sendRequest('/backend/agent/1/message', 'POST', array( |
| 100 | + 'User-Agent' => 'Fusio TestCase', |
| 101 | + 'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf' |
| 102 | + ), json_encode([ |
| 103 | + 'type' => 'text', |
| 104 | + 'content' => 'What is the meaning of life?', |
| 105 | + ])); |
| 106 | + |
| 107 | + $body = (string) $response->getBody(); |
| 108 | + $expect = <<<'JSON' |
| 109 | +{ |
| 110 | + "id": 4, |
| 111 | + "role": "assistant", |
| 112 | + "content": { |
| 113 | + "type": "text", |
| 114 | + "content": "The answer ist: 42" |
| 115 | + } |
| 116 | +} |
| 117 | +JSON; |
| 118 | + |
| 119 | + $this->assertEquals(201, $response->getStatusCode(), $body); |
| 120 | + $this->assertJsonStringEqualsJsonString($expect, $body, $body); |
| 121 | + |
| 122 | + // check database |
| 123 | + $sql = $this->connection->createQueryBuilder() |
| 124 | + ->select('origin', 'content') |
| 125 | + ->from('fusio_agent_message') |
| 126 | + ->where('agent_id = :agent_id') |
| 127 | + ->orderBy('id', 'DESC') |
| 128 | + ->getSQL(); |
| 129 | + |
| 130 | + $row = $this->connection->fetchAssociative($sql, ['agent_id' => 1]); |
| 131 | + |
| 132 | + $this->assertEquals(2, $row['origin']); |
| 133 | + $this->assertJsonStringEqualsJsonString(Parser::encode(['type' => 'text', 'content' => 'The answer ist: 42']), $row['content']); |
| 134 | + } |
| 135 | + |
| 136 | + public function testPut() |
| 137 | + { |
| 138 | + $response = $this->sendRequest('/backend/agent/1/message', 'PUT', array( |
| 139 | + 'User-Agent' => 'Fusio TestCase', |
| 140 | + 'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf' |
| 141 | + ), json_encode([ |
| 142 | + 'foo' => 'bar', |
| 143 | + ])); |
| 144 | + |
| 145 | + $body = (string) $response->getBody(); |
| 146 | + |
| 147 | + $this->assertEquals(404, $response->getStatusCode(), $body); |
| 148 | + } |
| 149 | + |
| 150 | + public function testDelete() |
| 151 | + { |
| 152 | + $response = $this->sendRequest('/backend/agent/1/message', 'DELETE', array( |
| 153 | + 'User-Agent' => 'Fusio TestCase', |
| 154 | + 'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf' |
| 155 | + ), json_encode([ |
| 156 | + 'foo' => 'bar', |
| 157 | + ])); |
| 158 | + |
| 159 | + $body = (string) $response->getBody(); |
| 160 | + |
| 161 | + $this->assertEquals(404, $response->getStatusCode(), $body); |
| 162 | + } |
| 163 | +} |
0 commit comments