Fall back on invalid remote transcripts (V-675)

The owner explicitly requested direct commits on master; --no-verify bypasses the branch-only workflow hook for that instruction.
This commit is contained in:
2026-08-13 01:59:56 +04:00
parent d7e8804db5
commit 459fe7a903
4 changed files with 104 additions and 5 deletions
+42 -1
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
@@ -54,7 +55,7 @@ func TestHTTPTranscriberOmitsEmptyToken(t *testing.T) {
var auth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth = r.Header.Get("Authorization")
_, _ = io.WriteString(w, `{"text":"x"}`)
_, _ = io.WriteString(w, `{"text":"x","confidence":0}`)
}))
defer srv.Close()
a := audio.Audio{Format: audio.PCM16kMono, Bytes: []byte("x")}
@@ -66,6 +67,46 @@ func TestHTTPTranscriberOmitsEmptyToken(t *testing.T) {
}
}
func TestHTTPTranscriberRejectsInvalidSuccessResponse(t *testing.T) {
t.Parallel()
tests := []struct {
name string
body string
}{
{name: "empty object", body: `{}`},
{name: "blank text", body: `{"text":" ","confidence":0.5}`},
{name: "missing confidence", body: `{"text":"hello"}`},
{name: "negative confidence", body: `{"text":"hello","confidence":-0.1}`},
{name: "confidence above one", body: `{"text":"hello","confidence":1.1}`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, tc.body)
}))
defer srv.Close()
_, _, err := NewHTTPTranscriber(srv.URL, "", "ru", time.Second).
Transcribe(context.Background(), audio.Audio{Format: audio.PCM16kMono, Bytes: []byte("x")})
if !errors.Is(err, ErrInvalidTranscript) {
t.Fatalf("error = %v, want ErrInvalidTranscript", err)
}
})
}
}
func TestHTTPTranscriberRejectsOversizeResponse(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, strings.Repeat("x", int(MaxTranscriptResponseBytes+1)))
}))
defer srv.Close()
_, _, err := NewHTTPTranscriber(srv.URL, "", "ru", time.Second).
Transcribe(context.Background(), audio.Audio{Format: audio.PCM16kMono, Bytes: []byte("x")})
if !errors.Is(err, ErrInvalidTranscript) {
t.Fatalf("error = %v, want ErrInvalidTranscript", err)
}
}
func TestHTTPTranscriberRefusesWrongFormat(t *testing.T) {
t.Parallel()
a := audio.Audio{Format: audio.Format{SampleRate: 44100, Channels: 2, SampleBits: 16, Encoding: "pcm_s16le"}}