Files
AskaEth 4a2d276881
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(capability): 转码能力包(vendor 纯 Go AMR-NB 解码器 + 黄金向量逐样本对照 + 8 哨兵 + 注册面)
2026-10-05 13:02:28 +08:00

74 lines
2.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package transcode
import "testing"
// TestFormatOfNormalizes 断言「字符串 → Format」只有一处转换,且归一化口径稳定:
// 去空白、转小写、去前导点(design/25 §6.2:与 mediafetch.AudioFormatOf 同源)。
func TestFormatOfNormalizes(t *testing.T) {
cases := map[string]Format{
"amr": FormatAMR,
".amr": FormatAMR,
"AMR": FormatAMR,
" .AmR": FormatAMR,
"wav": FormatWAV,
".WAV": FormatWAV,
"": Format(""),
}
for in, want := range cases {
if got := FormatOf(in); got != want {
t.Errorf("FormatOf(%q) = %q, want %q", in, got, want)
}
}
}
// TestFormatName 断言 FormatName 是 FormatOf 的逆(对已归一的格式名)。
func TestFormatName(t *testing.T) {
for _, f := range []Format{FormatAMR, FormatAMRWB, FormatWAV} {
if got := FormatOf(FormatName(f)); got != f {
t.Errorf("FormatOf(FormatName(%q)) = %q, want %q", f, got, f)
}
}
}
// TestSniff 断言魔数嗅探:认得出(AMR-NB / AMR-WB / WAV)、嗅不出即 false
// (deny-by-default)。
func TestSniff(t *testing.T) {
riffWAV := append([]byte("RIFF\x24\x00\x00\x00WAVE"), make([]byte, 8)...)
cases := []struct {
name string
data []byte
want Format
ok bool
}{
{"amr-nb magic", []byte(amrNBMagic), FormatAMR, true},
{"amr-nb stream", amrStubBytes(0x3c, 1, 2, 3), FormatAMR, true},
{"amr-wb magic", []byte(amrWBMagic), FormatAMRWB, true},
{"wav", riffWAV, FormatWAV, true},
{"empty", nil, "", false},
{"short", []byte("#!AM"), "", false},
{"text", []byte("hello world"), "", false},
{"riff without wave", []byte("RIFF\x24\x00\x00\x00AVI "), "", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, ok := Sniff(tc.data)
if ok != tc.ok || got != tc.want {
t.Fatalf("Sniff = (%q, %v), want (%q, %v)", got, ok, tc.want, tc.ok)
}
})
}
}
// TestAMRWBMagicIsRecognizedButUnsupported 断言「认得出但没这条路」:
// AMR-WB 的魔数能被嗅出(报错因此能说清问题),但注册表里没有它 →
// ErrUnsupportedFormat(design/25 §3.3 后置)。
func TestAMRWBMagicIsRecognizedButUnsupported(t *testing.T) {
eng := NewEngine(DefaultLimits(), WithCodecs(DefaultCodecs()...))
if got, ok := Sniff([]byte(amrWBMagic)); !ok || got != FormatAMRWB {
t.Fatalf("Sniff(amr-wb) = (%q, %v)", got, ok)
}
if eng.Supports(FormatAMRWB, FormatWAV) {
t.Fatalf("amr-wb -> wav must stay unsupported this round")
}
}