-
Notifications
You must be signed in to change notification settings - Fork 300
feat(ai-agents): show command surfaces agent_endpoint via GetAgent #7938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,10 @@ | |
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "azureaiagent/internal/pkg/agents/agent_api" | ||
|
|
@@ -13,6 +16,25 @@ | |
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func captureStdout(t *testing.T, fn func() error) string { | ||
| t.Helper() | ||
| orig := os.Stdout | ||
| r, w, err := os.Pipe() | ||
| require.NoError(t, err) | ||
| os.Stdout = w | ||
|
|
||
| runErr := fn() | ||
|
|
||
| require.NoError(t, w.Close()) | ||
| os.Stdout = orig | ||
|
|
||
| var buf bytes.Buffer | ||
| _, err = io.Copy(&buf, r) | ||
| require.NoError(t, err) | ||
| require.NoError(t, runErr) | ||
| return buf.String() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: consider using .Cleanup to ensure stdout is restored even if the callback panics or a require trips early. |
||
| } | ||
|
|
||
| func TestShowCommand_AcceptsPositionalArg(t *testing.T) { | ||
| cmd := newShowCommand() | ||
| err := cmd.Args(cmd, []string{"my-agent"}) | ||
|
|
@@ -83,7 +105,7 @@ | |
| CreatedAt: 1735689600, // 2025-01-01T00:00:00Z | ||
| } | ||
|
|
||
| err := printAgentVersionJSON(version) | ||
| err := printAgentVersionJSON(version, nil) | ||
| require.NoError(t, err) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rendering tests are thorough. One gap: the key contract that show still succeeds when GetAgent fails (but fails when GetAgentVersion fails) doesn't have a direct test. If Run() is later refactored, this fallback behavior could regress without failing any test. Worth adding a test that exercises Run() with a mocked/fake client where GetAgent returns an error and verifying the command still produces valid output. |
||
|
|
||
|
|
@@ -165,10 +187,85 @@ | |
| }, | ||
| } | ||
|
|
||
| err := printAgentVersionTable(version) | ||
| err := printAgentVersionTable(version, nil) | ||
| require.NoError(t, err) | ||
| } | ||
|
Comment on lines
188
to
192
|
||
|
|
||
| func TestPrintAgentVersionJSON_WithEndpoint(t *testing.T) { | ||
| version := &agent_api.AgentVersionObject{ | ||
| Object: "agent.version", | ||
| ID: "ver-ep", | ||
| Name: "ep-agent", | ||
| Version: "1", | ||
| } | ||
| endpoint := &agent_api.AgentEndpointInfo{ | ||
| Protocols: []string{"responses", "a2a"}, | ||
| AuthorizationSchemes: []agent_api.AuthorizationScheme{ | ||
| { | ||
| Type: "EntraIDAuth", | ||
| IsolationKeySource: &agent_api.IsolationKeySource{Kind: "ProjectScopedManagedIdentity"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| out := captureStdout(t, func() error { return printAgentVersionJSON(version, endpoint) }) | ||
|
|
||
| var result map[string]any | ||
| require.NoError(t, json.Unmarshal([]byte(out), &result)) | ||
|
|
||
| ep, ok := result["agent_endpoint"].(map[string]any) | ||
| require.True(t, ok, "agent_endpoint missing in JSON output") | ||
| protocols := ep["protocols"].([]any) | ||
| assert.ElementsMatch(t, []any{"responses", "a2a"}, protocols) | ||
| schemes := ep["authorization_schemes"].([]any) | ||
| require.Len(t, schemes, 1) | ||
| scheme := schemes[0].(map[string]any) | ||
| assert.Equal(t, "EntraIDAuth", scheme["type"]) | ||
| assert.Equal(t, "ProjectScopedManagedIdentity", scheme["isolation_key_source"].(map[string]any)["kind"]) | ||
| } | ||
|
|
||
| func TestPrintAgentVersionJSON_NilEndpointOmitsField(t *testing.T) { | ||
| version := &agent_api.AgentVersionObject{ | ||
| Object: "agent.version", | ||
| ID: "ver-no-ep", | ||
| Name: "no-ep-agent", | ||
| Version: "1", | ||
| } | ||
|
|
||
| out := captureStdout(t, func() error { return printAgentVersionJSON(version, nil) }) | ||
|
|
||
| var result map[string]any | ||
| require.NoError(t, json.Unmarshal([]byte(out), &result)) | ||
| _, hasEndpoint := result["agent_endpoint"] | ||
| assert.False(t, hasEndpoint, "agent_endpoint should be omitted when nil") | ||
| } | ||
|
|
||
| func TestPrintAgentVersionTable_WithEndpoint(t *testing.T) { | ||
| version := &agent_api.AgentVersionObject{ | ||
| Object: "agent.version", | ||
| ID: "ver-ep", | ||
| Name: "ep-agent", | ||
| Version: "1", | ||
| } | ||
| endpoint := &agent_api.AgentEndpointInfo{ | ||
| Protocols: []string{"responses", "a2a"}, | ||
| AuthorizationSchemes: []agent_api.AuthorizationScheme{ | ||
| {Type: "EntraIDAuth", IsolationKeySource: &agent_api.IsolationKeySource{Kind: "ProjectScopedManagedIdentity"}}, | ||
| {Type: "ApiKeyAuth"}, | ||
| }, | ||
| } | ||
|
|
||
| out := captureStdout(t, func() error { return printAgentVersionTable(version, endpoint) }) | ||
|
|
||
| assert.Contains(t, out, "Endpoint Protocols") | ||
| assert.Contains(t, out, "responses, a2a") | ||
| assert.Contains(t, out, "Endpoint Auth[0]") | ||
| assert.Contains(t, out, "EntraIDAuth") | ||
| assert.Contains(t, out, "isolation: ProjectScopedManagedIdentity") | ||
| assert.Contains(t, out, "Endpoint Auth[1]") | ||
| assert.Contains(t, out, "ApiKeyAuth") | ||
| } | ||
|
|
||
| func TestPrintAgentVersionTable_MinimalFields(t *testing.T) { | ||
| version := &agent_api.AgentVersionObject{ | ||
| Object: "agent.version", | ||
|
|
@@ -177,6 +274,6 @@ | |
| Version: "1", | ||
| } | ||
|
|
||
| err := printAgentVersionTable(version) | ||
| err := printAgentVersionTable(version, nil) | ||
| require.NoError(t, err) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider logging the error in debug mode when GetAgent fails. Without it, users can't distinguish 'the agent has no endpoint' from 'the fetch failed silently'. Something like:
This aligns with the log.Printf pattern used in helpers.go for non-blocking errors.