Files
go-sip/internal/rpc/control_policy_validation_test.go

49 lines
2.6 KiB
Go

package rpc
import (
"context"
"testing"
"time"
agentv1 "git.ipao.vip/rogee/go-sip/gen/agent/v1"
)
func TestControlPolicyDoesNotInventMediaCompletion(t *testing.T) {
for _, tc := range []struct {
name, mode string
action agentv1.ControlAction
policy agentv1.ActiveCallPolicy
wantError, terminal bool
}{
{name: "missing policy", mode: "mock", action: agentv1.ControlAction_CONTROL_ACTION_STOP, wantError: true},
{name: "invalid policy", mode: "mock", action: agentv1.ControlAction_CONTROL_ACTION_STOP, policy: agentv1.ActiveCallPolicy(99), wantError: true},
{name: "mixed hangup requires media adapter", mode: "mixed", action: agentv1.ControlAction_CONTROL_ACTION_STOP, policy: agentv1.ActiveCallPolicy_ACTIVE_CALL_POLICY_HANGUP, wantError: true},
{name: "real pause hangup requires media adapter", mode: "real", action: agentv1.ControlAction_CONTROL_ACTION_PAUSE, policy: agentv1.ActiveCallPolicy_ACTIVE_CALL_POLICY_HANGUP, wantError: true},
{name: "mock pause hangup", mode: "mock", action: agentv1.ControlAction_CONTROL_ACTION_PAUSE, policy: agentv1.ActiveCallPolicy_ACTIVE_CALL_POLICY_HANGUP, terminal: true},
{name: "mock pause drain", mode: "mock", action: agentv1.ControlAction_CONTROL_ACTION_PAUSE, policy: agentv1.ActiveCallPolicy_ACTIVE_CALL_POLICY_DRAIN},
} {
t.Run(tc.name, func(t *testing.T) {
s := activatedServer(time.Date(2026, 9, 18, 1, 0, 0, 0, time.UTC), t)
// Unit-only adapter mode selection: no provider or network call is made.
s.mode = tc.mode
binding := &agentv1.ExecutionBinding{ExecutionId: "execution-policy", TenantId: "tenant-1", TenantKey: "tenant-key", TaskId: "task-1", TaskItemId: "item-1", TaskRevision: 1}
s.executions[binding.ExecutionId] = &executionRecord{binding: binding, taskRevision: 1, state: agentv1.ExecutionState_EXECUTION_STATE_OBSERVED}
response, err := s.ApplyTaskControl(context.Background(), &agentv1.ApplyTaskControlRequest{Meta: testMeta("control", "control-key", 1), Binding: binding, ExpectedTaskRevision: 1, Action: tc.action, ActiveCallPolicy: tc.policy})
if tc.wantError {
if err == nil || response != nil {
t.Fatal("unsupported policy reported applied")
}
if s.executions[binding.ExecutionId].taskRevision != 1 || s.executions[binding.ExecutionId].state != agentv1.ExecutionState_EXECUTION_STATE_OBSERVED {
t.Fatal("failed policy changed execution")
}
return
}
require.NoError(t, err)
require.Equal(t, agentv1.ResultCode_RESULT_CODE_APPLIED, response.Receipt.Result)
if (response.State == agentv1.ExecutionState_EXECUTION_STATE_TERMINAL) != tc.terminal {
t.Fatal("incorrect call termination")
}
})
}
}