This test performs an integration-style test for tasks functionality. nolint:tparallel // The sub-tests of this test must be run sequentially.
(t *testing.T)
| 37 | // |
| 38 | //nolint:tparallel // The sub-tests of this test must be run sequentially. |
| 39 | func Test_Tasks(t *testing.T) { |
| 40 | t.Parallel() |
| 41 | |
| 42 | // Given: a template configured for tasks |
| 43 | var ( |
| 44 | ctx = testutil.Context(t, testutil.WaitLong) |
| 45 | client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) |
| 46 | owner = coderdtest.CreateFirstUser(t, client) |
| 47 | userClient, _ = coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 48 | initMsg = agentapisdk.Message{ |
| 49 | Content: "test task input for " + t.Name(), |
| 50 | Id: 0, |
| 51 | Role: "user", |
| 52 | Time: time.Now().UTC(), |
| 53 | } |
| 54 | authToken = uuid.NewString() |
| 55 | echoAgentAPI = startFakeAgentAPI(t, fakeAgentAPIEcho(ctx, t, initMsg, "hello")) |
| 56 | taskTpl = createAITaskTemplate(t, client, owner.OrganizationID, withAgentToken(authToken), withSidebarURL(echoAgentAPI.URL())) |
| 57 | taskName = strings.ReplaceAll(testutil.GetRandomName(t), "_", "-") |
| 58 | ) |
| 59 | |
| 60 | for _, tc := range []struct { |
| 61 | name string |
| 62 | cmdArgs []string |
| 63 | assertFn func(stdout string, userClient *codersdk.Client) |
| 64 | }{ |
| 65 | { |
| 66 | name: "create task", |
| 67 | cmdArgs: []string{"task", "create", "test task input for " + t.Name(), "--name", taskName, "--template", taskTpl.Name}, |
| 68 | assertFn: func(stdout string, userClient *codersdk.Client) { |
| 69 | require.Contains(t, stdout, taskName, "task name should be in output") |
| 70 | }, |
| 71 | }, |
| 72 | { |
| 73 | name: "list tasks after create", |
| 74 | cmdArgs: []string{"task", "list", "--output", "json"}, |
| 75 | assertFn: func(stdout string, userClient *codersdk.Client) { |
| 76 | var tasks []codersdk.Task |
| 77 | err := json.NewDecoder(strings.NewReader(stdout)).Decode(&tasks) |
| 78 | require.NoError(t, err, "list output should unmarshal properly") |
| 79 | require.Len(t, tasks, 1, "expected one task") |
| 80 | require.Equal(t, taskName, tasks[0].Name, "task name should match") |
| 81 | require.Equal(t, initMsg.Content, tasks[0].InitialPrompt, "initial prompt should match") |
| 82 | require.True(t, tasks[0].WorkspaceID.Valid, "workspace should be created") |
| 83 | // For the next test, we need to wait for the workspace to be healthy |
| 84 | ws := coderdtest.MustWorkspace(t, userClient, tasks[0].WorkspaceID.UUID) |
| 85 | coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, ws.LatestBuild.ID) |
| 86 | agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(authToken)) |
| 87 | _ = agenttest.New(t, client.URL, authToken, func(o *agent.Options) { |
| 88 | o.Client = agentClient |
| 89 | }) |
| 90 | coderdtest.NewWorkspaceAgentWaiter(t, userClient, tasks[0].WorkspaceID.UUID).WithContext(ctx).WaitFor(coderdtest.AgentsReady) |
| 91 | // Report the task app as idle so that waitForTaskIdle |
| 92 | // can proceed during the "send task message" step. |
| 93 | require.NoError(t, agentClient.PatchAppStatus(ctx, agentsdk.PatchAppStatus{ |
| 94 | AppSlug: "task-sidebar", |
| 95 | State: codersdk.WorkspaceAppStatusStateIdle, |
| 96 | Message: "ready", |
nothing calls this directly
no test coverage detected