MCPcopy Create free account
hub / github.com/compozy/agh / TestUnixSocketClientAgentContextAndSpawnMethods

Function TestUnixSocketClientAgentContextAndSpawnMethods

internal/cli/client_test.go:1087–1148  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1085}
1086
1087func TestUnixSocketClientAgentContextAndSpawnMethods(t *testing.T) {
1088 t.Parallel()
1089
1090 t.Run("Should get agent context and spawn child sessions", func(t *testing.T) {
1091 t.Parallel()
1092
1093 credentials := agentidentity.Credentials{
1094 SessionID: "sess-1",
1095 AgentName: "coder",
1096 WorkspaceID: "ws-1",
1097 }
1098 client := &unixSocketClient{
1099 socketPath: "/tmp/agh.sock",
1100 httpClient: &http.Client{
1101 Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
1102 assertAgentRequestHeaders(t, req, credentials)
1103 switch {
1104 case req.Method == http.MethodGet && req.URL.Path == "/api/agent/context":
1105 return newHTTPResponse(
1106 http.StatusOK,
1107 `{"context":{"self":{"session_id":"sess-1","agent_name":"coder","provider":"test"},"workspace":{"id":"ws-1"},"session":{"id":"sess-1","agent_name":"coder","workspace_id":"ws-1","workspace_path":"/tmp","state":"active","created_at":"2026-04-03T12:00:00Z","updated_at":"2026-04-03T12:00:00Z"}}}`,
1108 ), nil
1109 case req.Method == http.MethodPost && req.URL.Path == "/api/agent/spawn":
1110 var request AgentSpawnRequest
1111 if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
1112 t.Fatalf("decode spawn body: %v", err)
1113 }
1114 if request.AgentName != "worker" || request.SpawnRole != "reviewer" {
1115 t.Fatalf("spawn request = %#v, want worker reviewer", request)
1116 }
1117 return newHTTPResponse(
1118 http.StatusCreated,
1119 `{"spawn":{"session":{"id":"sess-child","agent_name":"worker","workspace_id":"ws-1","workspace_path":"/tmp","state":"active","created_at":"2026-04-03T12:00:00Z","updated_at":"2026-04-03T12:00:00Z"},"lineage":{"parent_session_id":"sess-1","root_session_id":"sess-1","depth":1},"permissions":{}}}`,
1120 ), nil
1121 default:
1122 t.Fatalf("unexpected request = %s %s", req.Method, req.URL.Path)
1123 return nil, nil
1124 }
1125 }),
1126 },
1127 }
1128
1129 contextRecord, err := client.AgentContext(context.Background(), credentials)
1130 if err != nil {
1131 t.Fatalf("AgentContext() error = %v", err)
1132 }
1133 if contextRecord.Self.SessionID != "sess-1" || contextRecord.Workspace.ID != "ws-1" {
1134 t.Fatalf("AgentContext() = %#v, want caller context", contextRecord)
1135 }
1136
1137 spawn, err := client.AgentSpawn(context.Background(), AgentSpawnRequest{
1138 AgentName: "worker",
1139 SpawnRole: "reviewer",
1140 }, credentials)
1141 if err != nil {
1142 t.Fatalf("AgentSpawn() error = %v", err)
1143 }
1144 if spawn.Session.ID != "sess-child" || spawn.Session.AgentName != "worker" {

Callers

nothing calls this directly

Calls 6

AgentContextMethod · 0.95
AgentSpawnMethod · 0.95
roundTripperFuncFuncType · 0.85
newHTTPResponseFunction · 0.70
RunMethod · 0.65

Tested by

no test coverage detected