|
4 | 4 | package agent_api |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "io" |
7 | 8 | "net/http" |
| 9 | + "strings" |
8 | 10 | "testing" |
9 | 11 |
|
10 | 12 | "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
@@ -86,3 +88,87 @@ func TestGetSession_404ReturnsError(t *testing.T) { |
86 | 88 | ) |
87 | 89 | require.Error(t, err, "404 should be an error from GetSession") |
88 | 90 | } |
| 91 | + |
| 92 | +// fakeBodyTransport returns a canned status code and JSON body. |
| 93 | +type fakeBodyTransport struct { |
| 94 | + statusCode int |
| 95 | + body string |
| 96 | +} |
| 97 | + |
| 98 | +func (f *fakeBodyTransport) Do(req *http.Request) (*http.Response, error) { |
| 99 | + return &http.Response{ |
| 100 | + StatusCode: f.statusCode, |
| 101 | + Header: http.Header{"Content-Type": {"application/json"}}, |
| 102 | + Body: io.NopCloser(strings.NewReader(f.body)), |
| 103 | + Request: req, |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +func TestCreateSession_Returns201WithBody(t *testing.T) { |
| 108 | + body := `{ |
| 109 | + "agent_session_id": "sess-new", |
| 110 | + "version_indicator": {"type": "version_ref", "agent_version": "3"}, |
| 111 | + "status": "running", |
| 112 | + "created_at": 1700000000, |
| 113 | + "last_accessed_at": 1700000100, |
| 114 | + "expires_at": 1700086400 |
| 115 | + }` |
| 116 | + |
| 117 | + client := newTestClient( |
| 118 | + "https://test.example.com/api/projects/proj", |
| 119 | + &fakeBodyTransport{ |
| 120 | + statusCode: http.StatusCreated, |
| 121 | + body: body, |
| 122 | + }, |
| 123 | + ) |
| 124 | + |
| 125 | + session, err := client.CreateSession( |
| 126 | + t.Context(), "my-agent", "", |
| 127 | + &CreateAgentSessionRequest{ |
| 128 | + VersionIndicator: &VersionIndicator{ |
| 129 | + Type: "version_ref", |
| 130 | + AgentVersion: "3", |
| 131 | + }, |
| 132 | + }, |
| 133 | + "2025-11-15-preview", |
| 134 | + ) |
| 135 | + |
| 136 | + require.NoError(t, err) |
| 137 | + require.Equal(t, "sess-new", session.AgentSessionID) |
| 138 | + require.Equal(t, "3", session.VersionIndicator.AgentVersion) |
| 139 | + require.Equal(t, AgentSessionStatus("running"), session.Status) |
| 140 | +} |
| 141 | + |
| 142 | +func TestListSessions_Returns200WithPagination(t *testing.T) { |
| 143 | + body := `{ |
| 144 | + "data": [ |
| 145 | + { |
| 146 | + "agent_session_id": "sess-1", |
| 147 | + "version_indicator": {"type": "version_ref", "agent_version": "2"}, |
| 148 | + "status": "running", |
| 149 | + "created_at": 1700000000, |
| 150 | + "last_accessed_at": 1700000100, |
| 151 | + "expires_at": 1700086400 |
| 152 | + } |
| 153 | + ], |
| 154 | + "pagination_token": "next-page-abc" |
| 155 | + }` |
| 156 | + |
| 157 | + client := newTestClient( |
| 158 | + "https://test.example.com/api/projects/proj", |
| 159 | + &fakeBodyTransport{ |
| 160 | + statusCode: http.StatusOK, |
| 161 | + body: body, |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + result, err := client.ListSessions( |
| 166 | + t.Context(), "my-agent", nil, nil, "2025-11-15-preview", |
| 167 | + ) |
| 168 | + |
| 169 | + require.NoError(t, err) |
| 170 | + require.Len(t, result.Data, 1) |
| 171 | + require.Equal(t, "sess-1", result.Data[0].AgentSessionID) |
| 172 | + require.NotNil(t, result.PaginationToken) |
| 173 | + require.Equal(t, "next-page-abc", *result.PaginationToken) |
| 174 | +} |
0 commit comments