|
74 | 74 | expect($assistantResponse->role())->toBe(Role::ASSISTANT); |
75 | 75 | expect($assistantResponse->isError())->toBeTrue(); |
76 | 76 | }); |
| 77 | + |
| 78 | +it('creates a json response', function (): void { |
| 79 | + $data = ['key' => 'value', 'number' => 123]; |
| 80 | + $response = Response::json($data); |
| 81 | + |
| 82 | + expect($response->content())->toBeInstanceOf(Text::class); |
| 83 | + expect($response->isNotification())->toBeFalse(); |
| 84 | + expect($response->isError())->toBeFalse(); |
| 85 | + expect($response->role())->toBe(Role::USER); |
| 86 | + |
| 87 | + $content = $response->content(); |
| 88 | + expect((string) $content)->toBe(json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); |
| 89 | +}); |
| 90 | + |
| 91 | +it('handles nested arrays in json response', function (): void { |
| 92 | + $data = [ |
| 93 | + 'user' => [ |
| 94 | + 'name' => 'John Doe', |
| 95 | + |
| 96 | + 'roles' => ['admin', 'developer'] |
| 97 | + ], |
| 98 | + 'active' => true |
| 99 | + ]; |
| 100 | + $response = Response::json($data); |
| 101 | + |
| 102 | + expect($response->content())->toBeInstanceOf(Text::class); |
| 103 | + |
| 104 | + $content = $response->content(); |
| 105 | + expect((string) $content)->toBe(json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); |
| 106 | +}); |
| 107 | + |
| 108 | +it('throws JsonException for invalid json data', function (): void { |
| 109 | + $data = ['invalid' => INF]; |
| 110 | + |
| 111 | + expect(function () use ($data): void { |
| 112 | + Response::json($data); |
| 113 | + })->toThrow(JsonException::class); |
| 114 | +}); |
| 115 | + |
| 116 | +it('handles empty array in json response', function (): void { |
| 117 | + $data = []; |
| 118 | + $response = Response::json($data); |
| 119 | + |
| 120 | + expect($response->content())->toBeInstanceOf(Text::class); |
| 121 | + |
| 122 | + $content = $response->content(); |
| 123 | + expect((string) $content)->toBe(json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); |
| 124 | +}); |
0 commit comments