2025-08-26 15:24:42 +01:00
package cli_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
2025-08-26 15:57:44 +01:00
"time"
2025-08-26 15:24:42 +01:00
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/serpent"
)
func TestTaskCreate ( t * testing . T ) {
t . Parallel ()
var (
2025-08-26 15:57:44 +01:00
taskCreatedAt = time . Now ()
2025-08-26 15:24:42 +01:00
organizationID = uuid . New ()
2025-08-26 19:42:02 +01:00
anotherOrganizationID = uuid . New ()
2025-08-26 15:24:42 +01:00
templateID = uuid . New ()
templateVersionID = uuid . New ()
templateVersionPresetID = uuid . New ()
2025-09-04 10:03:22 +01:00
taskID = uuid . New ()
2025-08-26 15:24:42 +01:00
)
2025-09-29 15:42:03 +01:00
templateAndVersionFoundHandler := func ( t * testing . T , ctx context . Context , orgID uuid . UUID , templateName , templateVersionName , presetName , prompt , taskName , username string ) http . HandlerFunc {
2025-08-26 15:24:42 +01:00
t . Helper ()
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
2025-08-26 19:42:02 +01:00
ID : orgID ,
2025-08-26 15:24:42 +01:00
}},
})
2025-09-04 10:03:22 +01:00
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/%s/versions/%s" , orgID , templateName , templateVersionName ):
2025-08-26 15:24:42 +01:00
httpapi . Write ( ctx , w , http . StatusOK , codersdk . TemplateVersion {
ID : templateVersionID ,
})
2025-09-04 10:03:22 +01:00
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/%s" , orgID , templateName ):
2025-08-26 15:24:42 +01:00
httpapi . Write ( ctx , w , http . StatusOK , codersdk . Template {
ID : templateID ,
ActiveVersionID : templateVersionID ,
})
case fmt . Sprintf ( "/api/v2/templateversions/%s/presets" , templateVersionID ):
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Preset {
{
ID : templateVersionPresetID ,
Name : presetName ,
},
})
2025-09-02 09:07:25 +01:00
case "/api/v2/templates" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Template {
{
ID : templateID ,
Name : templateName ,
ActiveVersionID : templateVersionID ,
},
})
2025-11-25 15:24:25 +00:00
case fmt . Sprintf ( "/api/v2/tasks/%s" , username ):
2025-08-26 15:24:42 +01:00
var req codersdk . CreateTaskRequest
if ! httpapi . Read ( ctx , w , r , & req ) {
return
}
2025-09-26 12:18:08 +01:00
assert . Equal ( t , prompt , req . Input , "prompt mismatch" )
2025-08-26 15:24:42 +01:00
assert . Equal ( t , templateVersionID , req . TemplateVersionID , "template version mismatch" )
if presetName == "" {
assert . Equal ( t , uuid . Nil , req . TemplateVersionPresetID , "expected no template preset id" )
} else {
assert . Equal ( t , templateVersionPresetID , req . TemplateVersionPresetID , "template version preset id mismatch" )
}
2025-09-24 14:05:59 +01:00
created := codersdk . Task {
2025-09-04 10:03:22 +01:00
ID : taskID ,
2025-09-24 14:05:59 +01:00
Name : taskName ,
2025-08-26 15:57:44 +01:00
CreatedAt : taskCreatedAt ,
2025-09-24 14:05:59 +01:00
}
if req . Name != "" {
assert . Equal ( t , req . Name , taskName , "name mismatch" )
created . Name = req . Name
}
httpapi . Write ( ctx , w , http . StatusCreated , created )
2025-08-26 15:24:42 +01:00
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
}
tests := [] struct {
args [] string
env [] string
2025-09-02 09:07:25 +01:00
stdin string
2025-08-26 15:24:42 +01:00
expectError string
expectOutput string
handler func ( t * testing . T , ctx context . Context ) http . HandlerFunc
}{
{
2025-09-02 09:07:25 +01:00
args : [] string { "--stdin" },
stdin : "reads prompt from stdin" ,
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "reads prompt from stdin" , "task-wild-goldfish-27" , codersdk . Me )
2025-09-02 09:07:25 +01:00
},
},
{
args : [] string { "my custom prompt" },
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
},
},
{
args : [] string { "my custom prompt" , "--owner" , "someone-else" },
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , "someone-else" )
2025-09-24 14:05:59 +01:00
},
},
{
args : [] string { "--name" , "abc123" , "my custom prompt" },
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "abc123" ), cliui . Timestamp ( taskCreatedAt )),
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "abc123" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" , "--template-version" , "my-template-version" , "--org" , organizationID . String ()},
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-09-02 09:07:25 +01:00
},
},
{
args : [] string { "my custom prompt" , "--template" , "my-template" , "--org" , organizationID . String ()},
2025-08-26 15:24:42 +01:00
env : [] string { "CODER_TASK_TEMPLATE_VERSION=my-template-version" },
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--org" , organizationID . String ()},
2025-08-26 15:24:42 +01:00
env : [] string { "CODER_TASK_TEMPLATE_NAME=my-template" , "CODER_TASK_TEMPLATE_VERSION=my-template-version" },
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" , "--org" , organizationID . String ()},
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" , "--preset" , "my-preset" , "--org" , organizationID . String ()},
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" },
2025-08-26 15:24:42 +01:00
env : [] string { "CODER_TASK_PRESET_NAME=my-preset" },
2025-08-26 15:57:44 +01:00
expectOutput : fmt . Sprintf ( "The task %s has been created at %s!" , cliui . Keyword ( "task-wild-goldfish-27" ), cliui . Timestamp ( taskCreatedAt )),
2025-08-26 15:24:42 +01:00
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
2025-09-04 10:03:22 +01:00
{
args : [] string { "my custom prompt" , "-q" },
expectOutput : taskID . String (),
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-09-04 10:03:22 +01:00
},
},
2025-08-26 15:24:42 +01:00
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" , "--preset" , "not-real-preset" },
2025-08-26 15:24:42 +01:00
expectError : `preset "not-real-preset" not found` ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
2025-09-29 15:42:03 +01:00
return templateAndVersionFoundHandler ( t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" , "task-wild-goldfish-27" , codersdk . Me )
2025-08-26 15:24:42 +01:00
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "my-template" , "--template-version" , "not-real-template-version" },
2025-08-26 15:24:42 +01:00
expectError : httpapi . ResourceNotFoundResponse . Message ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
ID : organizationID ,
}},
})
2025-09-02 09:07:25 +01:00
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/my-template" , organizationID ):
httpapi . Write ( ctx , w , http . StatusOK , codersdk . Template {
ID : templateID ,
ActiveVersionID : templateVersionID ,
})
2025-08-26 15:24:42 +01:00
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/my-template/versions/not-real-template-version" , organizationID ):
httpapi . ResourceNotFound ( w )
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "my custom prompt" , "--template" , "not-real-template" , "--org" , organizationID . String ()},
2025-08-26 15:24:42 +01:00
expectError : httpapi . ResourceNotFoundResponse . Message ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
ID : organizationID ,
}},
})
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/not-real-template" , organizationID ):
httpapi . ResourceNotFound ( w )
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
2025-08-26 19:42:02 +01:00
{
2025-09-02 09:07:25 +01:00
args : [] string { "my-custom-prompt" , "--template" , "template-in-different-org" , "--org" , anotherOrganizationID . String ()},
2025-08-26 19:42:02 +01:00
expectError : httpapi . ResourceNotFoundResponse . Message ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
ID : anotherOrganizationID ,
}},
})
case fmt . Sprintf ( "/api/v2/organizations/%s/templates/template-in-different-org" , anotherOrganizationID ):
httpapi . ResourceNotFound ( w )
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
{
2025-09-02 09:07:25 +01:00
args : [] string { "no-org-prompt" },
2025-08-26 19:42:02 +01:00
expectError : "Must select an organization with --org=<org_name>" ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {})
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
2025-09-02 09:07:25 +01:00
{
args : [] string { "no task templates" },
expectError : "no task templates configured" ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
ID : organizationID ,
}},
})
case "/api/v2/templates" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Template {})
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
{
args : [] string { "no template name provided" },
expectError : "template name not provided, available templates: wibble, wobble" ,
handler : func ( t * testing . T , ctx context . Context ) http . HandlerFunc {
return func ( w http . ResponseWriter , r * http . Request ) {
switch r . URL . Path {
case "/api/v2/users/me/organizations" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Organization {
{ MinimalOrganization : codersdk . MinimalOrganization {
ID : organizationID ,
}},
})
case "/api/v2/templates" :
httpapi . Write ( ctx , w , http . StatusOK , [] codersdk . Template {
{ Name : "wibble" },
{ Name : "wobble" },
})
default :
t . Errorf ( "unexpected path: %s" , r . URL . Path )
}
}
},
},
2025-08-26 15:24:42 +01:00
}
for _ , tt := range tests {
t . Run ( strings . Join ( tt . args , "," ), func ( t * testing . T ) {
t . Parallel ()
var (
ctx = testutil . Context ( t , testutil . WaitShort )
srv = httptest . NewServer ( tt . handler ( t , ctx ))
2025-08-29 10:41:32 +02:00
client = codersdk . New ( testutil . MustURL ( t , srv . URL ))
2025-11-25 15:50:22 +02:00
args = [] string { "task" , "create" }
2025-08-26 15:24:42 +01:00
sb strings . Builder
err error
)
t . Cleanup ( srv . Close )
inv , root := clitest . New ( t , append ( args , tt . args ... ) ... )
inv . Environ = serpent . ParseEnviron ( tt . env , "" )
2025-09-02 09:07:25 +01:00
inv . Stdin = strings . NewReader ( tt . stdin )
2025-08-26 15:24:42 +01:00
inv . Stdout = & sb
inv . Stderr = & sb
clitest . SetupConfig ( t , client , root )
err = inv . WithContext ( ctx ). Run ()
if tt . expectError == "" {
assert . NoError ( t , err )
} else {
assert . ErrorContains ( t , err , tt . expectError )
}
assert . Contains ( t , sb . String (), tt . expectOutput )
})
}
}