(t *testing.T)
| 2199 | } |
| 2200 | |
| 2201 | func TestPostWorkspaceAgentTaskSnapshot(t *testing.T) { |
| 2202 | t.Parallel() |
| 2203 | |
| 2204 | // Shared coderd with mock clock for all tests. |
| 2205 | clock := quartz.NewMock(t) |
| 2206 | ownerClient, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{ |
| 2207 | Clock: clock, |
| 2208 | }) |
| 2209 | owner := coderdtest.CreateFirstUser(t, ownerClient) |
| 2210 | |
| 2211 | createTaskWorkspace := func(t *testing.T, agentToken string) (taskID uuid.UUID, workspaceID uuid.UUID) { |
| 2212 | t.Helper() |
| 2213 | workspaceBuild := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{ |
| 2214 | OrganizationID: owner.OrganizationID, |
| 2215 | OwnerID: owner.UserID, |
| 2216 | }).WithTask(database.TaskTable{ |
| 2217 | Prompt: "test prompt", |
| 2218 | }, &proto.App{ |
| 2219 | Slug: "task-app", |
| 2220 | Url: "http://localhost:8080", |
| 2221 | }).WithAgent(func(agents []*proto.Agent) []*proto.Agent { |
| 2222 | agents[0].Auth = &proto.Agent_Token{Token: agentToken} |
| 2223 | return agents |
| 2224 | }).Do() |
| 2225 | return workspaceBuild.Task.ID, workspaceBuild.Workspace.ID |
| 2226 | } |
| 2227 | |
| 2228 | makePayload := func(t *testing.T, content string) []byte { |
| 2229 | t.Helper() |
| 2230 | data := agentapisdk.GetMessagesResponse{ |
| 2231 | Messages: []agentapisdk.Message{ |
| 2232 | {Id: 0, Role: "agent", Content: content, Time: time.Now()}, |
| 2233 | }, |
| 2234 | } |
| 2235 | b, err := json.Marshal(data) |
| 2236 | require.NoError(t, err) |
| 2237 | return b |
| 2238 | } |
| 2239 | |
| 2240 | makeRequest := func(t *testing.T, taskID uuid.UUID, agentToken string, payload []byte, format string) *http.Response { |
| 2241 | t.Helper() |
| 2242 | ctx := testutil.Context(t, testutil.WaitShort) |
| 2243 | |
| 2244 | url := ownerClient.URL.JoinPath("/api/v2/workspaceagents/me/tasks", taskID.String(), "log-snapshot").String() |
| 2245 | if format != "" { |
| 2246 | url += "?format=" + format |
| 2247 | } |
| 2248 | |
| 2249 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload)) |
| 2250 | require.NoError(t, err) |
| 2251 | req.Header.Set(codersdk.SessionTokenHeader, agentToken) |
| 2252 | res, err := http.DefaultClient.Do(req) |
| 2253 | require.NoError(t, err) |
| 2254 | return res |
| 2255 | } |
| 2256 | |
| 2257 | unmarshalSnapshot := func(t *testing.T, snapshotJSON json.RawMessage) agentapisdk.GetMessagesResponse { |
| 2258 | t.Helper() |
nothing calls this directly
no test coverage detected