Files
go-sip/internal/dispatcher/current_ai_test.go
T

114 lines
4.1 KiB
Go

package dispatcher
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"git.ipao.vip/rogee/go-sip/internal/configread"
"git.ipao.vip/rogee/go-sip/internal/store"
)
func unsupportedCurrentASRTask(t *testing.T) []byte {
t.Helper()
original := currentConfigExample(t, "config-read-task-asr")
modified := bytes.Replace(original, []byte(`"language":"zh-CN"`), []byte(`"language":"fr-FR"`), 1)
if bytes.Equal(original, modified) {
t.Fatal("ASR fixture did not contain the expected language")
}
return modified
}
func TestCurrentBootstrapRejectsSDKUnsupportedTaskBeforeOpeningAdmission(t *testing.T) {
id := "c046b893-8628-4589-ae50-619d049248a6"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body []byte
switch r.URL.Path {
case "/internal/v1/dispatcher/sip":
body = currentConfigExample(t, "config-read-sip")
case "/internal/v1/dispatcher/tasks":
if r.URL.Query().Get("after") == "" {
body = currentConfigExample(t, "task-discovery-page")
} else {
body = currentConfigExample(t, "task-discovery-end")
}
case "/internal/v1/dispatcher/ai-providers":
body = currentConfigExample(t, "config-read-providers")
case "/internal/v1/dispatcher/task/task-asr":
body = unsupportedCurrentASRTask(t)
case "/internal/v1/dispatcher/tenant/1001/quota":
body = currentConfigExample(t, "config-read-quota")
default:
http.Error(w, "unknown path", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(body)
}))
defer server.Close()
client, err := configread.NewClient(server.URL, id, "test-secret", server.Client())
if err != nil {
t.Fatal(err)
}
db, err := store.OpenCurrent(filepath.Join(t.TempDir(), "state.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
drained := false
b := CurrentBootstrap{
DispatcherID: id, Client: client, Store: db,
VerifySIP: func(context.Context, configread.CurrentSIP) error { return nil },
DrainControls: func(context.Context) error { drained = true; return nil },
}
err = b.Run(context.Background())
if err == nil || !strings.Contains(err.Error(), "ASR language") || drained {
t.Fatalf("SDK-unsupported task must not be admitted or drained: %v, drained=%t", err, drained)
}
if admitted, err := db.CanAdmit(id, 1001, "task-asr"); err != nil || admitted {
t.Fatalf("invalid AI task opened admission: admitted=%t err=%v", admitted, err)
}
if _, err := db.ReadSnapshot(id, 1001, "task-asr"); err == nil {
t.Fatal("unsupported AI snapshot was persisted as runnable")
}
}
func TestCurrentExecuteCorruptAISnapshotFailsClosedBeforeReservation(t *testing.T) {
db, err := store.OpenCurrent(filepath.Join(t.TempDir(), "state.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
snapshot := currentPolicySnapshot(t)
if err := snapshot.Task.UnmarshalJSON(unsupportedCurrentASRTask(t)); err != nil {
t.Fatal(err)
}
id := snapshot.Task.DispatcherID
if err := db.ApplyDiscoverySnapshot(id, []configread.CurrentDiscoveredTask{{TaskID: snapshot.Task.TaskID, TenantID: snapshot.Task.TenantID, TaskRevision: snapshot.Task.TaskRevision, Status: "running"}}); err != nil {
t.Fatal(err)
}
if err := db.SaveSnapshot(snapshot); err != nil {
t.Fatal(err)
}
if err := db.MarkReadyForSIP(id, snapshot.SIP.Revision); err != nil {
t.Fatal(err)
}
originator := &currentFakeOriginator{loaded: map[string]int64{"trunk-mock": 8}}
controller := &CurrentExecuteController{
DispatcherID: id, Store: db, Originator: originator, Publisher: &currentFakePublisher{},
Now: func() time.Time { return currentMonday(9, 30) },
}
err = controller.ProcessExecute(context.Background(), currentExecuteBody(t, "bad-ai", "15003164745"))
if err == nil || !strings.Contains(err.Error(), "ASR language") || len(originator.calls) != 0 {
t.Fatalf("corrupt AI config must not reach Agent: err=%v calls=%d", err, len(originator.calls))
}
if admitted, err := db.CanAdmit(id, 1001, "task-asr"); err != nil || admitted {
t.Fatalf("corrupt persisted task remained eligible: admitted=%t err=%v", admitted, err)
}
}