//go:build perf package equipotest import ( "context" "encoding/json" "fmt" "io" "log/slog" "net/http" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/mail" "amadan.net/rastrillo/rastrillo/perf/perftest" equipo "amadan.net/oficina/equipo/internal/equipo" ) // The perf lane: make perf, serial and uncached, failing only on CI. // Every GET screen the router serves is held to 150ms to first byte; a // new route fails here until it is measured or has a record in // .rastrillo/budgets.txt. Never t.Parallel() in this file: a perf test // sharing the machine measures its neighbours. func TestPerfScreens(t *testing.T) { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) f := runnerFixtureNew(t, false) d := f.d mergeTask, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "Performance merge", "perf-merge") executionMust(t, err) mergeTask, err = f.s.PrepareBug(connectionContext, f.principal, mergeTask.ID, mergeTask.Revision, bugPrepared(f.connectionFixture)) executionMust(t, err) mergeTask, err = f.s.StartBug(connectionContext, f.principal, mergeTask.ID, mergeTask.Revision) executionMust(t, err) renderID, renderHash := perfRenderedChange(t, f) f.claim, err = f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "perf-claim"}) executionMust(t, err) f.x.SetRunnerValidators(equipo.RunnerValidators{Question: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) q := runnerproto.Question{ID: "perf-question", TaskID: f.a.TaskID, ServerID: "server", AgentID: "agent", SessionID: "session", PermissionID: "permission", Kind: "question", Revision: 1, RawRequest: json.RawMessage(`{"id":"permission","kind":"question","provider":"claude","name":"AskUserQuestion","input":{"questions":[{"question":"Which?","header":"Which?"}]}}`)} request := f.request() request.Question = &q _, err = f.x.Mutate(connectionContext, f.principal, "questions", request) executionMust(t, err) mustSQL(t, d, "UPDATE jobs SET state='succeeded' WHERE id=?", mergeTask.JobID) mustSQL(t, d, "UPDATE tasks SET merge_owner=? WHERE id=?", ownerAddress, mergeTask.ID) mustSQL(t, d, "UPDATE decision_owners SET address=? WHERE project_id=? AND decision='merge'", ownerAddress, f.project.ID) mailer := &capturingMailer{} cfg := testConfig(testOrigin) // The GitHub section is measured with its flag on, as an owner sees it. cfg.GitHubEnabled = true r, err := equipo.RouterWithStore(d, cfg, mailer, logger, f.s) executionMust(t, err) defer equipo.CloseLive(r) opts := rastrillo.Options{Logger: logger} equipo.Configure(&opts, equipo.Mux(r), time.Now()) defer opts.Background.Stop() h, closeAll, err := rastrillo.Handler(opts) if err != nil { t.Fatal(err) } defer closeAll() project := f.project projectPath := fmt.Sprintf("/projects/%d", project.ID) paths := map[string]string{"/projects/{id}": projectPath, "/projects/{id}/edit": projectPath + "/edit", "/specialists/{key}": "/specialists/engineering", "/team/{specialist}": "/team/review", "/tasks/{id}": "/tasks/" + f.a.TaskID, "/tasks/{id}/questions/{question}": "/tasks/" + f.a.TaskID + "/questions/perf-question", "/tasks/{id}/merge": "/tasks/" + mergeTask.ID + "/merge"} planChecks := map[string]string{} inviteURL := perfPlan3Paths(t, f, paths, planChecks, renderID, renderHash) perftest.Screens(t, perftest.ScreenConfig{ Routes: r, Handler: h, Paths: paths, Signin: func(c *http.Client, base string) { signinEmail(t, c, base, testOrigin, mailer, ownerAddress) res, err := c.Get(base + inviteURL) executionMust(t, err) res.Body.Close() if res.StatusCode != http.StatusSeeOther { t.Fatalf("invite bootstrap status %d", res.StatusCode) } checks := map[string]string{ "/work": `rst-shell-sidebar="index"`, "/needs-you": `id="activity"`, "/journey": `equipo-steps`, "/projects": `work.projects_empty`, "/projects/accounts": `data-project-count="1"`, "/tasks": `work.tasks_empty`, "/projects/new": `action="/projects"`, projectPath: "Shop", projectPath + "/edit": `action="` + projectPath + `"`, "/connections": `/connections"`, "/people": `action="/team/members"`, "/settings": `action="/team/budgets"`, } for _, key := range []string{"product", "design", "copy", "engineering", "review"} { checks["/specialists/"+key] = `action="/specialists/` + key + `"` } for path, want := range planChecks { checks[path] = want } for path, want := range checks { body := pageBody(t, c, base, path) if !strings.Contains(body, want) { t.Errorf("%s missing page content %q", path, want) } } }, Expect: map[string]int{"/": 303, "/signin": 303, "/auth/callback": 303, "/auth/verify": 303, // Settings in sections: the old URLs lead to their sections. "/conversation/connections": 303, "/conversation/notebook": 303, "/settings/thinking": 303, // GitHub's two callbacks without a setup in flight end on a clean URL. "/github/connect/installation": 303, "/github/connect/manifest": 303}, }) } // A cold start, from exec to the first byte of "/", held to 500ms. func TestPerfBoot(t *testing.T) { perftest.Boot(t, perftest.BootConfig{ Prepare: schema.Path, Build: func(path string) (http.Handler, func(), error) { started := time.Now() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(path, logger) if err != nil { return nil, nil, err } mux, err := equipo.App(d, testConfig(testOrigin), mail.Logged(logger), logger) if err != nil { d.Close() return nil, nil, err } opts := rastrillo.Options{Logger: logger} equipo.Configure(&opts, mux, started) h, closeAll, err := rastrillo.Handler(opts) if err != nil { opts.Background.Stop() d.Close() return nil, nil, err } return h, func() { closeAll(); d.Close() }, nil }, Path: "/", Expect: http.StatusSeeOther, }) }