Files
go-sip/internal/ai/bailian_tts_test.go
T

49 lines
1.3 KiB
Go

package ai
import (
"context"
"github.com/coder/websocket"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func mockBailianTTS(t *testing.T, pcm []byte, observe func(string)) (string, *atomic.Int32) {
t.Helper()
calls := &atomic.Int32{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
return
}
defer c.CloseNow()
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
start := mockRead(t, c, ctx)
calls.Add(1)
id := start.Header.TaskID
mockEvent(c, ctx, id, "task-started", map[string]any{})
next := mockRead(t, c, ctx)
text := next.Payload["input"].(map[string]any)["text"].(string)
if observe != nil {
observe(text)
}
mockRead(t, c, ctx)
if err := c.Write(ctx, websocket.MessageBinary, pcm); err != nil {
return
}
mockEvent(c, ctx, id, "task-finished", map[string]any{})
}))
t.Cleanup(server.Close)
return "ws" + strings.TrimPrefix(server.URL, "http"), calls
}
func TestBailianFailureSummaryNeverLogsProviderPayload(t *testing.T) {
kind, status := bailianFailureSummary(context.DeadlineExceeded)
if kind != "deadline" || status != 0 {
t.Fatal("context failure lost")
}
}