<?php
namespace boru\ai\Tests\Provider\OpenAI;

use boru\ai\Http\HttpResponse;
use boru\ai\Provider\OpenAI\OpenAIClient;
use boru\ai\Tests\Fakes\FakeHttpClient;
use PHPUnit\Framework\TestCase;

class OpenAIClientUploadTest extends TestCase
{
    public function testUploadFileBuildsMultipartRequest()
    {
        $httpClient = new FakeHttpClient();
        $response = new HttpResponse();
        $response->statusCode(200)->body('{"id":"file_123","object":"file"}');
        $httpClient->queueResponse($response);

        $client = new OpenAIClient('test-key', array(), $httpClient);
        $result = $client->uploadFile(__FILE__, 'assistants', array(
            'filename' => 'upload-test.php',
            'mime_type' => 'text/plain',
        ));

        $this->assertSame('file_123', $result['id']);
        $this->assertCount(1, $httpClient->requests);

        $request = $httpClient->requests[0];
        $this->assertSame('POST', $request->method());
        $this->assertSame('https://api.openai.com/v1/files', $request->url());
        $this->assertSame('Bearer test-key', $request->header('Authorization'));
        $this->assertNotNull($request->multipart());
        $this->assertSame('purpose', $request->multipart()[0]['name']);
        $this->assertSame('assistants', $request->multipart()[0]['contents']);
        $this->assertSame('file', $request->multipart()[1]['name']);
        $this->assertSame('upload-test.php', $request->multipart()[1]['filename']);
    }
}
