@@ -4508,17 +4508,30 @@ func TestWorkspaceFilterHasAITask(t *testing.T) {
4508
4508
ctx := testutil .Context (t , testutil .WaitLong )
4509
4509
4510
4510
// Helper function to create workspace with AI task configuration
4511
- createWorkspaceWithAIConfig := func (hasAITask sql.NullBool , jobCompleted bool , aiTaskPrompt * string ) database.WorkspaceTable {
4511
+ createWorkspaceWithAIConfig := func (hasAITask sql.NullBool , jobCompleted bool , aiTaskPrompt * string , conf * database. WorkspaceTable ) database.WorkspaceTable {
4512
4512
// When a provisioner job uses these tags, no provisioner will match it.
4513
4513
// We do this so jobs will always be stuck in "pending", allowing us to exercise the intermediary state when
4514
4514
// has_ai_task is nil and we compensate by looking at pending provisioning jobs.
4515
4515
// See GetWorkspaces clauses.
4516
4516
unpickableTags := database.StringMap {"custom" : "true" }
4517
4517
4518
4518
ws := dbgen .Workspace (t , db , database.WorkspaceTable {
4519
- OwnerID : user .UserID ,
4520
- OrganizationID : user .OrganizationID ,
4521
- TemplateID : template .ID ,
4519
+ OwnerID : user .UserID ,
4520
+ OrganizationID : user .OrganizationID ,
4521
+ TemplateID : template .ID ,
4522
+ ID : conf .ID ,
4523
+ CreatedAt : conf .CreatedAt ,
4524
+ UpdatedAt : conf .UpdatedAt ,
4525
+ Deleted : conf .Deleted ,
4526
+ Name : conf .Name ,
4527
+ AutostartSchedule : conf .AutostartSchedule ,
4528
+ Ttl : conf .Ttl ,
4529
+ LastUsedAt : conf .LastUsedAt ,
4530
+ DormantAt : conf .DormantAt ,
4531
+ DeletingAt : conf .DeletingAt ,
4532
+ AutomaticUpdates : conf .AutomaticUpdates ,
4533
+ Favorite : conf .Favorite ,
4534
+ NextStartAt : conf .NextStartAt ,
4522
4535
})
4523
4536
4524
4537
jobConfig := database.ProvisionerJob {
@@ -4563,18 +4576,49 @@ func TestWorkspaceFilterHasAITask(t *testing.T) {
4563
4576
return ws
4564
4577
}
4565
4578
4579
+ expectWorkspaces := func (workspaces []codersdk.Workspace , order []uuid.UUID ) {
4580
+ ids := make ([]uuid.UUID , len (workspaces ))
4581
+ for i , ws := range workspaces {
4582
+ t .Logf ("Workspace %d: ID=%s, Name=%s, Status=%s" , i , ws .ID , ws .Name , ws .LatestBuild .Status )
4583
+ ids [i ] = ws .ID
4584
+ }
4585
+ t .Logf ("Expected IDs: %s" , order )
4586
+ require .Len (t , workspaces , len (order ))
4587
+ require .Equal (t , order , ids )
4588
+ }
4589
+
4566
4590
// Create test workspaces with different AI task configurations
4567
- wsWithAITask := createWorkspaceWithAIConfig (sql.NullBool {Bool : true , Valid : true }, true , nil )
4568
- wsWithoutAITask := createWorkspaceWithAIConfig (sql.NullBool {Bool : false , Valid : true }, false , nil )
4591
+ wsWithAITask := createWorkspaceWithAIConfig (sql.NullBool {Bool : true , Valid : true }, true , nil , & database.WorkspaceTable {
4592
+ Name : "alpha" ,
4593
+ CreatedAt : time .Now ().Add (- 30 * time .Minute ),
4594
+ })
4595
+ wsWithoutAITask := createWorkspaceWithAIConfig (sql.NullBool {Bool : false , Valid : true }, false , nil , & database.WorkspaceTable {
4596
+ Name : "beta" ,
4597
+ CreatedAt : time .Now ().Add (- 20 * time .Minute ),
4598
+ })
4569
4599
4570
4600
aiTaskPrompt := "Build me a web app"
4571
- wsWithAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, false , & aiTaskPrompt )
4601
+ wsWithAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, false , & aiTaskPrompt , & database.WorkspaceTable {
4602
+ Name : "gamma" ,
4603
+ CreatedAt : time .Now ().Add (- 10 * time .Minute ),
4604
+ })
4572
4605
4573
4606
anotherTaskPrompt := "Another task"
4574
- wsCompletedWithAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, true , & anotherTaskPrompt )
4607
+ wsCompletedWithAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, true , & anotherTaskPrompt , & database.WorkspaceTable {
4608
+ Name : "delta" ,
4609
+ CreatedAt : time .Now ().Add (- 5 * time .Minute ),
4610
+ })
4575
4611
4576
4612
emptyPrompt := ""
4577
- wsWithEmptyAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, false , & emptyPrompt )
4613
+ wsWithEmptyAITaskParam := createWorkspaceWithAIConfig (sql.NullBool {Valid : false }, false , & emptyPrompt , & database.WorkspaceTable {
4614
+ Name : "epsilon" ,
4615
+ CreatedAt : time .Now (),
4616
+ })
4617
+
4618
+ // Expected orders.
4619
+ orderByWithAITask := []uuid.UUID {wsWithAITaskParam .ID , wsWithAITask .ID }
4620
+ orderByWithoutAITask := []uuid.UUID {wsCompletedWithAITaskParam .ID , wsWithoutAITask .ID , wsWithEmptyAITaskParam .ID }
4621
+ orderByAll := []uuid.UUID {wsWithAITask .ID , wsCompletedWithAITaskParam .ID , wsWithoutAITask .ID , wsWithEmptyAITaskParam .ID , wsWithAITaskParam .ID }
4578
4622
4579
4623
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
4580
4624
defer cancel ()
@@ -4594,14 +4638,8 @@ func TestWorkspaceFilterHasAITask(t *testing.T) {
4594
4638
})
4595
4639
require .NoError (t , err )
4596
4640
t .Logf ("Expected 2 workspaces for has-ai-task:true, got %d" , len (res .Workspaces ))
4597
- t .Logf ("Expected workspaces: %s, %s" , wsWithAITask .ID , wsWithAITaskParam .ID )
4598
- for i , ws := range res .Workspaces {
4599
- t .Logf ("AI Task True Workspace %d: ID=%s, Name=%s" , i , ws .ID , ws .Name )
4600
- }
4601
- require .Len (t , res .Workspaces , 2 )
4602
- workspaceIDs := []uuid.UUID {res .Workspaces [0 ].ID , res .Workspaces [1 ].ID }
4603
- require .Contains (t , workspaceIDs , wsWithAITask .ID )
4604
- require .Contains (t , workspaceIDs , wsWithAITaskParam .ID )
4641
+ // Should be ordered by starting/running then created date.
4642
+ expectWorkspaces (res .Workspaces , orderByWithAITask )
4605
4643
4606
4644
// Test filtering for workspaces without AI tasks
4607
4645
// Should include: wsWithoutAITask, wsCompletedWithAITaskParam, wsWithEmptyAITaskParam
@@ -4612,21 +4650,15 @@ func TestWorkspaceFilterHasAITask(t *testing.T) {
4612
4650
4613
4651
// Debug: print what we got
4614
4652
t .Logf ("Expected 3 workspaces for has-ai-task:false, got %d" , len (res .Workspaces ))
4615
- for i , ws := range res .Workspaces {
4616
- t .Logf ("Workspace %d: ID=%s, Name=%s" , i , ws .ID , ws .Name )
4617
- }
4618
- t .Logf ("Expected IDs: %s, %s, %s" , wsWithoutAITask .ID , wsCompletedWithAITaskParam .ID , wsWithEmptyAITaskParam .ID )
4619
-
4620
- require .Len (t , res .Workspaces , 3 )
4621
- workspaceIDs = []uuid.UUID {res .Workspaces [0 ].ID , res .Workspaces [1 ].ID , res .Workspaces [2 ].ID }
4622
- require .Contains (t , workspaceIDs , wsWithoutAITask .ID )
4623
- require .Contains (t , workspaceIDs , wsCompletedWithAITaskParam .ID )
4624
- require .Contains (t , workspaceIDs , wsWithEmptyAITaskParam .ID )
4653
+ // Should be ordered by running then name.
4654
+ expectWorkspaces (res .Workspaces , orderByWithoutAITask )
4625
4655
4626
4656
// Test no filter returns all
4627
4657
res , err = client .Workspaces (ctx , codersdk.WorkspaceFilter {})
4658
+ t .Logf ("Expected 5 workspaces without filter, got %d" , len (res .Workspaces ))
4628
4659
require .NoError (t , err )
4629
- require .Len (t , res .Workspaces , 5 )
4660
+ // Should be ordered by running then name.
4661
+ expectWorkspaces (res .Workspaces , orderByAll )
4630
4662
}
4631
4663
4632
4664
func TestWorkspaceAppUpsertRestart (t * testing.T ) {
0 commit comments