66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package logging
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
func TestRPCLogsObserveActualMessageWithoutMutatingIt(t *testing.T) {
|
|
out := installTestLogger(t, true)
|
|
raw := []byte(`{"task_id":"4","params":{"speed":1.0},"api_key":"private-key","prompt":"private-prompt"}`)
|
|
req := &agentpb.ExecuteApprovedRequest{Meta: &agentpb.RequestMeta{RequestId: "request-1"}, CallId: "execution-1", TaskConfigJson: raw, ProvidersJson: []byte(`[{"api_key":"private-session"}]`)}
|
|
res := &agentpb.ExecuteApprovedResponse{}
|
|
interceptor := UnaryClientInterceptor("dispatcher", "agent")
|
|
err := interceptor(context.Background(), "/sip.agent.v1.AgentControlService/ExecuteApproved", req, res, nil, func(_ context.Context, _ string, sent, received any, _ *grpc.ClientConn, _ ...grpc.CallOption) error {
|
|
if sent != req || received != res || !bytes.Equal(req.TaskConfigJson, raw) {
|
|
t.Fatal("logger mutated RPC")
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := out.String()
|
|
for _, secret := range []string{"private-key", "private-prompt", "private-session"} {
|
|
if strings.Contains(text, secret) {
|
|
t.Fatalf("leaked %s", secret)
|
|
}
|
|
}
|
|
for _, expected := range []string{"request-1", "execution-1", "dispatcher->agent", "agent->dispatcher", "response_received", `\"speed\":1.0`} {
|
|
if !strings.Contains(text, expected) {
|
|
t.Fatalf("missing %s: %s", expected, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRPCNormalModeAndErrorsNeverIncludeBody(t *testing.T) {
|
|
out := installTestLogger(t, false)
|
|
req := &agentpb.ReportCallEndedRequest{SourceEventId: "execution-2"}
|
|
expected := errors.New("connection failed")
|
|
_, err := UnaryServerInterceptor("agent", "dispatcher")(context.Background(), req, &grpc.UnaryServerInfo{FullMethod: "/ReportCallEnded"}, func(context.Context, any) (any, error) { return nil, expected })
|
|
if !errors.Is(err, expected) {
|
|
t.Fatalf("error changed: %v", err)
|
|
}
|
|
text := out.String()
|
|
if strings.Contains(text, "private-token") || strings.Contains(text, `"body"`) {
|
|
t.Fatal(text)
|
|
}
|
|
for _, part := range []string{"execution-2", "response_failed", "duration_ms", "connection failed"} {
|
|
if !strings.Contains(text, part) {
|
|
t.Fatalf("missing %s", part)
|
|
}
|
|
}
|
|
for _, line := range strings.Split(strings.TrimSpace(text), "\n") {
|
|
if !json.Valid([]byte(line)) {
|
|
t.Fatal(line)
|
|
}
|
|
}
|
|
}
|