| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestPersonAPIOwnerRosterBudgets(t *testing.T) { |
| 14 | f := newFixture(t) |
| 15 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 16 | for _, path := range []string{"/roster", "/budgets"} { |
| 17 | expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1"+path, "", "", ""), 403) |
| 18 | } |
| 19 | path := "/api/v1/roster/" + peer.Address |
| 20 | body := `{"role":"member","active":true}` |
| 21 | expectHTTP(t, apiRequest(t, f, peer, "PUT", path, body, "peer", `"1"`), 403) |
| 22 | expectHTTP(t, apiRequest(t, f, owner, "PUT", path, body, "roster", `"1"`), 200) |
| 23 | expectHTTP(t, apiRequest(t, f, owner, "PUT", path, body, "roster", `"1"`), 200) |
| 24 | if f.count(t, "SELECT count(*) FROM audit_events WHERE kind='roster.set' AND record_id=?", peer.Address) != 2 { |
| 25 | t.Fatal("roster replay duplicated audit") |
| 26 | } |
| 27 | w := apiRequest(t, f, owner, "PUT", path, `{"role":"owner","active":true}`, "stale", `"1"`) |
| 28 | expectHTTP(t, w, 409) |
| 29 | if !strings.Contains(w.Body.String(), `"current_revision":2`) { |
| 30 | t.Fatal("missing current revision", w.Body.String()) |
| 31 | } |
| 32 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/roster/"+owner.Address, `{"role":"member","active":true}`, "last", `"1"`), 403) |
| 33 | budget := `{"team":{"calls":10,"input_tokens":1000,"output_tokens":1000,"seconds":1000},"projects":{}}` |
| 34 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", budget, "budget", `"0"`), 200) |
| 35 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", budget, "budget", `"0"`), 200) |
| 36 | if f.count(t, "SELECT count(*) FROM audit_events WHERE kind='budget.save'") != 1 { |
| 37 | t.Fatal("budget replay duplicated audit") |
| 38 | } |
| 39 | expectHTTP(t, apiRequest(t, f, peer, "PUT", "/api/v1/budgets", budget, "deny", `"1"`), 403) |
| 40 | f.exec(t, "CREATE TRIGGER reject_api_owner BEFORE INSERT ON person_api_requests BEGIN SELECT RAISE(ABORT,'fixture'); END") |
| 41 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", budget, "rollback", `"1"`), 503) |
| 42 | b, err := f.Store.Budgets(ctx) |
| 43 | must(t, err) |
| 44 | if b.Revision != 1 { |
| 45 | t.Fatal("budget failed to roll back", b) |
| 46 | } |
| 47 | f.exec(t, "DROP TRIGGER reject_api_owner") |
| 48 | must(t, f.Store.SetMember(ctx, owner, peer.Address, equipo.RoleOwner, true, 2)) |
| 49 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", owner.Address) |
| 50 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", budget, "budget", `"0"`), 403) |
| 51 | } |
| 52 | |
| 53 | func TestPersonAPIOwnerDecisionOwners(t *testing.T) { |
| 54 | f := newFixture(t) |
| 55 | p := proposed(t, f) |
| 56 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: p.ProjectID, Decision: "copy", Address: peer.Address}).Error) |
| 57 | path := "/api/v1/roster/" + peer.Address |
| 58 | // Team role and project decision ownership are independent. Demotion retains the assignment. |
| 59 | expectHTTP(t, apiRequest(t, f, owner, "PUT", path, `{"role":"member","active":true}`, "demote", `"1"`), 200) |
| 60 | if f.count(t, "SELECT count(*) FROM decision_owners WHERE address=?", peer.Address) != 1 { |
| 61 | t.Fatal("demotion removed decision assignment") |
| 62 | } |
| 63 | w := apiRequest(t, f, owner, "PUT", path, `{"role":"member","active":false}`, "remove", `"2"`) |
| 64 | expectHTTP(t, w, 409) |
| 65 | if !strings.Contains(w.Body.String(), `"copy_key":"roster.revision_conflict"`) { |
| 66 | t.Fatal("missing roster failure copy", w.Body.String()) |
| 67 | } |
| 68 | m, err := f.Store.Member(ctx, peer.Address) |
| 69 | must(t, err) |
| 70 | if !m.Active || m.Revision != 2 { |
| 71 | t.Fatal("failed removal changed decision owner", m) |
| 72 | } |
| 73 | } |
| 74 | func TestPersonAPIOwnerBudgetNextAdmission(t *testing.T) { |
| 75 | f := newFixture(t) |
| 76 | p := proposed(t, f) |
| 77 | x, _, scope := f.planCore(t, p) |
| 78 | job, err := x.Core.GetJob(ctx, scope) |
| 79 | must(t, err) |
| 80 | _, err = x.Core.Claim(ctx, scope, "reproduce", "runner", "workspace") |
| 81 | must(t, err) |
| 82 | before, err := x.Core.PeriodUsage(ctx, scope.Account, 0, time.Now()) |
| 83 | must(t, err) |
| 84 | w := apiRequest(t, f, owner, "GET", "/api/v1/budgets", "", "", "") |
| 85 | expectHTTP(t, w, 200) |
| 86 | body := fmt.Sprintf(`{"team":{"calls":1,"input_tokens":1,"output_tokens":1,"seconds":1},"projects":{"%d":{"calls":1,"input_tokens":1,"output_tokens":1,"seconds":1}}}`, p.ProjectID) |
| 87 | expectHTTP(t, apiRequest(t, f, owner, "PUT", "/api/v1/budgets", body, "lower", w.Header().Get("ETag")), 200) |
| 88 | after, err := x.Core.PeriodUsage(ctx, scope.Account, 0, time.Now()) |
| 89 | must(t, err) |
| 90 | if before != after { |
| 91 | t.Fatal("lowering budget changed usage") |
| 92 | } |
| 93 | other, err := x.Core.CreateJob(ctx, team.JobSpec{Account: job.Account, Project: job.Project, Requester: job.Requester, Target: job.Target, Evidence: job.Evidence, Policy: job.Policy, GrantRef: job.Grant.Ref, Steps: team.SoftwareSteps(false)}) |
| 94 | must(t, err) |
| 95 | _, err = x.Core.Claim(ctx, team.Scope{Account: scope.Account, Job: other.ID}, "reproduce", "runner", "workspace") |
| 96 | if !errors.Is(err, team.ErrBudget) { |
| 97 | t.Fatal("API-lowered budget allowed next admission", err) |
| 98 | } |
| 99 | w = apiRequest(t, f, owner, "PUT", "/api/v1/budgets", `{"team":{"calls":0,"input_tokens":1,"output_tokens":1,"seconds":1},"projects":{}}`, "invalid", `"2"`) |
| 100 | expectHTTP(t, w, 422) |
| 101 | if !strings.Contains(w.Body.String(), `"copy_key":"budgets.invalid_input"`) { |
| 102 | t.Fatal("missing budget failure copy", w.Body.String()) |
| 103 | } |
| 104 | } |
| 105 | |