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

use boru\ai\Provider\OpenAI\OpenAIMapper;
use boru\ai\Provider\OpenAI\OpenAIStreamParser;
use boru\ai\Provider\ProviderSignal;
use PHPUnit\Framework\TestCase;

class OpenAIStreamParserTest extends TestCase
{
    public function testParsesOutputTextDeltaAndCompletedEvents()
    {
        $parser = new OpenAIStreamParser(new OpenAIMapper());
        $events = array();

        $chunk = "data: {\"type\":\"response.output_text.delta\",\"delta\":\"Hello \"}\n\n";
        $chunk .= "data: {\"type\":\"response.output_text.delta\",\"delta\":\"world\"}\n\n";
        $chunk .= "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.1\",\"status\":\"completed\",\"output\":[{\"type\":\"message\",\"content\":[{\"type\":\"output_text\",\"text\":\"Hello world\"}]}]}}\n\n";
        $chunk .= "data: [DONE]\n\n";

        $parser->push($chunk, function ($event) use (&$events) {
            $events[] = $event;
        });

        $this->assertCount(4, $events);
        $this->assertSame(ProviderSignal::TEXT_DELTA, $events[0]->type());
        $this->assertSame('Hello ', $events[0]->data('delta'));
        $this->assertSame(ProviderSignal::TEXT_DELTA, $events[1]->type());
        $this->assertSame('world', $events[1]->data('delta'));
        $this->assertSame(ProviderSignal::RESPONSE_COMPLETED, $events[2]->type());
        $this->assertSame(ProviderSignal::TEXT_COMPLETED, $events[3]->type());
        $this->assertSame('Hello world', $events[3]->data('text'));
        $this->assertSame('Hello world', $parser->finalResponse()->text());
    }
}
