oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "testing"
6 "time"
7
8 "amadan.net/oficina/equipo/internal/wire"
9 "gorm.io/gorm"
10)
11
12// The hint fetches from the person's Base within the call: data only ever
13// comes from Base, the 200 carries the revision now held, and a fetch
14// that failed or one less than 5 s after the last answers 202.
15func TestAbilitiesHintFetches(t *testing.T) {
16 h, base := boardBase(t)
17 base.set(func(f *fakeBoardBase) { f.board.Revision = 5 })
18 rev, status, err := h.s.AbilitiesHint(context.Background(), abPerson)
19 if err != nil || status != 200 || rev != 5 {
20 t.Fatalf("hint: %d %d %v", rev, status, err)
21 }
22 if _, status, _ := h.s.AbilitiesHint(context.Background(), abPerson); status != 202 {
23 t.Errorf("a second hint at once: %d", status)
24 }
25 if n, _ := base.counts(); n != 1 {
26 t.Errorf("throttled hint fetched: %d", n)
27 }
28 h.s.abilities.mu.Lock()
29 h.s.abilities.last[abPerson] = time.Now().Add(-6 * time.Second)
30 h.s.abilities.mu.Unlock()
31 base.set(func(f *fakeBoardBase) { f.status = 503 })
32 if _, status, _ := h.s.AbilitiesHint(context.Background(), abPerson); status != 202 {
33 t.Errorf("a failed fetch: %d", status)
34 }
35 // Someone with no host has nothing to fetch.
36 if _, status, _ := h.s.AbilitiesHint(context.Background(), "owner@example.com"); status != 202 {
37 t.Errorf("no host: %d", status)
38 }
39}
40
41// A person turn starts with a fresh board when the copy is older than
42// 60 s, and not otherwise.
43func TestAbilitiesTurnStartRefresh(t *testing.T) {
44 h, base := boardBase(t)
45 refresh(t, h)
46 if err := h.s.refreshIfStale(context.Background(), abPerson); err != nil {
47 t.Fatal(err)
48 }
49 if n, _ := base.counts(); n != 1 {
50 t.Errorf("fresh copy fetched again: %d", n)
51 }
52 h.advance(61 * time.Second)
53 if err := h.s.refreshIfStale(context.Background(), abPerson); err != nil {
54 t.Fatal(err)
55 }
56 if n, _ := base.counts(); n != 2 {
57 t.Errorf("stale copy not fetched: %d", n)
58 }
59}
60
61// Every minute, people with a turn in flight or an unfinished job are
62// refreshed; an idle person isn't.
63func TestAbilitiesActiveRefreshEvery60s(t *testing.T) {
64 base := startBoardBase(t, "host-token-0123456789abcdef")
65 h := newAbilitiesHarness(t, base.origin())
66 for _, p := range []string{"turn@example.com", "job@example.com", "idle@example.com"} {
67 h.register(t, p, base.origin(), base.token)
68 }
69 if err := h.s.db.G.Create(&turnRecord{ID: "t_run", Person: "turn@example.com", State: wire.TurnRunning, CreatedAt: h.now(), UpdatedAt: h.now()}).Error; err != nil {
70 t.Fatal(err)
71 }
72 if err := h.s.db.G.Create(&handoffRecord{ID: "h_open", Person: "job@example.com", State: handoffOpen, LedgerKey: "k1", CreatedAt: h.now(), UpdatedAt: h.now()}).Error; err != nil {
73 t.Fatal(err)
74 }
75 if err := h.s.RefreshActiveAbilities(context.Background()); err != nil {
76 t.Fatal(err)
77 }
78 for p, want := range map[string]bool{"turn@example.com": true, "job@example.com": true, "idle@example.com": false} {
79 if got := h.row(t, p, base.origin()).ConfirmedAt != nil; got != want {
80 t.Errorf("%s refreshed %v, want %v", p, got, want)
81 }
82 }
83}
84
85// Allowed refreshes a stale copy first, outside any transaction, then
86// reads it: an idle person's first act after a failed hint sees the new
87// board.
88func TestIdleEntryPointRefreshes(t *testing.T) {
89 h, base := boardBase(t)
90 refresh(t, h)
91 if err := h.s.Allowed(context.Background(), abPerson, wire.AbilityAppsRead); err != nil {
92 t.Fatal(err)
93 }
94 base.set(func(f *fakeBoardBase) { f.board.Abilities["apps_read"], f.board.Revision = false, 2 })
95 h.advance(2 * time.Minute)
96 err := h.s.Allowed(context.Background(), abPerson, wire.AbilityAppsRead)
97 if code := refusalCode(err); code != wire.CodeAbilityOff {
98 t.Errorf("after Base turned it off: %v", err)
99 }
100}
101
102// The person's own abilities: every key, the fixed ones, and who to ask.
103func TestOwnAbilitiesRoute(t *testing.T) {
104 h, base := boardBase(t)
105 base.set(func(f *fakeBoardBase) { f.board.Abilities["go_live"] = false })
106 refresh(t, h)
107 fixed, _ := ParseFixed("web=off")
108 h.s.SetFixed(fixed)
109 out, err := h.s.OwnAbilities(context.Background(), abPerson)
110 if err != nil {
111 t.Fatal(err)
112 }
113 if len(out.Abilities) != len(wire.Abilities) || out.Abilities["web"] || out.Abilities["go_live"] || !out.Abilities["build"] ||
114 len(out.Fixed) != 1 || out.Fixed[0] != "web" || out.Ask != "Paul" {
115 t.Errorf("own abilities %+v", out)
116 }
117}
118
119// Acts outside a turn fetch a stale board first, as a turn's start does:
120// Carry on now, a card's answer (the Equipo page's and a device's) and a
121// merge. What the act's own transaction then reads is the fetched board:
122// off once Base turned building off, the copy it had when the fetch
123// failed, and on again once Base turned it back on (Astra phase 2 round
124// 4, P2; round 5, P2).
125func TestActsRefreshStaleBoard(t *testing.T) {
126 h, base := boardBase(t)
127 refresh(t, h)
128 ctx := context.Background()
129 canBuild := func() bool {
130 var sw Switches
131 if err := h.s.read(ctx, func(tx *gorm.DB) error {
132 var err error
133 sw, err = (&txn{s: h.s, ctx: ctx, db: tx}).switches(abPerson)
134 return err
135 }); err != nil {
136 t.Fatal(err)
137 }
138 return sw.Can(wire.AbilityBuild)
139 }
140 acts := map[string]func(){
141 "carry on": func() { _, _ = h.s.CarryOn(ctx, abPerson, "nope", wire.CarryOn{IdempotencyKey: "k"}) },
142 "answer": func() { _, _, _ = h.s.AnswerDevice(ctx, abPerson, "", "nope", wire.AnswerCard{Choice: "Yes"}) },
143 "merge": func() { _, _ = h.s.MergeTask(ctx, abPerson, "nope") },
144 }
145 rev := int64(1)
146 board := func(build bool, status int) {
147 rev++
148 base.set(func(f *fakeBoardBase) { f.board.Abilities["build"], f.board.Revision, f.status = build, rev, status })
149 }
150 for name, act := range acts {
151 before, _ := base.counts()
152 board(false, 200)
153 act()
154 if n, _ := base.counts(); n != before || !canBuild() {
155 t.Errorf("%s fetched a fresh copy again: %d", name, n-before)
156 }
157 h.advance(61 * time.Second)
158 act()
159 if n, _ := base.counts(); n != before+1 || canBuild() {
160 t.Errorf("%s didn't act on the stale copy fetched first: %d fetches, building %v", name, n-before, canBuild())
161 }
162 // A failed fetch leaves the copy it had.
163 h.advance(61 * time.Second)
164 board(true, 500)
165 act()
166 if canBuild() {
167 t.Errorf("%s: a failed fetch changed the copy", name)
168 }
169 board(true, 200)
170 act()
171 if !canBuild() {
172 t.Errorf("%s: building back on wasn't fetched", name)
173 }
174 }
175}
176