100 lines
4.4 KiB
Go
100 lines
4.4 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/configread"
|
|
)
|
|
|
|
func currentPolicySnapshot(t *testing.T) configread.CurrentSnapshot {
|
|
t.Helper()
|
|
var snapshot configread.CurrentSnapshot
|
|
if err := json.Unmarshal(currentConfigExample(t, "config-read-task-asr"), &snapshot.Task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sip := string(currentConfigExample(t, "config-read-sip"))
|
|
for _, replacement := range [][2]string{{`"transport":null`, `"transport":"udp"`}, {`"auth_mode":null`, `"auth_mode":"ip"`}, {`"registration_required":null`, `"registration_required":false`}, {`"max_concurrent_calls":null`, `"max_concurrent_calls":2`}} {
|
|
sip = strings.Replace(sip, replacement[0], replacement[1], 1)
|
|
}
|
|
if err := json.Unmarshal([]byte(sip), &snapshot.SIP); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(currentConfigExample(t, "config-read-quota"), &snapshot.Quota); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var providerList struct {
|
|
Providers []configread.CurrentProvider `json:"providers"`
|
|
}
|
|
if err := json.Unmarshal(currentConfigExample(t, "config-read-providers"), &providerList); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
snapshot.Providers = make(map[string]configread.CurrentProvider, len(providerList.Providers))
|
|
for _, provider := range providerList.Providers {
|
|
snapshot.Providers[provider.ProviderRef] = provider
|
|
}
|
|
return snapshot
|
|
}
|
|
|
|
func currentMonday(hour, minute int) time.Time {
|
|
return time.Date(2026, 9, 21, hour, minute, 0, 0, time.FixedZone("Asia/Shanghai", 8*60*60))
|
|
}
|
|
|
|
func TestCurrentSelectTrunkWhitelistScheduleAndCaller(t *testing.T) {
|
|
snapshot := currentPolicySnapshot(t)
|
|
at := currentMonday(9, 30)
|
|
selected, err := SelectCurrentTrunk(snapshot, "15003164745", at, map[string]int64{}, map[string]int64{"trunk-mock": 8})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if selected.TrunkID != "trunk-mock" || selected.CallerID != "BD00000000" || selected.Callee != "15003164745" || selected.DialedCallee != "15003164745" || selected.Deadline.After(at.Add(120*time.Second)) || !selected.Deadline.After(at) || selected.MaxCallDurationMS != snapshot.Task.MaxCallDurationMS {
|
|
t.Fatalf("selected route/prefix/caller/deadline invalid: %+v", selected)
|
|
}
|
|
for _, number := range []string{"", "708915003164745", "15003164746", "abc", "158304610470"} {
|
|
if _, err := SelectCurrentTrunk(snapshot, number, at, nil, map[string]int64{"trunk-mock": 8}); !errors.Is(err, ErrCurrentCalleeRejected) {
|
|
t.Fatalf("unapproved number %q not rejected: %v", number, err)
|
|
}
|
|
}
|
|
for _, at := range []time.Time{currentMonday(8, 59), currentMonday(20, 0)} {
|
|
if _, err := SelectCurrentTrunk(snapshot, "15003164745", at, nil, map[string]int64{"trunk-mock": 8}); !errors.Is(err, ErrCurrentRuleWait) {
|
|
t.Fatalf("outside half-open schedule did not wait: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCurrentSelectTrunkFailsClosedForUnknownLineAndLimits(t *testing.T) {
|
|
snapshot := currentPolicySnapshot(t)
|
|
at := currentMonday(9, 30)
|
|
if _, err := SelectCurrentTrunk(snapshot, "15003164745", at, nil, nil); !errors.Is(err, ErrCurrentRuleWait) {
|
|
t.Fatalf("unloaded line did not hold command: %v", err)
|
|
}
|
|
if _, err := SelectCurrentTrunk(snapshot, "15003164745", at, map[string]int64{"trunk-mock": 2}, map[string]int64{"trunk-mock": 8}); !errors.Is(err, ErrCurrentRuleWait) {
|
|
t.Fatalf("full trunk did not hold command: %v", err)
|
|
}
|
|
sip := string(currentConfigExample(t, "config-read-sip"))
|
|
if err := json.Unmarshal([]byte(sip), &snapshot.SIP); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := SelectCurrentTrunk(snapshot, "15003164745", at, nil, map[string]int64{"trunk-mock": 8}); !errors.Is(err, ErrCurrentRuleInvalid) {
|
|
t.Fatalf("unknown transport/auth/quota was not fail-closed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCurrentSelectTrunkCapsCallByAIAndPreservesPrefix(t *testing.T) {
|
|
snapshot := currentPolicySnapshot(t)
|
|
snapshot.Task.Agent.Mode = "full_ai"
|
|
snapshot.Task.Agent.Raw = []byte(`{"mode":"full_ai","conversation":{"max_duration_ms":60000}}`)
|
|
snapshot.SIP.Trunks = []byte(strings.Replace(string(snapshot.SIP.Trunks), `"dial_prefix":""`, `"dial_prefix":"7089"`, 1))
|
|
at := currentMonday(9, 30)
|
|
selected, err := SelectCurrentTrunk(snapshot, "15830461047", at, nil, map[string]int64{"trunk-mock": 8})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if selected.DialedCallee != "708915830461047" || selected.Callee != "15830461047" || !selected.Deadline.Equal(at.Add(time.Minute)) || selected.MaxCallDurationMS != 60000 {
|
|
t.Fatalf("wrong prefix/AI duration bound: %+v", selected)
|
|
}
|
|
}
|