refactor: extract flag parsing, validation, and packet processing into functions; add README and .gitignore

This commit is contained in:
2026-07-16 11:56:40 +03:00
parent f473846bf6
commit d042637351
13 changed files with 585 additions and 8358 deletions
+60
View File
@@ -0,0 +1,60 @@
package main
import (
"encoding/binary"
"math"
"strings"
"testing"
"time"
)
func TestPCM16LEToFloat32(t *testing.T) {
pcm := make([]byte, 6)
binary.LittleEndian.PutUint16(pcm[0:], uint16(0))
binary.LittleEndian.PutUint16(pcm[2:], uint16(32767))
binary.LittleEndian.PutUint16(pcm[4:], uint16(32768))
got := pcm16LEToFloat32(pcm)
want := []float32{0, 32767.0 / 32768.0, -1}
for i := range want {
if math.Abs(float64(got[i]-want[i])) > 0.00001 {
t.Fatalf("sample %d = %f, want %f", i, got[i], want[i])
}
}
}
func TestDurationSamples(t *testing.T) {
if got := durationSamples(time.Second); got != sampleRate {
t.Fatalf("one second = %d samples, want %d", got, sampleRate)
}
if got := durationSamples(1500 * time.Millisecond); got != 24000 {
t.Fatalf("1.5 seconds = %d samples, want 24000", got)
}
}
func TestRMSDBFS(t *testing.T) {
if got := rmsDBFS([]float32{1, -1}); math.Abs(got) > 0.00001 {
t.Fatalf("full-scale RMS = %f dBFS, want 0", got)
}
if got := rmsDBFS([]float32{0, 0}); !math.IsInf(got, -1) {
t.Fatalf("silence = %f dBFS, want -Inf", got)
}
}
func TestValidateSettings(t *testing.T) {
if err := validateSettings(settings{mode: "fixed", chunkDuration: time.Second, threads: 1}); err != nil {
t.Fatalf("valid settings failed: %v", err)
}
if err := validateSettings(settings{mode: "", chunkDuration: time.Second, threads: 1}); err == nil {
t.Fatal("missing mode unexpectedly passed")
}
}
func TestValidateModelsExplainsHowToInstallThem(t *testing.T) {
_, err := validateModels(t.TempDir(), "vad")
if err == nil {
t.Fatal("missing models unexpectedly passed validation")
}
if !strings.Contains(err.Error(), "./scripts/download-models.sh") {
t.Fatalf("missing-model error did not provide setup command: %v", err)
}
}