package asr

import (
	"context"
	"strings"
	"testing"

	"dudu/server/pkg/protocol"
)

func TestMockProviderStreams(t *testing.T) {
	p := NewMock()
	p.Script = []string{"你好，世界。"}
	s, err := p.StartSession(context.Background(), SessionConfig{SampleRate: 16000})
	if err != nil {
		t.Fatal(err)
	}
	frame := make([]byte, protocol.FrameBytes)
	for i := 0; i < 10; i++ { // 1s 音频 = 10 帧 → 全句 6 字识别完
		if err := s.SendAudio(frame); err != nil {
			t.Fatal(err)
		}
	}
	if err := s.Close(); err != nil {
		t.Fatal(err)
	}

	var finals []string
	partials := 0
	for r := range s.Results() {
		if r.IsFinal {
			finals = append(finals, r.Text)
		} else {
			partials++
		}
	}
	got := strings.Join(finals, "")
	if got != "你好，世界。" {
		t.Fatalf("final text mismatch: %q (finals=%v)", got, finals)
	}
	if partials == 0 {
		t.Fatal("expect partial results before finals")
	}
}
