| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/equipo" |
| 16 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 17 | "amadan.net/rastrillo/rastrillo/db" |
| 18 | "amadan.net/rastrillo/rastrillo/dbtest" |
| 19 | "amadan.net/rastrillo/rastrillo/mail" |
| 20 | "amadan.net/rastrillo/rastrillo/sessions" |
| 21 | ) |
| 22 | |
| 23 | const origin = "http://127.0.0.1:8080" |
| 24 | |
| 25 | var ctx = context.Background() |
| 26 | var owner = equipo.Actor{Address: "owner@example.com"} |
| 27 | var peer = equipo.Actor{Address: "peer@example.com"} |
| 28 | var schema = dbtest.FromSet(equipo.BootSchema) |
| 29 | |
| 30 | type fixture struct { |
| 31 | DB *db.DB |
| 32 | Store *equipo.Store |
| 33 | Handler http.Handler |
| 34 | Cookies map[string]*http.Cookie |
| 35 | TeamID string |
| 36 | } |
| 37 | |
| 38 | func newFixture(t *testing.T) *fixture { |
| 39 | t.Helper() |
| 40 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 41 | d, err := db.Open(schema.Path(t), logger) |
| 42 | must(t, err) |
| 43 | t.Cleanup(func() { must(t, d.Close()) }) |
| 44 | if !filepath.IsAbs(os.TempDir()) { |
| 45 | t.Fatal("TMPDIR must be absolute") |
| 46 | } |
| 47 | s := equipo.NewStore(d) |
| 48 | router, err := equipo.RouterWithStore(d, equipo.Config{Origin: origin, InstanceKey: "plan3-fixture", AllowLoggedMail: true, Owners: []string{owner.Address}}, mail.Logged(logger), logger, s) |
| 49 | must(t, err) |
| 50 | f := &fixture{DB: d, Store: s, Handler: equipo.Mux(router), Cookies: map[string]*http.Cookie{}} |
| 51 | must(t, s.SetMember(ctx, owner, peer.Address, equipo.RoleOwner, true, 0)) |
| 52 | must(t, d.Writer().QueryRow("SELECT id FROM teams").Scan(&f.TeamID)) |
| 53 | for _, actor := range []equipo.Actor{owner, peer} { |
| 54 | token := rand.Text() |
| 55 | now := time.Now().UTC() |
| 56 | f.exec(t, "INSERT INTO sessions(token_hash,subject,method,auth_time,created_at,expires_at) VALUES(?,?,?,?,?,?)", sessions.HashToken(token), actor.Address, "fixture", now.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano), now.Add(time.Hour).Format(time.RFC3339Nano)) |
| 57 | f.Cookies[actor.Address] = &http.Cookie{Name: "rastrillo_session", Value: token, Path: "/"} |
| 58 | } |
| 59 | return f |
| 60 | } |
| 61 | func must(t *testing.T, err error) { |
| 62 | t.Helper() |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | } |
| 67 | func (f *fixture) exec(t *testing.T, q string, args ...any) { |
| 68 | t.Helper() |
| 69 | _, err := f.DB.Writer().Exec(q, args...) |
| 70 | must(t, err) |
| 71 | } |
| 72 | func (f *fixture) count(t *testing.T, q string, args ...any) int64 { |
| 73 | t.Helper() |
| 74 | var n int64 |
| 75 | must(t, f.DB.Writer().QueryRow(q, args...).Scan(&n)) |
| 76 | return n |
| 77 | } |
| 78 | func (f *fixture) get(t *testing.T, actor equipo.Actor, path string) *httptest.ResponseRecorder { |
| 79 | t.Helper() |
| 80 | r := httptest.NewRequest("GET", path, nil) |
| 81 | r.AddCookie(f.Cookies[actor.Address]) |
| 82 | w := httptest.NewRecorder() |
| 83 | f.Handler.ServeHTTP(w, r) |
| 84 | return w |
| 85 | } |
| 86 | func (f *fixture) task(t *testing.T, actor equipo.Actor, id string) equipo.Task { |
| 87 | t.Helper() |
| 88 | // Historical tasks intentionally have no thread; reads backfill them. |
| 89 | task := equipo.Task{ID: id, RequestID: id, TeamID: f.TeamID, Requester: actor.Address, Revision: 1, State: "running", Description: "fixture task"} |
| 90 | must(t, f.DB.G.Create(&task).Error) |
| 91 | return task |
| 92 | } |
| 93 | func (f *fixture) createTask(t *testing.T) equipo.Task { |
| 94 | t.Helper() |
| 95 | p := equipo.Project{Name: "fixture", Repository: "amadan.net/test/fixture", DefaultBranch: "main", Kind: equipo.ProjectRepository, Revision: 1, GrantVersion: 1} |
| 96 | must(t, f.DB.G.Create(&p).Error) |
| 97 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: p.ID, Decision: "merge", Address: owner.Address}).Error) |
| 98 | limits := team.PeriodLimits{Calls: 24, InputTokens: 200000, OutputTokens: 40000, Seconds: 10000} |
| 99 | must(t, f.Store.SaveBudgets(ctx, owner, equipo.BudgetConfig{Team: limits, Projects: map[team.ProjectID]team.PeriodLimits{team.ProjectID(p.ID): limits}})) |
| 100 | task, err := f.Store.CreateBug(ctx, owner, p.ID, "fixture bug", "fixture-bug") |
| 101 | must(t, err) |
| 102 | return task |
| 103 | } |
| 104 | |
| 105 | func expectHTTP(t *testing.T, w *httptest.ResponseRecorder, code int) { |
| 106 | t.Helper() |
| 107 | if w.Code != code { |
| 108 | t.Fatalf("HTTP %d, want %d: %s", w.Code, code, w.Body.String()) |
| 109 | } |
| 110 | } |
| 111 | |