mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
controllers COMPILES on zip. This is the last of it: the writer swap. /v1/responses is a chat completion read in a second dialect, and it used to get one by reaching into the request context, replacing the writer it found with a translating one, calling the chat route back, and reading whatever came through. It now asks for the completion and passes a sink saying where the answer should go — a stream is decorated as it is produced, a whole body is handed over entire. Two functions, because a stream and a body are two different things. Collapsing them is what made the old bridge complicated. Standing in for a ResponseWriter, it saw the status on the same call as the bytes, so it had to intercept WriteHeader, withhold a success it was not ready to send, buffer a body it meant to rewrite, and remember which of those it had done. It receives the two things it wants separately now, and all of that machinery is gone with the reason for it. The status never leaves the handler. The transcription bound gets a name — `fits` — because both transports ask it and the same comparison was written twice. Stated as the acceptance rather than the refusal: AudioSpeech has a local `oversize`, and a package function of that name would sit shadowed inside it, reading as the bound while being a string. Tests moved with it. The bridge translates into a buffer, which is all a recorder was ever standing in for, and the non-streaming test asks the translation directly because the bridge no longer sees a body. The four form tests read the one reader. One is DELETED rather than moved: `?model=whisper` beside a multipart body used to resolve, because net/http merged the query into the form — and it was only ever reachable because the form-field read was broken. Nothing crossing ZAP has a query string, so it could not have worked on both. Still red: routers/ (88), and 58 pre-existing test-side errors in this package from earlier commits — 26 of them controllers standing themselves up the old way. The package is all-or-nothing, so the bridge tests cannot RUN until those clear.
181 lines
6.6 KiB
Go
181 lines
6.6 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
// The SIZE bounds on the speech endpoints, asserted where they BITE: an upload
|
|
// that outgrows the pod, and a synthesis request that buys unbounded CPU with one
|
|
// string. Each test states the abuse it refuses, not the branch it covers.
|
|
//
|
|
// The bound on the WORK — who may run how much of it at once — is a different
|
|
// question and lives with the mechanism, in speech_admission_test.go.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"mime/multipart"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// transcribeBody builds an OpenAI transcription form carrying fileBytes of audio.
|
|
func transcribeBody(t *testing.T, fileBytes int) (body *bytes.Buffer, contentType string) {
|
|
t.Helper()
|
|
body = &bytes.Buffer{}
|
|
w := multipart.NewWriter(body)
|
|
fw, err := w.CreateFormFile("file", "audio.webm")
|
|
if err != nil {
|
|
t.Fatalf("CreateFormFile: %v", err)
|
|
}
|
|
// Written in chunks so the test does not itself allocate the whole payload twice.
|
|
chunk := bytes.Repeat([]byte("A"), 1<<20)
|
|
for written := 0; written < fileBytes; {
|
|
n := len(chunk)
|
|
if remaining := fileBytes - written; remaining < n {
|
|
n = remaining
|
|
}
|
|
if _, err := fw.Write(chunk[:n]); err != nil {
|
|
t.Fatalf("write audio: %v", err)
|
|
}
|
|
written += n
|
|
}
|
|
if err := w.WriteField("model", "whisper"); err != nil {
|
|
t.Fatalf("WriteField: %v", err)
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
return body, w.FormDataContentType()
|
|
}
|
|
|
|
// TestTranscribeUploadOverLimitRefused proves the upload bound bites: a body
|
|
// past MaxTranscribeUpload is reported as oversize, so the handler answers 413
|
|
// instead of spending the parse, the copies and the upstream call on it.
|
|
func TestTranscribeUploadOverLimitRefused(t *testing.T) {
|
|
body, _ := transcribeBody(t, MaxTranscribeUpload+(1<<20))
|
|
if fits(body.Bytes()) {
|
|
t.Fatalf("a %d MiB upload was accepted; the %d MiB bound did not bite",
|
|
body.Len()>>20, MaxTranscribeUpload>>20)
|
|
}
|
|
}
|
|
|
|
// TestTranscribeUploadAtLimitAccepted proves the bound is a CEILING and not an
|
|
// off-by-one that refuses the largest legitimate upload. This is the mutation
|
|
// that a `>=` in place of `>` would introduce, and the reason the reader takes
|
|
// one byte past the bound rather than exactly the bound.
|
|
func TestTranscribeUploadAtLimitAccepted(t *testing.T) {
|
|
// A form whose TOTAL is just under the bound: audio plus the MIME framing.
|
|
body, _ := transcribeBody(t, MaxTranscribeUpload-(1<<16))
|
|
if body.Len() > MaxTranscribeUpload {
|
|
t.Fatalf("test fixture is %d bytes, over the %d bound", body.Len(), MaxTranscribeUpload)
|
|
}
|
|
|
|
if !fits(body.Bytes()) {
|
|
t.Fatal("an upload UNDER the bound was refused as oversize")
|
|
}
|
|
// And it parses, which is the half a bound alone does not prove: a ceiling that
|
|
// admits a body the reader then chokes on has moved the failure, not raised it.
|
|
form, err := parseTranscribeForm(body.Bytes())
|
|
if err != nil {
|
|
t.Fatalf("parseTranscribeForm: %v", err)
|
|
}
|
|
if form.model != "whisper" {
|
|
t.Errorf("model = %q, want whisper", form.model)
|
|
}
|
|
if form.audio == nil {
|
|
t.Error("the largest legitimate upload produced no audio")
|
|
}
|
|
}
|
|
|
|
// TestSpeechInputOverLimitRefused drives the real ZAP speech handler with an
|
|
// input past MaxSpeechInput and proves it is refused 413 — before any upstream
|
|
// synthesis is dispatched. Synthesis cost is linear in this length, so this is
|
|
// the bound that makes a TTS request's cost knowable in advance.
|
|
func TestSpeechInputOverLimitRefused(t *testing.T) {
|
|
body, err := json.Marshal(audioSpeechRequest{
|
|
Model: "kokoro",
|
|
Input: strings.Repeat("a", MaxSpeechInput+1),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
msgOut, hErr := zapAudioSpeechHandler(context.Background(), "Bearer probe", body)
|
|
status, msg := cloudStatus(t, msgOut, hErr)
|
|
if status != 413 {
|
|
t.Fatalf("status = %d, want 413 for an input of %d chars over the %d limit (msg=%q)",
|
|
status, MaxSpeechInput+1, MaxSpeechInput, msg)
|
|
}
|
|
if !strings.Contains(msg, "4096") {
|
|
t.Errorf("refusal %q does not name the limit — a caller cannot act on it", msg)
|
|
}
|
|
}
|
|
|
|
// TestSpeechInputAtLimitPasses proves the bound is a ceiling, not an off-by-one
|
|
// that refuses the largest legitimate request — the mutation a `>=` would
|
|
// introduce.
|
|
//
|
|
// The proof is that the request reaches the AUTH SEAM, which sits directly below
|
|
// the size gate: identity resolution reads a database this unit test has no
|
|
// business standing up, so it faults there. Faulting below the gate is
|
|
// admission; being refused 413 is not. The over-limit twin above never gets
|
|
// that far, and that difference is the assertion.
|
|
func TestSpeechInputAtLimitPasses(t *testing.T) {
|
|
body, err := json.Marshal(audioSpeechRequest{
|
|
Model: "kokoro",
|
|
Input: strings.Repeat("a", MaxSpeechInput),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
admitted := false
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
admitted = true // faulted in identity resolution, below the gate
|
|
}
|
|
}()
|
|
msgOut, hErr := zapAudioSpeechHandler(context.Background(), "Bearer probe", body)
|
|
if hErr == nil && msgOut != nil {
|
|
if msgOut.Root().Uint32(object.CloudRespStatus) != 413 {
|
|
admitted = true
|
|
}
|
|
}
|
|
}()
|
|
|
|
if !admitted {
|
|
t.Fatalf("an input of exactly %d chars was refused 413; the bound is off by one", MaxSpeechInput)
|
|
}
|
|
}
|
|
|
|
// TestZapTranscribeOverLimitRefused drives the real ZAP transcribe handler with
|
|
// a body past MaxTranscribeUpload and proves it is refused 413 BEFORE the
|
|
// multipart parse — which is the point, since the parse is what turns one body
|
|
// into three more copies of it.
|
|
func TestZapTranscribeOverLimitRefused(t *testing.T) {
|
|
body, _ := transcribeBody(t, MaxTranscribeUpload+(1<<20))
|
|
msgOut, hErr := zapAudioTranscribeHandler(context.Background(), "Bearer probe", body.Bytes())
|
|
status, msg := cloudStatus(t, msgOut, hErr)
|
|
if status != 413 {
|
|
t.Fatalf("status = %d, want 413 for a %d MiB body over the %d MiB limit (msg=%q)",
|
|
status, body.Len()>>20, MaxTranscribeUpload>>20, msg)
|
|
}
|
|
if !strings.Contains(msg, "25") {
|
|
t.Errorf("refusal %q does not name the limit", msg)
|
|
}
|
|
}
|