Files
ai_academy/internal/models/api/openaichataudio/golden_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

162 lines
5.8 KiB
Go

package openaichataudio
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"academy/internal/models/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// qwenASRResponse is the non-streaming response example from Alibaba's
// Qwen-ASR reference, copied from the page:
// https://help.aliyun.com/zh/model-studio/qwen-asr-api-reference
const qwenASRResponse = `{
"choices": [{
"finish_reason": "stop",
"index": 0,
"message": {
"annotations": [{"emotion": "neutral", "language": "zh", "type": "audio_info"}],
"content": "欢迎使用阿里云。",
"role": "assistant"
}
}],
"created": 1767683986,
"id": "chatcmpl-487abe5f-d4f2-9363-a877-xxxxxxx",
"model": "qwen3-asr-flash",
"object": "chat.completion",
"usage": {"completion_tokens": 12, "prompt_tokens": 42, "total_tokens": 54}
}`
// withSeconds is a reply carrying the usage.seconds that the reference's
// schema documents ("音频时长(秒)") but its example omits.
const withSeconds = `{"choices":[{"message":{"role":"assistant","content":"x"}}],"usage":{"seconds":3}}`
func newClient(url, model string) *Client {
return newClientWith(url, model, api.TranscriptionsSettings{})
}
func newClientWith(url, model string, settings api.TranscriptionsSettings) *Client {
return New(Config{
Endpoint: api.Endpoint{BaseURL: url, Model: model, Auth: api.BearerAuth("k")},
Settings: settings,
})
}
// The request is the one both references show: a single user message whose
// only part is the audio as a data URI, no instruction, no stream flag.
func TestRequestBodyMatchesTheReferences(t *testing.T) {
body, err := newClient("https://example.invalid/v1", "qwen3-asr-flash").
BuildRequestBody(api.TranscriptionRequest{Audio: []byte("ID3"), FileName: "clip.MP3"})
require.NoError(t, err)
assert.Equal(t, map[string]any{
"model": "qwen3-asr-flash",
"messages": []any{map[string]any{
"role": "user",
"content": []any{map[string]any{
"type": "input_audio",
"input_audio": map[string]any{"data": "data:audio/mpeg;base64,SUQz"},
}},
}},
}, body)
}
func TestDataURIMediaTypeFollowsTheExtension(t *testing.T) {
uri, err := DataURI([]byte("RIFF"), "a.wav")
require.NoError(t, err)
assert.Equal(t, "data:audio/wav;base64,UklGRg==", uri)
// No audio type can be named for it, and the references show one.
_, err = DataURI([]byte("x"), "noext")
assert.Error(t, err)
}
// Both references cap the string as sent, so the data: prefix counts. A file
// sized to exactly the encoded budget without the prefix goes over it.
func TestEncodedCeilingCountsTheWholeDataURI(t *testing.T) {
const limit = 1024
c := newClientWith("https://example.invalid", "m", api.TranscriptionsSettings{MaxEncodedBytes: limit})
audio := make([]byte, limit*3/4) // base64 of this is exactly limit bytes
_, err := c.BuildRequestBody(api.TranscriptionRequest{Audio: audio, FileName: "a.wav"})
require.Error(t, err)
prefix := len("data:audio/wav;base64,")
fits := make([]byte, (limit-prefix)/4*3)
_, err = c.BuildRequestBody(api.TranscriptionRequest{Audio: fits, FileName: "a.wav"})
require.NoError(t, err)
}
func TestLanguageGoesInASROptionsWhereDeclared(t *testing.T) {
req := api.TranscriptionRequest{Audio: []byte("x"), FileName: "a.wav", Language: "zh"}
body, err := newClient("https://example.invalid", "m").BuildRequestBody(req)
require.NoError(t, err)
assert.NotContains(t, body, "asr_options", "undeclared, so not sent")
body, err = newClientWith("https://example.invalid", "m",
api.TranscriptionsSettings{LanguageParam: api.LanguageASROptions}).BuildRequestBody(req)
require.NoError(t, err)
assert.Equal(t, map[string]any{"language": "zh"}, body["asr_options"])
}
func TestDecodesTheDocumentedResponse(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
var path string
var sent map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&sent)
_, _ = w.Write([]byte(qwenASRResponse))
}))
defer server.Close()
out, err := newClient(server.URL+"/compatible-mode/v1", "qwen3-asr-flash").
Transcribe(context.Background(), api.TranscriptionRequest{Audio: []byte("RIFF"), FileName: "a.wav"})
require.NoError(t, err)
assert.Equal(t, "/compatible-mode/v1/chat/completions", path)
assert.Equal(t, &api.Transcription{Text: "欢迎使用阿里云。"}, out)
}
// A reply with no assistant content is not silence.
func TestReplyWithoutContentIsAnError(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"choices":[]}`))
}))
defer server.Close()
_, err := newClient(server.URL, "m").
Transcribe(context.Background(), api.TranscriptionRequest{Audio: []byte("x"), FileName: "a.wav"})
assert.Error(t, err)
}
func TestDurationFromUsageSeconds(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(withSeconds))
}))
defer server.Close()
out, err := newClient(server.URL, "m").
Transcribe(context.Background(), api.TranscriptionRequest{Audio: []byte("x"), FileName: "a.wav"})
require.NoError(t, err)
assert.Equal(t, float64(3), out.Duration)
}
func TestEmptyContentIsSilence(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"choices":[{"message":{"role":"assistant","content":""}}]}`))
}))
defer server.Close()
out, err := newClient(server.URL, "m").
Transcribe(context.Background(), api.TranscriptionRequest{Audio: []byte("x"), FileName: "a.wav"})
require.NoError(t, err)
assert.Equal(t, "", out.Text)
}