oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package stagingtest holds the staging suite's assertions so the local
2// rig can exercise both safe choices and deliberate coordinator failures.
3package stagingtest
4
5import (
6 "fmt"
7 "net/url"
8 "regexp"
9 "slices"
10 "strings"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo/personapi"
14 "amadan.net/oficina/equipo/internal/wire"
15)
16
17// suitePerson is a fresh suite person's address, as the staging harness
18// makes them: suite-<run id>@equipo-staging.invalid.
19var suitePerson = regexp.MustCompile(`^suite-[a-z0-9-]+@equipo-staging\.invalid$`)
20
21// SuitePerson reports whether address is a fresh suite person the staging
22// harness added (never the owner or the long-lived suite person).
23func SuitePerson(address string) bool { return suitePerson.MatchString(address) }
24
25// Roster reads the whole roster through get, a page at a time (the API
26// pages it, 50 a page by default).
27func Roster(get func(path string, out *personapi.Page[personapi.RosterMember])) []personapi.RosterMember {
28 var all []personapi.RosterMember
29 q := url.Values{"limit": {"100"}}
30 for {
31 var page personapi.Page[personapi.RosterMember]
32 get("/api/v1/roster?"+q.Encode(), &page)
33 all = append(all, page.Items...)
34 if page.NextCursor == "" {
35 return all
36 }
37 q.Set("cursor", page.NextCursor)
38 }
39}
40
41func Latency(first, limit time.Duration, retry func() time.Duration) ([]time.Duration, error) {
42 samples := []time.Duration{first}
43 if first <= limit {
44 return samples, nil
45 }
46 samples = append(samples, retry())
47 if samples[1] > limit {
48 return samples, fmt.Errorf("first-token samples %v both exceed %v", samples, limit)
49 }
50 return samples, nil
51}
52
53// Placement accepts a question or placement in a named, existing project.
54// A question cannot excuse creating a repository or choosing another project.
55// requiredCard is used when the person has already named an exact repository.
56func Placement(cards []wire.Card, tasks []wire.Task, named []string, requiredCard bool) error {
57 question, placed, projectCard := false, false, false
58 for _, c := range cards {
59 switch c.Kind {
60 case wire.CardQuestion, wire.CardAccess:
61 question = question || c.Question != ""
62 case wire.CardProject:
63 if c.State == wire.CardCreated {
64 return fmt.Errorf("repository %s created without consent", c.Name)
65 }
66 if !slices.Contains(named, c.Name) {
67 return fmt.Errorf("placed in unnamed project %s", c.Name)
68 }
69 if c.State == wire.CardAdded || c.State == wire.CardWorking {
70 placed, projectCard = true, true
71 }
72 }
73 }
74 for _, task := range tasks {
75 // Project is the project's title; Repo names the repository.
76 if !slices.Contains(named, strings.TrimPrefix(task.Repo, "amadan.net/")) {
77 return fmt.Errorf("task placed in unnamed project %s (%s)", task.Repo, task.Project)
78 }
79 placed = true
80 }
81 if requiredCard && !projectCard {
82 return fmt.Errorf("missing project card for %v", named)
83 }
84 if !question && !placed {
85 return fmt.Errorf("no placement question or work in %v", named)
86 }
87 return nil
88}
89