92 lines
3 KiB
Go
92 lines
3 KiB
Go
package node
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
runtime "iop/packages/go/agentruntime"
|
|
iop "iop/proto/gen/iop"
|
|
)
|
|
|
|
func TestRunRequestFromProtoPreservesWireFields(t *testing.T) {
|
|
policy, err := structpb.NewStruct(map[string]any{"allow": true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
input, err := structpb.NewStruct(map[string]any{"prompt": "hello"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire := &iop.RunRequest{
|
|
RunId: "run-1",
|
|
Adapter: "cli@primary",
|
|
Target: "codex",
|
|
SessionId: "session-1",
|
|
SessionMode: iop.RunSessionMode_RUN_SESSION_MODE_REQUIRE_EXISTING,
|
|
Background: true,
|
|
Workspace: "/workspace",
|
|
Policy: policy,
|
|
Input: input,
|
|
TimeoutSec: 30,
|
|
Metadata: map[string]string{"source": "test"},
|
|
}
|
|
|
|
got := runRequestFromProto(wire)
|
|
if got.RunID != wire.RunId || got.Adapter != wire.Adapter || got.Target != wire.Target ||
|
|
got.SessionID != wire.SessionId || got.SessionMode != runtime.SessionModeRequireExisting ||
|
|
got.Background != wire.Background || got.Workspace != wire.Workspace ||
|
|
got.TimeoutSec != int(wire.TimeoutSec) {
|
|
t.Fatalf("runRequestFromProto() = %#v", got)
|
|
}
|
|
if !reflect.DeepEqual(got.Policy, policy.AsMap()) || !reflect.DeepEqual(got.Input, input.AsMap()) ||
|
|
!reflect.DeepEqual(got.Metadata, wire.Metadata) {
|
|
t.Fatalf("mapped maps = policy %#v input %#v metadata %#v", got.Policy, got.Input, got.Metadata)
|
|
}
|
|
}
|
|
|
|
func TestRunEventToProtoPreservesLegacyValues(t *testing.T) {
|
|
timestamp := time.Unix(0, 1234)
|
|
event := runtime.RuntimeEvent{
|
|
RunID: "run-1",
|
|
Type: runtime.EventTypeError,
|
|
Error: "legacy error",
|
|
Failure: &runtime.Failure{Code: runtime.FailureCodeProvider, Message: "typed error"},
|
|
Metadata: map[string]string{"key": "value"},
|
|
Usage: &runtime.UsageStats{
|
|
InputTokens: 1,
|
|
OutputTokens: 2,
|
|
ReasoningTokens: 3,
|
|
CachedInputTokens: 4,
|
|
},
|
|
Timestamp: timestamp,
|
|
}
|
|
|
|
got := runEventToProto(event, "node-1", "session-1", true)
|
|
if got.GetRunId() != "run-1" || got.GetType() != "error" || got.GetError() != "legacy error" ||
|
|
got.GetNodeId() != "node-1" || got.GetSessionId() != "session-1" || !got.GetBackground() ||
|
|
got.GetTimestamp() != timestamp.UnixNano() {
|
|
t.Fatalf("runEventToProto() = %#v", got)
|
|
}
|
|
if got.GetUsage().GetInputTokens() != 1 || got.GetUsage().GetOutputTokens() != 2 ||
|
|
got.GetUsage().GetReasoningTokens() != 3 || got.GetUsage().GetCachedInputTokens() != 4 {
|
|
t.Fatalf("usage = %#v", got.GetUsage())
|
|
}
|
|
if !reflect.DeepEqual(got.GetMetadata(), event.Metadata) {
|
|
t.Fatalf("metadata = %#v, want %#v", got.GetMetadata(), event.Metadata)
|
|
}
|
|
}
|
|
|
|
func TestRunEventToProtoUsesTypedFailureMessageAsFallback(t *testing.T) {
|
|
got := runEventToProto(runtime.RuntimeEvent{
|
|
RunID: "run-1",
|
|
Type: runtime.EventTypeError,
|
|
Failure: &runtime.Failure{Code: runtime.FailureCodeUnavailable, Message: "unavailable"},
|
|
Timestamp: time.Unix(0, 1),
|
|
}, "node-1", "default", false)
|
|
if got.GetError() != "unavailable" {
|
|
t.Fatalf("error = %q, want unavailable", got.GetError())
|
|
}
|
|
}
|