package equipotest import ( "context" "errors" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" ) func TestBugWithoutPairWaits(t *testing.T) { for _, setup := range []bool{false, true} { t.Run(map[bool]string{false: "family", true: "setup"}[setup], func(t *testing.T) { f := bugFixture(t) if setup { executionMust(t, setDirectAvailable(f.s, f.principal, f.reports, false)) } else { mustSQL(t, f.d, "DELETE FROM connections WHERE id=?", f.ids["codex"]) } task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "waiting bug", "wait") executionMust(t, err) want := "task.wait.family" if setup { want = "task.wait.setup" } if task.WaitCode != want || task.JobID != 0 || task.State != "waiting" { t.Fatal("missing waiting task", task) } view, err := f.s.TaskView(connectionContext, ownerActor, task.ID) executionMust(t, err) if view.StateKey != want { t.Fatal("waiting reason absent from page data", view.StateKey) } if scalar(t, f.d, "SELECT count(*) FROM runner_assignments") != 0 { t.Fatal("waiting task dispatched") } }) } } func TestSyncSkipsStalePreparedTask(t *testing.T) { for _, change := range []string{"specialist", "budget", "project", "requester"} { t.Run(change, func(t *testing.T) { f := runnerFixtureNew(t, false) task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "stale bug", "stale") executionMust(t, err) task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f.connectionFixture)) executionMust(t, err) switch change { case "specialist": mustSQL(t, f.d, "UPDATE specialists SET revision=revision+1 WHERE key='engineering'") case "budget": mustSQL(t, f.d, "UPDATE task_outboxes SET budget_revision=budget_revision-1 WHERE task_id=?", task.ID) case "project": mustSQL(t, f.d, "UPDATE task_outboxes SET project_revision=project_revision-1 WHERE task_id=?", task.ID) case "requester": mustSQL(t, f.d, "UPDATE tasks SET requester='removed@example.com' WHERE id=?", task.ID) } executionMust(t, f.x.Sync(connectionContext)) var wait string executionMust(t, f.d.Writer().QueryRow("SELECT wait_code FROM tasks WHERE id=?", task.ID).Scan(&wait)) if wait != "task.wait.setup" { t.Fatal("stale task has no setup wait", wait) } }) } } func TestPollReplacesStaleUnclaimedAssignment(t *testing.T) { f := runnerFixtureNew(t, false) mustSQL(t, f.d, "UPDATE specialists SET revision=revision+1 WHERE key='engineering'") ctx, cancel := context.WithTimeout(connectionContext, 250*time.Millisecond) defer cancel() assignments, err := f.x.Poll(ctx, f.principal, "") if err != nil && !errors.Is(err, context.DeadlineExceeded) { t.Fatal("one stale task failed poll", err) } if len(assignments) != 0 { t.Fatal("stale assignment dispatched") } var wait string executionMust(t, f.d.Writer().QueryRow("SELECT wait_code FROM tasks WHERE id=?", f.a.TaskID).Scan(&wait)) if wait != "task.wait.setup" { t.Fatal("stale assignment has no wait", wait) } executionMust(t, f.x.Sync(connectionContext)) retryCtx, retryCancel := context.WithTimeout(connectionContext, 250*time.Millisecond) defer retryCancel() aa, err := f.x.Poll(retryCtx, f.principal, "") if err != nil || len(aa) != 1 || aa[0].ID == f.a.ID || aa[0].Revision <= f.a.Revision || aa[0].Selection.SpecialistRevision <= f.a.Selection.SpecialistRevision { t.Fatal("unclaimed assignment was not replaced with a current snapshot", aa, err) } if scalar(t, f.d, "SELECT count(*) FROM runner_assignments WHERE id=? AND state='fenced' AND claim_request=''", f.a.ID) != 1 { t.Fatal("stale assignment retained authority") } } func TestPreparationTaintDoesNotInvalidate(t *testing.T) { f := bugFixture(t) task := bugTask(t, f) mustSQL(t, f.d, `UPDATE task_outboxes SET engineering=json_set(engineering,'$.Isolation.runner_tainted',json('false'),'$.Isolation.acknowledgement_ids',json('[]')) WHERE task_id=?`, task.ID) _, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) executionMust(t, err) } func TestExpiredPreparationDoesNotBlockClaim(t *testing.T) { for _, state := range []string{"claimed", "fenced"} { t.Run(state, func(t *testing.T) { f := runnerFixtureNew(t, false) mustSQL(t, f.d, `INSERT INTO runner_assignments(protocol_version,id,task_id,step_key,workspace_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence,claim_request,lease_deadline,brief) SELECT 2,'old-prepare',task_id,'prepare','old',team_id,runner_id,person,selection,isolation,'prepare',?,1,0,0,1,'old',?,'{}' FROM runner_assignments WHERE id=?`, state, f.clock.Now().Add(-time.Minute), f.a.ID) _, err := f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "claim"}) executionMust(t, err) }) } } func TestPreparationCapacityIsPerSpecialist(t *testing.T) { f := runnerFixtureNew(t, false) mustSQL(t, f.d, `UPDATE steps SET depends_on='[]' WHERE key='review'`) mustSQL(t, f.d, `UPDATE runner_assignments SET step_key='review',selection=(SELECT review FROM task_outboxes WHERE task_id=runner_assignments.task_id),brief=json_set(brief,'$.step.StepKey','review','$.step.Action','review','$.step.SpecialistKey','review') WHERE id=?`, f.a.ID) mustSQL(t, f.d, `INSERT INTO runner_assignments(protocol_version,id,task_id,step_key,workspace_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence,claim_request,lease_deadline,brief) SELECT 2,'other-prepare',task_id,'prepare','other',team_id,runner_id,person,selection,isolation,'prepare','claimed',1,0,0,1,'other',?,'{}' FROM runner_assignments WHERE id=?`, f.clock.Now().Add(time.Minute), f.a.ID) _, err := f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "review"}) executionMust(t, err) } func TestWaitingBugResumesAfterSetup(t *testing.T) { f := bugFixture(t) f.reports[1].Available = false executionMust(t, f.s.ReportConnections(connectionContext, f.principal, f.reports)) task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "waiting", "waiting") executionMust(t, err) x := attachBugExecution(t, f) executionMust(t, x.Sync(connectionContext)) if scalar(t, f.d, "SELECT count(*) FROM runner_assignments") != 0 { t.Fatal("waiting task dispatched") } f.reports[1].Available = true executionMust(t, f.s.ReportConnections(connectionContext, f.principal, f.reports)) executionMust(t, x.Sync(connectionContext)) ctx, cancel := context.WithTimeout(connectionContext, time.Second) defer cancel() aa, err := x.Poll(ctx, f.principal, "") executionMust(t, err) if len(aa) != 1 || aa[0].TaskID != task.ID || aa[0].Kind != "prepare" { t.Fatal("waiting task did not resume", aa) } }