76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
package configread
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestReadTaskTrunkConcurrency(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, trunks string
|
|
valid bool
|
|
}{
|
|
{"objects", `[{"trunk_id":"12","concurrency":5},{"trunk_id":"15","concurrency":5}]`, true},
|
|
{"disabled", `[{"trunk_id":"12","concurrency":0}]`, true},
|
|
{"old strings", `["12"]`, false},
|
|
{"missing concurrency", `[{"trunk_id":"12"}]`, false},
|
|
{"negative", `[{"trunk_id":"12","concurrency":-1}]`, false},
|
|
{"duplicate IDs", `[{"trunk_id":"12","concurrency":1},{"trunk_id":"12","concurrency":2}]`, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var task map[string]json.RawMessage
|
|
if err := json.Unmarshal(example(t, "config-read-task-asr"), &task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task["allowed_trunk_ids"] = json.RawMessage(tc.trunks)
|
|
body, err := json.Marshal(task)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/internal/v1/dispatcher/task/task-asr":
|
|
_, _ = w.Write(body)
|
|
case "/internal/v1/dispatcher/tenant/1001/quota":
|
|
_, _ = w.Write(example(t, "config-read-quota"))
|
|
case "/internal/v1/dispatcher/ai-providers":
|
|
_, _ = w.Write(example(t, "config-read-providers"))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
client, err := NewClient(server.URL, "c046b893-8628-4589-ae50-619d049248a6", "isolated-secret", server.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providers, err := client.ReadProviders(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
snapshot, err := client.ReadTask(context.Background(), "task-asr", 1001, approvedSIP(t), providers)
|
|
if (err == nil) != tc.valid {
|
|
t.Fatalf("accepted=%t, want %t: %v", err == nil, tc.valid, err)
|
|
}
|
|
if !tc.valid {
|
|
return
|
|
}
|
|
encoded, err := json.Marshal(snapshot.Task)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var roundTrip map[string]json.RawMessage
|
|
if err := json.Unmarshal(encoded, &roundTrip); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(roundTrip["allowed_trunk_ids"]) != tc.trunks {
|
|
t.Fatalf("trunk limits changed: %s", roundTrip["allowed_trunk_ids"])
|
|
}
|
|
})
|
|
}
|
|
}
|