|
| 1 | +package usecase |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + _ "github.com/mattn/go-sqlite3" |
| 12 | + |
| 13 | + "github.com/chaitin/MonkeyCode/backend/consts" |
| 14 | + "github.com/chaitin/MonkeyCode/backend/db" |
| 15 | + "github.com/chaitin/MonkeyCode/backend/db/enttest" |
| 16 | + "github.com/chaitin/MonkeyCode/backend/domain" |
| 17 | + "github.com/chaitin/MonkeyCode/backend/pkg/taskflow" |
| 18 | +) |
| 19 | + |
| 20 | +func TestHostUsecase_markRecycledTasksFinished(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + client := enttest.Open(t, "sqlite3", "file:host-usecase-task-finish-test?mode=memory&cache=shared&_fk=1") |
| 25 | + defer client.Close() |
| 26 | + |
| 27 | + userID := uuid.New() |
| 28 | + if _, err := client.User.Create(). |
| 29 | + SetID(userID). |
| 30 | + SetName("tester"). |
| 31 | + SetRole(consts.UserRoleIndividual). |
| 32 | + SetStatus(consts.UserStatusActive). |
| 33 | + Save(ctx); err != nil { |
| 34 | + t.Fatalf("create user: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + createTask := func(status consts.TaskStatus) *db.Task { |
| 38 | + taskID := uuid.New() |
| 39 | + tk, err := client.Task.Create(). |
| 40 | + SetID(taskID). |
| 41 | + SetUserID(userID). |
| 42 | + SetKind(consts.TaskTypeDevelop). |
| 43 | + SetContent(string(status)). |
| 44 | + SetStatus(status). |
| 45 | + Save(ctx) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("create task(%s): %v", status, err) |
| 48 | + } |
| 49 | + return tk |
| 50 | + } |
| 51 | + |
| 52 | + processingTask := createTask(consts.TaskStatusProcessing) |
| 53 | + finishedTask := createTask(consts.TaskStatusFinished) |
| 54 | + errorTask := createTask(consts.TaskStatusError) |
| 55 | + |
| 56 | + taskRepo := &hostTaskRepoStub{client: client} |
| 57 | + u := &HostUsecase{ |
| 58 | + taskRepo: taskRepo, |
| 59 | + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| 60 | + } |
| 61 | + |
| 62 | + vm := &db.VirtualMachine{ |
| 63 | + ID: "vm-1", |
| 64 | + Edges: db.VirtualMachineEdges{ |
| 65 | + Tasks: []*db.Task{ |
| 66 | + processingTask, |
| 67 | + finishedTask, |
| 68 | + errorTask, |
| 69 | + nil, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + if err := u.markRecycledTasksFinished(ctx, vm); err != nil { |
| 75 | + t.Fatalf("markRecycledTasksFinished() error = %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + gotProcessing, err := client.Task.Get(ctx, processingTask.ID) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("query processing task: %v", err) |
| 81 | + } |
| 82 | + if gotProcessing.Status != consts.TaskStatusFinished { |
| 83 | + t.Fatalf("processing task status = %s, want %s", gotProcessing.Status, consts.TaskStatusFinished) |
| 84 | + } |
| 85 | + if gotProcessing.CompletedAt.IsZero() { |
| 86 | + t.Fatal("expected processing task completed_at to be set") |
| 87 | + } |
| 88 | + |
| 89 | + gotFinished, err := client.Task.Get(ctx, finishedTask.ID) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("query finished task: %v", err) |
| 92 | + } |
| 93 | + if !gotFinished.CompletedAt.IsZero() { |
| 94 | + t.Fatal("expected already finished task completed_at to remain unchanged") |
| 95 | + } |
| 96 | + |
| 97 | + gotError, err := client.Task.Get(ctx, errorTask.ID) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("query error task: %v", err) |
| 100 | + } |
| 101 | + if gotError.Status != consts.TaskStatusError { |
| 102 | + t.Fatalf("error task status = %s, want %s", gotError.Status, consts.TaskStatusError) |
| 103 | + } |
| 104 | + |
| 105 | + if len(taskRepo.updatedIDs) != 1 || taskRepo.updatedIDs[0] != processingTask.ID { |
| 106 | + t.Fatalf("updated task ids = %v, want only %s", taskRepo.updatedIDs, processingTask.ID) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +type hostTaskRepoStub struct { |
| 111 | + client *db.Client |
| 112 | + updatedIDs []uuid.UUID |
| 113 | +} |
| 114 | + |
| 115 | +func (s *hostTaskRepoStub) GetByID(ctx context.Context, id uuid.UUID) (*db.Task, error) { |
| 116 | + return s.client.Task.Get(ctx, id) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *hostTaskRepoStub) Stat(context.Context, uuid.UUID) (*domain.TaskStats, error) { |
| 120 | + panic("unexpected call to Stat") |
| 121 | +} |
| 122 | + |
| 123 | +func (s *hostTaskRepoStub) StatByIDs(context.Context, []uuid.UUID) (map[uuid.UUID]*domain.TaskStats, error) { |
| 124 | + panic("unexpected call to StatByIDs") |
| 125 | +} |
| 126 | + |
| 127 | +func (s *hostTaskRepoStub) Info(context.Context, *domain.User, uuid.UUID, bool) (*db.Task, error) { |
| 128 | + panic("unexpected call to Info") |
| 129 | +} |
| 130 | + |
| 131 | +func (s *hostTaskRepoStub) List(context.Context, *domain.User, domain.TaskListReq) ([]*db.ProjectTask, *db.PageInfo, error) { |
| 132 | + panic("unexpected call to List") |
| 133 | +} |
| 134 | + |
| 135 | +func (s *hostTaskRepoStub) Create(context.Context, *domain.User, domain.CreateTaskReq, string, func(*db.ProjectTask, *db.Model, *db.Image) (*taskflow.VirtualMachine, error)) (*db.ProjectTask, error) { |
| 136 | + panic("unexpected call to Create") |
| 137 | +} |
| 138 | + |
| 139 | +func (s *hostTaskRepoStub) Update(ctx context.Context, _ *domain.User, id uuid.UUID, fn func(up *db.TaskUpdateOne) error) error { |
| 140 | + s.updatedIDs = append(s.updatedIDs, id) |
| 141 | + up := s.client.Task.UpdateOneID(id) |
| 142 | + if err := fn(up); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + return up.Exec(ctx) |
| 146 | +} |
| 147 | + |
| 148 | +func (s *hostTaskRepoStub) RefreshLastActiveAt(context.Context, uuid.UUID, time.Time, time.Duration) error { |
| 149 | + panic("unexpected call to RefreshLastActiveAt") |
| 150 | +} |
| 151 | + |
| 152 | +func (s *hostTaskRepoStub) Stop(context.Context, *domain.User, uuid.UUID, func(*db.Task) error) error { |
| 153 | + panic("unexpected call to Stop") |
| 154 | +} |
| 155 | + |
| 156 | +func (s *hostTaskRepoStub) Delete(context.Context, *domain.User, uuid.UUID) error { |
| 157 | + panic("unexpected call to Delete") |
| 158 | +} |
0 commit comments