Files
ai/controllers/voice_test.go
zeekayandHanzo Dev 3b5c4b007f Serve the spoken conversation from the host that already serves the audio
hanzoai/voice holds a conversation and hanzoai/speech holds the models; what was
missing was somewhere to run. It had none, so /v1/voice answered 404 in
production while the code sat finished, measured and tested in main.

api.hanzo.ai already answers /v1/audio/speech and /v1/audio/transcriptions, and
the realtime socket is that same audio with the turns joined up — one API host,
rather than a second service to build, deploy, route and operate for one
package. It adapts through zip.AdaptNetHTTP because a WebSocket has no
request/response pair to map a controller method onto.

It mounts from controllers because a conversation is billed and usageRecord is
written there. The tenant charged is the one IAM resolved, NOT the token's
`owner` claim — owner names the application a token was minted through, and
reading it as the tenant bills one org for another org's audio. Heard and spoken
are both charged, and the caller's own bearer travels to the model so the
thinking is billed to whoever spoke rather than to a service account.

With no IAM configured nothing is registered at all. A WebSocket does not
observe the same-origin policy, so an ungated one is any page on the internet
opening a microphone as whoever is signed in; that is the property the first
test holds.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-19 22:25:17 -07:00

65 lines
2.2 KiB
Go

// Copyright 2023-2025 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
import (
"net/http"
"net/http/httptest"
"testing"
)
// Without an IAM to check a bearer against there must be no socket at all.
//
// This is the property worth holding: a WebSocket is exempt from the same-origin
// policy, so an ungated one is any page on the internet opening a microphone as
// whoever is signed in. Serving it "open for now" is not a smaller surface, it
// is the whole surface. Nil here, and the router registers nothing.
func TestVoiceIsNotServedWithoutIAM(t *testing.T) {
t.Setenv("IAM_URL", "")
if h := VoiceHandler(); h != nil {
t.Fatal("a voice socket was offered with no IAM to gate it")
}
}
// With one configured it answers, and health is reachable without a ticket —
// that is the endpoint a probe reads.
func TestVoiceServesHealthWhenGated(t *testing.T) {
t.Setenv("IAM_URL", "https://hanzo.id")
h := VoiceHandler()
if h == nil {
t.Fatal("no handler with an IAM configured")
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/v1/voice/health", nil))
if rec.Code != http.StatusOK {
t.Errorf("health answered %d, want 200", rec.Code)
}
}
// A session is minted from a bearer, so an anonymous POST must be refused
// rather than handed a ticket.
func TestVoiceSessionRefusesAnonymous(t *testing.T) {
t.Setenv("IAM_URL", "https://hanzo.id")
h := VoiceHandler()
if h == nil {
t.Fatal("no handler with an IAM configured")
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/voice/session", nil))
if rec.Code == http.StatusOK {
t.Error("a session was minted for a request carrying no bearer")
}
}