84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/contracts"
|
|
"git.ipao.vip/rogee/go-sip/internal/contract"
|
|
)
|
|
|
|
func TestRootHasExplicitRoles(t *testing.T) {
|
|
root := newRootCommand()
|
|
if _, _, err := root.Find([]string{"agent"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := root.Find([]string{"dispatcher"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestAgentRejectsInvalidMediaPortEnvironment(t *testing.T) {
|
|
t.Setenv("AGENT_CALL_MEDIA_PORT", "invalid")
|
|
root := newRootCommand()
|
|
root.SetArgs([]string{"agent"})
|
|
if err := root.Execute(); err == nil || !strings.Contains(err.Error(), "AGENT_CALL_MEDIA_PORT") {
|
|
t.Fatalf("expected invalid port error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWriteResultIsJSON(t *testing.T) {
|
|
var b bytes.Buffer
|
|
data, err := marshalResultForTest(map[string]string{"status": "ok"})
|
|
if err != nil || len(data) == 0 {
|
|
t.Fatalf("data=%q err=%v", data, err)
|
|
}
|
|
b.Write(data)
|
|
if b.Len() == 0 {
|
|
t.Fatal("empty result")
|
|
}
|
|
}
|
|
|
|
func TestValidateCallAISnapshotAllowsASROnly(t *testing.T) {
|
|
raw, err := contracts.Read("examples/agent-version-asr-only.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
snapshot, err := validateCallAISnapshot(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if snapshot.Mode != "asr_only" {
|
|
t.Fatalf("snapshot mode=%q, want asr_only", snapshot.Mode)
|
|
}
|
|
}
|
|
|
|
func TestBuildCallEndpointUsesArtifactRoute(t *testing.T) {
|
|
raw, err := contracts.Files.ReadFile("local/v0.3/examples/static-cell-artifact-real-v2.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
artifact, err := contract.ValidateStaticArtifact(raw, contract.StaticArtifactExpectation{CellID: "cell-single", Mode: "real"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
endpoint, err := buildCallEndpoint(artifact, "provider-third", "15830461047")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if endpoint != "PJSIP/mka75515830461047@provider-third" {
|
|
t.Fatalf("endpoint=%q", endpoint)
|
|
}
|
|
media, err := mediaForCall(artifact, "provider-third")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if media.Format != "alaw" || media.SampleRateHz != 8000 || media.PayloadType != 8 {
|
|
t.Fatalf("media=%+v", media)
|
|
}
|
|
if _, err := buildCallEndpoint(artifact, "provider-third", "10000000000"); err == nil {
|
|
t.Fatal("disallowed target was accepted")
|
|
}
|
|
}
|