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
9func boardBase(t *testing.T) (*abilitiesHarness, *fakeBoardBase) {
10 t.Helper()
11 base := startBoardBase(t, "host-token-0123456789abcdef")
12 h := newAbilitiesHarness(t, base.origin())
13 h.register(t, abPerson, base.origin(), base.token)
14 return h, base
15}
16
17func refresh(t *testing.T, h *abilitiesHarness) (int64, bool) {
18 t.Helper()
19 rev, fresh, err := h.s.RefreshAbilities(context.Background(), abPerson)
20 if err != nil {
21 t.Fatal(err)
22 }
23 return rev, fresh
24}
25
26// A 200 is applied: the board, confirmed now, and the origin has now
27// answered the board.
28func TestAbilitiesFetchApplies200(t *testing.T) {
29 h, base := boardBase(t)
30 base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["web"], f.board.Admin = 7, false, true })
31 rev, fresh := refresh(t, h)
32 r := h.row(t, abPerson, base.origin())
33 b, _ := r.board()
34 if rev != 7 || !fresh || r.Source != SourceBase || !r.AuthoritativeSeen || r.ConfirmedAt == nil || !r.ConfirmedAt.Equal(h.now()) ||
35 b.Abilities["web"] || !b.Abilities["build"] || !b.Admin || b.Ask != "Paul" {
36 t.Errorf("rev %d fresh %v row %+v board %+v", rev, fresh, r, b)
37 }
38 if got := base.tokens[0]; got != "Bearer "+base.token {
39 t.Errorf("fetched with %q", got)
40 }
41}
42
43// An unchanged board, answered again, keeps the copy confirmed: hours of
44// equal-revision 200s never let it expire.
45func TestUnchangedBoardStaysConfirmed(t *testing.T) {
46 h, base := boardBase(t)
47 refresh(t, h)
48 for i := 0; i < 5; i++ {
49 h.advance(time.Hour)
50 refresh(t, h)
51 }
52 if r := h.row(t, abPerson, base.origin()); r.ConfirmedAt == nil || !r.ConfirmedAt.Equal(h.now()) || r.Revision != 1 {
53 t.Errorf("row %+v", r)
54 }
55}
56
57// A legacy row (an old Base) is replaced by the first 200, whatever its
58// revision.
59func TestLegacyPromotedByAnyRevision(t *testing.T) {
60 h, base := boardBase(t)
61 base.set(func(f *fakeBoardBase) { f.status = 404 })
62 refresh(t, h)
63 if r := h.row(t, abPerson, base.origin()); r.Source != SourceLegacy || r.AuthoritativeSeen || r.ConfirmedAt == nil {
64 t.Fatalf("legacy row %+v", r)
65 }
66 base.set(func(f *fakeBoardBase) { f.status, f.board.Revision, f.board.Abilities["build"] = 200, 0, false })
67 refresh(t, h)
68 r := h.row(t, abPerson, base.origin())
69 b, _ := r.board()
70 if r.Source != SourceBase || !r.AuthoritativeSeen || b.Abilities["build"] {
71 t.Errorf("after Base's board: %+v %+v", r, b)
72 }
73}
74
75// Two fetches overlap and the older one's answer comes last: it changes
76// nothing.
77func TestStaleFetchDoesNotOverwrite(t *testing.T) {
78 h, base := boardBase(t)
79 entered, release := base.holdNext()
80 done := make(chan error, 1)
81 go func() {
82 _, _, err := h.s.RefreshAbilities(context.Background(), abPerson)
83 done <- err
84 }()
85 <-entered
86 // Meanwhile Base changes, and another process fetches it.
87 base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["web"] = 2, false })
88 newer, outcome, err := h.s.fetchBoard(context.Background(), base.origin(), base.token, false)
89 if err != nil || outcome != fetchOK {
90 t.Fatal(outcome, err)
91 }
92 seq := h.s.nextFetchSeq()
93 if err := h.s.tx(context.Background(), func(t *txn) error {
94 _, err := t.applyBoard(abPerson, base.origin(), seq, newer, fetchOK)
95 return err
96 }); err != nil {
97 t.Fatal(err)
98 }
99 // The old answer (revision 2 as well now: what matters is it started
100 // first) arrives last.
101 base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["web"] = 1, true })
102 release()
103 if err := <-done; err != nil {
104 t.Fatal(err)
105 }
106 r := h.row(t, abPerson, base.origin())
107 if b, _ := r.board(); r.Revision != 2 || b.Abilities["web"] {
108 t.Errorf("the slow old answer overwrote the newer: %+v %+v", r, b)
109 }
110}
111
112// A lower revision (a restored Base) is applied, as Base is the
113// authority, and audited.
114func TestRevisionDownAudited(t *testing.T) {
115 h, base := boardBase(t)
116 base.set(func(f *fakeBoardBase) { f.board.Revision = 9 })
117 refresh(t, h)
118 base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["go_live"] = 4, false })
119 rev, _ := refresh(t, h)
120 r := h.row(t, abPerson, base.origin())
121 if b, _ := r.board(); rev != 4 || b.Abilities["go_live"] {
122 t.Errorf("lower revision not applied: %d %+v", rev, b)
123 }
124 if got := h.audited("assistant.abilities.revision_down"); len(got) != 1 {
125 t.Errorf("audits %v", got)
126 }
127}
128
129// A 401 revokes the copy at once.
130func TestAbilities401RevokesAtOnce(t *testing.T) {
131 h, base := boardBase(t)
132 refresh(t, h)
133 base.set(func(f *fakeBoardBase) { f.token = "another-token-0123456789" })
134 if _, fresh := refresh(t, h); fresh {
135 t.Error("a 401 counted as fresh")
136 }
137 if r := h.row(t, abPerson, base.origin()); r.RevokedAt == nil {
138 t.Errorf("not revoked: %+v", r)
139 }
140 if got := h.audited("assistant.abilities.revoked"); len(got) != 1 {
141 t.Errorf("audits %v", got)
142 }
143}
144
145// Once Base has answered the board, a 404 is a failure (a rollback), not
146// a legacy Base: nothing changes, and nothing widens.
147func TestAbilities404AfterBoardIsFailure(t *testing.T) {
148 h, base := boardBase(t)
149 base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false })
150 refresh(t, h)
151 before := h.row(t, abPerson, base.origin())
152 h.advance(time.Minute)
153 base.set(func(f *fakeBoardBase) { f.status = 404 })
154 if _, fresh := refresh(t, h); fresh {
155 t.Error("a 404 after a board counted as fresh")
156 }
157 after := h.row(t, abPerson, base.origin())
158 if b, _ := after.board(); after.Source != SourceBase || !after.ConfirmedAt.Equal(*before.ConfirmedAt) || b.Abilities["build"] {
159 t.Errorf("after the 404: %+v", after)
160 }
161 if _, ops := base.counts(); ops != 0 {
162 t.Errorf("the operations route was asked %d times after a board", ops)
163 }
164}
165
166// An old Base keeps its member on the defaults across several 10-minute
167// periods, renewed through its operations route; upgraded, its first 200
168// is the board, and a later 404 is a failure.
169func TestLegacyBaseAcrossExpiry(t *testing.T) {
170 h, base := boardBase(t)
171 base.set(func(f *fakeBoardBase) { f.status = 404 })
172 for i := 0; i < 3; i++ {
173 h.advance(10 * time.Minute)
174 if _, fresh := refresh(t, h); !fresh {
175 t.Fatalf("renewal %d not fresh", i)
176 }
177 if r := h.row(t, abPerson, base.origin()); r.Source != SourceLegacy || !r.ConfirmedAt.Equal(h.now()) {
178 t.Fatalf("renewal %d: %+v", i, r)
179 }
180 }
181 base.set(func(f *fakeBoardBase) { f.status = 200 })
182 refresh(t, h)
183 if r := h.row(t, abPerson, base.origin()); r.Source != SourceBase || !r.AuthoritativeSeen {
184 t.Fatalf("upgraded: %+v", r)
185 }
186 base.set(func(f *fakeBoardBase) { f.status = 404 })
187 h.advance(time.Minute)
188 if _, fresh := refresh(t, h); fresh {
189 t.Error("a 404 after the upgrade counted as fresh")
190 }
191 if r := h.row(t, abPerson, base.origin()); r.Source != SourceBase {
192 t.Errorf("after the upgrade's 404: %+v", r)
193 }
194}
195
196// A 200 that isn't a whole board (null, {}, an error, abilities missing
197// or not true and false) is a failure: it changes nothing, and never
198// turns on what Base turned off (Astra phase 2, P1-2).
199func TestBoardEnvelopeValidated(t *testing.T) {
200 h, base := boardBase(t)
201 base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false })
202 refresh(t, h)
203 before := h.row(t, abPerson, base.origin())
204 for _, raw := range []string{`null`, `{}`, `{"error":"unavailable"}`, `{"revision":2,"admin":false,"ask":"Paul","abilities":null}`,
205 `{"revision":2,"admin":false,"ask":"Paul"}`, `{"revision":2,"admin":false,"ask":"Paul","abilities":{"build":"yes"}}`, `{"revision":"2","admin":false,"ask":"Paul","abilities":{}}`, `[]`} {
206 h.advance(time.Minute)
207 base.set(func(f *fakeBoardBase) { f.raw = raw })
208 if _, fresh := refresh(t, h); fresh {
209 t.Errorf("%s counted as fresh", raw)
210 }
211 if r := h.row(t, abPerson, base.origin()); r.Abilities != before.Abilities || !r.ConfirmedAt.Equal(*before.ConfirmedAt) {
212 t.Errorf("%s changed the copy: %+v", raw, r)
213 }
214 }
215}
216