| 1 | package stagingtest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | func TestPlacement(t *testing.T) { |
| 12 | project := wire.Card{Kind: wire.CardProject, Name: "oficina/sample", State: wire.CardWorking} |
| 13 | question := wire.Card{Kind: wire.CardQuestion, Question: "Where should this go?"} |
| 14 | created := wire.Card{Kind: wire.CardProject, Name: "oficina/sample", State: wire.CardCreated} |
| 15 | for _, tc := range []struct { |
| 16 | name string |
| 17 | cards []wire.Card |
| 18 | tasks []wire.Task |
| 19 | required bool |
| 20 | bad bool |
| 21 | }{ |
| 22 | {"question", []wire.Card{question}, nil, false, false}, |
| 23 | {"named project", []wire.Card{project}, nil, false, false}, |
| 24 | {"project with clarification", []wire.Card{project, question}, nil, true, false}, |
| 25 | {"no placement", nil, nil, false, true}, |
| 26 | {"missing project card", []wire.Card{question}, nil, true, true}, |
| 27 | {"created without consent", []wire.Card{created, question}, nil, false, true}, |
| 28 | {"wrong project", []wire.Card{{Kind: wire.CardProject, Name: "oficina/other", State: wire.CardWorking}, question}, nil, false, true}, |
| 29 | // A task's Project is the project's title; its Repo is the repository. |
| 30 | {"task in named repo", nil, []wire.Task{{Project: "Sample", Repo: "oficina/sample"}}, false, false}, |
| 31 | // Live tasks name the repository with its forge host. |
| 32 | {"task in named repo with host", nil, []wire.Task{{Project: "Sample", Repo: "amadan.net/oficina/sample"}}, false, false}, |
| 33 | {"task in other repo with host", nil, []wire.Task{{Project: "Sample", Repo: "amadan.net/oficina/other"}}, false, true}, |
| 34 | {"task in other repo", nil, []wire.Task{{Project: "oficina/sample", Repo: "oficina/other"}}, false, true}, |
| 35 | } { |
| 36 | t.Run(tc.name, func(t *testing.T) { |
| 37 | err := Placement(tc.cards, tc.tasks, []string{"oficina/sample"}, tc.required) |
| 38 | if (err != nil) != tc.bad { |
| 39 | t.Fatalf("Placement = %v, want bad=%v", err, tc.bad) |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestLatency(t *testing.T) { |
| 46 | for _, tc := range []struct { |
| 47 | name string |
| 48 | first, retry time.Duration |
| 49 | calls int |
| 50 | bad bool |
| 51 | }{ |
| 52 | {"fast", time.Second, 0, 0, false}, |
| 53 | {"provider blip", 11400 * time.Millisecond, 1900 * time.Millisecond, 1, false}, |
| 54 | {"persistently slow", 6 * time.Second, 7 * time.Second, 1, true}, |
| 55 | } { |
| 56 | t.Run(tc.name, func(t *testing.T) { |
| 57 | calls := 0 |
| 58 | samples, err := Latency(tc.first, 5*time.Second, func() time.Duration { calls++; return tc.retry }) |
| 59 | if calls != tc.calls || len(samples) != tc.calls+1 || (err != nil) != tc.bad { |
| 60 | t.Fatalf("samples %v calls %d error %v", samples, calls, err) |
| 61 | } |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Only a fresh suite person the harness added may be removed by it: the |
| 67 | // owner, the long-lived suite person and anyone else are refused. |
| 68 | func TestSuitePerson(t *testing.T) { |
| 69 | for addr, want := range map[string]bool{ |
| 70 | "suite-20261128t161056z-800e724b1ffd@equipo-staging.invalid": true, |
| 71 | "suite@equipo-staging.invalid": false, |
| 72 | "paul@tito.io": false, |
| 73 | "suite-x@equipo-staging.invalid.example.com": false, |
| 74 | "suite-x@equipo-staging.invalid,paul@tito.io": false, |
| 75 | "Suite-X@equipo-staging.invalid": false, |
| 76 | "suite-@equipo-staging.invalid": false, |
| 77 | } { |
| 78 | if SuitePerson(addr) != want { |
| 79 | t.Errorf("%q: %v", addr, !want) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // The roster comes a page at a time (50 by default, 100 at most): every |
| 85 | // page is read, so a suite person past the first is still found. |
| 86 | func TestRosterReadsEveryPage(t *testing.T) { |
| 87 | var asked []string |
| 88 | pages := map[string]personapi.Page[personapi.RosterMember]{ |
| 89 | "/api/v1/roster?limit=100": {Items: []personapi.RosterMember{{Person: personapi.Person{Address: "a@example.com"}}}, NextCursor: "c1"}, |
| 90 | "/api/v1/roster?cursor=c1&limit=100": {Items: []personapi.RosterMember{{Person: personapi.Person{Address: "b@example.com"}, Revision: 7}}}, |
| 91 | } |
| 92 | got := Roster(func(path string, out *personapi.Page[personapi.RosterMember]) { |
| 93 | asked = append(asked, path) |
| 94 | *out = pages[path] |
| 95 | }) |
| 96 | if len(got) != 2 || got[1].Address != "b@example.com" || got[1].Revision != 7 || len(asked) != 2 { |
| 97 | t.Fatalf("roster %+v asked %v", got, asked) |
| 98 | } |
| 99 | } |
| 100 | |