package plan3test import ( "crypto/sha256" "encoding/hex" "errors" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestPlanApprovalPrecedenceDispatch(t *testing.T) { f := newFixture(t) p := proposed(t, f) f.planConnections(t, p.ProjectID) must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID)) must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require"})) if err := f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) { t.Fatal("switch to required did not stop dispatch", err) } m := equipo.Mutation{RequestID: "approve", Revision: 1} approved, err := f.Store.ApprovePlan(ctx, owner, p.TaskID, m) must(t, err) if approved.ApprovalRevision != 1 { t.Fatal("approval is not bound to plan") } must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID)) p.Steps[1].Instructions = "second revision" p, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "edit-approved", Revision: 1}) must(t, err) if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) { t.Fatal("edited plan retained approval", err) } replay, err := f.Store.ApprovePlan(ctx, owner, p.TaskID, m) must(t, err) if replay.ApprovalRevision != 1 { t.Fatal("approval retry lost saved result") } if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) { t.Fatal("old approval replay authorized new revision", err) } _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-two", Revision: p.Revision}) must(t, err) must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID)) f.exec(t, "UPDATE specialists SET model_selection='fixed',provider='unavailable' WHERE key='engineering'") if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); err == nil { t.Fatal("dispatch ignored current model constraints") } if _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-model", Revision: p.Revision}); err == nil { t.Fatal("approval ignored current model constraints") } f.exec(t, "UPDATE specialists SET model_selection='best_fit' WHERE key='engineering'") f.exec(t, "DELETE FROM budget_records") if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); err == nil { t.Fatal("dispatch ignored missing budget") } if _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-budget", Revision: p.Revision}); err == nil { t.Fatal("approval ignored missing budget") } } func TestPlanApprovalOverrideAuthority(t *testing.T) { f := newFixture(t) p := proposed(t, f) f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) if err := f.Store.SetProjectApproval(ctx, peer, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "not-owner"}); !errors.Is(err, equipo.ErrForbidden) { t.Fatal("non-owner changed project approval", err) } mode := equipo.ApprovalRequired must(t, f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, &mode, equipo.Mutation{RequestID: "own-override"})) must(t, f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, nil, equipo.Mutation{RequestID: "own-inherit", Revision: 1})) f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address) if err := f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, nil, equipo.Mutation{RequestID: "own-inherit", Revision: 1}); !errors.Is(err, equipo.ErrForbidden) { t.Fatal("removed member replay allowed", err) } } func TestPlanApprovalPrecedenceClaim(t *testing.T) { f := newFixture(t) p := proposed(t, f) f.planConnections(t, p.ProjectID) var path string must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) must(t, err) t.Cleanup(func() { must(t, x.Core.Close()) }) _, err = x.Core.Resume(ctx) must(t, err) f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC()) must(t, x.Sync(ctx)) var id string var revision int64 must(t, f.DB.Writer().QueryRow("SELECT id,revision FROM runner_assignments WHERE task_id=? AND state='pending'", p.TaskID).Scan(&id, &revision)) must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-before-claim"})) _, err = x.Claim(ctx, equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"}, equipo.ClaimRequest{Version: 2, AssignmentID: id, RequestID: "claim", Revision: revision}) if !errors.Is(err, equipo.ErrConflict) { t.Fatal("claim ignored current approval mode", err) } if f.count(t, "SELECT count(*) FROM runner_cleanups") != 0 || f.count(t, "SELECT count(*) FROM attempts") != 0 || f.count(t, "SELECT count(*) FROM runner_assignments WHERE id=? AND state='pending'", id) != 1 { t.Fatal("denied claim partially committed") } } func TestPlanApprovalPrecedenceStart(t *testing.T) { f := newFixture(t) p := proposed(t, f) f.planConnections(t, p.ProjectID) var path string must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) must(t, err) t.Cleanup(func() { must(t, x.Core.Close()) }) _, err = x.Core.Resume(ctx) must(t, err) f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC()) principal := equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"} must(t, x.Sync(ctx)) var project equipo.Project must(t, f.DB.G.First(&project, p.ProjectID).Error) definition := []byte("fixture check") hash := sha256.Sum256(definition) prepared := equipo.PreparedBug{Repository: project.Repository, DefaultBranch: project.DefaultBranch, BaseCommit: strings.Repeat("a", 40), CheckDefinition: definition, CheckDefinitionHash: hex.EncodeToString(hash[:]), BaselineTestSetHash: strings.Repeat("b", 64)} must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-prepare"})) if _, err = f.Store.PrepareBug(ctx, principal, p.TaskID, 1, prepared); !errors.Is(err, equipo.ErrConflict) { t.Fatal("prepare ignored current approval", err) } must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalImmediate, equipo.Mutation{RequestID: "immediate-prepare", Revision: 1})) task, err := f.Store.PrepareBug(ctx, principal, p.TaskID, 1, prepared) must(t, err) must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-start", Revision: 2})) if _, err = f.Store.StartBug(ctx, principal, p.TaskID, task.Revision); !errors.Is(err, equipo.ErrConflict) { t.Fatal("start ignored current approval", err) } if f.count(t, "SELECT count(*) FROM jobs") != 0 { t.Fatal("denied start left core job") } task, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-start", Revision: 1}) must(t, err) task, err = f.Store.StartBug(ctx, principal, p.TaskID, task.Revision) must(t, err) if task.JobID == 0 { t.Fatal("approved plan not dispatched") } }